<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="160px" :rules="formRules">
                <el-row>
                    <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_num">
                            <el-input v-model="formData.contract_num" clearable placeholder="请输入合同编号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <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.cost_contract_type" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="甲方签约单位" prop="part_a">
                            <el-input v-model="formData.part_a" clearable placeholder="请输入甲方签约单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="甲方联系人" prop="part_a_contract">
                            <el-input v-model="formData.part_a_contract" clearable placeholder="请输入甲方联系人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="业主地址" prop="part_a_address">
                            <el-input v-model="formData.part_a_address" clearable placeholder="请输入业主地址" />
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <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="乙方签约人" prop="part_b_signatory">
                            <el-input v-model="formData.part_b_signatory" clearable placeholder="请输入乙方签约人" />
                        </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" clearable
                                placeholder="请选择业务性质">
                                <el-option v-for="(item, index) in dictData.cost_consultation_business_nature"
                                    :key="index" :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="行业性质" prop="industry_nature">
                            <el-select class="flex-1" v-model="formData.industry_nature" clearable
                                placeholder="请选择行业性质">
                                <el-option v-for="(item, index) in dictData.cost_consultation_industry_nature"
                                    :key="index" :label="item.name" :value="parseInt(item.value)" />
                            </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" clearable placeholder="请选择建设区域">
                                <el-option v-for="(item, index) in dictData.cost_consultation_const_area" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </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" clearable placeholder="请选择资金来源">
                                <el-option v-for="(item, index) in dictData.cost_consultation_fund_sources" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="工程总投资" prop="project_money">
                            <el-input v-model="formData.project_money" clearable placeholder="请输入工程总投资" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="工程规模及概况" prop="project_scale">
                            <el-input v-model="formData.project_scale" clearable placeholder="请输入工程规模及概况"
                                type="textarea" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="签订金额(元)" prop="money">
                            <el-input v-model="formData.money" clearable placeholder="请输入签订金额(元)" />
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="合同服务工期" prop="contract_service">
                            <el-input v-model="formData.contract_service" clearable placeholder="请输入合同服务工期" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同签订费率(%)" prop="rate">
                            <el-input v-model="formData.rate" clearable placeholder="请输入合同签订费率(%)" type="number" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="合同计划开始日期" prop="start_date">
                            <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-row>
                <el-row>
                    <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="合同履约金(元)" prop="performance">
                            <el-input v-model="formData.performance" clearable placeholder="请输入合同履约金(元)"
                                type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="履约金到期时间" prop="due_time">
                            <el-date-picker class="flex-1 !flex" v-model="formData.due_time" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="请选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="合同付款方式" prop="payment">
                            <el-input v-model="formData.payment" clearable placeholder="请输入合同付款方式" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同主要内容" prop="content">
                            <el-input v-model="formData.content" clearable placeholder="请输入合同主要内容" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="创建人" prop="founder">
                            <el-input v-model="formData.founder" clearable placeholder="请输入创建人" />
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="创建日期" prop="create_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.create_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="dept">
                            <el-input v-model="formData.dept" clearable placeholder="请输入签订部门" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="签订负责人" prop="signing_person">
                            <el-input v-model="formData.signing_person" clearable placeholder="请输入签订负责人" />
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="监管部门" prop="regulators">
                            <el-input v-model="formData.regulators" clearable placeholder="请输入监管部门" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目总监/经理" prop="project_director">
                            <el-input v-model="formData.project_director" clearable placeholder="请输入项目总监/经理" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实施方式" prop="way">
                            <el-select class="flex-1" v-model="formData.way" clearable placeholder="请选择实施方式">
                                <el-option v-for="(item, index) in dictData.cost_consultation_way" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="盖章名称" prop="seal_name">
                            <el-select class="flex-1" v-model="formData.seal_name" clearable placeholder="请选择盖章名称">
                                <el-option v-for="(item, index) in dictData.cost_consultation_seal_name" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否限制" prop="limit">
                            <el-select class="flex-1" v-model="formData.limit" clearable placeholder="请选择">
                                <el-option label="是" :value="(1)" />
                                <el-option label="否" :value="(0)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="限制次数" prop="limit_num">
                            <el-input v-model="formData.limit_num" clearable type="number" placeholder="请输入限制次数" />
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="文件类型" prop="file_type">
                            <el-select class="flex-1" v-model="formData.file_type" clearable placeholder="请选择盖章名称">
                                <el-option v-for="(item, index) in dictData.cost_consultation_file_type" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>

                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="盖章份数" prop="seal_num">
                            <el-input v-model="formData.seal_num" clearable type="number" placeholder="请输入盖章份数" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="盖章人" prop="sealer">
                            <el-input v-model="formData.sealer" clearable placeholder="请输入盖章人" />
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="骑缝盖章" prop="plance_seal">
                            <el-select class="flex-1" v-model="formData.plance_seal" clearable placeholder="请选择">
                                <el-option label="是" :value="(1)" />
                                <el-option label="否" :value="(0)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="发出日期" prop="send_date">
                            <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="邮寄编号" prop="number">
                            <el-input v-model="formData.number" clearable placeholder="请输入邮寄编号" />
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-form-item label="附件">
                    <uploadAnnex :formData="formData"></uploadAnnex>

                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="costApprovedProjectEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiCostApprovedProjectAdd, apiCostApprovedProjectEdit, apiCostApprovedProjectDetail } from '@/api/cost_approved_project'
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_name: '',
    contract_num: '',
    contract_type: '',
    part_a: '',
    part_a_contract: '',
    part_a_address: '',
    part_b: '',
    part_b_signatory: '',
    business_nature: '',
    industry_nature: '',
    fund_sources: '',
    const_area: '',
    project_money: '',
    project_scale: '',
    money: '',
    rate: '',
    contract_service: '',
    start_date: '',
    end_date: '',
    performance: '',
    due_time: '',
    payment: '',
    content: '',
    founder: '',
    create_date: '',
    dept: '',
    signing_person: '',
    regulators: '',
    project_director: '',
    way: '',
    seal_name: '',
    limit: '',
    limit_num: '',
    file_type: '',
    seal_num: '',
    sealer: '',
    plance_seal: '',
    send_date: '',
    number: '',
    "annex": []
})

const handleAvatarSuccess_four = (response: any) => {
    formData.annex ||= []
    // @ts-ignore
    response.code != 0 ? formData.annex.push({ uri: response.data.uri, name: response.data.name }) : ElMessage.error(response.msg);
};

// 删除上传的文件
const delFileFn = (index: number) => { formData.annex.splice(index, 1) }
// 表单验证
const formRules = reactive<any>({
    contract_name: [{
        required: true,
        message: '请输入合同名称',
        trigger: ['blur']
    }],
    contract_num: [{
        required: true,
        message: '请输入合同编号',
        trigger: ['blur']
    }],
    part_a: [{
        required: true,
        message: '请输入甲方签约单位',
        trigger: ['blur']
    }],
    part_b: [{
        required: true,
        message: '请输入乙方签约单位',
        trigger: ['blur']
    }],
    money: [{
        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 apiCostApprovedProjectDetail({
        id: row.id
    })
    setFormData(data)
}


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