<!-- 账号管理-资金账户管理-银行签约信息-编辑 -->
<template>
    <app-drawer :title="t('account.fundacct.sign.edit.title')" width="900" v-model:show="show" :refresh="refresh"
        :loading="loading">
        <el-form ref="formRef" class="el-form--horizontal" label-width="140px" :model="formData" :rules="formRules"
            :show-message="false">
            <fieldset class="g-fieldset el-form--horizontal">
                <legend class="g-fieldset__legend">{{ t('common.baseinfo') }}</legend>
                <el-form-item :label="t('account.fundacct.sign.edit.userinfotype')" v-if="userInfo">
                    {{ userinfotypeEnum.getEnumTypeName(userInfo.userinfotype) }}
                </el-form-item>
                <el-form-item :label="t('account.fundacct.sign.edit.cardtypeid')" v-if="userInfo">
                    {{ userInfo.cardtypeid }}
                </el-form-item>
                <el-form-item :label="t('account.fundacct.sign.edit.cusbankid')" prop="cusbankid">
                    <el-select v-model="formData.cusbankid" :placeholder="t('common.pleasechoice')" @change="onCusBankChange">
                        <el-option v-for="item in cusbankList" :key="item.cusbankid" :label="item.cusbankname"
                            :value="item.cusbankid" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('account.fundacct.sign.edit.certid')" v-if="userInfo">
                    {{ handleNoneValue(formData.certid) }}
                </el-form-item>
                <el-form-item :label="t('account.fundacct.sign.edit.exbankname')" prop="exbankname">
                    <app-select-bank v-model:selected="selectedBank" :cusbankid="formData.cusbankid"
                        @change="onBankChange" />
                </el-form-item>
                <el-form-item
                    :label="userInfo.userinfotype === UserInfoType.Personal ? t('account.fundacct.sign.edit.bankaccountno') : t('account.fundacct.sign.edit.bankaccountno1')"
                    prop="bankaccountno" v-if="userInfo">
                    <el-input v-model="formData.bankaccountno" maxlength="50" :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item :label="t('account.fundacct.sign.edit.bankaccountname')" prop="bankaccountname">
                    <el-input v-model="formData.bankaccountname" maxlength="50"
                        :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item :label="t('account.fundacct.sign.edit.mobilephone')" prop="mobilephone">
                    <el-input type="number" v-model="formData.mobilephone" maxlength="50"
                        :placeholder="t('common.pleaseenter')" />
                </el-form-item>
            </fieldset>
            <fieldset class="g-fieldset el-form--horizontal" v-if="expandableList.length">
                <legend class="g-fieldset__legend">{{ t('common.extendinfo') }}</legend>
                <template v-for="(item, index) in expandableList" :key="index">
                    <el-form-item :label="item.fieldname" :prop="item.fieldcode">
                        <el-input v-model="formData.extend[item.fieldcode]" maxlength="50"
                            :placeholder="t('common.pleaseenter')" />
                    </el-form-item>
                </template>
            </fieldset>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.close') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('operation.submit') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { shallowRef, reactive, PropType, toRaw } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { decryptAES } from '@/services/crypto'
import { handleNoneValue } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { getSignBankById, dealAccountSignInit, queryCommonFlag, dealAccountSign } from '@/services/api/account'
import {  UserInfoType } from '@/constants/member'
import { i18n } from '@/stores'
import { queryBooleanForAmount } from '@/services/api/bank'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppSelectBank from '@pc/components/modules/select-bank/index.vue'

const props = defineProps({
    accountId: {
        type: Number,
        required: true
    },
    selectedItem: {
        type: Object as PropType<Model.AccountSignRsp>
    }
})

const userinfotypeEnum = useEnum('userinfotype') // 所有者类型

const { global: { t } } = i18n
const formRef = shallowRef<FormInstance>()
const show = shallowRef(true)
const refresh = shallowRef(false)
const selectedBank = shallowRef<Model.BankInfoByCusBankRsp>()

const formData = reactive<Partial<Model.DealAccountSignReq>>({
    accountcode: props.accountId.toString(),
    operatetype: 1,
    type: 0,
    extend: {}
})

// 查询托管银行
const { dataList: cusbankList } = useRequest(dealAccountSignInit, {
    params: {
        accountid: props.accountId
    }
})

// 查询用户信息
const { data: userInfo, loading } = useRequest(queryCommonFlag, {
    params: {
        accountid: props.accountId
    },
    onSuccess: (res) => {
        formData.accounttype = res.data.userinfotype
        formData.certtype = res.data.cardtypeid
        formData.certid = decryptAES(res.data.cardnum)
        formData.bankaccountname = res.data.customername
        formData.mobilephone = decryptAES(res.data.mobile)

        if (props.selectedItem) {
            const { accountCode, applyexchticket, cusbankid } = props.selectedItem
            getSignBankInfo({
                accountcode: accountCode,
                applyexchticket,
                cusbankid
            })
        }
    }
})

// 查询签约详情
const { run: getSignBankInfo } = useRequest(getSignBankById, {
    manual: true,
    onSuccess: ((res) => {
        const { bankAccountsign, extendInfo } = res.data
        if (bankAccountsign.signstatus !== 1) {
            formData.certtype = Number(bankAccountsign.cardtype)
            formData.certid = decryptAES(bankAccountsign.cardno)
            formData.bankaccountname = decryptAES(bankAccountsign.bankaccountname)
        }
        formData.accounttype = bankAccountsign.accounttype
        formData.accountcode = bankAccountsign.accountcode
        formData.mobilephone = decryptAES(bankAccountsign.mobilephone)
        formData.bankaccountno = decryptAES(bankAccountsign.bankaccountno)
        formData.cusbankid = bankAccountsign.cusbankid
        formData.openbankaccid = bankAccountsign.bankid
        formData.exbankname = bankAccountsign.bankname
        formData.extend = extendInfo
        formData.operatetype = 2

        selectedBank.value = {
            bankId: bankAccountsign.bankid,
            bankName: bankAccountsign.bankname
        }

        getExpandableList({
            cusbankid: formData.cusbankid,
            usabletype: formData.accounttype
        })
    }),
    onError: ((err) => {
        ElMessage.error(err)
    })
})

// 查询扩展信息
const { dataList: expandableList, run: getExpandableList } = useRequest(queryBooleanForAmount, {
    manual: true,
    params: {
        extendbiztype: '1',
    },
    onError: () => {
        expandableList.value = []
    }
})

// 表单验证规则
const formRules: FormRules = {
    cusbankid: [{ required: true }],
    exbankname: [{ required: true }],
    bankaccountno: [{ required: true }],
    bankaccountname: [{ required: true }],
    mobilephone: [{ required: true }]
}

// 选择托管银行
const onCusBankChange = (cusbankid?: string) => {
    if (cusbankid) {
        getExpandableList({
            cusbankid,
            usabletype: userInfo.value?.userinfotype
        })
    } else {
        expandableList.value = []
    }
}

// 选择开户银行
const onBankChange = (item?: Model.BankInfoByCusBankRsp) => {
    formData.openbankaccid = item?.bankId
    formData.exbankname = item?.bankName
}

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

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            const rawData = toRaw(formData)
            loading.value = true
            dealAccountSign({
                data: rawData
            }).then(() => {
                ElMessage.success(t('common.tips3'))
                onCancel(true)
            }).catch((err) => {
                ElMessage.error(t('common.tips4') + err)
            }).finally(() => {
                loading.value = false
            })
        }
    })
}
</script>