<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80%" @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="dataid">
                            <el-input v-model="formData.dataid" clearable placeholder="系统自动生成" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="单据编号" prop="num">
                            <el-input v-model="formData.num" clearable placeholder="系统自动生成" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project_name">
                            <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                                @click="showDialog = true" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目编号" prop="project_num">
                            <el-input v-model="formData.project_num" clearable placeholder="请输入项目编号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="任务名称" prop="task">
                            <el-input v-model="formData.task" clearable placeholder="请输入任务名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="工序" prop="processes">
                            <el-input v-model="formData.processes" clearable placeholder="请输入工序" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="任务类型" prop="rtype">
                            <el-select class="flex-1" v-model="formData.rtype" clearable placeholder="请选择任务类型">
                                <el-option v-for="(item, index) in dictData.task_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="zType">
                            <el-select class="flex-1" v-model="formData.zType" clearable placeholder="请选择专业类型">
                                <el-option v-for="(item, index) in dictData.major_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="engineer">
                            <el-input v-model="formData.engineer" clearable placeholder="请输入工程师"
                                @click="userclick('engineer')" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目负责人" prop="head">
                            <el-input v-model="formData.head" clearable placeholder="请输入项目负责人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="审核流程" prop="examine">
                            <el-select class="flex-1" v-model="formData.examine" clearable placeholder="请选择审核流程">
                                <el-option v-for="(item, index) in dictData.review_process " :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="one">
                            <el-input v-model="formData.one" clearable placeholder="请输入一级审核人" @click="userclick('one')" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="二级审核人" prop="two">
                            <el-input v-model="formData.two" clearable placeholder="请输入二级审核人" @click="userclick('two')" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="三级审核人" prop="three">
                            <el-input v-model="formData.three" clearable placeholder="请输入三级审核人"
                                @click="userclick('three')" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="登记日期" prop="apptime">
                            <el-date-picker v-model="formData.apptime" clearable type="date" value-format="YYYY-MM-DD"
                                class="!flex flex-1" placeholder="请选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="任务级次" prop="level">
                            <el-input v-model="formData.level" clearable placeholder="请输入任务级次" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="建筑面积" prop="area">
                            <el-input v-model="formData.area" clearable placeholder="请输入建筑面积" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="建筑结构类型" prop="type">
                            <el-select class="flex-1" v-model="formData.type" clearable placeholder="请选择建筑结构类型">
                                <el-option v-for="(item, index) in dictData.building_structure_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="use">
                            <el-input v-model="formData.use" clearable placeholder="请输入建筑用途" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="施工单位" prop="construction">
                            <el-input v-model="formData.construction" clearable placeholder="请输入施工单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="编制要求" prop="ask">
                            <el-select class="flex-1" v-model="formData.ask" clearable placeholder="请选择建筑结构类型">
                                <el-option v-for="(item, index) in dictData.preparation_requirements " :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="according">
                            <el-select class="flex-1" v-model="formData.according" clearable placeholder="请选择造价依据">
                                <el-option v-for="(item, index) in dictData.cost_sentence  " :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="clbc">
                            <el-select class="flex-1" v-model="formData.clbc" clearable placeholder="请选择材料补差">
                                <el-option v-for="(item, index) in dictData.material_compensation   " :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="ssje">
                            <el-input v-model="formData.ssje" clearable placeholder="请输入送审金额" type="number" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="工程师核定价" prop="gcshd">
                            <el-input v-model="formData.gcshd" clearable placeholder="请输入工程师核定价" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="核增/核减金额" prop="hzhj" label-width="130px">
                            <el-input v-model="formData.hzhj" clearable placeholder="请输入核增/核减金额" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="人工单价" prop="rg">
                            <el-input v-model="formData.rg" clearable placeholder="请输入人工单价" type="number" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目负责人核定价" prop="xmhd" label-width="160px">
                            <el-input v-model="formData.xmhd" clearable placeholder="请输入项目负责人核定价" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="部门负责人核定价" prop="bmhd">
                            <el-input v-model="formData.bmhd" clearable placeholder="请输入部门负责人核定价" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="审核部核定价" prop="shbhd">
                            <el-input v-model="formData.shbhd" clearable placeholder="请输入审核部核定价" type="number" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="开工日期" prop="kaigong">
                            <el-date-picker v-model="formData.kaigong" clearable type="date" class="!flex flex-1"
                                value-format="YYYY-MM-DD" placeholder="请选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="竣工日期" prop="jungong">
                            <el-date-picker v-model="formData.jungong" clearable type="date" value-format="YYYY-MM-DD"
                                class="!flex flex-1" placeholder="请选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="备注" prop="bz">
                            <el-input v-model="formData.bz" clearable placeholder="请输入备注" type="textarea" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="结果文件" prop="result_file">
                            <uploadAnnex :formData="formData" value="result_file"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="项目负责人附件" prop="master_annex">
                            <uploadAnnex :formData="formData" value="master_annex"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="部门负责人附件" prop="bm_annex">
                            <uploadAnnex :formData="formData" value="bm_annex"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="审核部附件" prop="shb_annex">
                            <uploadAnnex :formData="formData" value="shb_annex"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <dialogTable :config="cost_project" @customEvent="customEvent">
                    </dialogTable>
                </el-dialog>
                <div v-if="showDialog3">
                    <personnelselector ref="personnel" @confirm="submituser" type="1">
                    </personnelselector>
                </div>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup name="taskHandlingThreeLevelReviewEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiTaskHandlingThreeLevelReviewAdd, apiTaskHandlingThreeLevelReviewEdit, } from '@/api/task_handling_three_level_review'
