<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="contract_id">
                    <el-input v-model="formData.contract_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="洽商单名称" prop="negotiation_name">
                    <el-input v-model="formData.negotiation_name" clearable placeholder="请输入洽商单名称" />
                </el-form-item>
                <el-form-item label="洽商编号" prop="negotiation_no">
                    <el-input v-model="formData.negotiation_no" clearable placeholder="请输入洽商编号" />
                </el-form-item>
                <el-form-item label="洽商金额" prop="negotiation_amount">
                    <el-input v-model="formData.negotiation_amount" clearable placeholder="请输入洽商金额" />
                </el-form-item>
                <el-form-item label="洽商类别" prop="negotiation_type">
                    <el-radio-group v-model="formData.negotiation_type" placeholder="请选择洽商类别">
                        <el-radio 
                            v-for="(item, index) in dictData.negotiation_type"
                            :key="index"
                            :label="parseInt(item.value)"
                        >
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="成本金额（人工）" prop="labor_costs">
                    <el-input v-model="formData.labor_costs" clearable placeholder="请输入成本金额（人工）" />
                </el-form-item>
                <el-form-item label="成本金额（材料" prop="material_costs">
                    <el-input v-model="formData.material_costs" clearable placeholder="请输入成本金额（材料" />
                </el-form-item>
                <el-form-item label="洽商质保金额" prop="warranty_amount">
                    <el-input v-model="formData.warranty_amount" clearable placeholder="请输入洽商质保金额" />
                </el-form-item>
                <el-form-item label="洽商质保到期时间" prop="Warranty_expire_date">
                    <el-date-picker 
                        class="flex-1 !flex"
                        v-model="formData.Warranty_expire_date"
                        clearable
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="选择洽商质保到期时间">
                    </el-date-picker>
                </el-form-item>
                
                <el-form-item label="利润" prop="profit">
                    <el-input v-model="formData.profit" clearable placeholder="请输入利润" />
                </el-form-item>
                <el-form-item label="利润率" prop="profit_rate">
                    <el-input v-model="formData.profit_rate" clearable placeholder="请输入利润率" />
                </el-form-item>
                <el-form-item label="洽商内容" prop="negotiation_content">
                    <el-input v-model="formData.negotiation_content" clearable placeholder="请输入洽商内容" />
                </el-form-item>
                <el-form-item label="具体说明" prop="negotiation_detail">
                    <el-input v-model="formData.negotiation_detail" clearable placeholder="请输入具体说明" />
                </el-form-item>
                <el-form-item label="签证报价表" prop="negotiation_quotation">
                    <el-input v-model="formData.negotiation_quotation" clearable placeholder="请输入签证报价表" />
                </el-form-item>
                <el-form-item label="洽商依据" prop="negotiation_basis">
                    <el-input v-model="formData.negotiation_basis" clearable placeholder="请输入洽商依据" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="contractNegotiationEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiContractNegotiationAdd, apiContractNegotiationEdit, apiContractNegotiationDetail } from '@/api/contract_negotiation'
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: '',
    contract_id: '',
    approve_id: '',
    negotiation_name: '',
    negotiation_no: '',
    negotiation_amount: '',
    negotiation_type: '',
    labor_costs: '',
    material_costs: '',
    warranty_amount: '',
    Warranty_expire_date: '',
    profit: '',
    profit_rate: '',
    negotiation_content: '',
    negotiation_detail: '',
    negotiation_quotation: '',
    negotiation_basis: '',
})


// 表单验证
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.Warranty_expire_date = timeFormat(formData.Warranty_expire_date,'yyyy-mm-dd hh:MM:ss') 
}

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


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