<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Reportprovince extends MX_Controller {

    const TOKENPW = 'violet';

    public function __construct() {
        parent::__construct();
        $this->load->helper('cookie');
        $this->load->helper('language');
        $this->lang->load('messages', 'message');
    }

    public function index() {
        $data = array();
        $admin_info = $this->session->userdata('adminInfo');
        if ($admin_info) {
            $data['content'] = $this->getProvince();
            $this->load->view('reportprovince/index', $data);
        } else {
            $this->load->view('login');
        }
    }

    public function getProvince($filters = array()) {

        $this->load->model('reportmodel');
        $data['month'] = date("m");
        $data['year'] = date("Y");
        if ($this->input->post('month')) {
            $data['month'] = $this->input->post('month');
            $data['year'] = $this->input->post('year');
        }
        $data = $this->getdays($data);
        $data['provinces'] = $this->reportmodel->getProvinces($data);

        if ($this->input->is_ajax_request()) {
            return $this->load->view('reportprovince/province', $data);
        }
        return $this->load->view('reportprovince/province', $data, true);
    }

    public function getdays($data) {
        $data['month_days'] = 1;
        if ("" . $data['year'] . "-" . $data['month'] . "" == date("Y-m")) {
            $data['month_days'] = date("d");
        } else {
            $data['month_days'] = cal_days_in_month(CAL_GREGORIAN, $data['month'], $data['year']); // 31  
        }
        $data['year_days'] = 1;
        if ($data['year'] == date("Y")) {
            $data['year_days'] = date('z') + 1;
        } else {
            $data['year_days'] = date("z", mktime(0, 0, 0, 12, 31, $data['year'])) + 1;
        }
        return $data;
    }

    public function export() {
        $this->load->model('reportmodel');
        $data['month'] = $this->uri->segment(4);
        $data['year'] = $this->uri->segment(5);
        $data = $this->getdays($data);
        $data['provinces'] = $this->reportmodel->getProvinces($data);

        $this->load->library("PHPExcel");
        $sheet = $this->phpexcel->getActiveSheet();
        $sheet->setTitle("bao cao thue bao " . $data['month'] . " - " . $data['year']);
        $sheet->getColumnDimension('A')->setWidth(10);
        $sheet->getColumnDimension('B')->setWidth(25);
        $sheet->getColumnDimension('C')->setWidth(15);
        $sheet->getColumnDimension('D')->setWidth(15);
        $sheet->getColumnDimension('E')->setWidth(15);
        $sheet->getColumnDimension('F')->setWidth(15);
        $sheet->getColumnDimension('G')->setWidth(15);
        $sheet->getColumnDimension('H')->setWidth(15);
        $sheet->setCellValue('A1', "Báo cáo thuê bao theo tỉnh");
        $sheet->setCellValue('A2', "Tháng: " . $data['month'] . " - " . $data['year']);
        $sheet->setCellValue('C2', "Ngày: ".date('d')." - ".date('m')." - ". date('Y').""); 
        $sheet->setCellValue('A4', "STT");
        $sheet->setCellValue('B4', "Tỉnh/Thành phố");
        $sheet->setCellValue('C4', "TB Ngày");
        $sheet->setCellValue('D4', "Lũy kế TB tháng");
        $sheet->setCellValue('E4', "Tổng TB Ngày");
        $sheet->setCellValue('F4', "DT Ngày");
        $sheet->setCellValue('G4', "Lũy kế DT tháng");
        $sheet->setCellValue('H4', "Tổng DT năm");

        foreach ($data['provinces'] as $index => $provinces) {
            $sheet->setCellValue('A' . ($index + 5), $provinces['stt']);
            $sheet->setCellValue('B' . ($index + 5), $provinces['province']);
            $sheet->setCellValue('C' . ($index + 5), $provinces['tbng']);
            $sheet->setCellValue('D' . ($index + 5), $provinces['tbt']);
            $sheet->setCellValue('E' . ($index + 5), $provinces['tbn']);
            $sheet->setCellValue('F' . ($index + 5), $provinces['dtng']);
            $sheet->setCellValue('G' . ($index + 5), $provinces['dtt']);
            $sheet->setCellValue('H' . ($index + 5), $provinces['dtn']);
        }
        $writer = new PHPExcel_Writer_Excel5($this->phpexcel);
		header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename="thue_bao_' . $data['month'] . "_" . $data['year'] . '.xls"');
        $writer->save('php://output');
    }

}
