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

class sendsms extends OWN_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    function __construct() {
        parent::__construct();
        $this->admin_login_check();
        $this->load->model('landcustomerregistrationmodel/landcustomerregistrationmodel', 'CREGModel');
    }

    public function index() {
        $data['allLandcustomer'] = $this->CREGModel->getlandCustomer();
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/sms/sendsms', $data);
            $this->load->view('common/footer');
        }
        else{
            redirect(base_url('admin','refresh'));
        }
    }

    public function send(){
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            
              if(isset($_POST['btnSend']))
              {
                  $sdata = $this->input->post('sdata', TRUE);
                  $sdata['sms'];
                
                  $data = $this->input->post('data', TRUE);
                  if(!empty($data)){
                      foreach ($data as $mobile) {
                          
                            $to = "".$mobile."";
                            $token = "cc92332d1f38e1817dc20cdd3f77bd18";
                            $message = "" .$sdata['sms']."";
        
                            $url = "http://api.greenweb.com.bd/api.php";
                            $data= array(
                            'to'=>"$to",
                            'message'=>"$message",
                            'token'=>"$token"
                            ); // Add parameters in key value
                            $ch = curl_init(); // Initialize cURL
                            curl_setopt($ch, CURLOPT_URL,$url);
                            curl_setopt($ch, CURLOPT_ENCODING, '');
                            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
                            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                            curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
                            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
                            $smsresult = curl_exec($ch);
                          
                        echo $mobile;
                        echo " <span style='color:green'>... Success</span><br>";
                    }
                }else{
                    echo "SMS Can't Send. No Customer selected.<br>";
                }
                
                // $ndata = $this->input->post('ndata', TRUE);
                //   if(!empty($ndata)){
                //       foreach ($ndata as $nmobile) {
                          
                //             $to = "".$nmobile."";
                //             $token = "cc92332d1f38e1817dc20cdd3f77bd18";
                //             $message = "" .$sdata['sms']."";
        
                //             $url = "http://api.greenweb.com.bd/api.php";
                //             $data= array(
                //             'to'=>"$to",
                //             'message'=>"$message",
                //             'token'=>"$token"
                //             ); // Add parameters in key value
                //             $ch = curl_init(); // Initialize cURL
                //             curl_setopt($ch, CURLOPT_URL,$url);
                //             curl_setopt($ch, CURLOPT_ENCODING, '');
                //             curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
                //             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                //             curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
                //             curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
                //             $smsresult = curl_exec($ch);
                          
                //         echo $nmobile;
                //         echo " <span style='color:green'>... Success</span><br>";
                        
                //     }
                // }else{
                //     echo "SMS Can't Send. No Nominee selected.<br>";
                // }
                    
                echo "<br> Your SMS: <span style='color:blue'>";
                echo $sdata['sms'];
                echo "</span><br><br><br><a style='padding:10px 15px; color:white; background-color:green;' href='http://ccare.hasanhomesltd.com/admin/sendsms'>Go Back</a>";
                  
                //send sms api integration
                    //send sms api integration
                    //redirect('admin/sendsms/index', 'refresh');
              }
              else
              {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/user/sendsms');
                $this->load->view('common/footer');
                
              }
        }
        else{
            redirect(base_url('admin','refresh'));
        }
    }
}