<?php

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

class Concent_controller extends CI_Controller {

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

    public function open_concent_list($visit_id){

        $sql="SELECT * 
        FROM `concent_forms`
        join concent_category on concent_category.id_concent_category=concent_forms.category_id
        Where ifnull(form_structure,'') != ''
        AND form_status='Active'
        group by id_concent_category, id_concent_forms;";

        $query = $this->db->query($sql);
        $data['concentforms']= $query->result_array();
        $data['visit_id']= $visit_id;

        $this->load->view('includes/header', $data);
        $this->load->view('concentforms/concentformlist');
        $this->load->view('includes/footer');
    }

    public function open_concent_form($concent_form_id=1, $visit_id=null){


        //get customer visit details
        $sql = "select id_customer_visits, id_customers, customers.customer_name, 
            ifnull(customers.customer_area,'') customer_area, customers.customer_cell, 
            date_format(customer_visits.customer_visit_date ,'%Y-%m-%d') as customer_visit_date
            from customer_visits 
            join customers on customers.id_customers = customer_visits.customer_id
            where customer_visits.visit_status in ('open', 'invoiced')
            and id_customer_visits = ".$visit_id.";";
            
        $query = $this->db->query($sql);
        $data['customerdetails']= $query->row();


        //check if a similar signed form exits for this visit
        $this->db->SELECT('*');
        $this->db->where("visit_id",$visit_id);
        $this->db->where("concent_form_id", $concent_form_id);
        $query1 = $this->db->get('concent_form_data');
        
        //$data['customerdetails']= $query1->row();
        if($query1->num_rows() > 0){
            
            $data['formdata']=$query1->row();

            $this->load->view('includes/header', $data);
            $this->load->view('concentforms/signedconcent');
            $this->load->view('includes/footer');
        }else{

            $data['formstruction']=$this->concent_model->get_form_structure($concent_form_id);
            
            $data['staff']=$this->staff_model->get_active_scheduler_staff($this->session->userdata('businessid'));
            
            $this->load->view('includes/header', $data);
            $this->load->view('concentforms/concentform');
            $this->load->view('includes/footer');
        }

    }


    public function opensigview(){

        $this->load->view('includes/header');
        $this->load->view('concentforms/signature');
        $this->load->view('includes/footer');

    }

    public function savesig(){

        $data=array(
            'sig_type'=> htmlspecialchars($this->input->post('sig_type')),
            'sig_data'=> htmlspecialchars($this->input->post('sig_data')),
            'customer_signature'=> htmlspecialchars($this->input->post('customer_signature'))
        );
        $this->db->insert('concent_forms', $data);

        
        echo 'done';

    }

    public function getsig(){
        $sql="SELECT CONCAT(sig_type, ',', CONVERT(sig_data USING utf8)) as sig_pair, customer_signature as sig_data 
        FROM concent_forms order by id_concent_forms desc;";

        $query=$this->db->query($sql);
        echo json_encode($query->row());

    }

    public function save_data(){
        //exit();
        $data=array(
            
            "form_title"=> $this->input->post('form_title')==null ? '' : urldecode($this->input->post('form_title')),
            "concent_form_id"=> $this->input->post('concent_form_id'),
            "customer_id"=> $this->input->post('customer_id'),
            "visit_id"=> $this->input->post('visit_id'),
            "customer_name"=> $this->input->post('customer_name')==null ? '' : urldecode($this->input->post('customer_name')),
            "customer_email"=> $this->input->post('customer_email')==null ? '' : urldecode($this->input->post('customer_email')),
            "customer_cell"=> $this->input->post('customer_cell') ==null ? '' : urldecode($this->input->post('customer_cell')),
            "customer_area"=> $this->input->post('customer_area') ==null ? '' : urldecode($this->input->post('customer_area')),
            "customer_signature"=> $this->input->post('customer_signature'),
            "concent_data"=>  $this->input->post('concent_data', false),
            "event_date"=> $this->input->post('event_date'),
            "visit_date"=> $this->input->post('visit_date'),
            "consultant_name"=> $this->input->post('consultant_name') ==null ? '' : urldecode($this->input->post('consultant_name')),
            "staff_id"=> $this->input->post('staff_id'),
            "customer_signature" => htmlspecialchars($this->input->post('customer_signature')),
            "consultant_signature" => htmlspecialchars($this->input->post('consultant_signature'))
        );

        $this->db->insert('concent_form_data', $data);
        $id = $this->db->insert_id();
        
        if($id>0){
            echo 'success|'.$id;
        }else{
            echo 'error|Form not saved';
        }
    }

