<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="购买标书id" prop="buy_bidding_document_id">
                    <el-input v-model="formData.buy_bidding_document_id" clearable placeholder="请输入购买标书id" />
                </el-form-item>
                <el-form-item label="合同名称" prop="contract_name">
                    <el-input v-model="formData.contract_name" clearable placeholder="请输入合同名称" />
                </el-form-item>
                <el-form-item label="合同编号" prop="contract_code">
                    <el-input v-model="formData.contract_code" clearable placeholder="请输入合同编号" />
                </el-form-item>
                <el-form-item label="合同类型" prop="contract_type">
                    <el-select class="flex-1" v-model="formData.contract_type" clearable placeholder="请选择合同类型">
                        <el-option 
                            v-for="(item, index) in dictData.contract_type"
                            :key="index" 
                            :label="item.name"
                            :value="parseInt(item.value)"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="合同计价方式" prop="contract_pricing_method">
                    <el-select class="flex-1" v-model="formData.contract_pricing_method" clearable placeholder="请选择合同计价方式">
                        <el-option 
                            v-for="(item, index) in dictData.contract_pricing_method"
                            :key="index" 
                            :label="item.name"
                            :value="parseInt(item.value)"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="甲方" prop="party_a">
                    <el-input v-model="formData.party_a" clearable placeholder="请输入甲方" />
                </el-form-item>
                <el-form-item label="甲方通讯地址" prop="party_a_contact_address">
                    <el-input v-model="formData.party_a_contact_address" clearable placeholder="请输入甲方通讯地址" />
                </el-form-item>
                <el-form-item label="甲方主要负责人" prop="party_a_diretor">
                    <el-input v-model="formData.party_a_diretor" clearable placeholder="请输入甲方主要负责人" />
                </el-form-item>
                <el-form-item label="甲方电话" prop="party_a_phone">
                    <el-input v-model="formData.party_a_phone" clearable placeholder="请输入甲方电话" />
                </el-form-item>
                <el-form-item label="甲方手机" prop="party_a_mobile">
                    <el-input v-model="formData.party_a_mobile" clearable placeholder="请输入甲方手机" />
                </el-form-item>
                <el-form-item label="甲方邮箱" prop="party_a_email">
                    <el-input v-model="formData.party_a_email" clearable placeholder="请输入甲方邮箱" />
                </el-form-item>
                <el-form-item label="乙方" prop="party_b">
                    <el-input v-model="formData.party_b" clearable placeholder="请输入乙方" />
                </el-form-item>
                <el-form-item label="乙方通讯地址" prop="party_b_contact_address">
                    <el-input v-model="formData.party_b_contact_address" clearable placeholder="请输入乙方通讯地址" />
                </el-form-item>
                <el-form-item label="乙方主要负责人" prop="party_b_diretor">
                    <el-input v-model="formData.party_b_diretor" clearable placeholder="请输入乙方主要负责人" />
                </el-form-item>
                <el-form-item label="乙方电话" prop="party_b_phone">
                    <el-input v-model="formData.party_b_phone" clearable placeholder="请输入乙方电话" />
                </el-form-item>
                <el-form-item label="乙方手机" prop="party_b_mobile">
                    <el-input v-model="formData.party_b_mobile" clearable placeholder="请输入乙方手机" />
                </el-form-item>
                <el-form-item label="乙方邮箱" prop="party_b_email">
                    <el-input v-model="formData.party_b_email" clearable placeholder="请输入乙方邮箱" />
                </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_daxie">
                    <el-input v-model="formData.amount_daxie" clearable placeholder="请输入金额大写" />
                </el-form-item>
                <el-form-item label="业务负责人" prop="business_director">
                    <el-input v-model="formData.business_director" clearable placeholder="请输入业务负责人" />
                </el-form-item>
                <el-form-item label="合同状态" prop="contract_status">
                    <el-input v-model="formData.contract_status" clearable placeholder="请输入合同状态" />
                </el-form-item>
                <el-form-item label="合同有效期" prop="expire">
                    <el-date-picker 
                        class="flex-1 !flex"
                        v-model="formData.expire"
                        clearable
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="选择合同有效期">
                    </el-date-picker>
                </el-form-item>
                
                <el-form-item label="签约日期" prop="contract_date">
                    <el-date-picker 
                        class="flex-1 !flex"
                        v-model="formData.contract_date"
                        clearable
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="选择签约日期">
                    </el-date-picker>
                </el-form-item>
                
                <el-form-item label="主要条款" prop="main_content">
                    <el-input v-model="formData.main_content" 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="contractEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiContractAdd, apiContractEdit, apiContractDetail } from '@/api/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({
    approve_id: '',
    customer_id: '',
    buy_bidding_document_id: '',
    contract_name: '',
    contract_code: '',
    contract_type: '',
    contract_pricing_method: '',
    party_a: '',
    party_a_contact_address: '',
    party_a_diretor: '',
    party_a_phone: '',
    party_a_mobile: '',
    party_a_email: '',
    party_b: '',
    party_b_contact_address: '',
    party_b_diretor: '',
    party_b_phone: '',
    party_b_mobile: '',
    party_b_email: '',
    amount: '',
    amount_daxie: '',
    business_director: '',
    contract_status: '',
    expire: '',
    contract_date: '',
    main_content: '',
    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 apiContractDetail({
        approve_id: row.approve_id
    })
    setFormData(data)
}


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