<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="related_contract_id">
                    <el-input v-model="formData.related_contract_id" clearable placeholder="请输入关联主合同id" />
                </el-form-item>
                <el-form-item label="合同类型 0-主合同 1-框架协议 2-补充协议" prop="contract_type">
                    <el-input v-model="formData.contract_type" clearable placeholder="请输入合同类型 0-主合同 1-框架协议 2-补充协议" />
                </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="part_a">
                    <el-input v-model="formData.part_a" clearable placeholder="请输入甲方签约单位" />
                </el-form-item>
                <el-form-item label="联系人" prop="part_a_contact">
                    <el-input v-model="formData.part_a_contact" clearable placeholder="请输入联系人" />
                </el-form-item>
                <el-form-item label="业主地址" prop="part_a_address">
                    <el-input v-model="formData.part_a_address" clearable placeholder="请输入业主地址" />
                </el-form-item>
                <el-form-item label="乙方签约单位" prop="part_b">
                    <el-input v-model="formData.part_b" clearable placeholder="请输入乙方签约单位" />
                </el-form-item>
                <el-form-item label="乙方签约人" prop="part_b_signatory">
                    <el-input v-model="formData.part_b_signatory" clearable placeholder="请输入乙方签约人" />
                </el-form-item>
                <el-form-item label="业务性质" prop="business_nature">
                    <el-input v-model="formData.business_nature" clearable placeholder="请输入业务性质" />
                </el-form-item>
                <el-form-item label="行业性质" prop="industry_nature">
                    <el-input v-model="formData.industry_nature" clearable placeholder="请输入行业性质" />
                </el-form-item>
                <el-form-item label="资金来源" prop="fund_sources">
                    <el-input v-model="formData.fund_sources" clearable placeholder="请输入资金来源" />
                </el-form-item>
                <el-form-item label="建设区域" prop="const_area">
                    <el-input v-model="formData.const_area" clearable placeholder="请输入建设区域" />
                </el-form-item>
                <el-form-item label="协议性质" prop="agreement_nature">
                    <el-input v-model="formData.agreement_nature" clearable placeholder="请输入协议性质" />
                </el-form-item>
                <el-form-item label="工程总投资" prop="project_money">
                    <el-input v-model="formData.project_money" clearable placeholder="请输入工程总投资" />
                </el-form-item>
                <el-form-item label="工程规模及概况" prop="project_scale">
                    <el-input v-model="formData.project_scale" clearable placeholder="请输入工程规模及概况" />
                </el-form-item>
                <el-form-item label="签订金额" prop="signed_amount">
                    <el-input v-model="formData.signed_amount" clearable placeholder="请输入签订金额" />
                </el-form-item>
                <el-form-item label="合同签订费率" prop="signed_rate">
                    <el-input v-model="formData.signed_rate" clearable placeholder="请输入合同签订费率" />
                </el-form-item>
                <el-form-item label="合同服务工期" prop="service_duration">
                    <el-input v-model="formData.service_duration" clearable placeholder="请输入合同服务工期" />
                </el-form-item>
                <el-form-item label="计划开始日期" prop="start_date">
                    <el-input v-model="formData.start_date" clearable placeholder="请输入计划开始日期" />
                </el-form-item>
                <el-form-item label="计划结束日期" prop="end_date">
                    <el-input v-model="formData.end_date" clearable placeholder="请输入计划结束日期" />
                </el-form-item>
                <el-form-item label="履约金" prop="performance_money">
                    <el-input v-model="formData.performance_money" clearable placeholder="请输入履约金" />
                </el-form-item>
                <el-form-item label="履约金到期时间" prop="performance_money_expiration_time">
                    <el-input v-model="formData.performance_money_expiration_time" clearable placeholder="请输入履约金到期时间" />
                </el-form-item>
                <el-form-item label="合同付款方式" prop="payment_method">
                    <el-input v-model="formData.payment_method" clearable placeholder="请输入合同付款方式" />
                </el-form-item>
                <el-form-item label="合同主要内容" prop="content">
                    <el-input v-model="formData.content" clearable placeholder="请输入合同主要内容" />
                </el-form-item>
                <el-form-item label="附件" prop="annex">
                    <el-input v-model="formData.annex" clearable placeholder="请输入附件" />
                </el-form-item>
                <el-form-item label="签订部门" prop="signed_dept">
                    <el-input v-model="formData.signed_dept" clearable placeholder="请输入签订部门" />
                </el-form-item>
                <el-form-item label="签订负责人" prop="signed_head">
                    <el-input v-model="formData.signed_head" clearable placeholder="请输入签订负责人" />
                </el-form-item>
                <el-form-item label="监管部门" prop="supervise_dept">
                    <el-input v-model="formData.supervise_dept" clearable placeholder="请输入监管部门" />
                </el-form-item>
                <el-form-item label="项目总监/经理" prop="project_manager">
                    <el-input v-model="formData.project_manager" clearable placeholder="请输入项目总监/经理" />
                </el-form-item>
                <el-form-item label="实施方式" prop="implement_method">
                    <el-input v-model="formData.implement_method" clearable placeholder="请输入实施方式" />
                </el-form-item>
                <el-form-item label="盖章名称" prop="seal_name">
                    <el-input v-model="formData.seal_name" clearable placeholder="请输入盖章名称" />
                </el-form-item>
                <el-form-item label="是否限制" prop="is_limit">
                    <el-input v-model="formData.is_limit" clearable placeholder="请输入是否限制" />
                </el-form-item>
                <el-form-item label="限制次数" prop="limit_num">
                    <el-input v-model="formData.limit_num" clearable placeholder="请输入限制次数" />
                </el-form-item>
                <el-form-item label="文件类型" prop="file_type">
                    <el-input v-model="formData.file_type" clearable placeholder="请输入文件类型" />
                </el-form-item>
                <el-form-item label="盖章份数" prop="seal_num">
                    <el-input v-model="formData.seal_num" clearable placeholder="请输入盖章份数" />
                </el-form-item>
                <el-form-item label="盖章人" prop="seal_user">
                    <el-input v-model="formData.seal_user" clearable placeholder="请输入盖章人" />
                </el-form-item>
                <el-form-item label="骑缝盖章" prop="plance_seal">
                    <el-input v-model="formData.plance_seal" clearable placeholder="请输入骑缝盖章" />
                </el-form-item>
                <el-form-item label="发出日期" prop="send_date">
                    <el-input v-model="formData.send_date" clearable placeholder="请输入发出日期" />
                </el-form-item>
                <el-form-item label="邮寄编号" prop="mailing_number">
                    <el-input v-model="formData.mailing_number" clearable placeholder="请输入邮寄编号" />
                </el-form-item>
                <el-form-item label="评审状态 0-待移交 1-已移交" prop="review_status">
                    <el-input v-model="formData.review_status" clearable placeholder="请输入评审状态 0-待移交 1-已移交" />
                </el-form-item>
                <el-form-item label="立项状态 0-待立项 1-已立项" prop="status">
                    <el-input v-model="formData.status" clearable placeholder="请输入立项状态 0-待立项 1-已立项" />
                </el-form-item>
                <el-form-item label="录入人" prop="create_user">
                    <el-input v-model="formData.create_user" clearable placeholder="请输入录入人" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="marketingContractEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiMarketingContractAdd, apiMarketingContractEdit, apiMarketingContractDetail } from '@/api/marketing_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: '',
    related_contract_id: '',
    contract_type: '',
    contract_name: '',
    contract_code: '',
    part_a: '',
    part_a_contact: '',
    part_a_address: '',
    part_b: '',
    part_b_signatory: '',
    business_nature: '',
    industry_nature: '',
    fund_sources: '',
    const_area: '',
    agreement_nature: '',
    project_money: '',
    project_scale: '',
    signed_amount: '',
    signed_rate: '',
    service_duration: '',
    start_date: '',
    end_date: '',
    performance_money: '',
    performance_money_expiration_time: '',
    payment_method: '',
    content: '',
    annex: '',
    signed_dept: '',
    signed_head: '',
    supervise_dept: '',
    project_manager: '',
    implement_method: '',
    seal_name: '',
    is_limit: '',
    limit_num: '',
    file_type: '',
    seal_num: '',
    seal_user: '',
    plance_seal: '',
    send_date: '',
    mailing_number: '',
    review_status: '',
    status: '',
    create_user: '',
})


