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

use backendApi\modules\v1\models\Admin;
use common\helpers\Cache;
use common\helpers\http\BackendToFrontendApi;
use common\helpers\user\Info;
use common\libs\dataList\DataListInterface;
use common\models\DecRole;
use common\models\OpenBank;
use common\models\Region;
use common\models\User;
use common\models\UserInfo;
use common\libs\dataList\column\DateTime;
use common\libs\dataList\column\YesNo;
use common\models\UserMove;
use common\models\UserNetwork;
use common\models\UserSystem;
use Yii;

class MoveList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '会员移网列表';
    }

    /**
     * 列表筛选到的数据
     */
    public function dataHandle()
    {
        $this->listData = User::lists($this->condition, $this->params, [
            'select' => 'UM.*,
                CU.USER_NAME MOVE_USER_NAME,CU.REAL_NAME MOVE_REAL_NAME,FUI.USER_NAME FROM_USER_NAME,TUI.USER_NAME TO_USER_NAME,ADMC.ADMIN_NAME MOVE_ADMIN_NAME,ADMU.ADMIN_NAME AUDIT_ADMIN_NAME
                ',
            'orderBy' => 'UM.CREATED_AT DESC, UM.ID DESC',
            'from' => UserMove::tableName() . ' AS UM',
            'join' => [
                ['LEFT JOIN', User::tableName() . ' AS CU', 'UM.USER_ID=CU.ID'],
                ['LEFT JOIN', UserInfo::tableName() . ' AS FUI', 'UM.FROM_UID=FUI.USER_ID'],
                ['LEFT JOIN', UserInfo::tableName() . ' AS TUI', 'UM.TO_UID=TUI.USER_ID'],
                ['LEFT JOIN', Admin::tableName() . ' AS ADMC', 'UM.MOVE_ADMIN_ID=ADMC.ID'],
                ['LEFT JOIN', Admin::tableName() . ' AS ADMU', 'UM.AUDIT_ADMIN_ID=ADMU.ID'],
            ],
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        if(!$this->columns){
            $this->columns = [
                'ID' => null,
                'USER_ID' => null,
                'MOVE_USER_NAME' => [
                    'header' => '移动的会员',
                    'headerOther' => ['width' => '150'],
                ],
                'MOVE_REAL_NAME' => [
                    'header' => '移动的会员姓名',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'success', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'FROM_USER_NAME' => [
                    'header' => '移动前接点',
                    'headerOther' => ['width' => '150'],
                ],
                'TO_USER_NAME' => [
                    'header' => '移动后接点',
                    'headerOther' => ['width' => '150'],
                ],
                'TYPE' => [
                    'header' => '移网类型',
                    'headerOther' => [
                        'width' => '120',
                    ],
                    'value' => function($row) {
                        return $row['TYPE'] == UserMove::TYPE_NETWORK ? '安置网络' : '开拓网络';
                    },
                ],
                'LOCATION' => [
                    'header' => '移动到位置',
                    'headerOther' => ['width' => '150'],
                ],
                'PERIOD_NUM' => '移动时的期数',
                'IS_MOVING' => [
                    'header' => '是否正在移动',
                    'headerOther' => ['width' => '100'],
                ],
                'MOVE_PERCENT' => [
                    'header' => '移动百分比',
                ],
                'MOVE_ADMIN_NAME' => [
                    'header' => '移网管理员',
                    'headerOther' => ['width' => '120'],
                ],
                'CREATED_AT' => [
                    'header' => '加入日期',
                    'value' => function($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '170'],
                ],
                'AUDIT_STATUS' => null,
                'AUDIT_STATUS_NAME' => [
                    'header' => '审核状态',
                    'headerOther' => ['width' => '100'],
                    'value' => function ($row) {
                        return UserMove::STATUS_NAME[$row['AUDIT_STATUS']];
                    },
                ],
                'AUDIT_ADMIN_NAME' => [
                    'header' => '审核管理员',
                    'headerOther' => ['width' => '100'],
                ],
                'AUDITED_AT' => [
                    'header' => '审核时间',
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['AUDITED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '150'],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'MOVE_USER_NAME'=> ['isUserTable' => false,'name'=> '移动的会员'],
                'MOVE_REAL_NAME'=> ['isUserTable' => false,'name'=> '移动的会员姓名'],
                'filterStatus'=> ['isUserTable' => false,'name'=> '审核状态'],
//                'moveType'=> ['isUserTable' => false,'name'=> '移网类型', 'other'=> 'select','selectData'=>UserMove::TYPE_NAME],
//                'FROM_USER_NAME'=> ['isUserTable' => false,'name'=> '移动前接点'],
//                'TO_USER_NAME'=> ['isUserTable' => false,'name'=> '移动后接点', 'other'=> 'decLevel'],
                'PERIOD_NUM'=> ['isUserTable' => false,'name'=> '移动时的期数'],
                'MOVE_ADMIN_NAME'=> ['isUserTable' => false,'name'=> '移网管理员'],
                'AUDIT_ADMIN_NAME'=> ['isUserTable' => false,'name'=> '审核管理员'],
//                'CREATED_AT' => ['isUserTable' => false, 'name' => '申请时间', 'other' => 'date'],
                'AUDITED_AT' => ['isUserTable' => false, 'name' => '审核时间', 'other' => 'date'],
            ];
        }
        return $this->filterTypes;
    }
}