<?php

class Car_m extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function get_all($txtsearch, $offset, $per_page = 20, $url = '/car/ajax_get_grid') {
        if ($txtsearch != '') {
            $this->db->like('code', $txtsearch);
            $this->db->or_like('brand', $txtsearch);
            $this->db->or_like('model', $txtsearch);
        }
        $total = $this->db->get('tbcar')->num_rows();

        if ($txtsearch != '') {
            $this->db->like('code', $txtsearch);
            $this->db->or_like('brand', $txtsearch);
            $this->db->or_like('model', $txtsearch);
        }
        $this->db->limit($per_page, $offset);
        $this->db->order_by('code', 'asc');
        $query = $this->db->get('tbcar')->result_array();

        $this->load->library('pagination');
        $data['pagination'] = $this->pagination->pagin($total, $url, $per_page);
        $data['query'] = $query;
        return $data;
    }

    function get_carcode($car_id) {
        $carcode = '';
        $this->db->where('id', $car_id);
        $row = $this->db->get('tbcar')->row_array();
        if (isset($row['id'])) {
            $carcode = $row['code'];
        }

        return $carcode;
    }

    /*
     * ตรวจสอบรถที่ต้องการว่างหรือไม่
     * return true = ว่าง, false = ไม่ว่าง
     */

    function car_busy($car_id, $rdate, $start_time, $end_time) {
        $this->load->library('mydate');

        $this->db->where('id', $car_id);
        $car = $this->db->get('tbcar')->row_array();
        $car_code = $car['code'];

        $this->db->where('car_id', $car_id);
        $this->db->where('rdate', $rdate);
        $this->db->where('status <> ', '2');
        $this->db->where('status <> ', '3');
        $result = $this->db->get('tbcarreserv')->result_array();

        $ret = $this->car_busy_process($result, $rdate, $start_time, $end_time, $car_code);
        $msg = $ret['msg'];
        $busy = $ret['busy'];
        $msg_short = $ret['msg_short'];

        //ตรวจสอบที่ตาราง tbbooking_car ด้วย
        if ($busy) {
            $sql = "SELECT tbbooking_car.car_id, tbbooking_car.rdate, tbbooking_car.start_time, tbbooking_car.end_time,";
            $sql.= " tbcarreserv.status, tbcarreserv.member_id FROM tbcarreserv Inner Join tbbooking_car ON tbbooking_car.doc_id = tbcarreserv.id";
            $sql.= " Where tbbooking_car.car_id='$car_id' And tbbooking_car.rdate='$rdate' And tbcarreserv.status<>'2' And tbcarreserv.status<>'3'";
            $result = $this->db->query($sql)->result_array();

            $ret = $this->car_busy_process($result, $rdate, $start_time, $end_time, $car_code);
            $msg = $ret['msg'];
            $busy = $ret['busy'];
            $msg_short = $ret['msg_short'];
        }

        if ($busy) {
            if ($this->db->table_exists('tmp_carbooking')) {
                $this->db->where('car_id', $car_id);
                $this->db->where('rdate', $rdate);
                $result = $this->db->get('tmp_carbooking')->result_array();

                $ret = $this->car_busy_process($result, $rdate, $start_time, $end_time, $car_code);
                $msg = $ret['msg'];
                $busy = $ret['busy'];
                $msg_short = $ret['msg_short'];
            }
        }

        $data = array(
            'msg' => $msg,
            'busy' => $busy,
            'msg_short' => $msg_short
        );
        return $data;
    }

    function car_busy_process($result, $rdate, $start_time, $end_time, $car_code) {
        $msg = 'รถว่างสามารถจองได้';
        $busy = true;
        $msg_short = '';

        foreach ($result as $r) {
            $member = $this->get_member($r['member_id']);
            $member_txt = " <b>ผู้จอง:</b> " . $member['name'] . " ติดต่อ " . $member['mobile'] . " ," . $member['tel'];

            $db_start_time = $r['start_time'];
            $db_end_time = $r['end_time'];

            if ($start_time >= $db_start_time) {
                if ($start_time <= $db_end_time) {
                    if ($r['status'] == '0') {
                        $msg = "<b>$car_code</b> ณ .วันที่ " . $this->mydate->dateToText($rdate) . " ในช่วงเวลา " . $r['start_time'] . " - " . $r['end_time'] . " อยู่ระหว่างการรออนุมัติ ไม่สามารถจองได้ในขณะนี้ $member_txt <br/>";
                        $msg_short = "&nbsp;&nbsp;&nbsp; " . $r['start_time'] . " - " . $r['end_time'] . " อยู่ระหว่างการรออนุมัติ";
                        $busy = false;
                    } else if ($r['status'] == '1') {
                        $msg = "<b>$car_code</b> ไม่ว่าง ณ .วันที่ " . $this->mydate->dateToText($rdate) . " ในช่วงเวลา " . $r['start_time'] . " - " . $r['end_time'] . " $member_txt <br/>";
                        $msg_short = "&nbsp;&nbsp;&nbsp; " . $r['start_time'] . " - " . $r['end_time'] . " ไม่ว่าง";
                        $busy = false;
                    }
                }
            } else if ($start_time <= $db_start_time && $end_time >= $db_start_time) {
                if ($end_time <= $db_end_time) {
                    if ($r['status'] == '0') {
                        $msg = "<b>$car_code</b> ณ .วันที่ " . $this->mydate->dateToText($rdate) . " ในช่วงเวลา " . $r['start_time'] . " - " . $r['end_time'] . " อยู่ระหว่างการรออนุมัติ ไม่สามารถจองได้ในขณะนี้ $member_txt <br/>";
                        $msg_short = "&nbsp;&nbsp;&nbsp; " . $r['start_time'] . " - " . $r['end_time'] . " อยู่ระหว่างการรออนุมัติ";
                        $busy = false;
                    } else if ($r['status'] == '1') {
                        $msg = "<b>$car_code</b> ไม่ว่าง ณ .วันที่ " . $this->mydate->dateToText($rdate) . " ในช่วงเวลา " . $r['start_time'] . " - " . $r['end_time'] . " $member_txt <br/>";
                        $msg_short = "&nbsp;&nbsp;&nbsp; " . $r['start_time'] . " - " . $r['end_time'] . " ไม่ว่าง";
                        $busy = false;
                    }
                }
            }

            if ($start_time <= $db_start_time && $end_time >= $db_end_time) {
                if ($r['status'] == '0') {
                    $msg = "<b>$car_code</b> ณ .วันที่ " . $this->mydate->dateToText($rdate) . " ในช่วงเวลา " . $r['start_time'] . " - " . $r['end_time'] . " อยู่ระหว่างการรออนุมัติ ไม่สามารถจองได้ในขณะนี้ $member_txt <br/>";
                    $msg_short = "&nbsp;&nbsp;&nbsp; " . $r['start_time'] . " - " . $r['end_time'] . " อยู่ระหว่างการรออนุมัติ";
                    $busy = false;
                } else if ($r['status'] == '1') {
                    $msg = "<b>$car_code</b> ไม่ว่าง ณ .วันที่ " . $this->mydate->dateToText($rdate) . " ในช่วงเวลา " . $r['start_time'] . " - " . $r['end_time'] . " $member_txt <br/>";
                    $msg_short = "&nbsp;&nbsp;&nbsp; " . $r['start_time'] . " - " . $r['end_time'] . " ไม่ว่าง";
                    $busy = false;
                }
            }
        }

        $data = array(
            'msg' => $msg,
            'busy' => $busy,
            'msg_short' => $msg_short,
        );
        return $data;
    }

    function get_member($member_id) {
        $this->db->where('id', $member_id);
        $row = $this->db->get('tbmember')->row_array();

        $data = array();

        if (isset($row['id'])) {
            $data = array(
                'name' => $row['name'],
                'mobile' => $row['mobile'],
                'tel' => $row['tel']
            );
        }

        return $data;
    }

    /*
     * ดึงรายการรถที่ว่างทั้งหมดที่สามารถจองได้ ในช่วงวัน เวลาที่ต้องการ
     * แต่จะยังไม่เช็คสถานะการกำหนดการจองล่วงหน้าว่าต้องจองก่อนกี่นาที
     */

    function get_all_carbusy($rdate, $start_time, $end_time) {
        $this->db->where('status', '0');
        $query = $this->db->get('tbcar')->result_array();

        $result = array();
        foreach ($query as $r) {
            $ret = $this->car_busy($r['id'], $rdate, $start_time, $end_time);
            $hours_dif = $this->mydate->dateTimeDiff(date("Y-m-d H:i"), "$rdate $start_time");

            $ok = true;
            $ok_txt = '';
            if ($r['pre_hours'] > 0) {
                if ($hours_dif < $r['pre_hours']) {
                    $ok = false;
                    $ok_txt = 'จองล่วงหน้า ' . $r['pre_hours'] . " ชม.";
                }
            } else {
                if ($hours_dif < $r['pre_hours']) {
                    $ok = false;
                    $ok_txt = "จองย้อนหลังไม่ได้";
                }
            }
            //if ($ret['busy']) {
            if ($ret['busy'] == false) {
                $ok = false;
                $ok_txt = $ret['msg_short'];
            }
            $result[] = array(
                'id' => $r['id'],
                'code' => $r['code'],
                'brand' => $r['brand'],
                'model' => $r['model'],
                'pre_hours' => $r['pre_hours'],
                'ok' => $ok,
                'ok_txt' => $ok_txt
            );
            //}
        }

        return $result;
    }

}

?>
