<?php

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

use backendApi\modules\v1\models\Admin;
use common\helpers\Audit;
use common\helpers\Tool;
use common\libs\dataList\column\DateTime;
use common\libs\dataList\DataListInterface;
use common\models\OpenBank;
use common\models\Recharge;
use common\models\UserInfo;

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

    /**
     * 列表筛选到的数据
     */
    public function dataHandle() {
        $this->listData = Recharge::lists($this->condition, $this->params, [
            'select' => 'R.*,UI.USER_NAME,OB.BANK_NAME OPEN_BANK_NAME,ADMA.ADMIN_NAME AUDIT_ADMIN_NAME',
            'orderBy' => 'R.CREATED_AT DESC, R.ID DESC',
            'from' => Recharge::tableName() . ' AS R',
            'join' => [
                ['LEFT JOIN', UserInfo::tableName() . ' AS UI', 'UI.USER_ID=R.USER_ID'],
                ['LEFT JOIN', OpenBank::tableName() . ' AS OB', 'OB.BANK_CODE=R.OPEN_BANK'],
                ['LEFT JOIN', Admin::tableName() . ' AS ADMA', 'ADMA.ID=R.AUDIT_ADMIN'],
            ],
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn() {
        if (!$this->columns) {
            $this->columns = [
                'ID' => null, // 这种传输方式主要是用于索引，因为过滤后的字段可能没有这种ID，但是一些功能的操作还需要用这种ID去关联，例如前台会员列表中的勾选批量状态管理，这里需要的就是USER_ID
                'SN' => [
                    'header' => '充值申请流水号',
                    'headerOther' => [
                        'width' => '250',
                    ],
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'warning', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'USER_NAME' => [
                    'header' => '会员编号',
                    'headerOther' => ['width' => '150'],
                ],
                'OPEN_BANK_NAME' => [
                    'header' => '汇款银行',
                    'headerOther' => ['width' => '150'],
                ],
                'BANK_NO' => [
                    'header' => '汇款账号',
                    'headerOther' => ['width' => '150'],
                ],
                'BANK_PROVE' => null,
                'AUDIT_STATUS' => [
                    'header' => '审核状态',
                    'value' => function ($row) {
                        return Recharge::STATUS_NAME[$row['AUDIT_STATUS']];
                    },
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'AMOUNT' => [
                    'header' => '充值金额',
                    'value' => function ($row) {
                        return Tool::formatPrice($row['AMOUNT']);
                    },
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'CREATED_AT' => [
                    'header' => '申请时间',
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'REMARK' => [
                    'header' => '备注',
                    'headerOther' => [
                        'width' => '200',
                    ],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes() {
        if (!$this->filterTypes) {
            $this->filterTypes = [
                'SN' => ['isUserTable' => false, 'name' => '充值记录流水号'],
                'USER_NAME' => ['isUserTable' => false, 'name' => '会员编号'],
                'AUDIT_STATUS' => ['isUserTable' => false, 'name' => '审核状态', 'other' => 'select', 'selectData' => [['id' =>0, 'name' => '待审核'], ['id' => 1, 'name' => '已上传凭证'], ['id' => 2, 'name' => '已审核']]],
                'BANK_NO' => ['isUserTable' => false, 'name' => '汇款账号'],
                'AMOUNT' => ['isUserTable' => false, 'name' => '充值金额'],
                'CREATED_AT' => ['isUserTable' => false, 'name' => '申请时间', 'other' => 'date'],
            ];
        }
        return $this->filterTypes;
    }
}