    public function signedconcent($concentform_id = 0, $visit_id=0){

        $sql="select id_business, business_name, business_logo from business where id_business =".$this->session->userdata('businessid');
        $query = $this->db->query($sql);
        $data['business'] = $query->result_array(); 


        $sql="SELECT *, date_format(event_date,'%d-%m-%Y') as event_date, date_format(visit_date,'%d-%m-%Y') as visit_date from concent_form_data ";
        if($concentform_id > 0){
            $sql .= " WHERE id_concent_form_data = ". $concentform_id;
        } else {            
            $sql .= " WHERE visit_id = ". $visit_id;
        }
        

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

        $data['formdata']=$query->row();

        $this->load->view('includes/header', $data);
        $this->load->view('concentforms/signedconcent');
        $this->load->view('includes/footer');

    }

    public function add_concent_form(){

        $data= array(
            'category_id'=>$this->input->post('formcategory'),
            'concent_form'=>$this->input->post('formname'),
            'form_status'=>$this->input->post('formstatus')
        );
        $this->db->insert('concent_forms', $data);
        $id_concent_forms=$this->db->insert_id();

        echo 'success|'.$id_concent_forms;

    }

    public function add_structure_row(){

        $data= array(
            'seq'=>$this->input->post('seq'),
            'concent_form_id'=>$this->input->post('formid'),
            'section'=>$this->input->post('section'),
            'title'=>$this->input->post('title'),
            'option'=>$this->input->post('option'),
            'option_type'=>$this->input->post('optiontype')
        );
        $this->db->insert('concent_form_structure', $data);
        $id_concent_forms=$this->db->insert_id();

        echo 'success|'.$this->input->post('formid');

    }

    public function createformjson($formid=null){        
        
        $sql = "select * from concent_category";
        $query = $this->db->query($sql);
        $data['formcategory']=$query->result();

        $data['formdata']=null;
        $data['formid']=null;
        $data['formname']=null;
        $data['categoryid']=null;

        if($formid!=null){
            $this->db->select('*');            
            $this->db->where('id_concent_forms', $formid);
            $query1 = $this->db->get('concent_forms');
            $form = $query1->row();

            $this->db->select('*');            
            $this->db->where('concent_form_id', $formid);
            $this->db->order_by('seq');
            $query = $this->db->get('concent_form_structure');

            $data['formdata']=$query->result_array();
            $data['formid']=$formid;
            $data['formname']=$form->concent_form;
            $data['categoryid']=$form->category_id;
        }

        $this->load->view('includes/header', $data);
        $this->load->view('concentforms/createformjson');
        $this->load->view('includes/footer');
 
    }

    public function save_structure_json(){

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

        $structure = $this->input->post('table', TRUE);               
        //$structure = json_decode($structure,TRUE);
       
        $data = array(
            'category_id'=> $this->input->post('category'),
            'concent_form'=>$this->input->post('formname'),
            'form_structure'=>$myjson,                
            'form_status'=>$this->input->post('formstatus')
        );

        $this->db->insert('concent_forms', $data);
        $id_concent_forms=$this->db->insert_id();

        foreach($structure as $row){
            $data1 = array(
                'concent_form_id'=> $id_concent_forms,
                'seq'=> $row['seq'],
                'section'=> $row['section'],
                'title'=> $row['title'],
                'option'=> $row['option'],
                'option_type'=>$row['option_type']
            );
            $this->db->insert('concent_form_structure', $data1);
            $id_concent_structure=$this->db->insert_id();
        }

        
        echo 'success|'.$id_concent_forms;

    }


    public function update_structure_json(){

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

        $structure = $this->input->post('table', TRUE);               
        //$structure = json_decode($structure,TRUE);
       
        $data = array(
            'category_id'=> $this->input->post('category'),
            'concent_form'=>$this->input->post('formname'),
            'form_structure'=>$myjson,                
            'form_status'=>$this->input->post('formstatus')
        );
        $this->db->where('id_concent_forms', $this->input->post('formid'));
        $this->db->update('concent_forms', $data);

        echo 'success|1';

    }


    function save_edit_data(){
      

        $this->db->where('seq', $this->input->post('seq'));
        $this->db->where('concent_form_id', $this->input->post('form_id'));
        $this->db->set('section', $this->input->post('section'));
        $this->db->set('title', $this->input->post('title'));
        $this->db->set('option', $this->input->post('option'));
        $this->db->set('option_type', $this->input->post('optiontype'));

        $this->db->update('concent_form_structure');
        $id_concent_structure=$this->db->affected_rows();

        echo 'success|'.$this->input->post('form_id');
    }
}