<?php

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

class Sms_controller extends CI_Controller {
//SMS controller
    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('sms_model');
        $this->load->model('business_model');
        if($this->session->userdata('role')==''){
            redirect('logout');
        }
    }

    public function smssender() {
        checkroles(0,0,1);
        
        $data['nav'] = 'mybusiness';
        $data['subnav'] = 'smssender';

        $data['sms_text']=$this->sms_model->get_smstext();
        
        $this->load->view('includes/header', $data);
        $this->load->view('smssender');
        $this->load->view('includes/footer');
        
    }
    
    function get_customersbysearch(){
        
         //** get all table definitions **//
        $data['draw']= $this->input->post('draw');
        $data['$start'] = $this->input->post('start');
        $data['$length'] = $this->input->post('length');
        
        $data['customer_name']= $this->input->post('customer_name');
        $data['customer_gender']= $this->input->post('customer_gender');
        $data['customer_careof']= $this->input->post('customer_careof');
        $data['customer_cell']= $this->input->post('customer_cell');
        $data['visit_date_from']= $this->input->post('visit_date_from');
        $data['visit_date_to']= $this->input->post('visit_date_to');
        $data['customer_email']= $this->input->post('customer_email');
        $data['profession']= $this->input->post('profession');
       
        $data['referencing_staff']= $this->input->post('referencing_staff');
        
        $total= $this->sms_model->get_countofcustomers();
        $data['data'] = $this->sms_model->get_customersbysearch();

        $filtered=  sizeof($data['data']);
        
        $data['recordsTotal'] = $total;
        $data['recordsFiltered'] = $total;
        
        echo(json_encode($data));
        
    }
    
    function open_sms_sender(){
        checkroles(0,0,1);
        
        $data['nav'] = 'mybusiness';
        $data['subnav'] = 'smssender';
        $data['menu'] = 'hidden';
        
        if(null !==$this->input->post('visit_id')){
            $this->db->select('visit_service_start');
            $this->db->where('customer_visit_id', $this->input->post('visit_id'));
            $this->db->where('visit_service_status','Active');
            $this->db->order_by('visit_service_start', 'asc');
            $query = $this->db->get('visit_services');
            
            $vss = $query->row();
            $data1['visit_service_start']=$vss->visit_service_start;
            $date=date_create($vss->visit_service_start);
            $data1['visit_time']= $date->format('g:i A');
            $data1['visit_day']= $date->format('dS');
            $data1['visit_month']= $date->format('M');
        }else{
            $data1['visit_service_start']=$this->input->post('visit_service_start');
            $date=date_create($this->input->post('visit_service_start'));
            $data1['visit_time']= $date->format('g:i A');
            $data1['visit_day']= $date->format('dS');
            $data1['visit_month']= $date->format('M');
        }
        
        $data1['visit_id']=$this->input->post('visit_id'); 
        $data1['customer_name']=$this->input->post('customer_name'); 
        $data1['customer_cell']=$this->input->post('customer_cell');
        $data1['service_name']=$this->input->post('service_name');
        $data1['service_category']=$this->input->post('service_category');
        $data1['staff_name']=$this->input->post('staff_name');
       
        $data1['business']=$this->business_model->getbusinessdetails();
        
        $this->load->view('includes/header', $data);
        $this->load->view('smsshooter', $data1);
        $this->load->view('includes/footer');
    }
    
    
    function send_sms(){
        $using="SMSReminder";
        
        if(null!==$this->input->post('using')){
           $using=$this->input->post('using');
        }
        $sendsms = $this->sms_model->send_sms(mb_convert_encoding(trim($this->input->post('msg')), 'UTF-8'), $this->input->post('customercell'), FALSE, $using, true, $this->session->userdata('businessid')); 
        if($using!='LoyaltyOTP'){
            $this->sms_model->update_status($this->input->post('visit_id'));
        }
    }
    
    function smslog(){
        checkroles(0,0,1);
        $data['nav'] = 'mybusiness';
        $data['subnav'] = 'smslog';
       
        
        
        $this->load->view('includes/header',$data);
        $this->load->view('smslog');
        $this->load->view('includes/footer');
        
    }
    
    function  get_smslog(){
          //** get all table definitions **//
        $data['draw']= $this->input->post('draw');
        $data['$start'] = $this->input->post('start');
        $data['$length'] = $this->input->post('length');
        
        $data['msgdata']= $this->input->post('msgdata');
        $data['receiver']= $this->input->post('receiver');
        $data['senton']= $this->input->post('senton');
        $data['using']= $this->input->post('using');
                
        $total= $this->sms_model->get_countoflog();
        $data['data']=$this->sms_model->getsmslog();
        
        $filtered=  sizeof($data['data']);
        
        $data['recordsTotal'] = $total;
        $data['recordsFiltered'] = $total;
        
        echo(json_encode($data));
    }
            
    public function checkloyaltyotp(){
        $response = 'No';
        if ($this->db->field_exists('loyalty_otp', 'business')){
            $this->db->select('loyalty_otp');
            $this->db->where('id_business', $this->session->userdata('businessid'));
            $query = $this->db->get('business');
            $result = $query->row();

            $response = $result->loyalty_otp;

        }

        echo $response;    

    }    

    public function send_otp_sms(){
        $using="LoyaltyOTP";
        
        if(null!==$this->input->post('using')){
           $using=$this->input->post('using');
        }


        $generator = "1357902468"; 
        $result = "";   
        for ($i = 1; $i <= 4; $i++) { 
            $result .= substr($generator, (rand()%(strlen($generator))), 1); 
        } 
        
        $this->db->set('pin_code', $result);
        $this->db->where('id_customers', $this->input->post('customer_id'));
        $this->db->update('customers');

        $sendsms = $this->sms_model->send_sms(mb_convert_encoding(trim($this->input->post('msg').$result), 'UTF-8'), $this->input->post('customercell'), FALSE, $using, FALSE, $this->session->userdata('businessid')); 
        
    }
    
    public function check_customer_otp(){

        $Input = $this->input->post('input');

        $this->db->select('pin_code');
        $this->db->where('id_customers', $this->input->post('customer_id'));
        $query =   $this->db->get('customers');
        $result = $query->row();

        $pincode = $result->pin_code;

        if($pincode==$Input){
            echo 'verified';
        }else{
            echo 'invalid';
        }

    }

}
