<?php

class Approve extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('mydate');
        $this->load->model('room_booking_m');

        $s_imeeting['activemenu'] = '001';
        $this->session->set_userdata('s_imeeting', $s_imeeting);
    }

    function index() {
        $data['query'] = $this->room_booking_m->getApproveByManager();
        $data['mydate'] = $this->mydate;

        $data['page_title'] = "รายการจองห้องประชุม รออนุมัติ";
        $data['content'] = 'approve/approve_lst_v';
        $this->load->view('template_v', $data);
    }

    function edit($id = 0, $success = 0) {
        $sql = "SELECT tbreserv.id, tbreserv.meet_name, tbreserv.chairman, tbreserv.cdate, tbreserv.room_id, tbreserv.rdate, tbreserv.start_time,";
        $sql.= " tbreserv.end_time,tbreserv.amount_people,tbreserv.equip,tbreserv.contact_name,tbreserv.contact_tel,tbreserv.contact_email,";
        $sql.= " tbreserv.employee,tbreserv.meet_detail,tbreserv.status,tbreserv.status_detail,tbreserv.approve_time,tbreserv.chairman,";
        $sql.= " tbroom.name as room_name,tbreserv.member_id,tbmember.name as member_name,tbmember.mobile,tbmember.tel,tbdepart.name as depart_name ";
        $sql.= " ,tbreserv.file_tmpname,tbreserv.file_name FROM tbroom";
        $sql.= " Inner Join tbreserv ON tbreserv.room_id = tbroom.id Inner Join tbmember ON tbreserv.member_id = tbmember.id ";
        $sql.= " Inner Join tbdepart ON tbmember.depart_id = tbdepart.id WHERE tbreserv.id =  '$id'";
        $row = $this->db->query($sql)->row_array();
        

        $sql = "SELECT tbequip.name FROM tbbooking_equip Inner Join tbequip ON tbbooking_equip.equip_id = tbequip.id";
        $sql.= " WHERE tbbooking_equip.doc_id =  '" . $row['id'] . "'";
        $equip = $this->db->query($sql)->result_array();
        $data['equip'] = $equip;

        $sql = "SELECT tbroom.name as room_name, tbbooking.rdate,tbbooking.start_time, tbbooking.end_time FROM tbbooking";
        $sql.=" Inner Join tbroom ON tbbooking.room_id = tbroom.id WHERE tbbooking.doc_id =  '" . $row['id'] . "' AND tbbooking.wtype =  '0'";
        $sql.=" ORDER BY tbbooking.rdate ASC";
        $booking = $this->db->query($sql)->result_array();
        $data['booking'] = $booking;

        if ($success == '1') {
            $data['msg_text'] = 'บันทึกผลการอนุมัติเรียบร้อย';
        }

        $row['sendemail'] = '0';
        $cnf = $this->db->get('tbconfig')->row_array();
        if (isset($cnf['id'])) {
            if ($cnf['sendemail'] == '1') {
                $row['sendemail'] = '1';
            }
        }
        
        $data['row'] = $row;
        $data['mydate'] = $this->mydate;
        $data['page_title'] = "รายการจองห้องประชุม รออนุมัติ";
        $data['content'] = 'approve/approve_frm_v';
        $this->load->view('template_v', $data);
    }

    function save() {
        $this->load->model('room_m');
        $this->load->model('email_m');

        $s_login = $this->session->userdata('s_login');
        $id = $this->input->post('id');
        
        $data = array(
            'status' => $this->input->post('status'),
            'status_detail' => $this->input->post('status_detail'),
            'approve_time' => date("Y-m-d H:i:s"),
            'approve_id'=>$s_login['login_id']
        );
        $this->db->where('id', $id);
        $this->db->update('tbreserv', $data);

        //ส่งอีเมลล์แจ้งผลการอนุมัติ
        $this->db->where('id', $this->input->post('member_id'));
        $member = $this->db->get('tbmember')->row_array();

        if ($member['email'] != '') {

            $this->db->where('id', $id);
            $res = $this->db->get('tbreserv')->row_array();

            $subject = 'แจ้งผลการอนุมัติจองห้องประชุม';
            $message = '<b>วาระการประชุม</b> ' . $res['meet_name'] . "<br/>";
            $message.= '<b>วันที่จอง</b> ' . $this->mydate->dateToText($res['rdate']) . " <b>เวลา</b> " . $res['start_time'] . " <b>ถึง</b> " . $res['end_time'];
            $message.= " <b>ห้องประชุม</b> " . $this->room_m->get_roomname($res['room_id']) . "<br/>";

            $this->db->where('doc_id', $id);
            $this->db->where('wtype', '0');
            $booking = $this->db->get('tbbooking')->result_array();
            foreach ($booking as $b) {
                $message.= "วันที่ </b>" . $this->mydate->dateToText($b['rdate']) . " <b>เวลา</b> " . $b['start_time'] . " <b>ถึง</b> " . $b['end_time'];
                $message.= " <b>ห้องประชุม</b> " . $this->room_m->get_roomname($b['room_id']) . "<br/>";
            }

            $message.= "<b>ผู้จอง</b> " . $member['name'] . " <b>ติดต่อ</b> " . $member['mobile'] . ", " . $member['tel'] . "<br/>";
            $message.= "<b>ผลการอนุมัติ </b>=> " . $this->get_statusname($this->input->post('status')) . " <=";
            $message.= "<br/><b>เหตุผล</b> " . nl2br($this->input->post('status_detail'));
            $message.= "<br/><b>วันที่อนุมัติ</b> " . date("Y-m-d H:i");
            $this->email_m->sendmail($member['email'], 'ระบบ iMeeting', $member['email'], $subject, $message, $member['id']);
        }
        redirect("approve/edit/$id/1");
    }

    function get_statusname($status) {
        $st_name = 'รอการอนุมัติ';
        switch ($status) {
            case '1':
                $st_name = 'อนุมัติ';
                break;
            case '2':
                $st_name = 'ไม่อนุมัติ';
                break;
            case '3':
                $st_name = 'ยกเลิกจอง';
                break;
        }
        
        return $st_name;
    }

}

?>
