<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="120px" :rules="formRules">
                <el-form-item label="单位名称" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入单位名称" />
                </el-form-item>
                <el-form-item label="银行名称" prop="bank_name">
                    <el-input v-model="formData.bank_name" clearable placeholder="请输入银行名称" />
                </el-form-item>
                <el-form-item label="开户行名称" prop="bank_open_name">
                    <el-input v-model="formData.bank_open_name" clearable placeholder="请输入开户行名称" />
                </el-form-item>
                <el-form-item label="银行卡号" prop="bank_card">
                    <el-input v-model="formData.bank_card" clearable placeholder="请输入银行卡号" />
                </el-form-item>
                <el-form-item label="税务登记地址" prop="address">
                    <el-input v-model="formData.address" clearable placeholder="请输入税务登记地址" />
                </el-form-item>
                <el-form-item label="纳税号" prop="ns">
                    <el-input v-model="formData.ns" clearable placeholder="请输入纳税号" />
                </el-form-item>
                <el-form-item label="手机号码" prop="mobile">
                    <el-input v-model="formData.mobile" clearable placeholder="请输入银行卡绑定手机号码" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="partenBankEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiPartenBankAdd, apiPartenBankEdit, apiPartenBankDetail } from '@/api/parten_bank'
import { timeFormat } from '@/utils/util'
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({
    id: '',
    name: '',
    bank_name: '',
    bank_card: '',
    bank_open_name: '',
    address: '',
    ns: '',
    mobile: '',
})

// 表单验证
const formRules = reactive<any>({
    name: [
        { required: true, message: '请输入单位名称', trigger: 'blur' }
    ],
    bank_name: [
        { required: true, message: '请输入银行名称', trigger: 'blur' }
    ],
    bank_open_name: [
        { required: true, message: '请输入开户行名称', trigger: 'blur' }
    ],
    bank_card: [
        { required: true, message: '请输入银行卡号', trigger: 'blur' }
    ],
    address: [
        { required: true, message: '请输入税务登记地址', trigger: 'blur' }
    ],
    ns: [
        { required: true, message: '请输入纳税号', trigger: 'blur' }
    ],
    mobile: [
        { required: true, message: '请输入银行卡绑定手机号码', trigger: 'blur' },
        { pattern: /^1\d{10}$/, message: '请输入正确的11位手机号码', 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]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiPartenBankDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiPartenBankEdit(data)
        : await apiPartenBankAdd(data)
    popupRef.value?.close()
    emit('success')
}

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

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



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