<?php

namespace backendApi\modules\v1\models\lists\finance;

use backendApi\modules\v1\models\Admin;
use common\helpers\Audit;
use common\helpers\Cache;
use common\helpers\Tool;
use common\libs\dataList\column\DateTime;
use common\libs\dataList\DataListInterface;
use common\models\Countries;
use common\models\OpenBank;
use common\models\User;
use common\models\Withdraw;

class WithdrawList extends \common\libs\dataList\DataList implements DataListInterface {
    /**
     * 列表名称
     * @return string
     */
    public function getListName() {
        return '提现申请列表';
    }

    /**
     * 列表筛选到的数据
     */
    public function dataHandle() {
        $this->listData = Withdraw::lists($this->condition, $this->params, [
            'select' => 'W.*,U.USER_NAME,U.REAL_NAME AS USER_REAL_NAME,U.ID_CARD,U.MOBILE,U.OPEN_BANK,U.BANK_PROVINCE,U.BANK_CITY,U.BANK_COUNTY,U.BANK_ADDRESS,U.BANK_NO,ADM.ADMIN_NAME UPDATE_ADMIN_NAME,ADMA.ADMIN_NAME AUDIT_ADMIN_NAME,CN.NAME COUNTRY_NAME',
            'orderBy' => 'W.CREATED_AT DESC',
            'from' => Withdraw::tableName() . ' AS W',
            'join' => [
                ['LEFT JOIN', User::tableName() . ' AS U', 'W.USER_ID=U.ID'],
                ['LEFT JOIN', Admin::tableName() . ' AS ADM', 'ADM.ID=W.UPDATE_ADMIN'],
                ['LEFT JOIN', Admin::tableName() . ' AS ADMA', 'ADMA.ID=W.AUDIT_ADMIN'],
                ['LEFT JOIN', Countries::tableName() . ' AS CN', 'U.COUNTRY_ID=CN.ID'],
            ],
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn() {
        $regionConfig = Cache::getRegionConfig();
        if (!$this->columns) {
            $this->columns = [
                'ID' => null,
                'SN' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceWithdrawSn'), // 提现流水号
                    'headerOther' => [
                        'width' => '250',
                    ],
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'warning', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'WITHDRAW_PERIOD_NUM' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceWithdrawPeriod'), // 提现期数
                    'headerOther' => ['width' => '150'],
                ],
                'CREATED_AT' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceWithdrawAt'), // 提现时间
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'PAID_AT' => [
                    'header' => \Yii::t('ctx', 'modelListFinancePaidAt'), // 提现时间
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['PAID_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'AUDIT_STATUS' => null,
                'AUDIT_STATUS_NAME' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceWithdrawState'), // 提现状态
                    'headerOther' => ['width' => '150'],
                    'value' => function ($row) {
                        return Withdraw::getStatusName()[$row['AUDIT_STATUS']];
                        // return Withdraw::STATUS_NAME[$row['AUDIT_STATUS']];
                    },
                ],
                'USER_NAME' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceBalanceAuditListUserName'), // 会员编号
                    'headerOther' => ['width' => '150'],
                ],
                'USER_REAL_NAME' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceBalanceAuditListRealName'), // 会员姓名
                    'headerOther' => ['width' => '150'],
                ],
                'COUNTRY_NAME' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceBalanceAuditListCountryName'), // 国家
                    'headerOther' => ['width' => '150'],
                ],
                'AMOUNT' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceWithdrawAmount'), // 提现金额
                    'value' => function ($row) {
                        return Tool::formatPrice($row['AMOUNT']);
                    },
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'FEES' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceWithdrawFees'), // 手续费
                    'headerOther' => ['width' => '150'],
                ],
                'REAL_AMOUNT' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceWithdrawRealAmount'), // 实际到账金额
                    'headerOther' => ['width' => '150'],
                ],
                'OPEN_BANK' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceWithdrawBank'), // 提现银行
                    'headerOther' => ['width' => '150'],
                    'value' => function ($row) {
                        return $row['OPEN_BANK'] ? OpenBank::getCnName($row['OPEN_BANK']) : '';
                    },
                ],
                'BANK_NO' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceWithdrawBankNo'),//银行地区
                    'headerOther' => ['width' => '190'],
                    'value' => function ($row) {
                        return $row['BANK_NO'].' /';
                    },
                ],
                'MOBILE' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceWithdrawMobile'), // 手机号
                    'headerOther' => ['width' => '150'],
                ],
                'UPDATE_ADMIN_NAME' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceWithdrawUpdateAdminName'), // 操作管理员
                    'headerOther' => ['width' => '200'],
                ],
                'REMARK' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceBalanceAuditListRemark'), // 备注
                    'headerOther' => [
                        'width' => '200',
                    ],
                ],
                'BACK_REMARK' => [
                    'header' => \Yii::t('ctx', 'modelListFinanceWithdrawBackRemark'), // 退回备注
                    'headerOther' => [
                        'width' => '200',
                    ],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes() {
        if (!$this->filterTypes) {
            $this->filterTypes = [
                'SN' => ['isUserTable' => false, 'name' => \Yii::t('ctx', 'modelListFinanceWithdrawSn')], // 提现流水号
                'USER_NAME' => ['isUserTable' => false, 'name' => \Yii::t('ctx', 'modelListFinanceBalanceAuditListUserName')], // 会员编号
                'WITHDRAW_PERIOD_NUM' => ['isUserTable' => false, 'name' => \Yii::t('ctx', 'modelListFinanceWithdrawPeriod')], // 提现期数
                'CREATED_AT' => ['isUserTable' => false, 'name' => \Yii::t('ctx', 'modelListFinanceWithdrawAt'), 'other' => 'date'],  // 申请时间
                'PAID_AT' => ['isUserTable' => false, 'name' => \Yii::t('ctx', 'modelListFinancePaidAt'), 'other' => 'date'],  // 付款时间
                'REAL_AMOUNT' => ['isUserTable' => false, 'name' => \Yii::t('ctx', 'modelListFinanceWithdrawRealAmount')], // 实际到账金额

                'USER_REAL_NAME' => ['isUserTable' => false, 'name' => \Yii::t('ctx', 'modelListFinanceBalanceAuditListRealName')],
                'COUNTRY_NAME' => [
                    'name'=> \Yii::t('ctx', 'modelListFinanceBalanceAuditListCountryName'),
                    'other'=> 'select',
                    'selectData'=> self::getCountry()
                ],
                'OPEN_BANK' => ['isUserTable' => false, 'name' => \Yii::t('ctx', 'modelListFinanceWithdrawBank'),
                    'other'=> 'select',
                    'selectData'=> self::getAllOpenBank()],
                'MOBILE' => ['isUserTable' => false, 'name' => \Yii::t('ctx', 'modelListFinanceWithdrawMobile')],
            ];
        }
        return $this->filterTypes;
    }

    public function getCountry()
    {
        $countries = Countries::getFromCache();

        $data = [];
        foreach ($countries as $country) {
            $data[] = [
                'id' => $country['ID'],
                'name' => $country['NAME'],
            ];
        }

        return $data;
    }

    public function getAllOpenBank()
    {

        $banks =  OpenBank::find()->where('1=1')->indexBy('BANK_CODE')->orderBy('CREATED_AT ASC')->asArray()->all();

        $data = [];
        foreach ($banks as $bank) {
            $data[] = [
                'id' => $bank['ID'],
                'name' => $bank['BANK_NAME'],
            ];
        }

        return $data;
    }
}
