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

use backendApi\modules\v1\models\Admin;
use backendApi\modules\v1\models\User;
use common\models\Countries;
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, C.NAME AS COUNTRY',
            'from' => PerfOrder::tableName().' AS PO',
            'join' => [
                ['INNER JOIN', modelUser::tableName() . ' AS U', 'PO.USER_ID=U.ID'],
                ['INNER JOIN', Countries::tableName() . ' AS C', 'U.COUNTRY_ID=C.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' => Yii::t('ctx', 'payCycle'), // 结算期数
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'SN' => [
                    'header' => Yii::t('ctx', 'salesNo'),//业绩单编号
                    'value' => function($row) {
                        return "\t".$row['SN'];
                    },
                    'headerOther' => [
                        'width' => '220',
                    ],
                ],
                'DEC_TYPE' => [
                    'header' =>  Yii::t('ctx', 'saleType'), // 业绩单类型
                    'headerOther' => [
                        'width' => '160',
                    ],
                    'value' => function($row){
                        $decTypes = [
                            'ZC'=> 'Welcome Pack Order', // 首购单
                            'YH'=> 'Coupon', // 优惠单
                            'ZG'=> 'Additional Purchase Order', // 增购单
                            'LS'=> 'Retail Order', // 零售单
                            'FX'=> 'Re Sales Order', // 复销单
                        ];
                        return $decTypes[strtoupper($row['DEC_TYPE'])];
                    },
                ],
                'DEC_STATUS_NAME' => [
                    'header' => Yii::t('ctx', 'saleStatus'), // 业绩单状态
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'USER_NAME' => [
                    'header' => Yii::t('ctx', 'memberCode'), // 会员编号
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'REAL_NAME' => [
                    'header' => Yii::t('ctx', 'memberName'), // 会员姓名
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'COUNTRY' => [
                    'header' => Yii::t('ctx', 'country'),
                ],
                'LAST_REC_USER_NAME' => [
                    'header' => Yii::t('ctx', 'sponsorNoAtSettlement'), // 结算时开拓者编号
                    'headerOther' => [
                        'width' => '190',
                    ],
                ],
                'LAST_REC_REAL_NAME' => [
                    'header' => Yii::t('ctx', 'sponsorNameAtSettlement'), // 结算时开拓者姓名
                    'headerOther' => [
                        'width' => '210',
                    ],
                ],
                'PV' => [
                    'header' => Yii::t('ctx', 'entryBV'), // 报单BV
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['PV'],
                        ]))->result();
                    },
                ],
                'DEC_AMOUNT' => [
                    'header' => Yii::t('ctx', 'entryAmount'), // 报单金额
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['DEC_AMOUNT'],
                        ]))->result();
                    },
                ],
                'DEC_SN' => [
                    'header' => Yii::t('ctx', 'entryNo'), // 报单编号
                    'headerOther' => [
                        'width' => '250',
                    ],
                ],
                'FROM_TABLES' => [
                    'header' => Yii::t('ctx', 'orderSource'),
                    'headerOther' => [
                        'width' => '250',
                    ],
                ],
                'CREATED_AT' => [
                    'header' => Yii::t('ctx', 'createAt'), // 创建时间
                    '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'=> Yii::t('ctx', 'payCycle')], // 结算期数
                'SN'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'salesNo')], // 业绩单编号
                'DEC_STATUS_NAME'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'saleStatus'),'other'=> 'select',
                 'selectData'=> [['id'=> 1, 'name'=> Yii::t('ctx', 'reviewed')], ['id'=> 2, 'name'=> Yii::t('ctx', 'wastedisposal')]],], // 业绩单状态,已审核,置废
                'USER_NAME'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'memberCode')], // 会员编号
                'REAL_NAME'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'memberName')], // 会员姓名
                'LAST_REC_USER_NAME'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'sponsorNoAtSettlement')], // 结算时开拓者编号
                'LAST_REC_REAL_NAME'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'sponsorNameAtSettlement')], // 结算时开拓者姓名
                'PV'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'entryBV')], // 报单BV
                'DEC_AMOUNT'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'entryAmount')], // 报单金额
                'DEC_SN'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'entryNo')], // 报单编号
                'CREATED_AT'=> ['isUserTable'=> true, 'name'=> Yii::t('ctx', 'createAt'), 'other'=> 'date'], // 创建时间
            ];
        }
        return $this->filterTypes;
    }
}
