<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="项目id" prop="project_id">
                    <el-input v-model="formData.project_id" clearable placeholder="请选择项目" />
                </el-form-item>
                <el-form-item label="旁站编号" prop="side_station_code">
                    <el-input v-model="formData.side_station_code" clearable placeholder="请输入旁站编号" />
                </el-form-item>
                <el-form-item label="旁站类型" prop="side_station_type">
                    <el-input v-model="formData.side_station_type" clearable placeholder="请输入旁站类型" />
                </el-form-item>
                <el-form-item label="单位工程" prop="check_item_id">
                    <el-input v-model="formData.check_item_id" clearable placeholder="请输入单位工程" />
                </el-form-item>
                <el-form-item label="部位" prop="position">
                    <el-input v-model="formData.position" clearable placeholder="请输入部位" />
                </el-form-item>
                <el-form-item label="施工单位" prop="company_id">
                    <el-input v-model="formData.company_id" clearable placeholder="请输入施工单位" />
                </el-form-item>
                <el-form-item label="旁站开始时间" prop="start_time">
                    <el-input v-model="formData.start_time" clearable placeholder="请输入旁站开始时间" />
                </el-form-item>
                <el-form-item label="旁站结束时间" prop="end_time">
                    <el-input v-model="formData.end_time" clearable placeholder="请输入旁站结束时间" />
                </el-form-item>
                <el-form-item label="施工人数" prop="workers">
                    <el-input v-model="formData.workers" clearable placeholder="请输入施工人数" />
                </el-form-item>
                <el-form-item label="管理人数" prop="managers">
                    <el-input v-model="formData.managers" clearable placeholder="请输入管理人数" />
                </el-form-item>
                <el-form-item label="现场施工情况" prop="situation">
                    <el-input v-model="formData.situation" clearable placeholder="请输入现场施工情况" />
                </el-form-item>
                <el-form-item label="旁站结果 0-完成 1-交班" prop="side_station_result">
                    <el-input v-model="formData.side_station_result" clearable placeholder="请输入旁站结果 0-完成 1-交班" />
                </el-form-item>
                <el-form-item label="旁站人员" prop="side_station_user">
                    <el-input v-model="formData.side_station_user" clearable placeholder="请输入旁站人员" />
                </el-form-item>
                <el-form-item label="检查项选择" prop="check_item_detail_ids">
                    <el-input v-model="formData.check_item_detail_ids" clearable placeholder="请输入检查项选择" />
                </el-form-item>
                <el-form-item label="附件" prop="annex">
                    <el-input v-model="formData.annex" clearable placeholder="请输入附件" />
                </el-form-item>
                <el-form-item label="创建人" prop="create_user">
                    <el-input v-model="formData.create_user" clearable placeholder="请输入创建人" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionSideStationEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionSideStationAdd, apiSupervisionSideStationEdit, apiSupervisionSideStationDetail } from '@/api/supervision_side_station'
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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工程监理--旁站登记' : '新增工程监理--旁站登记'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    side_station_code: '',
    side_station_type: '',
    check_item_id: '',
    position: '',
    company_id: '',
    start_time: '',
    end_time: '',
    workers: '',
    managers: '',
    situation: '',
    side_station_result: '',
    side_station_user: '',
    check_item_detail_ids: '',
    annex: '',
    create_user: '',
})


// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请选择项目',
        trigger: ['blur']
    }],
    side_station_code: [{
        required: true,
        message: '请输入旁站编号',
        trigger: ['blur']
    }],
    side_station_type: [{
        required: true,
        message: '请输入旁站类型',
        trigger: ['blur']
    }],
    check_item_id: [{
        required: true,
        message: '请输入单位工程',
        trigger: ['blur']
    }],
    position: [{
        required: true,
        message: '请输入部位',
        trigger: ['blur']
    }],
    company_id: [{
        required: true,
        message: '请输入施工单位',
        trigger: ['blur']
    }],
    start_time: [{
        required: true,
        message: '请输入旁站开始时间',
        trigger: ['blur']
    }],
    end_time: [{
        required: true,
        message: '请输入旁站结束时间',
        trigger: ['blur']
    }],
    workers: [{
        required: true,
        message: '请输入施工人数',
        trigger: ['blur']
    }],
    managers: [{
        required: true,
        message: '请输入管理人数',
        trigger: ['blur']
    }],
    situation: [{
        required: true,
        message: '请输入现场施工情况',
        trigger: ['blur']
    }],
    side_station_result: [{
        required: true,
        message: '请输入旁站结果 0-完成 1-交班',
        trigger: ['blur']
    }],
    side_station_user: [{
        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]
        }
    }


}

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


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