<?php

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

class Wa_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        
        if($this->session->userdata('role')==''){
            redirect('logout');
        }
    }

    public function wa_log() {
        
        checkroles(0,0,1);
        $data['nav'] = 'mybusiness';
        $data['subnav'] = 'smslog';
       
        
        
        $this->load->view('includes/header',$data);
        $this->load->view('wa_log');
        $this->load->view('includes/footer');

        
    }    

    public function get_wa_log(){

        $sql = "SELECT id_wa, text_msg, receiver, created_on, replace(sent_on, '0000-00-00 00:00:00' ,'') as sent_on, msg_type, receipt_url , '' as btn
        FROM wa_msg_list where business_id= ".$this->session->userdata('businessid') ." ";
        
        
        if(null != $this->input->post('text_msg') && $this->input->post('text_msg') != ''){
            $sql.= " AND text_msg LIKE '%".$this->input->post('text_msg')."%' ";
        }


        if(null != $this->input->post('receiver') && $this->input->post('receiver') != ''){
            $sql.= " AND receiver LIKE '%".$this->input->post('receiver')."%' ";
        }
        
        if(null != $this->input->post('sent_on') && $this->input->post('sent_on') != ''){
            $sql.= " AND sent_on LIKE '%".$this->input->post('sent_on')."%' ";
        }
        
        if(null != $this->input->post('msg_type') && $this->input->post('msg_type') != ''){
            $sql.= " AND msg_type LIKE '%".$this->input->post('msg_type')."%' ";
        }

        if(null != $this->input->post('receipt_url') && $this->input->post('receipt_url') != ''){
            $sql.= " AND receipt_url LIKE '%".$this->input->post('receipt_url')."%' ";
        }

        $sql.= " ORDER BY id_wa DESC";
        
        
        $query = $this->db->query($sql);
        
        $log_data = $query->result_array();


        //** get all table definitions **//
        $data['draw']= $this->input->post('draw');
        $data['$start'] = $this->input->post('start');
        $data['$length'] = $this->input->post('length');
        
        $data['msg_text']= $this->input->post('text_msg');
        $data['receiver']= $this->input->post('receiver');
        $data['sent_on']= $this->input->post('sent_on');
        $data['msg_type']= $this->input->post('msg_type');
                

        $data['data']=$log_data;
        
        $filtered=  sizeof($data['data']);
        
        $data['recordsTotal'] = sizeof($data['data']);
        $data['recordsFiltered'] = sizeof($data['data']);
        
        echo(json_encode($data));


    }

    function resend_msg(){

        $this->input->post('csrf_test_name');
        $id_wa = $this->input->post('id_wa');

        $sql = "insert into wa_msg_list (invoice_id, receipt_url, text_msg, business_id, msg_type, receiver) 
        SELECT invoice_id, receipt_url, text_msg, business_id, msg_type, receiver FROM wa_msg_list where id_wa = ".$id_wa;

        $query = $this->db->query($sql);    

   
        redirect('wa_controller/wa_log');
    }


}