<?php

class Eyelashes_model extends CI_Model {

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

    function get_eyelashes_record() {
        $this->db->select('*');
        $this->db->where('business_id', $this->session->userdata('businessid'));
        $this->db->order_by('id_eyelashes', 'DESC');
        $query = $this->db->get('eyelashes_record');

        return $query->result_array();
    }

    function get_countofeyelash(){

        $result=0;
        
        $this->db->select('count(*) as count');
        ///Where
        $this->db->where('eyelashes_record.business_id', $this->session->userdata('businessid'));
       
        if(null !== $this->input->post('customer_name') && $this->input->post('customer_name')!==''){
            $this->db->like('customer_name', $this->input->post('customer_name'));
        }
        if(null !== $this->input->post('eyelash_type') && $this->input->post('eyelash_type')!==''){
            $this->db->like('eyelash_type', $this->input->post('eyelash_type'));
        }
        if(null !== $this->input->post('thickness') && $this->input->post('thickness')!==''){
            $this->db->like('thickness', $this->input->post('thickness'));
        }
        if(null !== $this->input->post('eyelength') && $this->input->post('eyelength')!==''){
            $this->db->like('length', $this->input->post('eyelength'));
        }
        if(null !== $this->input->post('curl') && $this->input->post('curl')!==''){
            $this->db->like('curl', $this->input->post('curl'));
        }
        if(null !== $this->input->post('full_set_refill') && $this->input->post('full_set_refill')!==''){
            $this->db->like('full_set_refill', $this->input->post('full_set_refill'));
        }
        if(null !== $this->input->post('created_date') && $this->input->post('created_date')!==''){
            $this->db->where('date(created_date)', $this->input->post('created_date'));
        }
        if(null !== $this->input->post('remarks') && $this->input->post('remarks')!==''){
            $this->db->like('remarks', $this->input->post('remarks'));
        }
        $query = $this->db->get('eyelashes_record');
       
        //echo $query; exit();
        foreach ($query->result() as $row){
            $result=$row->count;
        }
        return $result;

    }
    function get_eyelash_list_ajax(){


         //** 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_eyelashes';
             $orderdir = 'asc';
         }

         $this->db->select('id_eyelashes, customer_name,  eyelash_type, thickness, length, curl, full_set_refill, created_date date, price, remarks');
        $this->db->select('concat("<span style=\'cursor:pointer;\' class=\'text-primary\' onclick=\"openupdate(\'", eyelashes_record.id_eyelashes ,"\');\">","<button class=\'btn btn-sm btn-info\'><i class=\'fa fa-icon fa-keyboard-o\'></i></button>","</span>") action');        
        $this->db->where('eyelashes_record.business_id', $this->session->userdata('businessid'));
        
        if(null != $this->input->post('customer_name') && $this->input->post('customer_name')!=''){
            $this->db->like('customer_name', $this->input->post('customer_name'));
        }
        if(null != $this->input->post('eyelash_type') && $this->input->post('eyelash_type')!=''){
            $this->db->like('eyelash_type', $this->input->post('eyelash_type'));
        }
        if(null !== $this->input->post('thickness') && $this->input->post('thickness')!==''){
            $this->db->like('thickness', $this->input->post('thickness'));
        }
        if(null !== $this->input->post('length') && $this->input->post('length')!==''){
            $this->db->like('length', $this->input->post('length'));
        }
        if(null !== $this->input->post('curl') && $this->input->post('curl')!==''){
            $this->db->like('curl', $this->input->post('curl'));
        }
        if(null !== $this->input->post('full_set_refill') && $this->input->post('full_set_refill')!==''){
            $this->db->like('full_set_refill', $this->input->post('full_set_refill'));
        }
        
        if(null !== $this->input->post('created_date') && $this->input->post('created_date')!==''){
            $this->db->where('date(created_date)', $this->input->post('created_date'));
        }

        if(null !== $this->input->post('remarks') && $this->input->post('remarks')!==''){
            $this->db->like('remarks', $this->input->post('remarks'));
        }

        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('eyelashes_record');

        //print_r($query->result_array()); exit();
        return $query->result_array();
    }


    function eyelash_type_list(){
        
        $this->db->select('*');
        $this->db->where('business_id', $this->session->userdata('businessid'));
        $this->db->order_by('id_eyelash_types', 'DESC');
        $query = $this->db->get('eyelash_types');

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

        return $query->result_array();
    }
    
    function get_customer_eyelashes_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('customer_id', $customer_id);
        $this->db->where('business_id', $this->session->userdata('businessid'));
        $this->db->order_by('daysago asc');
        if($sh==true ||  $this->session->userdata('role')=="Sh-Users"){
            $this->db->limit(5);
        }
        $query = $this->db->get('eyelashes_record');

        return $query->result_array();
    }
    
    function edit_eyelashes_record() {
        $this->db->select('*');
        $this->db->where('eyelashes_record.business_id', $this->session->userdata('businessid'));
        $this->db->where('eyelashes_record.id_eyelashes', $this->input->post('id_eyelashes', TRUE));
        $query = $this->db->get('eyelashes_record');

        return $query->row();
    }

    function update_eyelashes() {
        $lengths=$this->input->post('length', TRUE);
        $selectedlengths="";
        foreach($lengths as $length){
            if($selectedlengths!==""){$selectedlengths=$selectedlengths.",";}
            $selectedlengths=$selectedlengths.$length;
        }
        $data = array(
            'customer_id' => $this->input->post('customer_id', TRUE),
            'customer_name' => $this->input->post('customer_name', TRUE),
            'eyelash_type' => $this->input->post('eyelash_type', TRUE),
            'thickness' => $this->input->post('thickness', TRUE),
            'length' => $selectedlengths,
            'curl' => $this->input->post('curl', TRUE),
            'date' => $this->input->post('date', TRUE),
            'price' => $this->input->post('price', TRUE),
            'full_set_refill' => $this->input->post('fullsetrefill', TRUE),
            'remarks' => $this->input->post('remarks', TRUE)
        );
        
        $this->db->where('eyelashes_record.id_eyelashes', $this->input->post('id_eyelashes', TRUE));
        $this->db->where('eyelashes_record.business_id', $this->session->userdata('businessid'));
        $this->db->update('eyelashes_record', $data);
        
        return $this->db->affected_rows();
    }

    function add_eyelashes() {
        $lengths=$this->input->post('length', TRUE);
        $selectedlengths="";
        foreach($lengths as $length){
            if($selectedlengths!==""){$selectedlengths=$selectedlengths.",";}
            $selectedlengths=$selectedlengths.$length;
        }
        
        $data = array(
            'customer_id' => $this->input->post('customer_id', TRUE),
            'customer_name' => $this->input->post('customer_name', TRUE),
            'eyelash_type' => $this->input->post('eyelash_type', TRUE),
            'thickness' => $this->input->post('thickness', TRUE),
            'length' => $selectedlengths,
            'curl' => $this->input->post('curl', TRUE),
            'date' => $this->input->post('date', TRUE),
            'full_set_refill' => $this->input->post('fullsetrefill', TRUE),
            'remarks' => $this->input->post('remarks', TRUE),
            'visit_id' => $this->input->post('visitid', TRUE),
            'price' => $this->input->post('price', TRUE),
            'business_id' => $this->session->userdata('businessid')
        );
        
        $this->db->insert('eyelashes_record', $data);
        return $this->db->insert_id();
    }

}
