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

class QuotaMonthList extends WP_List_Table{
    function column_default($item, $column_name)
    {
        switch($column_name){
            case "quota":
                return "<input type='text' name='".$item["id"]."' value='".$item["quota"]."'><button type='button' class='button action'>修改</button>";
                break;
            default:
                return $item[$column_name];
        }
    }

    function get_columns()
    {
        $columns['year'] = '年份';
        $columns['month'] = '月份';
        $columns['name'] = '姓名';
        $columns['quota'] = '目标业绩';
        return $columns;
    }

    function get_views(){
        global $wpdb;
        $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
        $current_url = remove_query_arg( array( 'paged','type'), $current_url );
        $sql = "select COUNT(*) as totle,COUNT(NULLIF(quota is not null, false)) as isset,COUNT(NULLIF(quota is null, false)) as unset from ".Config::TOSPUR_CONSULTANT." tc
            left join ".Config::TOSPUR_QUOTA_TABLE." tq on tq.consultantId = tc.id and tq.year = %d and tq.month = %d
            where subsidiaryId = %d";
        $result = $wpdb->get_row($wpdb->prepare($sql,$_REQUEST["year"],$_REQUEST["month"],$_REQUEST["oid"]));

        return array(
            "all" => '<a href="'.$current_url.'"'.(isset($_REQUEST["type"])?"":'class="current"').'>全部<span class="count">（'.$result->totle.'）</span></a>',
            "isset" => '<a href="'.$current_url.'&type=2" '.($_REQUEST["type"]==2?'class="current"':"").'>已设置业绩<span class="count">（'.$result->isset.'）</span></a>',
            "unset" => '<a href="'.$current_url.'&type=1" '.($_REQUEST["type"]==1?'class="current"':"").'>未设置业绩<span class="count">（'.$result->unset.'）</span></a>'
        );
    }

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

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

        $data = array();
        if(isset($_REQUEST["year"]) && isset($_REQUEST["month"]) && isset($_REQUEST["oid"])){
            $sql = "select tc.id,name,IFNULL(quota,0) as quota from ".Config::TOSPUR_CONSULTANT." tc
            left join ".Config::TOSPUR_QUOTA_TABLE." tq on tq.consultantId = tc.id and tq.year = %d and tq.month = %d
            where subsidiaryId = %d";
            if(isset($_REQUEST['type'])){
                $type = $_REQUEST['type'];
                $sql .= " and quota is";
                if($type == 2){
                    $sql .= " not";
                }
                $sql .=" null";
            }
            $result = $wpdb->get_results($wpdb->prepare($sql,$_REQUEST["year"],$_REQUEST["month"],$_REQUEST["oid"]));
            foreach($result as $key => $value){
                $data[$key] = array(
                    'id' => $value->id,
                    'year' => $_REQUEST["year"],
                    'month' => $_REQUEST["month"],
                    'name' => $value->name,
                    'quota' => $value->quota
                );
            }
        }

        $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' => ceil($total_items / $per_page)   //WE have to calculate the total number of pages
        ));
    }

    public static function replaceQuota(){
        $result = QuotaDao::replace($_REQUEST["year"],$_REQUEST["month"],$_REQUEST["id"],$_REQUEST["price"]);
        $res = array(
            "code"=>200,
            "msg"=>$result
        );
        if(!is_numeric($result)){
            $res["code"] = 500;
        }
        wp_send_json($res);
    }
} 