<?php

    namespace App\Controllers;
    
    use App\Models\Md_student;
    use App\Models\Md_sms;
    use App\Models\Md_cms;
    use App\Models\Md_database;

    class Sms extends BaseController
    {
        public function __construct() {
             $this->Md_sms= new Md_sms();
             $this->Md_cms= new Md_cms();
              $this->Md_database= new Md_database();
             helper('text');
        }
    
         public function SmsSend()
         {
                   
            header("Access-Control-Allow-Origin: *");
            // If you need to allow specific methods
            header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
            // If you need to allow specific headers
            header("Access-Control-Allow-Headers: Content-Type, Authorization");
            $datas = json_decode(file_get_contents("php://input"), true);
            
            $mobileno=$datas['mobileno'];
            $enquiry=$datas['enquiry'];
            $link=$datas['link'];
            $module=$datas['module'];
            
            $table = "sms_template";
            $select = array('*');
            $condigtion = array('titles' => $enquiry,'module'=>$module, 'status' => '1');
            $smsTemplate = $this->Md_database->getData($table, $select, $condigtion, '', '');
            
            
            $smsCentent=(!empty($smsTemplate[0]['template'])?$smsTemplate[0]['template']:'');
            
            if (!empty($mobileno)) 
            {
                //sms api details::
                $table = "sms_master";
                $select = array('link,username,password,senderid');
                $condigtion = array('status' => '1');
                $arraySMSApi = $this->Md_database->getData($table, $select, $condigtion, '', '1');

                if(!empty($arraySMSApi))
                {
                    $smslink="";
                    $username="";
                    $password="";
                    $senderid="";
                    $smslink=(!empty($arraySMSApi[0]['link'])?$arraySMSApi[0]['link']:'');
                    $username=(!empty($arraySMSApi[0]['username'])?$arraySMSApi[0]['username']:'');
                    $password=(!empty($arraySMSApi[0]['password'])?$arraySMSApi[0]['password']:'');
                    $senderid=(!empty($arraySMSApi[0]['senderid'])?$arraySMSApi[0]['senderid']:'');
                    
                    $reserved_words = array(
                    "||LINK||" => $link
                    );
                    
                    foreach ($reserved_words as $k => $v) {
                        $content = str_replace($k, $v, $smsCentent);
                    }
                
                    $message = urlencode($content);
                    $url = $smslink ."?username=". $username . "&password=" . $password . "&senderid=" . $senderid . "&route=1&numbers=$mobileno&message=$message&dndrefund=1";
  
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    $contents = curl_exec($ch);
                    $return_val = curl_close($ch);
                    $return_val;
                }
            }
            
             $final = array('status' => 1, 'sms' =>'SMS Send Successfully.');
             echo json_encode($final);
             exit();
        }

}