<?php

class Room_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 tbreserv.id, tbmember.depart_id FROM tbmember Inner Join tbreserv ON tbmember.id = tbreserv.member_id ";
            $sql.= " WHERE tbreserv.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('tbreserv')->num_rows();
        }

        return $count_wait;
    }

    //แสดงรายการจองรออนุมัติ ตามผู้อนุมัติ
    function getApproveByManager() {
        $s_login = $this->session->userdata('s_login');
        
        $data = array();
        if ($s_login['login_type'] == '2') {
            $sql = "SELECT tbreserv.id, tbreserv.meet_name, tbreserv.cdate, tbreserv.rdate, tbreserv.start_time, tbreserv.end_time,";
            $sql.= " tbreserv.status, tbroom.name as room_name, tbmember.depart_id FROM tbreserv Inner Join tbroom ON tbreserv.room_id = tbroom.id ";
            $sql.= " Inner Join tbmember ON tbreserv.member_id = tbmember.id WHERE tbreserv.status =  '0' ORDER BY tbreserv.rdate ASC";
            $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) {
                    $data[] = array(
                        'id' => $r['id'],
                        'meet_name' => $r['meet_name'],
                        'cdate' => $r['cdate'],
                        'rdate' => $r['rdate'],
                        'start_time' => $r['start_time'],
                        'end_time' => $r['end_time'],
                        'room_name' => $r['room_name']
                    );
                }
            }
        } else if ($s_login['login_type'] == '1') {
            $sql = "SELECT tbreserv.id, tbreserv.meet_name, tbreserv.cdate, tbreserv.rdate, tbreserv.start_time, tbreserv.end_time,";
            $sql.= " tbreserv.status, tbroom.name as room_name, tbmember.depart_id FROM tbreserv Inner Join tbroom ON tbreserv.room_id = tbroom.id ";
            $sql.= " Inner Join tbmember ON tbreserv.member_id = tbmember.id WHERE tbreserv.status =  '0' ORDER BY tbreserv.rdate ASC";
            $data = $this->db->query($sql)->result_array();
        }
        return $data;
    }

    function getApproveByManager_($status = '0', $start_date, $end_date, $txtsearch, $office, $per_page = 20, $url = '/approve/ajax_get_grid') {
        $sql = "SELECT tbreserv.id, tbreserv.meet_name, tbroom.name, tbreserv.rdate,";
        $sql.= " tbreserv.start_time, tbreserv.end_time, tbreserv.cdate FROM ";
        $sql.= " tbreserv Inner Join tbroom ON tbreserv.room_id = tbroom.id ";

        if ($status == '99') {
            $sql.= " Where tbroom.status<>='99'";
        } else {
            $sql.= " WHERE tbroom.status =  '$status'";
        }

        if ($start_date != '' && $end_date != '') {
            $sql.= " And (tbreserv.rdate >='" . $this->mydate->dateToMySql($start_date) . "' And tbreserv.rdate <='" . $this->mydate->dateToMySql($end_date) . "')";
        } else if ($start_date != '' && $end_date == '') {
            $sql.= " And (tbreserv.rdate >='" . $this->mydate->dateToMySql($start_date) . "')";
        } else if ($start_date == '' && $end_date != '') {
            $sql.= " And (tbreserv.rdate <='" . $this->mydate->dateToMySql($end_date) . "')";
        }

        if ($txtsearch != '') {
            $sql.= " And (tbreserv.meet_name like '%" . $txtsearch . "%')";
        }
        $total = $this->db->query($sql)->num_rows();

        $sql.=" order by tbreserv.rdate asc limit $office,$per_page";
        $query = $this->db->query($sql)->result_array();

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

}

?>
