<?php

class Chart extends MY_Controller {

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

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

    function index() {
        $data['page_title'] = "กราฟ";
        $data['content'] = 'chart/chart_v';
        $this->load->view('template_v', $data);
    }

    function room() {
        $data['extraHeadContent'] = $this->script_m->calendar();
        $data['extraHeadContent'].= $this->script_m->textformat();
        $data['extraHeadContent'].= $this->script_m->highchart();

        $first_date = strftime('%Y') . '-' . strftime('%m') . '-01';
        $data['start_date'] = $this->mydate->dateToText($first_date);
        $data['end_date'] = $this->mydate->lastDate();



        $data['page_title'] = "กราฟแสดงสถิติการใช้ห้องประชุม";
        $data['content'] = 'chart/roomchart_v';
        $this->load->view('template_v', $data);
    }

    function ajax_get_roomchart() {
        $start_date = $this->input->post('start_date');
        $end_date = $this->input->post('end_date');

        $title_text = "สถิติการใช้ห้องประชุม ระหว่างวันที่ " . $start_date . " ถึง " . $end_date;
        $data['title_text'] = $title_text;

        $this->db->order_by('name');
        $team = $this->db->get('tbteam')->result_array();

        $categories_data = array();
        $series = array();
        $i = 0;
        foreach ($team as $r) {
            $i++;
            array_push($categories_data, $r['name']);
            
            $count = $this->count_roomuse_byteam($r['id'], $this->mydate->dateToMySql($start_date), $this->mydate->dateToMySql($end_date));
            array_push($series, $count);
        }
        
        
        $data['categories_data'] = json_encode($categories_data);

        $series_data[] = array('name' => 'หน่วยงาน', 'data' => $series);
        $data['series_data'] = json_encode($series_data);

        $this->load->view('chart/roomchart_show_v', $data);
    }

    //นับจำนวนใช้ห้อง
    function count_roomuse_byteam($team_id, $start_date, $end_date) {
        $i1 = 0;
        $i2 = 0;

        $sql = "SELECT tbdepart.team_id FROM tbreserv";
        $sql.= " Inner Join tbmember ON tbreserv.member_id = tbmember.id";
        $sql.= " Inner Join tbdepart ON tbmember.depart_id = tbdepart.id";
        $sql.= " WHERE (tbreserv.status =  '1') And (tbdepart.team_id='$team_id')";
        $sql.= " And (tbreserv.rdate >= '$start_date' And tbreserv.rdate <= '$end_date')";
        $i1 = $this->db->query($sql)->num_rows();

        $sql = "SELECT tbdepart.team_id FROM tbreserv";
        $sql.= " Inner Join tbmember ON tbreserv.member_id = tbmember.id";
        $sql.= " Inner Join tbdepart ON tbmember.depart_id = tbdepart.id";
        $sql.= " Inner Join tbbooking ON tbbooking.doc_id = tbreserv.id";
        $sql.= " WHERE (tbreserv.status =  '1') And (tbdepart.team_id='$team_id')";
        $sql.= " And (tbbooking.rdate >= '$start_date' And tbbooking.rdate <= '$end_date')";
        $i2 = $this->db->query($sql)->num_rows();

        $count = $i1 + $i2;
        return $count;
    }
    
    function car() {
        $data['extraHeadContent'] = $this->script_m->calendar();
        $data['extraHeadContent'].= $this->script_m->textformat();
        $data['extraHeadContent'].= $this->script_m->highchart();

        $first_date = strftime('%Y') . '-' . strftime('%m') . '-01';
        $data['start_date'] = $this->mydate->dateToText($first_date);
        $data['end_date'] = $this->mydate->lastDate();



        $data['page_title'] = "กราฟแสดงสถิติการใช้รถ";
        $data['content'] = 'chart/carchart_v';
        $this->load->view('template_v', $data);
    }

    function ajax_get_carchart() {
        $start_date = $this->input->post('start_date');
        $end_date = $this->input->post('end_date');

        $title_text = "สถิติการใช้รถ ระหว่างวันที่ " . $start_date . " ถึง " . $end_date;
        $data['title_text'] = $title_text;

        $this->db->order_by('name');
        $team = $this->db->get('tbteam')->result_array();

        $categories_data = array();
        $series = array();
        $i = 0;
        foreach ($team as $r) {
            $i++;
            array_push($categories_data, $r['name']);
            
            $count = $this->count_caruse_byteam($r['id'], $this->mydate->dateToMySql($start_date), $this->mydate->dateToMySql($end_date));
            array_push($series, $count);
        }
        
        
        $data['categories_data'] = json_encode($categories_data);

        $series_data[] = array('name' => 'หน่วยงาน', 'data' => $series);
        $data['series_data'] = json_encode($series_data);

        $this->load->view('chart/carchart_show_v', $data);
    }

    //นับจำนวนใช้ห้อง
    function count_caruse_byteam($team_id, $start_date, $end_date) {
        $i1 = 0;
        $i2 = 0;

        $sql = "SELECT tbdepart.team_id FROM tbcarreserv";
        $sql.= " Inner Join tbmember ON tbcarreserv.member_id = tbmember.id";
        $sql.= " Inner Join tbdepart ON tbmember.depart_id = tbdepart.id";
        $sql.= " WHERE (tbcarreserv.status =  '1') And (tbdepart.team_id='$team_id')";
        $sql.= " And (tbcarreserv.rdate >= '$start_date' And tbcarreserv.rdate <= '$end_date')";
        $i1 = $this->db->query($sql)->num_rows();

        $sql = "SELECT tbdepart.team_id FROM tbcarreserv";
        $sql.= " Inner Join tbmember ON tbcarreserv.member_id = tbmember.id";
        $sql.= " Inner Join tbdepart ON tbmember.depart_id = tbdepart.id";
        $sql.= " Inner Join tbbooking_car ON tbbooking_car.doc_id = tbcarreserv.id";
        $sql.= " WHERE (tbcarreserv.status =  '1') And (tbdepart.team_id='$team_id')";
        $sql.= " And (tbbooking_car.rdate >= '$start_date' And tbbooking_car.rdate <= '$end_date')";
        $i2 = $this->db->query($sql)->num_rows();

        $count = $i1 + $i2;
        return $count;
    }

}

?>
