<?php
if (!class_exists('WP_List_Table')) {
    require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}

class QuotaYearList extends WP_List_Table
{

    function __construct()
    {
    }

    function column_default($item, $column_name)
    {
        return '<a href="' . admin_url('admin.php?page=quotaList&edit=true&year='. $item['year'] .'&month=' . $item['month'] . '&oid='. $item['oid']) .'&name='. $item['name'] .'">' . $item[$column_name] . '</a>';
    }

    function get_columns()
    {
        $columns['year'] = '年份';
        $columns['month'] = '月份';
        $columns['name'] = '分店名';
        $columns['totle'] = '分店总目标';
        return $columns;
    }

    function prepare_items()
    {
        global $wpdb;
        $per_page = 12;
        $columns = $this->get_columns();
        $hidden = array();

        $this->_column_headers = array($columns, $hidden);

        $data = array();
        if(isset($_REQUEST["year"]) && isset($_REQUEST["organization"])){
            $sql = "select *, month,sum(quota) as quota from ".Config::TOSPUR_QUOTA_TABLE." where consultantId in(select id from ".Config::TOSPUR_CONSULTANT."
            where subsidiaryId = %d) and year = %d group by year,month;";
            $result = DBManager::get_results($wpdb->prepare($sql,$_REQUEST["organization"],$_REQUEST["year"]));
            $name = DBManager::get_var($wpdb->prepare("select Name from tospur_organization where id = %d;",$_REQUEST["organization"]));
            $j = 0;
            for($i = 1;$i<=12;$i++){
                $item = $result[$j];
                $totle = 0;
                if($item->month==$i){
                    $totle = $item->quota;
                    $j++;
                }
                $data[$i] = array(
                    'oid' => $_REQUEST["organization"],
                    'year' => $_REQUEST["year"],
                    'month' => $i,
                    'name' => $name,
                    'totle' => $totle."元"
                );
            }
        }
        $current_page = $this->get_pagenum();

        $total_items = count($data);

        $data = array_slice($data, (($current_page - 1) * $per_page), $per_page);

        $this->items = $data;

        $this->set_pagination_args(array(
            'total_items' => $total_items,                  //WE have to calculate the total number of items
            'per_page' => $per_page,                     //WE have to determine how many items to show on a page
            'total_pages' => 1   //WE have to calculate the total number of pages
        ));
    }

    function showHtml(){
        $context = array();
        $context['req'] = $_REQUEST;
        if(isset($_REQUEST['edit'])){
            $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
            $current_url = remove_query_arg( array( 'paged'), $current_url );
            $context['current_url'] = $current_url;
            Timber::render("quotaMonthList.html",$context);
        }else{
            Timber::render("quotaYearList.html",$context);
        }
    }

    function displayMonthTable(){
        $_SERVER['REQUEST_URI'] = remove_query_arg( '_wp_http_referer', $_SERVER['REQUEST_URI'] );
        $quotaMonthList = new QuotaMonthList();
        $quotaMonthList->prepare_items();
        $quotaMonthList->views();
        $quotaMonthList->display();
    }

    function displayYearTable()
    {
        $_SERVER['REQUEST_URI'] = remove_query_arg( '_wp_http_referer', $_SERVER['REQUEST_URI'] );
        $quotaYearList = new QuotaYearList();
        $quotaYearList->prepare_items();
        $quotaYearList->display();
    }
}