<!-- 会员中心-会员信息-签约 -->
<template>
    <app-drawer title="签约" :width="800" v-model:show="show" :loading="loading" :refresh="refresh">
        <el-form ref="formRef" class="el-form--horizontal" label-width="110px" :model="formData" :rules="formRules">
            <el-form-item label="资金账号">
                <span>{{ formData.AccountCode }}</span>
            </el-form-item>
            <el-form-item label="托管银行" prop="CustodianBank">
                <el-select value-key="cusbankid" v-model="selectedCustodianBank">
                    <el-option :label="item.cusbankname" :value="item" v-for="(item, index) in custodianBanks"
                        :key="index" />
                </el-select>
            </el-form-item>
            <el-form-item label="账户类型">
                <span>对公</span>
            </el-form-item>
            <el-form-item label="开户银行" prop="DepositBank">
                <el-select value-key="bankid" v-model="selectedDepositBank" filterable>
                    <el-option :label="item.bankname" :value="item"
                        v-for="(item, index) in selectedCustodianBank?.Banklst ?? []" :key="index" />
                </el-select>
            </el-form-item>
            <el-form-item label="证件类型">
                <span>{{ getCertificateTypeName(Number(formData.CertType)) }}</span>
            </el-form-item>
            <el-form-item label="银行卡号" prop="BankAccountNo">
                <el-input placeholder="请输入" v-model="formData.BankAccountNo" />
            </el-form-item>
            <el-form-item label="证件号码">
                <span>{{ formData.CertID }}</span>
            </el-form-item>
            <el-form-item label="银行卡户名" prop="BankAccountName">
                <el-input placeholder="请输入" v-model="formData.BankAccountName" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)" plain>取消</el-button>
            <el-button type="primary" @click="onSubmit">提交</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { getCertificateTypeName } from '@/constants/certificate'
import { useBankSign } from '@/business/bank'
import AppDrawer from '@pc/components/base/drawer/index.vue'

const { loading, custodianBanks, selectedCustodianBank, selectedDepositBank, formData, formSubmit } = useBankSign()
const show = ref(true)
const refresh = ref(false)
const formRef = ref<FormInstance>()

const formRules: FormRules = {
    BankAccountNo: [{ required: true, message: '请输入银行卡号', trigger: 'blur' }],
    BankAccountName: [{ required: true, message: '请输入银行卡户名', trigger: 'blur' }],
    CustodianBank: [{
        required: true,
        validator: (rule, value, callback) => {
            if (selectedCustodianBank.value) {
                callback()
            } else {
                callback(new Error('请选择托管银行'))
            }
        }
    }],
    DepositBank: [{
        required: true,
        validator: (rule, value, callback) => {
            if (selectedDepositBank.value) {
                callback()
            } else {
                callback(new Error('请选择开户银行'))
            }
        }
    }],
}

const onCancel = (isRefresh = false) => {
    show.value = false
    refresh.value = isRefresh
}

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            formSubmit().then(() => {
                ElMessage.success('您的申请已提交，请稍后查询结果！')
                onCancel(true)
            }).catch((err) => {
                ElMessage.warning(err)
            })
        }
    })
}
</script>