<?php

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

class ReportAsistant extends MX_Controller {

    const TOKENPW = 'violet';

    public function __construct() {
        parent::__construct();
        $this->load->helper('cookie');
    }

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

    public function viewAsistant() {
        $data = array();

        $admin_info = $this->session->userdata('adminInfo');
        if ($admin_info) {
            $id = $this->uri->segment(3);
            $data['id'] = $id;
            $data['statistics'] = $this->getStatistics($id);
            $data['content'] = $this->getCollaboratorsUsers(array(), $id);
            $this->load->view('reportAsistant/indexCollaborator', $data);
        } else {
            $this->load->view('login');
        }
    }

    public function getStatistics($id = false) {
        $data = array();

        $this_year['start'] = strtotime(date("Y") . "-01-01 00:00:00");

        $this->load->model('collabolator_model');
        $collaborators = $this->collabolator_model->getAllCollaboratorUser($this_year['start'], $id);
        for ($i = 1; $i <= 12; $i++) {
            $statistics[$i] = 0;
        }
        $y = date("Y");
        foreach ($collaborators as $collaborator) {
            for ($i = 1; $i <= 12; $i++) {
                if ($i < 12) {
                    if (($collaborator['time'] > strtotime($y . "-" . $i . "-1 00:00:00")) && (($collaborator['time'] < strtotime($y . "-" . ($i + 1) . "-1 00:00:00")))) {
                        $statistics[$i] ++;
                    }
                } else {
                    if (($collaborator['time'] > strtotime($y . "-" . $i . "-1 00:00:00")) && (($collaborator['time'] < strtotime(($y + 1) . "-1-1 00:00:00")))) {
                        $statistics[$i] ++;
                    }
                }
            }
        }

        $max = 0;
        foreach ($statistics as $index => $total) {
            if ($total > $max) {
                $max = $total;
            }
            $chart[] = array($index, $total);
        }

        $data['max'] = $max;
        $chard[] = array("color" => "#e67e22", "label" => "Lượng người dùng giới thiệu bởi cộng tác viên theo từng tháng", "data" => $chart);
        $data['chard'] = $chard;
         
        return $this->load->view('reportAsistant/statistics', $data, true);
    }

    public function getLastquarter() {
        $current_month = date('m');
        $current_year = date('Y');

        if ($current_month >= 1 && $current_month <= 3) {
            $start_date = strtotime('1-October-' . ($current_year - 1));  // timestamp or 1-October Last Year 12:00:00 AM
            $end_date = strtotime('1-Janauary-' . $current_year);  // // timestamp or 1-January  12:00:00 AM means end of 31 December Last year
        } else if ($current_month >= 4 && $current_month <= 6) {
            $start_date = strtotime('1-January-' . $current_year);  // timestamp or 1-Janauray 12:00:00 AM
            $end_date = strtotime('1-April-' . $current_year);  // timestamp or 1-April 12:00:00 AM means end of 31 March
        } else if ($current_month >= 7 && $current_month <= 9) {
            $start_date = strtotime('1-April-' . $current_year);  // timestamp or 1-April 12:00:00 AM
            $end_date = strtotime('1-July-' . $current_year);  // timestamp or 1-July 12:00:00 AM means end of 30 June
        } else if ($current_month >= 10 && $current_month <= 12) {
            $start_date = strtotime('1-July-' . $current_year);  // timestamp or 1-July 12:00:00 AM
            $end_date = strtotime('1-October-' . $current_year);  // timestamp or 1-October 12:00:00 AM means end of 30 September
        }
        return array("start" => $start_date, "end" => $end_date);
    }

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

        $this->load->helper('pagging');
        $this->load->model('collabolator_model');
        $data['current_page'] = $this->uri->segment(4, 1);
        $data['itemsoptions'] = array(10, 25, 50, 100);
        $data['perpage'] = 10;
        $data['keyword'] = "";
        $data['sorting_order'] = "sorting_desc";
        $data['sorting_field'] = "id";

        if ($this->input->post('sorting_order')) {
            if ($this->input->post('sorting_order') != "sorting") {
                $data['sorting_order'] = $this->input->post('sorting_order');
                $data['sorting_field'] = $this->input->post('sorting_field');
            }
        }
        if ($this->input->post('items')) {
            $data['perpage'] = $this->input->post('items');
        }
        if ($this->input->post('keyword')) {
            $data['search_field'] = "";
            $data['keyword'] = $this->input->post('keyword');

            if (preg_match("/[0-9]/", $data['keyword'])) {
                $data['search_field'] = "cellphone";
            }
        }

