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

use common\helpers\Cache;
use common\helpers\http\BackendToFrontendApi;
use common\libs\dataList\DataListInterface;
use common\models\BaUser;
use common\models\BaUserInfo;
use common\models\User;
use common\libs\dataList\column\DateTime;
use common\models\UserNetwork;
use Yii;

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

    /**
     * 列表筛选到的数据
     */
    public function dataHandle()
    {
//        $this->condition .= ' AND UN.USER_ID=UI.USER_ID AND UN.PARENT_UID=UI.CON_UID';
        $this->listData = BaUser::lists($this->condition, $this->params, [
            'select' => 'U.*,
                UI.USER_ID, UI.ZC_PV, UI.CON_UID, UI.REC_UID, UI.CON_NUM, UI.REC_NUM, UI.NETWORK_DEEP, 
                UI.RELATION_DEEP, UI.SYSTEM_ID, UI.IS_GROUP_LEADER, UI.IS_SYSTEM_LEADER, UI.IS_TEAM, UI.IS_BIND, 
                UI.IS_TEAM_MAIN, UI.IS_BIND_MAIN, UI.IS_AUTO_WITHDRAW, UI.CLOSE_LOGIN, UI.REG_TYPE, UI.SHOULD_REG_TYPE, 
                UI.REG_NAME, UI.CREDIT_CODE, UI.PREMISES, UI.LEGAL_PERSON, UI.REG_EXPIRES, UI.STORE_TYPE, UI.INVOICE_BALANCE, 
                UI.HIGHEST_EMP_LV, UI.CLOSE_LOGIN_AT, UI.PULLED_AT, 
                UI.GROUP_LEADER_AT, UI.ALLOW_TRANSFER, UI.ALLOW_RECONSUME_SMS, UI.ALLOW_RECONSUME_SMS_TO, 
                UI.HIGHEST_EMP_LV_PERIOD, UI.TRANSFER_PROP, UI.LOGIN_NUMS, UI.FAIL_NUMS, UI.LAST_LOGIN_IP, 
                UI.LAST_LOGIN_AT,
                RU.USER_NAME REC_USER_NAME,RU.REAL_NAME REC_REAL_NAME,
                DU.USER_NAME DEC_USER_NAME',
            'orderBy' => 'UI.CREATED_AT DESC, UI.ID DESC',
            'from' => BaUser::tableName() . ' AS U',
            'join' => [
                ['LEFT JOIN', BaUserInfo::tableName() . ' AS UI', 'UI.USER_ID=U.ID'],
                ['LEFT JOIN', User::tableName() . ' AS RU', 'UI.REC_UID=RU.ID'],
                ['LEFT JOIN', BaUser::tableName() . ' AS DU', 'U.DEC_ID=DU.ID'],
            ],
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn()
    {
        $regionConfig = Cache::getRegionConfig();
        if(!$this->columns){
            $this->columns = [
                'USER_ID' => null, // 这种传输方式主要是用于索引，因为过滤后的字段可能没有这种ID，但是一些功能的操作还需要用这种ID去关联，例如前台会员列表中的勾选批量状态管理，这里需要的就是USER_ID
                'BTF_URL' => null,
                'USER_NAME' => [
                    'header' => 'Member Code', // 会员编号
                    'headerOther' => ['width' => '150'],
                ],
                'REAL_NAME' => [
                    'header' => 'Member Name', // 会员姓名
                    'headerOther' => [
                        'width' => '120',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'success', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'LOGIN_STATUS_NAME' => [
                    'header' => 'Login Status', // 登录状态
                    'value' => function($row) {
                        return $row['ALLOW_LOGIN'] == 1 ? 'Allow Login': 'Prohibit Login'; // '允许登录' : '禁止登录'
                    },
                    'headerOther' => ['width' => '110'],
                ],
                'STATUS' => [
                    'header' => 'Status', // 状态
                    'value' => function($row) {
                        return $row['STATUS'] == 1 ? 'Activate': 'Lock'; // '激活' : '锁定'
                    },
                    'headerOther' => ['width' => '110'],
                ],
                'ALLOW_LOGIN' => ['header' => 'Filter Allow Login','hidden'=>true], // 筛选允许登录
                'CREATED_AT' => [
                    'header' => 'Joining Date', // 加入日期
                    'value' => function($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '170'],
                ],
                'PERIOD_AT' => [
                    'header' => 'Joining Period', // 加入期数
                    'headerOther' => ['width' => '180'],
                ],
                'REC_USER_NAME' => [
                    'header' => 'Recommendation No', // 推荐编号
                    'headerOther' => ['width' => '160'],
                ],
                'REC_REAL_NAME' => [
                    'header' => 'Recommended Name', // 推荐姓名
                    'headerOther' => ['width' => '160'],
                ],
                'MOBILE' => [
                    'header' => 'Phone Number', // 手机号码
                    'value' => function($row) {
                        return "\t".$row['MOBILE'];
                    },
                    'headerOther' => ['width' => '120'],
                ],
                'TEL' => [
                    'header' => 'Alternate Phone Number', // 备用手机号码
                    'value' => function($row) {
                        return "\t".$row['TEL'];
                    },
                    'headerOther' => ['width' => '180'],
                ],
                'AREA' => [
                    'header' => 'Common Address', // 常用地址
                    'value' => function($row) use($regionConfig) {
                        $province = $regionConfig[$row['PROVINCE']]['REGION_NAME'] ?? '';
                        $city = $regionConfig[$row['CITY']]['REGION_NAME'] ?? '';
                        $county = $regionConfig[$row['COUNTY']]['REGION_NAME'] ?? '';
                        return $province.$city.$county;
                    },
                    'showValue' => function($row) {
                        $province = $regionConfig[$row['PROVINCE']]['REGION_NAME'] ?? '';
                        $city = $regionConfig[$row['CITY']]['REGION_NAME'] ?? '';
                        $county = $regionConfig[$row['COUNTY']]['REGION_NAME'] ?? '';
                        return '<div class="addr" title='.$province.$city.$county.'>'.$province.$city.$county.'</div>';
                    },
                    'headerOther' => [
                        'width' => '200'
                    ],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'USER_NAME'=> ['name'=> 'Member code'], // 会员编号
                'REAL_NAME'=> ['name'=> 'Member name'], // 会员姓名
                'CREATED_AT'=> ['name'=> 'Joining Date', 'other'=> 'date'], // 加入日期
                'PERIOD_AT'=> ['name'=> 'Joining Period'], // 加入期数
                'REC_USER_NAME'=> ['name'=> 'Recommendation No'],//推荐编号
                'MOBILE'=> ['name'=> 'Phone Number'],//手机号码
                'AREA'=> ['name'=> 'Common address', 'other'=> 'area'],//常用地址
                'STATUS'=> ['name'=> 'Active status', 'other'=> 'select', 'selectData'=> [['id'=> 0, 'name'=> 'locking'],['id'=> 1, 'name'=> 'activation']]],
            ];
        }
        return $this->filterTypes;
    }
}