<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="supplier_id">
                    <el-input v-model="formData.supplier_id" clearable placeholder="请输入供应商id" />
                </el-form-item>
                <el-form-item label="审批id" prop="approve_id">
                    <el-input v-model="formData.approve_id" clearable placeholder="请输入审批id" />
                </el-form-item>
                <el-form-item label="项目id" prop="project_id">
                    <el-input v-model="formData.project_id" clearable placeholder="请输入项目id" />
                </el-form-item>
                <el-form-item label="合同编号" prop="contract_no">
                    <el-input v-model="formData.contract_no" clearable placeholder="请输入合同编号" />
                </el-form-item>
                <el-form-item label="供应商合同号" prop="supplier_contract_no">
                    <el-input v-model="formData.supplier_contract_no" clearable placeholder="请输入供应商合同号" />
                </el-form-item>
                <el-form-item label="合同类型" prop="contract_type">
                    <el-radio-group v-model="formData.contract_type" placeholder="请选择合同类型">
                        <el-radio 
                            v-for="(item, index) in dictData.procurement_contract_type"
                            :key="index"
                            :label="parseInt(item.value)"
                        >
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="签订日期" prop="signing_date">
                    <el-date-picker 
                        class="flex-1 !flex"
                        v-model="formData.signing_date"
                        clearable
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="选择签订日期">
                    </el-date-picker>
                </el-form-item>
                
                <el-form-item label="付款方式" prop="pay_type">
                    <el-radio-group v-model="formData.pay_type" placeholder="请选择付款方式">
                        <el-radio 
                            v-for="(item, index) in dictData.pay_type"
                            :key="index"
                            :label="parseInt(item.value)"
                        >
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="账期" prop="account_period">
                    <el-radio-group v-model="formData.account_period" placeholder="请选择账期">
                        <el-radio 
                            v-for="(item, index) in dictData.account_period"
                            :key="index"
                            :label="parseInt(item.value)"
                        >
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="合同金额" prop="amount">
                    <el-input v-model="formData.amount" clearable placeholder="请输入合同金额" />
                </el-form-item>
                <el-form-item label="不含税金额" prop="amount_excluding_tax">
                    <el-input v-model="formData.amount_excluding_tax" clearable placeholder="请输入不含税金额" />
                </el-form-item>
                <el-form-item label="金额大写" prop="amount_daxie">
                    <el-input v-model="formData.amount_daxie" clearable placeholder="请输入金额大写" />
                </el-form-item>
                <el-form-item label="质保金比例" prop="retention_money_rate">
                    <el-input v-model="formData.retention_money_rate" clearable placeholder="请输入质保金比例" />
                </el-form-item>
                <el-form-item label="质保金" prop="retention_money">
                    <el-input v-model="formData.retention_money" 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="procurementContractEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProcurementContractAdd, apiProcurementContractEdit, apiProcurementContractDetail } from '@/api/procurement_contract'
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: '',
    supplier_id: '',
    approve_id: '',
    project_id: '',
    contract_no: '',
    supplier_contract_no: '',
    contract_type: '',
    signing_date: '',
    pay_type: '',
    account_period: '',
    amount: '',
    amount_excluding_tax: '',
    amount_daxie: '',
    retention_money_rate: '',
    retention_money: '',
    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]
        }
    }
    
    //@ts-ignore
    formData.signing_date = timeFormat(formData.signing_date,'yyyy-mm-dd hh:MM:ss') 
}

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


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