<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="auto" :rules="formRules">
                <el-row :gutter="10">
                    <el-col :span="24">
                        <el-form-item label="账户编码">
                            <el-input v-model="bank_no" clearable disabled placeholder="系统自动生成" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="开户银行" prop="deposit_bank"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.deposit_bank" clearable placeholder="请输入开户银行" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="开户名称" prop="account_name"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.account_name" clearable placeholder="请输入开户名称" />
                        </el-form-item>
                    </el-col>


                    <el-col :span="24">
                        <el-form-item label="账号" prop="account"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.account" clearable placeholder="请输入账号" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="开户日期" prop="account_opening_date"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.account_opening_date" clearable
                                type="date" value-format="YYYY-MM-DD" placeholder="选择开户日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="期初金额" prop="opening_amount"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.opening_amount" type="number" clearable placeholder="请输入期初金额" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" type="textarea" clearable placeholder="请输入备注" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </popup>

    </div>
</template>

<script lang="ts" setup name="projectEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { bankaccountAdd, bankaccountEdit, bankaccountDetail } from '@/api/bank_account'
const bank_no = ref('')


const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const showDialog3 = ref(false)

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

// 表单数据
const formData = reactive({
    id: '',
    deposit_bank: '',
    account_name: '',
    account: '',
    account_opening_date: '',
    opening_amount: '',
    remark: ''

})

const customEvent3 = (e: any) => {
    formData.deposit_bank = e.deposit_bank
    formData.account_name = e.account_name
    formData.account = e.account
    showDialog3.value = false
};
// 表单验证
const formRules = reactive<any>({

})


// 获取详情
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]
        }
    }
    bank_no.value = data.account_sn
}
const getDetail = async (row: Record<string, any>) => {
    const data = await bankaccountDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit'
        ? await bankaccountEdit(data)
        : await bankaccountAdd(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>