        $data['start'] = ($data['current_page'] - 1) * $data['perpage'];
        $data['total'] = $this->collabolator_model->countCountCollaboratorUser($data);
        $data['paidlogs'] = $this->collabolator_model->getCountCollaboratorUser($data);

        $data['paging_url'] = base_url() . "/admin/thong_ke_cong_tac_vien/trang/";
        $data['num_links'] = 2;
        $data['paging'] = pagging($data);

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

    public function getCollaboratorsUsers($filters = array(), $id = false) {

        $this->load->helper('pagging');
        $this->load->model('collabolator_model');
        $data['current_page'] = $this->uri->segment(5, 1);

        $data['itemsoptions'] = array(10, 25, 50, 100);
        $data['perpage'] = 10;
        if (!$id) {
            $data['id'] = $this->uri->segment(4, 1);
        } else {
            $data['id'] = $id;
        }

        $data['keyword'] = "";
        $data['sorting_order'] = "sorting_desc";
        $data['sorting_field'] = "id";

        if ($this->input->post('sorting_order')) {
            if ($this->input->post('sorting_order') != "sorting") {
                $data['sorting_order'] = $this->input->post('sorting_order');
                $data['sorting_field'] = $this->input->post('sorting_field');
            }
        }
        if ($this->input->post('items')) {
            $data['perpage'] = $this->input->post('items');
        }
        if ($this->input->post('keyword')) {
            $data['search_field'] = "";
            $data['keyword'] = $this->input->post('keyword');

            if (preg_match("/[0-9]/", $data['keyword'])) {
                $data['search_field'] = "cellphone";
            }
        }

		$data['daterange_start'] = "";
		$data['daterange_end'] = "";
		if ($this->input->post('daterange')){
			$daterange = explode(" - ", $this->input->post('daterange'));
			$daterange[0] = str_replace('/', '-', $daterange[0]);
			$daterange[1] = str_replace('/', '-', $daterange[1]);
			$data['daterange_start'] = date('Y-m-d H:i:s', strtotime($daterange[0]));
			$data['daterange_end'] = date('Y-m-d H:is:s', strtotime($daterange[1].' + 1 day'));
		}
		
        $data['start'] = ($data['current_page'] - 1) * $data['perpage'];
        $data['total'] = $this->collabolator_model->countCountCollaboratorUsers($data);
        $data['paidlogs'] = $this->collabolator_model->getCountCollaboratorUsers($data);
        foreach ($data['paidlogs'] as $index=>$paidlog)
        {
            $data['paidlogs'][$index]['username']=$this->get_fullname($paidlog['us_id']);
        }
        $data['paging_url'] = base_url() . "/admin/thong_ke_cong_tac_vien/trang_ctv/" . $data['id'];
        $data['num_links'] = 2;
        $data['paging'] = pagging($data);

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

    private function get_fullname($us_id) {
        $fullname="";
        $src = 'violet';
        $token = md5($us_id . self::TOKENPW);
        $this->load->model('user_model');
        $data = $this->user_model->get_user_info($src, $us_id, $token);
        parse_str($data,$data);
        if (isset($data['fullname']))
        {
            $fullname=$data['fullname'];
        }
        return $fullname;
    }

    public function test() {


        for ($i = 1; $i <= 5000; $i++) {
            $input['us_id'] = rand(1, 100);
            $input['paid_type'] = rand(1, 2);
            $input['amount'] = rand(3, 10) * 1000;
            $time = (rand((time() - (12 * 30 * 24 * 60 * 60)), time()));
            $input['paid_time'] = date("Y-m-d H:i:s", $time);
            $this->load->model('paidlog_model');
            $this->paidlog_model->insert($input);
        }

        die();

        for ($i = 1; $i <= 100; $i++) {
            $input['us_id'] = $i;
            $input['cellphone'] = "01234567" . (100 + $i);
            $input['collaborator'] = rand(1, 100);
            $input['acc_balanced'] = rand(100000, 1000000);
            $input['expire_date'] = date("Y-m-d H:i:s", time());
            $input['created_time'] = date("Y-m-d H:i:s", time());
            $input['updated_time'] = date("Y-m-d H:i:s", time());
            $this->load->model('user_model');
            $this->user_model->insert($input);
        }
    }

}
