<?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\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',
            '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'],
            ],
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn() {
        $regionConfig = Cache::getRegionConfig();
        if (!$this->columns) {
            $this->columns = [
                'ID' => null,
                'SN' => [
                    'header' => 'Withdrawal serial number', // 提现流水号
                    'headerOther' => [
                        'width' => '250',
                    ],
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'warning', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'WITHDRAW_PERIOD_NUM' => [
                    'header' => 'Period', // 提现期数
                    'headerOther' => ['width' => '150'],
                ],
                'CREATED_AT' => [
                    'header' => 'Withdrawal time', // 提现时间
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'AUDIT_STATUS' => null,
                'AUDIT_STATUS_NAME' => [
                    'header' => 'State', // 提现状态
                    'headerOther' => ['width' => '150'],
                    'value' => function ($row) {
                        return Withdraw::STATUS_NAME[$row['AUDIT_STATUS']];
                    },
                ],
                'USER_NAME' => [
                    'header' => 'Member code', // 会员编号
                    'headerOther' => ['width' => '150'],
                ],
                'USER_REAL_NAME' => [
                    'header' => 'Member name', // 会员姓名
                    'headerOther' => ['width' => '150'],
                ],
                'AMOUNT' => [
                    'header' => 'Withdrawal amount', // 提现金额
                    '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' => 'Bank charge', // 手续费
                    'headerOther' => ['width' => '150'],
                ],
                'REAL_AMOUNT' => [
                    'header' => 'Actual amount', // 实际到账金额
                    'headerOther' => ['width' => '150'],
                ],
                'OPEN_BANK' => [
                    'header' => 'Bank info',
                    'headerOther' => ['width' => '150'],
                    'value' => function ($row) {
                        return $row['OPEN_BANK'] ? OpenBank::getCnName($row['OPEN_BANK']) : '';
                    },
                ],
                'BANK_NO' => [
                    'header' => 'Bank account',
                    'headerOther' => ['width' => '190'],
                    'value' => function ($row) {
                        return $row['BANK_NO'].' /';
                    },
                ],
                'MOBILE' => [
                    'header' => 'Phone Number', // 手机号
                    'headerOther' => ['width' => '150'],
                ],
                'UPDATE_ADMIN_NAME' => [
                    'header' => 'Operations administrator', // 操作管理员
                    'headerOther' => ['width' => '200'],
                ],
                'REMARK' => [
                    'header' => 'Remark', // 备注
                    'headerOther' => [
                        'width' => '200',
                    ],
                ],
                'BACK_REMARK' => [
                    'header' => 'Return note', // 退回备注
                    'headerOther' => [
                        'width' => '200',
                    ],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes() {
        if (!$this->filterTypes) {
            $this->filterTypes = [
                'SN' => ['isUserTable' => false, 'name' => 'Withdrawal serial number'], // 提现流水号
                'USER_NAME' => ['isUserTable' => false, 'name' => 'Member code'], // 会员编号
                'WITHDRAW_PERIOD_NUM' => ['isUserTable' => false, 'name' => 'Period'], // 提现期数
                'CREATED_AT' => ['isUserTable' => false, 'name' => 'Withdrawal time', 'other' => 'date'],  // 申请时间
                'REAL_AMOUNT' => ['isUserTable' => false, 'name' => 'Actual Amount']
            ];
        }
        return $this->filterTypes;
    }
}
