<?php

class Api_model extends CI_Model {

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

    public function getOnlineStaff($businessid) {

        $this->db->select('*');
        $this->db->where('staff_firstname', 'Online');
        $this->db->where('business_id', $businessid);
        $query = $this->db->get('staff');
        return $query->row();
    }

    public function getcustomerbycell($cell) {  
        $resp = $this->db->query("SELECT * FROM customers WHERE customer_cell=" . $cell);
        return $resp->row();
    }

    public function getcustomerbyid($id) {
        $resp = $this->db->query("SELECT * FROM customers WHERE id_customers=" . $id);
        return $resp->row();
    }

    public function addnewcustomer($cell, $firstname, $lastname, $gender, $email, $businessid) {

        $data = array(
            'business_id' => $businessid,
            'customer_cell' => $cell,
            'customer_name' => $firstname . ' ' . $lastname,
            'customer_gender' => $gender,
            'customer_email' => $email,
            'created_by' => 'OnLine'
        );
        $this->db->insert('customers', $data);
        return $this->db->insert_id();
    }

    public function add_visit($customer_id, $start, $businessid, $comment, $visit_id = 0) {
        if ($visit_id == 0) {
            //$color = explode('|', $this->get_color_for_visit($last_color_code));
            $color = explode('|', $this->get_color_for_visit($start, $businessid));

            $visit = array(
                'business_id' => $businessid,
                'customer_id' => $customer_id,
                'visit_color' => $color[0],
                'visit_color_type' => $color[1],
                'created_by' => 'Online',
                'advance_comment' => $comment
            );
            $this->db->insert('customer_visits', $visit);
            $visit_id = $this->db->insert_id();
            return $visit_id;
        } else {
            return $visit_id;
        }
    }

    function add_visit_services($customer_visit_id, $customer_id, $customer_name, $services, $products, $staff, $start, $businessid, $loyaltyservice = null) {

        $this->db->select('id_business, business_opening_time, business_closing_time, rec_allow_prev as previous');
        $this->db->where('id_business', $businessid);
        $query = $this->db->get('business');
        $business = $query->row();

        $business_end_time = date("H:i:s", strtotime($business->business_closing_time . ":00"));

        $i = 0;
        $lastduration = 0;
        $newtime = '';

        foreach ($services as $service) {
            //get service details
            $this->db->select('id_business_services as service_id, service_name, service_duration, "servicetype" as service_flag, service_category_id');
            $this->db->where('id_business_services', $service);
            $query = $this->db->get('business_services');
            $service_details = $query->row();

            if (sizeof($service_details) == 0) {

                //service does not exist on the system
                $this->db->select('id_business_services as service_id, service_name, service_duration, "servicetype" as service_flag, service_category_id');
                $this->db->where('service_name', 'to be selected later');
                $query = $this->db->get('business_services');
                $service_details = $query->row();
            }


            if (isset($service_details)) {
                if ($i > 0) {

                    $durCal = explode(':', $lastduration);

                    $HoursInMin = 0;
                    if (intval($durCal[0]) > 0) {
                        $HoursInMin = intval($durCal) * 60;
                    }
                    $newduration = $HoursInMin + $durCal[1];
                    $newduration = date_interval_create_from_date_string($newduration . ' minutes');
                    $newstart = date_create($newtime);
                    $newtime = date_add($newstart, $newduration);
                    $newtime = date_format($newtime, "c");
                    $d = explode('+', (string) $newtime);
                    $newtime = $d[0];
                    //check business closing time
                    $thisstart = explode('T', $newtime);
                    if ($thisstart[1] >= $business_end_time) {
                        $newtime = $start;
                    }

                    //check if duration is present
                    if ($service_details->service_duration == "00:00:00") {
                        $lastduration = "00:15:00";
                    } else {
                        $lastduration = $service_details->service_duration;
                    }
                } else {
                    $newtime = str_replace(' ', 'T', $start);
                    $lastduration = $service_details->service_duration;
                }




                if (null !== $loyaltyservice) {
                    $data = array(
                        'business_id' => $businessid,
                        'customer_visit_id' => $customer_visit_id,
                        'service_id' => $service_details->service_id,
                        'service_name' => $service_details->service_name,
                        'service_flag' => $service_details->service_flag,
                        'id_service_category' => $service_details->id_service_category,
                        'visit_service_start' => $newtime, //$start
                        'update_date' => date('Y-m-d H:i:s'),
                        'loyalty_service' => $loyaltyservice
                    );
                } else {
                    $data = array(
                        'business_id' => $businessid,
                        'customer_visit_id' => $customer_visit_id,
                        'service_id' => $service_details->service_id,
                        'service_name' => $service_details->service_name,
                        'service_flag' => $service_details->service_flag,
                        'id_service_category' => $service_details->service_category_id,
                        'visit_service_start' => $newtime, //$start
                        'update_date' => date('Y-m-d H:i:s')
                    );
                }

                $this->db->insert('visit_services', $data);
                $id_visit_services = $this->db->insert_id();

                //Staff
                $data = array(
                    'business_id' => $businessid,
                    'customer_visit_id' => $customer_visit_id,
                    'visit_service_id' => $id_visit_services,
                    'staff_id' => $staff,
                    'staff_name' => 'Online Appointment',
                    'requested' => 'No',
                    'block_other' => 'No'
                );
                //var_dump($data);
                $this->db->insert('visit_service_staffs', $data);

                
                $i++;
            }
        }

        return $customer_visit_id;
    }

