<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="商户名称" prop="mer_name">
                    <el-input
                        v-model="formData.mer_name"
                        clearable
                        placeholder="请输入商户名称"
                        :readonly="false"
                    />
                </el-form-item>
                <el-form-item label="商户电话" prop="phone">
                    <el-input
                        v-model="formData.phone"
                        clearable
                        placeholder="请输入商户电话"
                        :readonly="false"
                    />
                </el-form-item>
                <el-form-item label="结算周期" prop="settle_cycle">
                    <el-input
                        v-model="formData.settle_cycle"
                        clearable
                        placeholder="请输入结算周期，单位天"
                        :readonly="false"
                    />
                </el-form-item>
                <el-form-item label="商户地址" prop="address">
                    <el-input
                        v-model="formData.address"
                        clearable
                        placeholder="请输入商户地址"
                        :readonly="false"
                    />
                </el-form-item>
                <el-form-item label="商户备注" prop="mark">
                    <el-input
                        v-model="formData.mark"
                        clearable
                        placeholder="请输入商户备注"
                        :readonly="false"
                    />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supplierEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupplierAdd, apiSupplierEdit, apiSupplierDetail } from '@/api/supplier'
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: '',
    mer_name: '',
    phone: '',
    settle_cycle: '',
    address: '',
    mark: '',
    status: ''
})

// 表单验证
const formRules = reactive<any>({
    mer_name: [
        {
            required: true,
            message: '请输入商户名称',
            trigger: ['blur']
        }
    ],
    phone: [
        {
            required: true,
            message: '请输入商户电话',
            trigger: ['blur']
        }
    ],
    settle_cycle: [
        {
            required: true,
            message: '请输入结算周期，单位天',
            trigger: ['blur']
        }
    ],
    address: [
        {
            required: true,
            message: '请输入商户地址',
            trigger: ['blur']
        }
    ],
    status: [
        {
            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]
        }
    }
}

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

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