import type { PropType } from 'vue'
import { cost_project } from "@/components/dialogTable/dialogTableConfig"
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 showDialog = ref(false)
const showDialog3 = ref(false)
const personnel = ref<any>()


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑任务办理与三级审核' : '新增任务办理与三级审核'
})

// 表单数据
const formData = reactive({
    id: '',
    dataid: '',
    num: '',
    project_id: '',
    project_name: '',
    project_num: '',
    task: '',
    processes: '',
    rtype: '',
    zType: '',
    engineer: '',
    head: '',
    examine: '',
    one: '',
    two: '',
    three: '',
    apptime: '',
    level: '',
    area: '',
    type: '',
    use: '',
    construction: '',
    ask: '',
    according: '',
    clbc: '',
    ssje: '',
    gcshd: '',
    hzhj: '',
    rg: '',
    xmhd: '',
    bmhd: '',
    shbhd: '',
    kaigong: '',
    jungong: '',
    bz: '',
    result_file: [],
    master_annex: [],
    bm_annex: [],
    shb_annex: [],
    projectInfo: {}

})


//确认
const submituser = (e: any) => {
    formData[tableKey.value] = e.name
    showDialog3.value = false
}

const tableKey = ref('')
//打开弹窗
const userclick = async (key) => {
    tableKey.value = key
    showDialog3.value = true
    await nextTick()
    personnel.value.open()

}

const customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    formData.project_num = e.project_num
    showDialog.value = false
}

const chekcDate = (rule: any, value: any, callback: any) => {
    if (new Date(formData.kaigong) < new Date(formData.apptime)) {
        callback(new Error('开工日期不能早于登记日期'))
    } else {
        callback()
    }
}


const chekcDate1 = (rule: any, value: any, callback: any) => {
    if (new Date(formData.jungong) < new Date(formData.apptime)) {
        callback(new Error('竣工日期不能早于登记日期'))
    }
    else if (new Date(formData.jungong) < new Date(formData.kaigong)) {
        callback(new Error('竣工日期不能早于开工日期'))
    }
    else {
        callback()
    }
}

// 表单验证
const formRules = reactive<any>({

    kaigong: [
        {
            validator: chekcDate,
            trigger: ['blur']
        }],
    jungong: [
        {
            validator: chekcDate1,
            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]
        }
    }
    formData.project_name = formData.projectInfo?.project_name
    formData.project_num = formData.projectInfo.project_num

    let toStringList = ['rtype', 'zType', 'examine', 'type', 'ask', 'according', 'clbc']
    toStringList.forEach(element => {
        formData[element] = Number(formData[element])
    });

}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiTaskHandlingThreeLevelReviewEdit(data)
        : await apiTaskHandlingThreeLevelReviewAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
})
</script>