    function get_color_for_visit($start, $businessid) {
        $today = explode(' ', $start);

        $sql = "
           select visit_color_code, visit_color_type from customer_visit_colors where visit_color_code not in(
            SELECT distinct(visit_color) 
            FROM customer_visits
            join visit_services on customer_visits.id_customer_visits = visit_services.customer_visit_id
            where date_format(visit_services.visit_service_start,'%Y-%m-%d') = '" . $today[0] . "'
            and visit_status != 'cancelled' and customer_visits.business_id = " . $businessid . " order by id_visit_colors desc
            ) Limit 1";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $row = $query->row();
            return $row->visit_color_code . '|' . $row->visit_color_type;
        } else {
            //return the first color
            $this->db->select('visit_color_code, visit_color_type');
            //$this->db->where('business_id', $this->session->userdata('businessid'));
            $this->db->limit(1);
            $query = $this->db->get('customer_visit_colors');
            $row = $query->row();
            return $row->visit_color_code . '|' . $row->visit_color_type;
        }
    }

    // public function checkMobileNumber($cell) {
    //     $resp = $this->db->query("SELECT * FROM customers WHERE customer_cell=" . $cell);
    //     if ($resp->num_rows() > 0) {
    //         $row = $resp->result_array();
    //         return $this->printResponse("true", "Number Registerd", "user", $row);
    //     } else {
    //         return $this->printResponse("false", "No User Found with this Cell Number");
    //     }
    // }

    
    public function getPriceList($business_id){
        
        
        $sql = "SELECT business_name AS 'Branch', 'Single Service' AS 'Delivery', replace(service_type,'/','-') AS 'Type', replace(service_category,'/','-') AS 'Category', replace(service_name,'/','-') AS 'Service', 
                business_services.service_duration, round(business_services.service_rate,2) AS 'Price', id_service_types, id_service_category, id_business_services, CONCAT('".  base_url() ."assets/images/servicetype/', service_type_image) as service_type_image
                FROM service_type
                join service_category on service_category.service_type_id = service_type.id_service_types
                join business_services on business_services.service_category_id = service_category.id_service_category
                join business on business.id_business = business_services.business_id
                Where service_type.service_type_active='Yes'
                AND service_category.service_category_active='Yes'
                AND business_services.service_active='Yes'
                AND business_services.business_id = ".$business_id.";";
        $query = $this->db->query($sql);
        $data1 = $query->result_array();

        $sql1 = "SELECT business_name AS 'Branch', 'Package' AS 'Delivery', replace(service_type,'/','-') AS 'Type', replace(service_category,'/','-') AS 'Category', replace(service_name,'/','-') AS 'Service', 
                business_services.service_duration, round(business_services.service_rate,2) AS 'Price', id_package_type as id_service_types, id_package_category as id_service_category, id_business_services, CONCAT('".  base_url() ."assets/images/packagetype/', service_category_image) as service_type_image
                 FROM package_type
                join package_category on package_category.package_type_id = package_type.id_package_type
                join package_services on package_services.package_category_id = package_category.id_package_category
                join business_services on business_services.id_business_services = package_services.service_id
                join business on business.id_business = business_services.business_id
                Where package_type.service_type_active='Yes'
                AND package_category.service_category_active='Yes'
                AND business_services.service_active='Yes'
                AND business_services.business_id = ".$business_id.";";
        
       
        $query1 = $this->db->query($sql1);
        $data2 = $query1->result_array();
        
        $data = array_merge($data1, $data2);
        
        return $data;
        //return $query->result_array();
    }

   
}
