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

class sms_model extends CI_Model {
  
  private $tableName = 'sales';
  
    function __construct() 
    {
    /* Call the Model constructor */
    parent::__construct();
    }
    /*function send_smsDJ($phonenumber,$msg)
    {
        //$phonenumber = substr($phonenumber, -9);
        $phonenumber = $this->format_phone($phonenumber);
        $url         = 'https://digitalreachapi.dialog.lk/refresh_token.php';
        $username    = "sakura_api";
        $password    = "sakura@89999";
        $data        = array(
            "u_name" => $username,
            "passwd" => $password
        );
        $data_json   = json_encode($data);
        $ch          = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json'
        ));
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);
        $access_token = preg_replace("/[^0-9]/", "", $response);
        $url          = 'https://digitalreachapi.dialog.lk/camp_req.php';
        date_default_timezone_set('Asia/Colombo');
        $s_time    = date("Y-m-d h:i:s");
        $e_time    = date('Y-m-d h:i:s', strtotime("+1 days", strtotime(date("Y-m-d h:i:s"))));
        $data      = array(
            "msisdn" => "" . $phonenumber . "",
            "channel" => "1",
            "mt_port" => "MANKI",
            "s_time" => $s_time,
            "e_time" => $e_time,
            "msg" => "" . $msg . "",
           // "callback_url" => "https://digitalreachapi.dialog.lk//call_back.php"
        );
        $data_json = json_encode($data);
        $ch        = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Authorization:' . $access_token . ''
        ));
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }*/
    function send_sms($dest,$msg){
        
        $msg = str_replace("\n","%0a",$msg);
        $msg = str_replace("&","%26",$msg);
        $msg = str_replace("'","%27",$msg);
        $msg = str_replace("/","%2F",$msg);
        $msg = str_replace("(","%7B*",$msg);
        $msg = str_replace(")","%7D*",$msg);
        
        $init_str = "https://richcommunication.dialog.lk/api/sms/inline/send?q=09f74603c003d81&destination=".$dest."&message=".$msg;
        $init_str = str_replace(" ","%20",$init_str);
        
        $ch = curl_init($init_str); // such as http://example.com/example.xml
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $response = curl_exec($ch);
        curl_close($ch);
        if($response == "0"){
            return $response;
        }else{
            return false;
        }
    }
    
    //$otp         = (rand(1000, 9999));
    
    // new shit begins below
    function smsGate(){
	    header("Content-type: application/json");
	    
	    $this->load->model("customer_model");
	    $this->load->model("sms_model");
	    $hw_cus = $this->customer_model->get_all_customers_hw();
	    $ds_cus = $this->customer_model->get_all_customers_ds();
	    $body2 = '{
                    "messages": [
                        {
                        "clientRef": "0934345",
                        "number": "947xxxxxxxx",
                        "mask": "PIZZAHUT",
                        "text": "This is a test message",
                        “campaignName”:”xmasPromo
                        },
                        {
                        "clientRef": "7945695",
                        "number": "947xxxxxxxx",
                        "mask": "BOC",
                        "text": "This is a scheduled test message",
                        "scheduledTime": "2017-09-14T17:39:04",
                        "campaignName":"xmasPromo2"
                        }
                    ]
                }';
          
	    $messages = array();
	    foreach($hw_cus as $rw){
	        if($this->format_phone($rw['cus_phone'])){
	            $data = array();
    	        $data["clientRef"]= "0934345";
    	        $data["number"]= $rw['cus_phone'];
    	        $data["mask"]= "PIZZAHUT";
    	        $data["text"]= "Text msg for ".$rw['cus_phone'];
    	        $data["campaignName"]= "Hot promo";
    	        $messages[] = $data;   
	        }
	    }
	    $body["messages"] = $messages;
	    
	    $this->sms_model->send_sms($body);
	}
	function testSMS(){
	    $ch = curl_init("https://richcommunication.dialog.lk/api/sms/inline/send?q=09f74603c003d81&destination=94774009171,94774009171&message=HI"); // such as http://example.com/example.xml
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $data = curl_exec($ch);
        curl_close($ch);
	}
	function format_phone($phone){
        $rv = "";
        $filtered_phone_number = filter_var($phone, FILTER_SANITIZE_NUMBER_INT);
        $phone_to_check = str_replace("-", "", $filtered_phone_number);
        $phone_to_check = intval($phone_to_check);
        if(strlen($phone_to_check) < 9 || strlen($phone_to_check) > 12){
            $rv = false;
        }else{
            $rv = substr($phone_to_check, -9);
            $rv = "94".$rv;
        }
        return $rv;
    }
}