<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="项目名称" prop="project_name">
                    <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="日期" prop="date">
                    <el-date-picker class="flex-1 !flex" v-model="formData.date" clearable value-format="YYYY-MM-DD"
                        placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="星期">
                    <el-input v-model="formData.week" clearable placeholder="请输入星期" />
                </el-form-item>
                <el-form-item label="气温" prop="air_temperature">
                    <el-input v-model="formData.air_temperature" clearable placeholder="请输入气温" />
                </el-form-item>
                <el-form-item label="气候" prop="climate">
                    <el-input v-model="formData.climate" clearable placeholder="请输入气候" />
                </el-form-item>
                <el-form-item label="监理人员" prop="user">
                    <el-input v-model="formData.user" clearable placeholder="请输入监理人员" />
                </el-form-item>
                <el-form-item label="监理总监" prop="director">
                    <el-input v-model="formData.director" clearable placeholder="请输入监理总监" />
                </el-form-item>
                <el-form-item label="工程动态" prop="engineering_dynamics">
                    <el-input v-model="formData.engineering_dynamics" clearable placeholder="请输入工程动态" type="textarea" />
                </el-form-item>
                <el-form-item label="监理工作">
                    <el-input v-model="formData.supervision_work" clearable placeholder="请输入监理工作" type="textarea" />
                </el-form-item>
                <el-form-item label="安全文明施工情况">
                    <el-input v-model="formData.construction_situation" clearable placeholder="请输入安全文明施工情况"
                        type="textarea" />
                </el-form-item>
                <el-form-item label="其他事项">
                    <el-input v-model="formData.other_matters" clearable placeholder="请输入其他事项" type="textarea" />
                </el-form-item>
                <el-form-item label="附件">
                    <uploadAnnex :formData="formData"></uploadAnnex>

                </el-form-item>
            </el-form>
            <el-dialog v-model="showDialog" title="选择项目" width="70%">
                <dialogTable @customEvent="customEvent" :config="supervision_project" />

            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionDiaryEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionDiaryAdd, apiSupervisionDiaryEdit, apiSupervisionDiaryDetail } from '@/api/supervision_diary'
import { supervision_project } from "@/components/dialogTable/dialogTableConfig"
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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工程监理--监理日记' : '新增工程监理--监理日记'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_name: "",
    date: '',
    week: '',
    air_temperature: '',
    climate: '',
    user: '',
    director: '',
    engineering_dynamics: '',
    supervision_work: '',
    construction_situation: '',
    other_matters: '',
    annex: [],
})
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 customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}

// 表单验证
const formRules = reactive<any>({
    date: [{
        required: true,
        message: '请输入日期',
        trigger: ['blur']
    }],
    week: [{
        required: true,
        message: '请输入星期',
        trigger: ['blur']
    }],
    air_temperature: [{
        required: true,
        message: '请输入气温',
        trigger: ['blur']
    }],
    climate: [{
        required: true,
        message: '请输入气候',
        trigger: ['blur']
    }],
    user: [{
        required: true,
        message: '请输入监理人员',
        trigger: ['blur']
    }],
    director: [{
        required: true,
        message: '请输入监理总监',
        trigger: ['blur']
    }],
    engineering_dynamics: [{
        required: true,
        message: '请输入工程动态',
        trigger: ['blur']
    }],
    supervision_work: [{
        required: true,
        message: '请输入监理工作',
        trigger: ['blur']
    }],
    construction_situation: [{
        required: true,
        message: '请输入安全文明施工情况',
        trigger: ['blur']
    }],
    other_matters: [{
        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 apiSupervisionDiaryDetail({
        id: row.id
    })
    setFormData(data)
}


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