<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80vw" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="关联协议框架" prop="related_contract_id">
                            <el-input v-model="formData.framework_agreement_name" clearable placeholder="点击选择协议框架"
                                @click="showDialog1 = true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同类型" prop="contract_type">
                            <el-select v-model="formData.contract_type" class="flex-1">
                                <el-option label="主合同" :value="0"></el-option>
                                <el-option label="框架协议" :value="1"></el-option>
                                <el-option label="补充协议" :value="2"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同名称" prop="contract_name">
                            <el-input v-model="formData.contract_name" clearable placeholder="请输入合同名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同编号" prop="contract_code">
                            <el-input v-model="formData.contract_code" clearable placeholder="请输入合同编号" v-type="'code'" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="甲方签约单位" prop="part_a">
                            <el-input v-model="formData.part_a_name" clearable placeholder="请输入甲方签约单位" readonly
                                @click="showDialog2 = true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系人">
                            <el-input v-model="formData.part_a_contact" clearable placeholder="请输入联系人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="业主地址">
                            <el-input v-model="formData.part_a_address" clearable placeholder="请输入业主地址" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="乙方签约单位" prop="part_b">
                            <el-input v-model="formData.part_b" clearable placeholder="请输入乙方签约单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="乙方签约人">
                            <el-input v-model="formData.part_b_signatory_name" clearable placeholder="点击选择"
                                @click="userclick('part_b_signatory')" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="业务性质" prop="business_nature">
                            <el-select class='flex-1' v-model="formData.business_nature">
                                <el-option v-for="(item, index) in  dictData.cost_consultation_business_nature "
                                    :key="index" :label="item.name" :value="parseInt(item.value)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="行业性质" prop="industry_nature">
                            <el-select class=' flex-1' v-model="formData.industry_nature">
                                <el-option v-for="(item, index) in  dictData.cost_consultation_industry_nature "
                                    :key="index" :label="item.name" :value="parseInt(item.value)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资金来源" prop="fund_sources">
                            <el-select class=' flex-1' v-model="formData.fund_sources">
                                <el-option v-for="(item, index) in  dictData.money_source " :key="index" :label="item.name"
                                    :value="parseInt(item.value)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="建设区域" prop="const_area">
                            <el-select class=' flex-1' v-model="formData.const_area">
                                <el-option v-for="(item, index) in  dictData.const_area " :key="index" :label="item.name"
                                    :value="parseInt(item.value)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="8">
                        <el-form-item label="协议性质" prop="agreement_nature">
                            <el-input v-model="formData.agreement_nature" clearable placeholder="请输入协议性质" />
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="8">
                        <el-form-item label="工程总投资">
                            <el-input v-model="formData.project_money" clearable placeholder="请输入工程总投资" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="工程规模及概况">
                            <el-input v-model="formData.project_scale" clearable placeholder="请输入工程规模及概况" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="签订金额" prop="signed_amount">
                            <el-input v-model="formData.signed_amount" clearable placeholder="请输入签订金额" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同签订费率">
                            <el-input v-model="formData.signed_rate" clearable placeholder="请输入合同签订费率" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同服务工期">
                            <el-input v-model="formData.service_duration" clearable placeholder="请输入合同服务工期" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="计划开始日期">
                            <el-date-picker class="flex-1 !flex" v-model="formData.start_date" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="选择发生日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="计划结束日期" prop="end_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.end_date" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="选择发生日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="履约金">
                            <el-input v-model="formData.performance_money" clearable placeholder="请输入履约金" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="履约金到期时间">
                            <el-date-picker class="flex-1 !flex" v-model="formData.performance_money_expiration_time"
                                clearable type="date" value-format="YYYY-MM-DD" placeholder="选择发生日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同付款方式">
                            <el-input v-model="formData.payment_method" clearable placeholder="请输入合同付款方式" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同主要内容">
                            <el-input v-model="formData.content" clearable placeholder="请输入合同主要内容" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="签订部门" prop="signed_dept">
                            <el-select v-model="formData.signed_dept" placeholder="请选择部门" class="flex-1">
                                <el-option :label="item.name" :value="item.id" v-for="item in deptList">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="签订负责人" prop="signed_head_name">
                            <el-input v-model="formData.signed_head_name" clearable placeholder="点击选择"
                                @click="userclick('signed_head')" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="监管部门" prop="supervise_dept">
                            <el-select v-model="formData.supervise_dept" placeholder="请选择部门" class="flex-1">
                                <el-option :label="item.name" :value="item.id" v-for="item in deptList">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目总监/经理">
                            <el-input v-model="formData.project_manager_name" clearable placeholder="点击选择"
                                @click="userclick('project_manager')" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实施方式">
                            <el-select class=' flex-1' v-model="formData.implement_method">
                                <el-option v-for="(item, index) in  dictData.cost_consultation_way " :key="index"
                                    :label="item.name" :value="parseInt(item.value)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="盖章名称">
                            <el-select class=' flex-1' v-model="formData.seal_name">
                                <el-option v-for="(item, index) in  dictData.cost_consultation_seal_name " :key="index"
                                    :label="item.name" :value="parseInt(item.value)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否限制">
                            <el-select v-model="formData.is_limit" class="flex-1">
                                <el-option label="否" :value="0"></el-option>
                                <el-option label="是" :value="1"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="formData.is_limit">
                        <el-form-item label="限制次数">
                            <el-input v-model="formData.limit_num" clearable placeholder="请输入限制次数" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="文件类型">
                            <el-select class='flex-1' v-model="formData.file_type">
                                <el-option v-for="( item, index ) in   dictData.cost_consultation_file_type  " :key="index"
                                    :label="item.name" :value="parseInt(item.value)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="盖章份数">
                            <el-input v-model="formData.seal_num" clearable placeholder="请输入盖章份数" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="盖章人">
                            <el-input v-model="formData.seal_user_name" clearable placeholder="点击选择"
                                @click="userclick('seal_user')" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="骑缝盖章">
                            <el-select class='flex-1' v-model="formData.plance_seal">
                                <el-option v-for="( item, index ) in   dictData.isqfgz  " :key="index" :label="item.name"
                                    :value="parseInt(item.value)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="发出日期">
                            <el-date-picker class="flex-1 !flex" v-model="formData.send_date" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="选择发生日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="邮寄编号">
                            <el-input v-model="formData.mailing_number" clearable placeholder="请输入邮寄编号" type="number" />
                        </el-form-item>
                    </el-col>

                    <!-- <el-col :span="8">
                        <el-form-item label="评审状态">
                            <el-select v-model="formData.review_status" class="flex-1">
                                <el-option label="待移交" :value="0"></el-option>
                                <el-option label="已移交" :value="1"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="立项状态">
                            <el-select v-model="formData.review_status" class="flex-1">
                                <el-option label="待立项" :value="0"></el-option>
                                <el-option label="已立项" :value="1"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col> -->
                    <createUserLable :form-data='formData' name='录入' flag></createUserLable>
                    <el-col :span="8">
                        <el-form-item label="附件">
                            <upload-annex :form-data="formData" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div v-if="showDialog">
                <personnelselector ref="personnel" @confirm="submituser" type="1">
                </personnelselector>
            </div>
            <el-dialog v-model="showDialog1" title="选择框架协议" width="70%">
                <dialogTable :config="marketing_framework_agreement" @customEvent="customEvent">
                </dialogTable>
            </el-dialog>
            <el-dialog v-model="showDialog2" title="选择甲方签订单位" width="70%">
                <dialogTable :config="marketing_custom" @customEvent="customEvent1">
                </dialogTable>
            </el-dialog>
        </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";
