<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="90px" :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_id">
                            <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-input v-model="formData.zType" clearable placeholder="请输入专业类型" /> -->
                            <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-input v-model="formData.apptime" clearable placeholder="请输入登记日期" /> -->
                            <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" label-width="120px">
                            <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-input v-model="formData.ask" clearable placeholder="请输入编制要求" /> -->
                            <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-input v-model="formData.according" clearable placeholder="请输入造价依据" /> -->
                            <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-input v-model="formData.clbc" clearable placeholder="请输入材料补差" /> -->
                            <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" label-width="120px">
                            <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" label-width="140px">
                            <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" label-width="120px">
                            <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-input v-model="formData.kaigong" clearable placeholder="请输入开工日期" /> -->
                            <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-input v-model="formData.jungong" clearable placeholder="请输入竣工日期" /> -->
                            <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="请输入备注" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="结果文件" prop="result_file">
                            <annexUpload :annex="formData.result_file" @handleAvatarSuccess="handleAvatarSuccess_four"
                                @delFile="delFileFn" />

                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="项目负责人附件" prop="master_annex" label-width="140px">
                            <annexUpload :annex="formData.master_annex" @handleAvatarSuccess="handleAvatarSuccess"
                                @delFile="delFileFn1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="部门负责人附件" prop="bm_annex" label-width="140px">
                            <annexUpload :annex="formData.bm_annex" @handleAvatarSuccess="handleAvatarSuccess2"
                                @delFile="delFileFn2" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="审核部附件" prop="shb_annex">
                            <annexUpload :annex="formData.shb_annex" @handleAvatarSuccess="handleAvatarSuccess3"
                                @delFile="delFileFn3" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <cost_project @customEvent="customEvent"></cost_project>
                </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 { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import file from "@/components/material/file.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 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 handleAvatarSuccess_four = (response: any) => {
    // @ts-ignore
    response.code != 0 ? formData.result_file.push({ uri: response.data.uri, name: response.data.name }) : ElMessage.error(response.msg);
};

// 删除上传的文件
const delFileFn = (index: number) => { formData.result_file.splice(index, 1) }



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

// 删除上传的文件
const delFileFn1 = (index: number) => { formData.master_annex.splice(index, 1) }

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

// 删除上传的文件
const delFileFn2 = (index: number) => { formData.bm_annex.splice(index, 1) }


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

// 删除上传的文件
const delFileFn3 = (index: number) => { formData.shb_annex.splice(index, 1) }

//确认
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 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]
        }
    }
    formData.project_name = formData.projectInfo?.project_name
    formData.project_num = formData.projectInfo.project_num

}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiTaskHandlingThreeLevelReviewDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
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,
    getDetail
})
</script>
