<?php
namespace backendApi\modules\v1\models\lists\bonus;

use backendApi\modules\v1\models\Admin;
use backendApi\modules\v1\models\User;
use common\models\User as modelUser;
use common\helpers\Cache;
use common\helpers\user\Info;
use common\libs\dataList\column\Price;
use common\libs\dataList\column\YesNo;
use common\libs\dataList\DataListInterface;
use common\models\PerfOrder;
use common\models\Region;
use common\models\UserBind;
use common\models\UserBonus;
use common\models\UserInfo;
use common\libs\dataList\column\DateTime;
use common\models\UserSystem;
use common\models\Withdraw;
use Yii;

class PerfOrderList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '业绩单列表';
    }

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle()
    {
        $data = PerfOrder::lists($this->condition, $this->params, [
            'select'=>'PO.*, U.USER_NAME, U.REAL_NAME',
            'from' => PerfOrder::tableName().' AS PO',
            'join' => [
                ['LEFT JOIN', modelUser::tableName() . ' AS U', 'PO.USER_ID=U.ID'],
            ],
            'orderBy' => 'PO.CREATED_AT DESC, PO.ID DESC',
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
        if($data['list']){
            foreach($data['list'] as $key=>$value){
                $data['list'][$key]['DEC_STATUS_NAME'] = PerfOrder::STATUS_NAME[$data['list'][$key]['DEC_STATUS']];
            }
        }
        $this->listData = $data;
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        if(!$this->columns){
            $this->columns = [
                'PERIOD_NUM' => [
                    'header' => 'Number of settlement periods',//结算期数
                    'headerOther' => [
                        'width' => '100',
                    ],
                ],
                'SN' => [
                    'header' => 'Performance sheet No',//业绩单编号
                    'value' => function($row) {
                        return "\t".$row['SN'];
                    },
                    'headerOther' => [
                        'width' => '250',
                    ],
                ],
                'DEC_TYPE' => [
                    'header' => 'Performance sheet type',//业绩单类型
                    'headerOther' => [
                        'width' => '110',
                    ],
                    'value' => function($row){
                        $decTypes = [
                            'ZC'=> 'First purchase order',//首购单
                            'YH'=> 'Coupon',//优惠单
                            'ZG'=> 'Additional purchase order',//增购单
                            'LS'=> 'Retail order',//零售单
                            'FX'=> 'Re sales order',//复销单
                        ];
                        return $decTypes[strtoupper($row['DEC_TYPE'])];
                    },
                ],
                'DEC_STATUS_NAME' => [
                    'header' => 'Performance sheet status',//业绩单状态
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'USER_NAME' => [
                    'header' => 'Member code',//会员编号
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'REAL_NAME' => [
                    'header' => 'Member name',//会员姓名
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'LAST_REC_USER_NAME' => [
                    'header' => 'Pioneer No. at settlement',//结算时开拓者编号
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'LAST_REC_REAL_NAME' => [
                    'header' => 'Pioneer name at settlement',//结算时开拓者姓名
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'PV' => [
                    'header' => 'Entry BV',//报单BV
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['PV'],
                        ]))->result();
                    },
                ],
                'DEC_AMOUNT' => [
                    'header' => 'Entry amount',//报单金额
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['DEC_AMOUNT'],
                        ]))->result();
                    },
                ],
                'DEC_SN' => [
                    'header' => 'Entry No',//报单编号
                    'headerOther' => [
                        'width' => '250',
                    ],
                ],
                'FROM_TABLES' => [
                    'header' => '订单来源',
                    'headerOther' => [
                        'width' => '250',
                    ],
                ],
                'CREATED_AT' => [
                    'header' => 'Creation time',//创建时间
                    'value' => function($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '170'],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'PERIOD_NUM'=> ['isUserTable'=> false, 'name'=> 'Number of settlement periods'],//结算期数
                'SN'=> ['isUserTable'=> false, 'name'=> 'Performance sheet No'],//业绩单编号
                'DEC_STATUS_NAME'=> ['isUserTable'=> false, 'name'=> 'Performance sheet status','other'=> 'select',
                 'selectData'=> [['id'=> 1, 'name'=> 'Reviewed'], ['id'=> 2, 'name'=> 'Waste disposal']],],//业绩单状态,已审核,置废
                'USER_NAME'=> ['isUserTable'=> false, 'name'=> 'Member code'],//会员编号
                'REAL_NAME'=> ['isUserTable'=> false, 'name'=> 'Member name'],//会员姓名
                'LAST_REC_USER_NAME'=> ['isUserTable'=> false, 'name'=> 'Pioneer No. at settlement'],//结算时开拓者编号
                'LAST_REC_REAL_NAME'=> ['isUserTable'=> false, 'name'=> 'Pioneer name at settlement'],//结算时开拓者姓名
                'PV'=> ['isUserTable'=> false, 'name'=> 'Entry BV'],//报单BV
                'DEC_AMOUNT'=> ['isUserTable'=> false, 'name'=> 'Entry amount'],//报单金额
                'DEC_SN'=> ['isUserTable'=> false, 'name'=> 'Entry No'],//报单编号
                'CREATED_AT'=> ['isUserTable'=> true, 'name'=> 'Creation time', 'other'=> 'date'],//创建时间
            ];
        }
        return $this->filterTypes;
    }
}