import { marketing_framework_agreement, marketing_custom } from "@/components/dialogTable/dialogTableConfig";



defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({}),
    },
    deptList: Array

});
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");
const showDialog = ref(false)
const showDialog1 = ref(false)
const showDialog2 = ref(false)
const personnel = ref(null)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == "edit"
        ? "编辑市场经营--合同信息"
        : "新增市场经营--合同信息";
});

// 表单数据
const formData = reactive({
    id: "",
    related_contract_id: "",
    framework_agreement_name: "",
    contract_type: "",
    contract_name: "",
    contract_code: "",
    part_a: "",
    part_a_name: "",
    part_a_contact: "",
    part_a_address: "",
    part_b: "",
    part_b_signatory: "",
    part_b_signatory_name: "",
    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: "",
    signed_head_name: "",
    supervise_dept: "",
    project_manager: "",
    project_manager_name: "",
    implement_method: "",
    seal_name: "",
    is_limit: "",
    limit_num: "",
    file_type: "",
    seal_num: "",
    seal_user: "",
    seal_user_name: "",
    plance_seal: "",
    send_date: "",
    mailing_number: "",
    review_status: "",
    status: "",
    create_user: "",
});


const customEvent = (e) => {
    formData.related_contract_id = e.id
    formData.framework_agreement_name = e.contract_name
    showDialog1.value = false
}

const customEvent1 = (e) => {
    formData.part_a = e.id
    formData.part_a_name = e.invoice_company
    showDialog2.value = false
}



let value;
const userclick = async (val) => {
    showDialog.value = true
    value = val
    await nextTick()
    personnel.value.open()

}

//确认
const submituser = (e: any) => {
    formData[value + '_name'] = e.name
    formData[value] = e.id
    showDialog.value = false
}

const chekcDate = (rule: any, value: any, callback: any) => {
    if (new Date(formData.end_date) < new Date(formData.start_date)) {
        callback(new Error('计划结束日期时间不能早于计划开始日期'))
    } else {
        callback()
    }
}

// 表单验证
const formRules = reactive<any>({
    related_contract_id: [
        {
            required: true,
            message: "请输入关联主合同id",
            trigger: ["blur"],
        },
    ],
    contract_type: [
        {
            required: true,
            message: "请输入合同类型",
            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"],
        },
        {
            validator: chekcDate,
            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: "请输入评审状态",
            trigger: ["blur"],
        },
    ],
    status: [
        {
            required: true,
            message: "请输入立项状态",
            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>
