<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="客户id" prop="customer_id">
                    <el-input v-model="formData.customer_id" clearable placeholder="请输入客户id" />
                </el-form-item>
                <el-form-item label="报价日期" prop="quotation_date">
                    <el-date-picker class="flex-1 !flex" v-model="formData.quotation_date" clearable type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择报价日期">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="联系人" prop="contacts">
                    <el-input v-model="formData.contacts" clearable placeholder="请输入联系人" />
                </el-form-item>
                <el-form-item label="联系电话" prop="contacts_phone">
                    <el-input v-model="formData.contacts_phone" clearable placeholder="请输入联系电话" />
                </el-form-item>
                <el-form-item label="制单人" prop="create_user">
                    <el-input v-model="formData.create_user" clearable placeholder="请输入制单人" />
                </el-form-item>
                <el-form-item label="发票类型" prop="invoice_type">
                    <el-radio-group v-model="formData.invoice_type" placeholder="请选择发票类型">
                        <el-radio v-for="(item, index) in dictData.invoice_type" :key="index" :label="parseInt(item.value)">
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="含税金额" prop="amount_including_tax">
                    <el-input v-model="formData.amount_including_tax" clearable placeholder="请输入含税金额" />
                </el-form-item>
                <el-form-item label="运费" prop="freight">
                    <el-input v-model="formData.freight" clearable placeholder="请输入运费" />
                </el-form-item>
                <el-form-item label="其他费用" prop="other_fee">
                    <el-input v-model="formData.other_fee" clearable placeholder="请输入其他费用" />
                </el-form-item>
                <el-form-item label="合计金额" prop="total_amount">
                    <el-input v-model="formData.total_amount" clearable placeholder="请输入合计金额" />
                </el-form-item>
                <el-form-item label="客户需求" prop="customer_require">
                    <el-input v-model="formData.customer_require" clearable placeholder="请输入客户需求" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="附件" prop="annex">
                    <el-input v-model="formData.annex" clearable placeholder="请输入附件" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="quotationEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiQuotationAdd, apiQuotationEdit, apiQuotationDetail } from '@/api/quotation'
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: '',
    customer_id: '',
    quotation_date: '',
    contacts: '',
    contacts_phone: '',
    create_user: '',
    invoice_type: '',
    amount_including_tax: '',
    freight: '',
    other_fee: '',
    total_amount: '',
    customer_require: '',
    remark: '',
    annex: '',
})


// 表单验证
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]
        }
    }


}

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


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