<?php

class Car extends MY_Controller {

    public $page_title = 'ข้อมูลรถ';

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

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

    function index() {
        $data['page_title'] = $this->page_title;
        $data['content'] = 'car/car_v';
        $this->load->view('template_v', $data);
    }

    function news() {
        $data['extraHeadContent'] = $this->script_m->formatCurrency();
        $data['extraHeadContent'].= $this->script_m->mytab();
        $data['extraHeadContent'].= $this->script_m->lightbox();

        $data['page_title'] = $this->page_title . " (เพิ่มข้อมูล)";
        $data['content'] = 'car/car_frm_v';
        $this->load->view('template_v', $data);
    }

    function edit($id, $success = '0') {
        $this->db->where('id', $id);
        $row = $this->db->get('tbcar')->row_array();
        $data['row'] = $row;
        if ($success == '1') {
            $data['msg_text'] = 'บันทึกข้อมูเสร็จเรียบร้อย';
        }

        $data['extraHeadContent'] = $this->script_m->formatCurrency();
        $data['extraHeadContent'].= $this->script_m->mytab();
        $data['extraHeadContent'].= $this->script_m->lightbox();

        $data['page_title'] = $this->page_title;
        $data['content'] = 'car/car_frm_v';
        $this->load->view('template_v', $data);
    }

    function ajax_validate() {
        $id = trim($_POST['id']);
        $code = trim($_POST['code']);

        $data['msg'] = '0';
        if ($this->db_m->validate('tbcar', $id, 'code', $code)) {
            $data['msg'] = '1';
            $data['msg_text'] = 'ทะเบียนรถนี้มีการบันทึกข้อมูลแล้ว ไม่สามารถสร้างซ้ำได้';
        }
        echo json_encode($data);
    }

    function save() {
        $id = $this->input->post('id');

        $data = array(
            'code' => $this->input->post('code'),
            'brand' => $this->input->post('brand'),
            'model' => $this->input->post('model'),
            'detail' => $this->input->post('detail'),
            'status' => $this->input->post('status'),
            'status_text' => $this->input->post('status_text'),
            'oilstatus'=>  number_format($this->input->post('oilstatus'),2)
        );

        if ($id == '') {
            $this->db->insert('tbcar', $data);
            $id = $this->db->insert_id();
        } else {
            $this->db->where('id', $id);
            $this->db->update('tbcar', $data);
        }
        redirect("car/edit/$id/1");
    }

    function ajax_get_grid($offset = 0) {
        $txtsearch = $this->input->post('txtsearch');

        $array = $this->car_m->get_all($txtsearch, $offset);
        $data['query'] = $array['query'];
        $data['pagination'] = $array['pagination'];
        $this->load->view('car/car_grid_v', $data);
    }

    function ajax_delete() {
        $this->db->where('id', $this->input->post('id'));
        $this->db->delete('tbcar');
        echo true;
    }

    function upload() {
        $this->load->library('myfile');
        $id = $this->input->post('id');

        for ($i = 0; $i < count($_FILES["fileUpload"]["name"]); $i++) {
            if ($_FILES["fileUpload"]["name"][$i] != "") {
                // rename
                $file_name = basename($_FILES['fileUpload']['name'][$i]);
                $ext_file = explode(".", $file_name);
                $ext_file = $ext_file[count($ext_file) - 1];
                $file_name = $id . "_" . $i . "." . $ext_file;

                move_uploaded_file($_FILES["fileUpload"]["tmp_name"][$i], $this->myfile->GetPhysicalFromURL() . "assets/upload_car/" . $file_name);
                $j = $i + 1;
                $data['picture' . $j] = $file_name;
                $this->db->where('id', $id);
                $this->db->update('tbcar', $data);
            }
        }

        redirect("car/edit/$id/1");
    }

    function ajax_carbusy_set_session() {
        $this->load->library('mydate');
        $s_carbusy = array(
            'rdate' => $this->mydate->dateToMySql($this->input->post('rdate')),
            'start_time' => $this->input->post('start_time'),
            'end_time' => $this->input->post('end_time')
        );
        $this->session->set_userdata('s_carbusy', $s_carbusy);
        echo true;
    }

    function carbusy_dlg() {
        $s_carbusy = $this->session->userdata('s_carbusy');

        $rdate = $s_carbusy['rdate'];
        $start_time = $s_carbusy['start_time'];
        $end_time = $s_carbusy['end_time'];

        $result = $this->car_m->get_all_carbusy($rdate, $start_time, $end_time);
        $data['query'] = $result;
        $data['content'] = 'car/carbusy_dlg_v';
        $this->load->view('template_modal_window_v', $data);
    }

}

?>
