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

class SMS extends CI_Controller {

    var $main_menu_name = "sms";
	var $sub_menu_name = "";

	public function __construct()
	{
		parent::__construct();
		$this->load->model('SMS_Model');
		$this->load->model('Customer_Model');
		$this->load->model('Common_Model');
		//$this->load->model('Route_Model');
		
		
	
	}
	
	public function index()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
		$data['routes_list']=array();//$this->Route_Model->get_all_active_routes();
		
		$this->load->view('sms_customers',$data);
	}
	
	public function get_all_customers_by_route_id()
	{
		$rote_id=$this->input->get('R');
		$customer_list=$this->Customer_Model->get_all_customers();
		echo json_encode($customer_list); 		
	}	
	
	
	public function list_customer()
	{
	$requestData= $_REQUEST;	
	$data = array();
	$rote_id=$this->input->get('route_id');
	$customer_list=$this->SMS_Model->get_all_customers_by_route_id($rote_id);	
	$totalData = count($customer_list);
	$totalFiltered = $totalData;  
	
	foreach ($customer_list as $row){
		$nestedData=array(); 
		$display=true;
		$cus_phone=trim($row['cus_phone']);
		$first_3_ltr=substr($cus_phone, 0, 3);
		if($first_3_ltr=='011'){
			$display=false;
		}else if($first_3_ltr=='031'){
			$display=false;
		}else if($first_3_ltr=='038'){
			$display=false;
		}else if($first_3_ltr=='051'){
			$display=false;
		}else if($first_3_ltr=='037'){
			$display=false;
		}else if($first_3_ltr=='034'){
			$display=false;
		}else if($cus_phone==''){
			$display=false;
		}else if(strlen($cus_phone)!=10){
			$display=false;
		}
		//echo "display:$display";
		
		if($display){
		$nestedData[] ='<input type="checkbox" name="sel_cus[]" value='.$row["cus_id"].' id=sel_cus-'.$row["cus_id"].'>';
		$nestedData[] =$row['cus_id'];
		$nestedData[] = $row['cus_name'];//."-".$first_3_ltr."/".$display;
		//$nestedData[] = '';//$row['route_name'];
		$nestedData[] =$row['cus_phone'];
		$cus_address='';
		if($row['cus_address']) $cus_address=$row['cus_address'];
		$nestedData[] =$cus_address;
		//$nestedData[] = $row['city_name'];
		$actionTxtDisble='';
		$actionTxtEnable='';
		$actionTxtUpdate='';
		$actionTxtDelete='';
		$actionTxtUpdate='<a onClick="click_customer_update_btn('.$row['cus_id'].')" data-toggle="modal" href="#" class="btn btn-xs btn-blue tooltips" data-placement="top" data-original-title="Edit customers"><i class="glyphicon fa fa-edit"></i></a> &nbsp;';
		if($row['cus_status']==1){
			$actionTxtDisble = '<a class="btn btn-xs btn-green tooltips" data-placement="top" data-original-title="Disable customer" onClick="disableCustomerData('.$row['cus_id'].')"><i class="glyphicon fa fa-check"></i></a> &nbsp;';
	}
		if($row['cus_status']==0){
			$actionTxtEnable = '<a class="btn btn-xs btn-warning tooltips" data-placement="top" data-original-title="Disable customer" onClick="enableCustomerData('.$row['cus_id'].')"><i class="glyphicon fa fa-minus-circle"></i></a> &nbsp;';
	}
		$actionTxtDelete='<a class="btn btn-xs btn-bricky tooltips" data-placement="top" data-original-title="Delete customer" onClick="deleteCustomerData('.$row['cus_id'].')">
															<i class="glyphicon fa fa-trash-o"></i></a>';
	
	//$nestedData[]=$actionTxtUpdate.$actionTxtDisble.$actionTxtEnable.$actionTxtDelete;
	$data[] = $nestedData;
		}
}

	$json_data = array(
			//"draw"            => intval( $requestData['draw'] ),  
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data 
			);

	echo json_encode($json_data); 
	}
	
	
	
	function enable_customer() {
		$cus_id	= $this->input->post('cus_id');
		$this->Customer_Model->enable_customer($cus_id);
        if ($cus_id) {
        	echo json_encode(array('id'=>$cus_id));
        } else {
        	echo json_encode(array('status'=>'error'));
        }
	}
	
	function send_message_to_customer(){
	$cus_id	= $this->input->post('cus_id');
	$count=$this->input->post('count');
	$message=$this->input->post('message');
	//get customer info by id
	 $cus_des=$this->Customer_Model->get_customer_info($cus_id);
	 $cus_name= $cus_des['cus_name'];
	 $cus_phone=$cus_des['cus_phone'];
	 
	 //send sms
	//$send_phone_first=$customer_details['cus_phone'];
	//$message="Thank you $cus_name payment of LKR $sale_pymnt_amount in respect of The SmartPhone Shop \nInv no : $sale_reference_no";
	//$message="SmartFixServiceCenter Sanath";
	//$myStr=123456789;
	$send_phone=substr($cus_phone, 0, 10);	
	if($send_phone){
	$this->Common_Model->send_sms($message,$send_phone);	
	}
	 
	 
	$row='<tr>';
	$row.="<td align=\"left\" style=\"text-align:left\">$count</td>";
	$row.="<td>$cus_name</td>";
	$row.="<td>$cus_phone</td>";
	$row.="<td>$message</td>";
	$row.="</tr>";
	
		$display=false;
		$display=true;
		if($display)
		{
		echo json_encode(array("row"=>$row));
		}else{
		echo json_encode(array("row"=>''));
		}
	}
	
	function send_sms_process()
	{
		$selected_customer=$this->input->post('sel_cus');
		$message_txt=$this->input->post('message_txt');
		
		$count=0;
		$data=array();
		foreach($selected_customer as $key)
		{
			$customer=$this->SMS_Model->get_customer_number_name($key);
			$cus_number=$customer['cus_phone'];
			$cus_name=$customer['cus_name'];
			$message=$message_txt;
			$new_message = str_replace(' ', '%20', $message);
			$count++;
			$url='';	
			//$url='https://www.google.lk';	
		$result=file_get_contents($url);
		
		}	
		$result=file_get_contents($url);	
		echo json_encode($result);
	}
	
	



	
}