<?php

class Colors_model extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function color_type_list() {
        $this->db->select('*');
        $this->db->where('color_types.business_id', $this->session->userdata('businessid'));
        $this->db->order_by('order_id','ASC');
        $query = $this->db->get('color_types');

        return $query->result_array();
    }

    function color_number_list() {
        $this->db->select('*');
        $this->db->where('color_number.business_id', $this->session->userdata('businessid'));
       // $this->db->order_by('order_id','ASC');
        $query = $this->db->get('color_number');

        return $query->result_array();
    }
    
    function water_content_list() {
        $this->db->select('*');
        $this->db->where('water_content.business_id', $this->session->userdata('businessid'));
       // $this->db->order_by('order_id','ASC');
        $query = $this->db->get('water_content');

        return $query->result_array();
    }
    
    
    function add_color_type() {

        $data = array(
            'type' => $this->input->post('type', TRUE),
            'order_id' => $this->input->post('orderid', TRUE),
            'business_id' => $this->session->userdata('businessid')
        );

        $this->db->insert('color_types', $data);
        return $this->db->insert_id();
    }

    function update_color_type() {

        $data = array(
            'type' => $this->input->post('type', TRUE),
            'status' => $this->input->post('status', TRUE)
        );

        $this->db->where('id', $this->input->post('id_type', TRUE));
        $this->db->where('business_id', $this->session->userdata('businessid'));
        $this->db->update('color_types', $data);

        return $this->db->affected_rows();
    }

    function get_all_color_number($type_id=null) {
        $this->db->select('*');
        $this->db->where('color_number.business_id', $this->session->userdata('businessid'));
        //$this->db->where('type_id=', $type_id);
        $query = $this->db->get('color_number');

        return $query->result_array();
    }

    function add_color_number() {

        $data = array(
            'number' => $this->input->post('color_number', TRUE),
            'type_id' => $this->input->post('type_id', TRUE),
            'business_id' => $this->session->userdata('businessid')
        );

        $this->db->insert('color_number', $data);
        return $this->db->insert_id();
    }

    function update_color_number() {

        $data = array(
            'number' => $this->input->post('color_number', TRUE),
            'status' => $this->input->post('status', TRUE),
            'business_id' => $this->session->userdata('businessid')
        );

        $this->db->where('id', $this->input->post('id_number', TRUE));
        $this->db->where('business_id', $this->session->userdata('businessid'));
        $this->db->update('color_number', $data);

        return $this->db->affected_rows();
    }

    function get_color_type_byid($id) {
        $this->db->select('*');
        $this->db->where('color_types.business_id', $this->session->userdata('businessid'));
        $this->db->where('id=', $id);
        $query = $this->db->get('color_types');
        
        
        return $query->result_array();
    }

    function get_color_records() {
        $this->db->select('*,DATE_FORMAT(date, "%d-%c-%Y") as date, datediff(now(), date) as daysago', False);
        $this->db->where('color_records.business_id', $this->session->userdata('businessid'));
        $this->db->order_by('id desc');
        $query = $this->db->get('color_records');
        
        
        
        return $query->result_array();
    }
    
    
    function get_customer_color_records($customer_id, $sh=false) {
        $this->db->select('*,DATE_FORMAT(date, "%d-%c-%Y") as date, datediff(now(), date) as daysago', False);
        $this->db->where('color_records.business_id', $this->session->userdata('businessid'));
        $this->db->where('color_records.customer_id', $customer_id);
        $this->db->order_by('daysago asc');
        if($sh==true ||  $this->session->userdata('role')=="Sh-Users"){
            $this->db->limit(5);
        }
        $query = $this->db->get('color_records');
        
        return $query->result_array();
    }
    
    function get_color_record($visit_id) {
        $this->db->select('*');
        $this->db->where('business_id', $this->session->userdata('businessid'));
        $this->db->where('customer_visit_id', $visit_id);
        $query = $this->db->get('color_records');
        return $query->result_array();
    }
    
    function get_facial_records($visit_id) {
        $this->db->select('*,DATE_FORMAT(date, "%d-%c-%Y") as date');
        $this->db->where('visit_id', $visit_id);
        $this->db->where('facial_records.business_id', $this->session->userdata('businessid'));
        $this->db->order_by('id desc');
        $query = $this->db->get('facial_records');

        return $query->result_array();
    }
    
    function get_eyelash_records($visit_id) {
        $this->db->select('*,DATE_FORMAT(created_date, "%d-%c-%Y") as date');
        $this->db->where('visit_id', $visit_id);
        $this->db->where('eyelashes_record.business_id', $this->session->userdata('businessid'));
        $this->db->order_by('id_eyelashes desc');
        $query = $this->db->get('eyelashes_record');

        return $query->result_array();
    }
    
    
    function color_record_add() {
      
        
        $rootsoptions = $this->input->post('rootsoptions');
        $lightsoptions= $this->input->post('lightsoptions');
        
        $myhtml='<ul style="list-style-type:none">';
        $myhtml.= '<li><strong>Base Color - Roots:</strong></li>';
        if(null !== $rootsoptions && $rootsoptions!==''){
            if(sizeof($rootsoptions)>0){
                foreach($rootsoptions as $ro){            
                    $myhtml.= '<li>'.$ro.'</li>';
                }
            }
         }
        $myhtml.= '<li><strong>Lights:</strong></li>';
        if(null !== $lightsoptions && $lightsoptions!==''){
            if(sizeof($lightsoptions)>0){
                foreach($lightsoptions as $lo){            
                    $myhtml.= '<li>'.$lo.'</li>';
                }
            }
        }
        $myhtml.='</ul>'; 
        
                
        $data = array(
            'customer_visit_id' => $this->input->post('customer_visit_id', TRUE),
            'customer_id' => $this->input->post('txtcustomer_id', TRUE),
            'customer' => $this->input->post('txtcustomer', TRUE),
            'color_type_id' => $this->input->post('txttype_id', TRUE),
            'color_type' => $this->input->post('txttype', TRUE),
            //'color_number_id' => $this->input->post('txttypenumber_id'),
            'color_number' => $this->input->post('txttypenumber', TRUE),
            'time' => $this->input->post('txttime', TRUE),
            'charge' => $this->input->post('txtcharge', TRUE),
            'remarks' => $myhtml.'</br>'.$this->input->post('txtremarks'),
            'recommendation' => $this->input->post('txtrecommendation'),
            'date' => $this->input->post('txtdate', TRUE),
            'water_content' => $this->input->post('water_content', TRUE),
            'business_id' => $this->session->userdata('businessid')
        );
        
        $this->db->insert('color_records', $data);
        
        $colorrecordid = $this->db->insert_id();
        
        if(null !== $rootsoptions && $rootsoptions!==''){
            if($colorrecordid){
                foreach($rootsoptions as $ro){
                    $data = array(
                        'color_record_id' => $colorrecordid,
                        'color_record_remark' => $ro,
                        'color_record_type' => 'RootsBase Color - Roots'
                    );
                    $this->db->insert('color_record_remarks', $data);
                }
            }
        }
        if(null !== $lightsoptions && $lightsoptions!==''){
            if($colorrecordid){
                foreach($lightsoptions as $lo){
                    $data = array(
                        'color_record_id' => $colorrecordid,
                        'color_record_remark' => $lo,
                        'color_record_type' => 'Lights'
                    );
                    $this->db->insert('color_record_remarks', $data);
                }
            }
        }
        
        return $colorrecordid;
    }
    
    function edit_color_record(){
        $this->db->select('*');
        $this->db->where('business_id', $this->session->userdata('businessid'));
        $this->db->where('id', $this->input->post('id', TRUE));
        $query = $this->db->get('color_records');
        return $query->row();
    }
    function edit_color_record_remarks(){
        $this->db->select('*');
        $this->db->where('color_record_id', $this->input->post('id', TRUE));
        $query = $this->db->get('color_record_remarks');
        return $query->result_array();
    }
    
            
    function color_record_update(){
        
        $rootsoptions = $this->input->post('rootsoptions');
        $lightsoptions= $this->input->post('lightsoptions');
        
        $myhtml='<ul style="list-style-type:none">';
        $myhtml.= '<li><strong>Base Color - Roots:</strong></li>';
        if(null !== $rootsoptions && $rootsoptions!=='') {if(sizeof($rootsoptions)>0){
            foreach($rootsoptions as $ro){            
                $myhtml.= '<li>'.$ro.'</li>';
            }
        }}
        $myhtml.= '<li><strong>Lights:</strong></li>';
        if(null !== $lightsoptions && $lightsoptions!==''){ 
            if(sizeof($lightsoptions)>0){
                foreach($lightsoptions as $lo){            
                    $myhtml.= '<li>'.$lo.'</li>';
                }
            }        
        }
        $myhtml.='</ul>'; 
        
        
        $data = array(
            'customer_id' => $this->input->post('txtcustomer_id', TRUE),
            'customer' => $this->input->post('txtcustomer', TRUE),
            'color_type_id' => $this->input->post('txttype_id', TRUE),
            'color_type' => $this->input->post('txttype', TRUE),
            //'color_number_id' => $this->input->post('txttypenumber_id'),
            'color_number' => $this->input->post('txttypenumber', TRUE),
            'time' => $this->input->post('txttime', TRUE),
            'charge' => $this->input->post('txtcharge', TRUE),
            'remarks' => $myhtml.'</br>'.$this->input->post('txtremarks'),
            'recommendation' => $this->input->post('txtrecommendation'),
            'date' => $this->input->post('txtdate', TRUE),
            'water_content' => $this->input->post('water_content', TRUE),
            'business_id' => $this->session->userdata('businessid')
        );
       // print_r($data);die;
        $this->db->where('id', $this->input->post('idcolor_record', TRUE));
        $this->db->where('business_id', $this->session->userdata('businessid'));
        $this->db->update('color_records', $data);
        $affected = $this->db->affected_rows();
        
        $colorrecordid = $this->input->post('idcolor_record', TRUE);
        
        $this->db->where('color_record_id',$colorrecordid);
        $this->db->delete('color_record_remarks');
        
        
        if(null !== $rootsoptions && $rootsoptions!==''){ 
            if($colorrecordid){
                foreach($rootsoptions as $ro){
                    $data = array(
                        'color_record_id' => $colorrecordid,
                        'color_record_remark' => $ro,
                        'color_record_type' => 'RootsBase Color - Roots'
                    );
                    $this->db->insert('color_record_remarks', $data);
                }
            }
        }
        if(null !== $lightsoptions && $lightsoptions!==''){ 
            if($colorrecordid && sizeof($lightsoptions)>0){
                foreach($lightsoptions as $lo){
                    $data = array(
                        'color_record_id' => $colorrecordid,
                        'color_record_remark' => $lo,
                        'color_record_type' => 'Lights'
                    );
                    $this->db->insert('color_record_remarks', $data);
                }
            }
        }
        return $affected;
    }
    
    function edit_color_types(){
        $this->db->select('*');
        $this->db->where('ct.business_id', $this->session->userdata('businessid'));
        $this->db->where('ct.id', $this->input->post('id_color_type'));
        $query = $this->db->get('color_types ct');

        return $query->row();
    }
    
    function edit_color_number(){
        $this->db->select('*');
        $this->db->where('cn.business_id', $this->session->userdata('businessid'));
        $this->db->where('cn.id', $this->input->post('id_color_number'));
        $query = $this->db->get('color_number cn');

        return $query->row();
    }
    
    function get_WaterContent(){
        $this->db->select('*');
        $query = $this->db->get_where('water_content', array('business_id' => $this->session->userdata('businessid')));
        return $query->result_array();
    }

    
    
    function get_countofcolor(){
        
        $result=0;
        
        $this->db->select('count(*) as count');
        ///Where
        $this->db->where('color_records.business_id', $this->session->userdata('businessid'));
        //$this->db->where('length(customers.customer_cell) =', 11);
       ///Where
        if(null !== $this->input->post('customer_name') && $this->input->post('customer_name')!==''){
            $this->db->like('color_records.customer', $this->input->post('customer_name'));
        }
        if(null !== $this->input->post('color_type') && $this->input->post('color_type')!==''){
            $this->db->like('color_records.color_type', $this->input->post('color_type'));
        }
        if(null !== $this->input->post('color_number') && $this->input->post('color_number')!==''){
            $this->db->like('color_records.color_number', $this->input->post('color_number'));
        }
        if(null !== $this->input->post('water_content') && $this->input->post('water_content')!==''){
            $this->db->like('color_records.water_content', $this->input->post('water_content'));
        }
        if(null !== $this->input->post('date') && $this->input->post('date')!==''){
            $this->db->like('color_records.date', $this->input->post('date'));
        }
        if(null !== $this->input->post('time') && $this->input->post('time')!==''){
            $this->db->like('color_records.time', $this->input->post('time'));
        }
        if(null !== $this->input->post('charge') && $this->input->post('charge')!==''){
            $this->db->like('color_records.charge', $this->input->post('charge'));
        }
        if(null !== $this->input->post('remarks') && $this->input->post('remarks')!==''){
            $this->db->like('color_records.remarks', $this->input->post('remarks'));
        }
        if(null !== $this->input->post('recommendation') && $this->input->post('recommendation')!==''){
            $this->db->like('color_records.recommendation', $this->input->post('recommendation'));
        }
        $query = $this->db->get('color_records');
       
        foreach ($query->result() as $row){
            $result=$row->count;
        }
        return $result;
        
    }
    
    function get_colorbysearch() {

//        
//        $this->db->select('*, DATE_FORMAT(date, "%d-%c-%Y") as date, datediff(now(), date) as daysago', False);
//        $this->db->where('color_records.business_id', $this->session->userdata('businessid'));
//        $this->db->order_by('id desc');
//        $query = $this->db->get('color_records');
        
 //       return $query->result_array();
        
        $today = date('Y-m-d');
        $tomorrow= date('Y-m-d H:i:s', strtotime('tomorrow'));
        
        
        //** get all table definitions **//
        $draw = $this->input->post('draw');
        $start = $this->input->post('start');
        $length = $this->input->post('length');
        
        if (Null!==$this->input->post('order')) {
            $order = $this->input->post('order');
            $columns = $this->input->post('columns');
            $orderby = $columns[$order[0]['column']]['data'];
            $orderdir = $order[0]['dir'];
        } else {
            $orderby = 'id';
            $orderdir = 'desc';
        }
        //openupdate
        
        $this->db->select('*, DATE_FORMAT(date, "%d-%c-%Y") as date, datediff(now(), date) as daysago, concat("<span style=\'cursor:pointer;\' class=\'text-primary\' onclick=\"openupdate(\'", color_records.id ,"\');\">","<button class=\'btn btn-sm btn-primary\'>edit</button>","</span>") action', False);
        $this->db->join('customers','customers.id_customers = color_records.customer_id');
                   
        $this->db->where('color_records.business_id', $this->session->userdata('businessid'));
        //$this->db->where('length(customers.customer_cell) =', 11);
        
        ///Where
        if(null !== $this->input->post('customer_name') && $this->input->post('customer_name')!==''){
            $this->db->like('customers.customer_name', $this->input->post('customer_name'));
        }
        if(null !== $this->input->post('customer_cell') && $this->input->post('customer_cell')!==''){
            $this->db->like('customers.customer_cell', $this->input->post('customer_cell'));
        }
        if(null !== $this->input->post('color_type') && $this->input->post('color_type')!==''){
            $this->db->like('color_records.color_type', $this->input->post('color_type'));
        }
        if(null !== $this->input->post('color_number') && $this->input->post('color_number')!==''){
            $this->db->like('color_records.color_number', $this->input->post('color_number'));
        }
        if(null !== $this->input->post('water_content') && $this->input->post('water_content')!==''){
            $this->db->like('color_records.water_content', $this->input->post('water_content'));
        }
        if(null !== $this->input->post('date') && $this->input->post('date')!==''){
            $this->db->like('color_records.date', $this->input->post('date'));
        }
        if(null !== $this->input->post('time') && $this->input->post('time')!==''){
            $this->db->like('color_records.time', $this->input->post('time'));
        }
        if(null !== $this->input->post('charge') && $this->input->post('charge')!==''){
            $this->db->like('color_records.charge', $this->input->post('charge'));
        }
        if(null !== $this->input->post('remarks') && $this->input->post('remarks')!==''){
            $this->db->like('color_records.remarks', $this->input->post('remarks'));
        }
        if(null !== $this->input->post('recommendation') && $this->input->post('recommendation')!==''){
            $this->db->like('color_records.recommendation', $this->input->post('recommendation'));
        }
        
        
        if(Null!==$this->input->post('order')){
            $this->db->order_by($orderby, $orderdir);
        }
        
        if($length>-1){
           $this->db->limit($length);
        }
        if($start>0){
            $this->db->offset($start);
        }
        
        $query = $this->db->get('color_records');
        
        return $query->result_array();
    }
    
    
}
