<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="商户名称">
                    <el-input v-model="formData.mer_name" clearable :readonly="true" />
                </el-form-item>
                <el-form-item label="类型">
                    <el-input v-model="formData.is_own_text" clearable :readonly="true" />
                </el-form-item>
                <el-form-item label="开户银行">
                    <el-input v-model="formData.name" clearable :readonly="true" />
                </el-form-item>
                <el-form-item label="账户名称">
                    <el-input v-model="formData.name" clearable :readonly="true" />
                </el-form-item>
                <el-form-item label="银行账号">
                    <el-input v-model="formData.bank_code" clearable :readonly="true" />
                </el-form-item>
                <el-form-item label="开户网点">
                    <el-input v-model="formData.bank_branch" clearable :readonly="true" />
                </el-form-item>
                <el-form-item label="身份证">
                    <el-input v-model="formData.id_card" clearable :readonly="true" />
                </el-form-item>
                <el-form-item label="电话">
                    <el-input v-model="formData.phone" clearable :readonly="true" />
                </el-form-item>
                <el-form-item label="审核意见" prop="is_check">
                    <el-radio-group v-model="formData.is_check" class="ml-4">
                        <el-radio :label="1" size="large">通过</el-radio>
                        <el-radio :label="2" size="large">不通过</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="拒绝原因" v-if="formData.is_check == 2" prop="fail_msg">
                    <el-input type="textarea" v-model="formData.fail_msg" clearable placeholder="请输入拒绝原因"
                        :readonly="false" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="bankEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apimMrchantbankCheck } from '@/api/bank'

import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑银行列表' : '新增银行列表'
})

// 表单数据
const formData = reactive({
    mer_name: "",
    "id": '',
    "mer_id": '',
    "bank_id": '',
    "bank_code": "",
    "bank_branch": "",
    "name": "",
    "id_card": "",
    "phone": "",
    "financial_img": "",
    "is_own": '',
    "is_check": '',
    "fail_msg": null,
    "admin_id": '',
    "admin_name": "",
    "bank_info": {
        "name": "",
        "image": ""
    },
    "is_own_text": "",
    "is_check_text": ""
})


// 表单验证
const formRules = reactive<any>({
    is_check: [{
        required: true,
        message: '请选择审核意见',
        trigger: ['blur']
    }],
    fail_msg: [{
        required: true,
        message: '请输入拒绝原因',
        trigger: ['blur']
    }]
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }

    formData.is_check ? '' : formData.is_check = ''

}



// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { id: formData.id, is_check: formData.is_check, fail_msg: formData.fail_msg }
    apimMrchantbankCheck(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
})
</script>
