<!-- 账号管理-资金账户管理-出入金 -->
<template>
    <app-drawer :title="t('account.fundacct.bankinout.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.bankinout.accountId')">
                    {{ record.accountId }}
                </el-form-item>
                <el-form-item :label="t('account.fundacct.bankinout.enableAmount')">
                    {{ data?.money ?? 0 }}
                </el-form-item>
                <el-form-item :label="t('account.fundacct.bankinout.inOutAmount')" prop="type">
                    <el-radio-group v-model="formData.type">
                        <el-radio :label="t('account.fundacct.bankinout.outAmount')" :value="1" />
                        <el-radio :label="t('account.fundacct.bankinout.inAmount')" :value="2" />
                    </el-radio-group>
                </el-form-item>
                <el-form-item :label="t('account.fundacct.bankinout.amount')" prop="money">
                    <el-input-number v-model="formData.money" :placeholder="t('common.pleaseenter')" :min="0" />
                </el-form-item>
                <el-form-item :label="t('account.fundacct.bankinout.cusbankid')" prop="cusbankid">
                    <el-select v-model="selectedBank" :placeholder="t('common.pleasechoice')" value-key="cusbankid"
                        @change="onBankChange">
                        <el-option v-for="item in bankList" :key="item.cusbankid" :label="item.cusbankname"
                            :value="item" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('account.fundacct.bankinout.accpwd')" prop="accpwd">
                    <el-input type="password" v-model="formData.accpwd" :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <template v-if="selectedBank">
                    <el-form-item :label="t('account.fundacct.bankinout.accounttype')">
                        {{ selectedBank.accounttype }}
                    </el-form-item>
                    <el-form-item :label="t('account.fundacct.bankinout.cardtype')">
                        {{ selectedBank.cardtype }}
                    </el-form-item>
                    <el-form-item :label="t('account.fundacct.bankinout.cardno')">
                        {{ selectedBank.cardno }}
                    </el-form-item>
                    <el-form-item :label="t('account.fundacct.bankinout.bankname')">
                        {{ selectedBank.bankname }}
                    </el-form-item>
                    <el-form-item :label="t('account.fundacct.bankinout.bankaccountno')">
                        {{ selectedBank.bankaccountno }}
                    </el-form-item>
                    <el-form-item :label="t('account.fundacct.bankinout.bankaccountname')">
                        {{ selectedBank.bankaccountname }}
                    </el-form-item>
                    <el-form-item :label="t('account.fundacct.bankinout.mobilephone')">
                        {{ selectedBank.mobilephone }}
                    </el-form-item>
                </template>
                <el-form-item class="el-form-item--row" :label="t('account.fundacct.bankinout.remark')" prop="remark">
                    <el-input type="textarea" v-model="formData.remark" maxlength="200" :rows="3"
                        :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.extendInfo[item.fieldcode]" maxlength="50"
                            :placeholder="t('common.pleaseenter')" />
                    </el-form-item>
                </template>
            </fieldset>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.cancel') }}</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 } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { decryptAES } from '@/services/crypto'
import { useRequest } from '@/hooks/request'
import { inOrOutAmount } from '@/services/api/account'
import { getBankAccountSign, queryMoney, queryBooleanForAmount } from '@/services/api/bank'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import { i18n } from '@/stores'

const props = defineProps({
    record: {
        type: Object as PropType<Model.TaAccountRsp>,
        required: true
    }
})

const { global: { t } } = i18n

const formRef = shallowRef<FormInstance>()
const show = shallowRef(true)
const refresh = shallowRef(false)
const loading = shallowRef(false)
const selectedBank = shallowRef<Model.BankAccountSignRsp>()

const formData = reactive<Model.InOrOutAmountReq>({
    accountcode: props.record.accountId,
    accpwd: '',
    cusbankid: '',
    money: 0,
    remark: '',
    type: 1,
    extendInfo: {}
})

const { dataList: bankList } = useRequest(getBankAccountSign, {
    params: {
        accountcode: props.record.accountId,
        signstatus: 4
    },
    onSuccess: (() => {
        bankList.value.forEach((e) => {
            e.cardno = decryptAES(e.cardno)
            e.bankaccountno = decryptAES(e.bankaccountno)
            e.bankaccountname = decryptAES(e.bankaccountname)
            e.mobilephone = decryptAES(e.mobilephone)
        })
    })
})

const { data } = useRequest(queryMoney, {
    params: {
        accountid: props.record.accountId,
        mtype: 2,
        type: 0
    }
})

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

// 表单验证规则
const formRules: FormRules = {
    cusbankid: [{ required: true }],
    money: [{ required: true }],
    accpwd: [{ required: true }],
}

// 选择银行
const onBankChange = (item: Model.BankAccountSignRsp) => {
    formData.cusbankid = item.cusbankid
    getExpandableList({
        cusbankid: item.cusbankid,
        usabletype: item.accounttype
    })
}

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

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            const rawData = { ...formData }
            loading.value = true

            inOrOutAmount({
                data: rawData
            }).then(() => {
                ElMessage.success(t('common.tips1'))
                onCancel(true)
            }).catch((err) => {
                ElMessage.error(t('common.tips2') + err)
            }).finally(() => {
                loading.value = false
            })
        }
    })
}
</script>