<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="project_name">
                            <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                                @click="showDialog = true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目通知编号">
                            <el-input v-model="formData.code" clearable placeholder="系统自动生成" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="要求回复日期" prop="reply_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.reply_date" clearable
                                value-format="YYYY-MM-DD" placeholder="选择时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="施工单位" prop="company_name">
                            <el-input v-model="formData.company_name" clearable placeholder="点击选择施工单位" readonly
                                @click="showDialog2 = true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="通知人员" prop="notify_user">
                            <el-input v-model="formData.notify_user" clearable placeholder="请输入通知人员" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="下达日期" prop="issue_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.issue_date" clearable
                                value-format="YYYY-MM-DD" placeholder="选择时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="附件" prop="annex">
                            <annexUpload :annex="formData.annex" @handleAvatarSuccess="handleAvatarSuccess_four"
                                @delFile="delFileFn" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <supervision_project @customEvent="customEvent"></supervision_project>
                </el-dialog>
                <div v-if="showDialog2">
                    <el-dialog v-model="showDialog2" width=" 70%" title="选择施工单位">
                        <supervision_participating_units @customEvent="customEvent1" :project_id="formData.project_id"
                            :unit_type="6">
                        </supervision_participating_units>
                    </el-dialog>
                </div>
            </el-form>
            <div style="margin-bottom: 30px;">通知问题</div>
            <div style="margin-bottom: 30px;">
                <el-table :data="formData.problem">
                    <el-table-column label="序号" width="150px">
                        <template #default="{ row }">
                            <el-button @click="handleAdd1()">+</el-button>
                            <el-button @click="handleDelete1(row)">-</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="事由描述" prop="problem_description">
                        <template #default="{ row }">
                            <el-input v-model="row.problem_description" />
                        </template>
                    </el-table-column>
                    <el-table-column label="内容描述" prop="content_description">
                        <template #default="{ row }">
                            <el-input v-model="row.content_description" />
                        </template>
                    </el-table-column>
                    <el-table-column label="要求描述" prop="demand_description">
                        <template #default="{ row }">
                            <el-input v-model="row.demand_description" />
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionNoticeEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionNoticeAdd, apiSupervisionNoticeEdit, apiSupervisionNoticeDetail, apisupervision_notice_problem, apisupervision_notice_problem_delete } from '@/api/supervision_notice'
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 showDialog = ref(false)
const showDialog2 = ref(false)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工程监理--监理通知单' : '新增工程监理--监理通知单'
})
const handleAvatarSuccess_four = (response: any) => {
    Array.isArray(formData.annex) ? '' : 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 formData = reactive({
    id: '',
    project_id: '',
    project_name: '',
    code: '',
    reply_date: '',
    company_id: '',
    company_name: '',
    notify_user: '',
    issue_date: '',
    annex: '',
    problem: [{
        "problem_description": "",
        "content_description": "",
        "demand_description": ""
    }]
})
const handleAdd1 = () => {
    formData.problem.push({
        "problem_description": "",
        "content_description": "",
        "demand_description": ""
    })
}
const handleDelete1 = async (row: any) => {
    if (row.id) {
        await apisupervision_notice_problem_delete({ id: row.id })
        const index = formData.problem.indexOf(row);
        formData.problem.splice(index, 1);
    } else {
        const index = formData.problem.indexOf(row);
        formData.problem.splice(index, 1);
    }
}

const customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
    formData.company_id = ''
    formData.company_name = ''
}
const customEvent1 = (e) => {
    formData.company_id = e.id
    formData.company_name = e.project_name
    showDialog2.value = false
}
// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    code: [{
        required: true,
        message: '请输入项目通知编号',
        trigger: ['blur']
    }],
    reply_date: [{
        required: true,
        message: '请输入要求回复日期',
        trigger: ['blur']
    }],
    company_id: [{
        required: true,
        message: '请输入施工单位',
        trigger: ['blur']
    }],
    notify_user: [{
        required: true,
        message: '请输入通知人员',
        trigger: ['blur']
    }],
    issue_date: [{
        required: true,
        message: '请输入下达日期',
        trigger: ['blur']
    }],
    rectification_reply_time: [{
        required: true,
        message: '请输入整改回复时间',
        trigger: ['blur']
    }],
    rectification_reply_user: [{
        required: true,
        message: '请输入整改回复人',
        trigger: ['blur']
    }],
    rectification_reply_opinions: [{
        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]
        }
    }

    let res = await apisupervision_notice_problem({ notice_id: formData.id })
    formData.problem = res.lists
}

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


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