<?php
defined('BASEPATH') OR exit('No direct script access allowed');


function encode($text){
    $obj = &get_instance();
    $obj->load->library('encryption');
    $data = $obj->encryption->encrypt($text);
    return str_replace(['/'],['~'],$data);
}

function decode($text){
    $text = str_replace(['~'],['/'],$text);
    $obj = &get_instance();
    $obj->load->library('encryption');
    $data = $obj->encryption->decrypt($text);
    return $data;
}

function decrypt_pass($text){
    $obj = &get_instance();
    $obj->load->library('encryption');
    $data = $obj->encryption->decrypt($text);
     return $data;
      
    }
    
    
    	if (!function_exists('inputJson')) {
		function inputJson(){
			$inputJSON = file_get_contents('php://input');
			$inputJSON = preg_replace('/,(\s*[\}\]])/', '$1', $inputJSON);
			$input = json_decode($inputJSON, true);

			if (json_last_error() !== JSON_ERROR_NONE || !is_array($input)) {
				outputJSON(400, 'error', 'Invalid JSON input.');
				return;
			}
			return array_map('trim', $input); 
		}
	}
    
    
    
    
    	if (!function_exists('checkValidation')) {
		 function checkValidation($input,$rules){
			$CI = &get_instance();
			if (!isset($CI->form_validation)) {
				$CI->load->library('form_validation');
			}
			$CI->form_validation->set_data($input);
			$CI->form_validation->set_rules($rules);

			if (!$CI->form_validation->run()) {
				outputJSON(400, 'error', 'Validation failed.', $CI->form_validation->error_array());
				return;
			}
		}
	}
    
    
    	 
	 if (!function_exists('outputJSON')) {
		function outputJSON($statusCode, $status, $message, $data = null)
		{
			$CI = &get_instance();
			$response = array(
				'status' => $status,
				'status_code' => $statusCode,
				'message' => $message,
			);
			// if (!empty($data)) {
				$response['data'] = $data;
			// }
			$CI->output
				->set_content_type('application/json')
				->set_status_header($statusCode)
				->set_output(json_encode($response))
				->_display();
			exit;
		}
	}
    
    
    
    
if (!function_exists('send_text_msg_new')) {
    function send_text_msg_new($mobile, $message, &$error = null)
    {
        // ✅ VALID credentials must be used here
        $username = 'bitspan';  // Check this is correct
        $apikey = 'yaEn19yQQEH4';  // Check this is correct
        $senderid = 'OTPVLE';
        $route = 'TRANS';
        $tid = '1207168938984742587';
        $peid = '1201159177161104885';

        $encoded_msg = urlencode($message);
        $url = "https://mdssend.in/api.php?username={$username}&apikey={$apikey}&senderid={$senderid}&route={$route}&mobile={$mobile}&text={$encoded_msg}&TID={$tid}&PEID={$peid}";

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $error = curl_error($ch);
        curl_close($ch);

        // ✅ Save to log file for debugging
        file_put_contents('sms_debug_log.txt', date('Y-m-d H:i:s') . " URL: $url | HTTP: $http_code | Response: $response | Error: $error\n", FILE_APPEND);

        if ($http_code == 200 && stripos($response, 'Invalid') === false) {
            return true;
        } else {
            $error = !empty($error) ? $error : $response;
            return false;
        }
    }
}




function encrypt_pass($text){
    $obj = &get_instance();
    $obj->load->library('encryption');
    $data = $obj->encryption->encrypt($text);
     return $data;
      
    }
    
    
    function ind_format($number){    	
$decimal = (string)($number - floor($number));
$money = floor($number);
$length = strlen($money);
$delimiter = '';
$money = strrev($money);

for($i=0;$i<$length;$i++){
	if(( $i==3 || ($i>3 && ($i-1)%2==0) )&& $i!=$length){
		$delimiter .=',';
	}
	$delimiter .=$money[$i];
}

$result = strrev($delimiter);
$decimal = preg_replace("/0\./i", ".", $decimal);
$decimal = substr($decimal, 0, 3);

if( $decimal != '0'){
	$result = $result.$decimal;
}
else
{
    $result=$result.".00";
}

return $result;
}



//web multidashboard notification


function sendNotificationmultidashbord($token,$body,$title,$click_action,$icon){
   // $url="https://fcm.googleapis.com/v1/projects/bitsmultiservice/messages:send";
   $url="https://asatvindia.in:5001/api/notification";

        
     $fields=array(
        "message"=>array("token"=>$token,
        "notification"=>array(
            "body"=>$body,
            "title"=>$title,
         ),
         "webpush"=>array("fcm_options"=>array('link'=>$click_action),
         "notification"=>array('icon'=>$icon),
         ),
        )
    );
   
   
    $headers=array(
        'Content-Type:application/json'
    );

    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
    $result=curl_exec($ch);
 //   print_r($result);
    curl_close($ch);
}

//App multidashbord notification
function sendNotificationapp($token,$body,$title,$orderdetail_id,$app_url,$channel_id,$common=''){
    //$url="https://fcm.googleapis.com/v1/projects/bitsmultiservice/messages:send";
    $url="https://asatvindia.in:5001/api/notification";


    $fields=array(
            "message"=>array("token"=>$token,
              "data"=>array(
               "notificaton_id"=>$orderdetail_id,
               "title"=>$title,
               "body"=>$body,
                "common"=>$common,
               "channel_id"=>$channel_id,
               "url"=>$app_url,
               )
            )
        );
        
    
    $headers=array(
        'Content-Type:application/json'
    );

    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
    $result=curl_exec($ch);
    //print_r($result);
    curl_close($ch);
}