// 表单验证
const formRules = reactive<any>({
    related_contract_id: [{
        required: true,
        message: '请输入关联主合同id',
        trigger: ['blur']
    }],
    contract_type: [{
        required: true,
        message: '请输入合同类型 0-主合同 1-框架协议 2-补充协议',
        trigger: ['blur']
    }],
    contract_name: [{
        required: true,
        message: '请输入合同名称',
        trigger: ['blur']
    }],
    contract_code: [{
        required: true,
        message: '请输入合同编号',
        trigger: ['blur']
    }],
    part_a: [{
        required: true,
        message: '请输入甲方签约单位',
        trigger: ['blur']
    }],
    part_a_contact: [{
        required: true,
        message: '请输入联系人',
        trigger: ['blur']
    }],
    part_a_address: [{
        required: true,
        message: '请输入业主地址',
        trigger: ['blur']
    }],
    part_b: [{
        required: true,
        message: '请输入乙方签约单位',
        trigger: ['blur']
    }],
    part_b_signatory: [{
        required: true,
        message: '请输入乙方签约人',
        trigger: ['blur']
    }],
    business_nature: [{
        required: true,
        message: '请输入业务性质',
        trigger: ['blur']
    }],
    industry_nature: [{
        required: true,
        message: '请输入行业性质',
        trigger: ['blur']
    }],
    fund_sources: [{
        required: true,
        message: '请输入资金来源',
        trigger: ['blur']
    }],
    const_area: [{
        required: true,
        message: '请输入建设区域',
        trigger: ['blur']
    }],
    agreement_nature: [{
        required: true,
        message: '请输入协议性质',
        trigger: ['blur']
    }],
    project_money: [{
        required: true,
        message: '请输入工程总投资',
        trigger: ['blur']
    }],
    project_scale: [{
        required: true,
        message: '请输入工程规模及概况',
        trigger: ['blur']
    }],
    signed_amount: [{
        required: true,
        message: '请输入签订金额',
        trigger: ['blur']
    }],
    signed_rate: [{
        required: true,
        message: '请输入合同签订费率',
        trigger: ['blur']
    }],
    service_duration: [{
        required: true,
        message: '请输入合同服务工期',
        trigger: ['blur']
    }],
    start_date: [{
        required: true,
        message: '请输入计划开始日期',
        trigger: ['blur']
    }],
    end_date: [{
        required: true,
        message: '请输入计划结束日期',
        trigger: ['blur']
    }],
    performance_money: [{
        required: true,
        message: '请输入履约金',
        trigger: ['blur']
    }],
    performance_money_expiration_time: [{
        required: true,
        message: '请输入履约金到期时间',
        trigger: ['blur']
    }],
    payment_method: [{
        required: true,
        message: '请输入合同付款方式',
        trigger: ['blur']
    }],
    content: [{
        required: true,
        message: '请输入合同主要内容',
        trigger: ['blur']
    }],
    signed_dept: [{
        required: true,
        message: '请输入签订部门',
        trigger: ['blur']
    }],
    signed_head: [{
        required: true,
        message: '请输入签订负责人',
        trigger: ['blur']
    }],
    supervise_dept: [{
        required: true,
        message: '请输入监管部门',
        trigger: ['blur']
    }],
    project_manager: [{
        required: true,
        message: '请输入项目总监/经理',
        trigger: ['blur']
    }],
    implement_method: [{
        required: true,
        message: '请输入实施方式',
        trigger: ['blur']
    }],
    seal_name: [{
        required: true,
        message: '请输入盖章名称',
        trigger: ['blur']
    }],
    is_limit: [{
        required: true,
        message: '请输入是否限制',
        trigger: ['blur']
    }],
    limit_num: [{
        required: true,
        message: '请输入限制次数',
        trigger: ['blur']
    }],
    file_type: [{
        required: true,
        message: '请输入文件类型',
        trigger: ['blur']
    }],
    seal_num: [{
        required: true,
        message: '请输入盖章份数',
        trigger: ['blur']
    }],
    seal_user: [{
        required: true,
        message: '请输入盖章人',
        trigger: ['blur']
    }],
    plance_seal: [{
        required: true,
        message: '请输入骑缝盖章',
        trigger: ['blur']
    }],
    send_date: [{
        required: true,
        message: '请输入发出日期',
        trigger: ['blur']
    }],
    mailing_number: [{
        required: true,
        message: '请输入邮寄编号',
        trigger: ['blur']
    }],
    review_status: [{
        required: true,
        message: '请输入评审状态 0-待移交 1-已移交',
        trigger: ['blur']
    }],
    status: [{
        required: true,
        message: '请输入立项状态 0-待立项 1-已立项',
        trigger: ['blur']
    }],
    create_user: [{
        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 apiMarketingContractDetail({
        id: row.id
    })
    setFormData(data)
}


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