<?php

class Car_booking_m extends CI_Model {

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

    //นับจำนวนรอ การอนุมัติ
    function countWait() {
        $count_wait = 0;
        //wheare จากผุ้มีสิทธิ์อนุมัติ แต่ถ้าเป็น admin เห็นหมด
        $s_login = $this->session->userdata('s_login');
        if ($s_login['login_type'] == '2') {
            $sql = "SELECT tbcarreserv.id, tbmember.depart_id FROM tbmember Inner Join tbcarreserv ON tbmember.id = tbcarreserv.member_id ";
            $sql.= " WHERE tbcarreserv.status =  '0'";
            $query = $this->db->query($sql)->result_array();
            foreach ($query as $r) {
                $this->db->where('member_id', $s_login['login_id']);
                $this->db->where('depart_id', $r['depart_id']);
                if ($this->db->get('tbmembertodepart')->num_rows() > 0) {
                    $count_wait++;
                }
            }
        } else if ($s_login['login_type'] == '1') {
            $this->db->where('status', '0');
            $count_wait = $this->db->get('tbcarreserv')->num_rows();
        }

        return $count_wait;
    }

    //แสดงรายการจองรออนุมัติ ตามผู้อนุมัติ
    function getApproveByManager() {
        $s_login = $this->session->userdata('s_login');

        $sql = "SELECT tbcarreserv.id, tbcarreserv.name, tbcarreserv.cdate, tbcarreserv.rdate, tbcarreserv.start_time, tbcarreserv.end_time,";
        $sql.= " tbcarreserv.status, tbcar.code as car_code, tbmember.depart_id FROM tbcarreserv Inner Join tbcar ON tbcarreserv.car_id = tbcar.id ";
        $sql.= " Inner Join tbmember ON tbcarreserv.member_id = tbmember.id WHERE tbcarreserv.status =  '0' ORDER BY tbcarreserv.rdate ASC";
        $query = $this->db->query($sql)->result_array();

        $data = array();
        if ($s_login['login_type'] == '2') {


            foreach ($query as $r) {
                $this->db->where('member_id', $s_login['login_id']);
                $this->db->where('depart_id', $r['depart_id']);
                if ($this->db->get('tbmembertodepart')->num_rows() > 0) {
                    $data[] = array(
                        'id' => $r['id'],
                        'name' => $r['name'],
                        'cdate' => $r['cdate'],
                        'rdate' => $r['rdate'],
                        'start_time' => $r['start_time'],
                        'end_time' => $r['end_time'],
                        'car_code' => $r['car_code']
                    );
                }
            }
        } else if ($s_login['login_type'] == '1') {
            $data = $this->db->query($sql)->result_array();
        }
        return $data;
    }

}

?>
