<?php

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

class Report 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->getPaidlogs();
            $this->load->view('report/index', $data);
        } else {
            $this->load->view('login');
        }
    }

    public function getStatistics() {
        $this->load->model('collabolator_model');
        $data = $this->collabolator_model->getStatistics();
        return $this->load->view('report/statistics', $data, true);
    }


    public function getPaidlogs($filters = array()) {
        $this->load->helper('pagging');
        $this->load->model('paidlog_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'] = "paid_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->paidlog_model->countPaidlog($data);
        $data['paidlogs'] = $this->paidlog_model->getPaidlogs($data);

        $data['paging_url'] = base_url() . "/admin/doanh_thu_tong_hop/trang/";
        $data['num_links'] = 2;
        $data['paging'] = pagging($data);
        foreach ($data['paidlogs'] as $index => $paidlog) {
            $data['paidlogs'][$index]['username'] = @$this->get_fullname($paidlog['us_id']); 
        }

        if ($this->input->is_ajax_request()) {
            return $this->load->view('report/listview', $data);
        }
        return $this->load->view('report/listview', $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 <= 200; $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());
            $time = (rand((time() - (12 * 30 * 24 * 60 * 60)), 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);
        }
    }

}
