<template>
    <el-dialog
        v-model="dialogFormVisible"
        :title="title"
        width="30%"
        :append-to-body="true"
        :before-close="close"
        :close-on-click-modal="false"
        :lazy="true"
        draggable
    >
        <!--监测点信息录入 基础信息+监测点信息  监测点信息要有用户名称、用户状态 -->
        <el-form
            :model="form"
            :validate-on-rule-change="false"
            :scroll-to-error="true"
            :rules="rules"
            ref="ruleFormRef"
            label-width="80px"
            label-position="right"
        >
            <el-form-item label="原因：" prop="reason">
                <el-input
                    v-model="form.reason"
                    autocomplete="off"
                    :autosize="{ minRows: 2, maxRows: 4 }"
                    type="textarea"
                    placeholder="请输入原因"
                />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="close()">取消</el-button>
                <!-- <el-button type="primary" @click="confirmForm(true)">保存</el-button> -->
                <el-button type="primary" @click="confirmForm(false)" :loading="loading" >提交审批</el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { ref, onMounted, reactive, defineExpose, defineProps, defineEmits, watch, onUnmounted } from 'vue'
import { useDictData } from '@/stores/dictData'
import { useAdminInfo } from '@/stores/adminInfo'
import { ElMessage } from 'element-plus'
import {
    addMointorPointTempLinedebug,
    updateMointorPointTempLinedebug
} from '@/api/supervision-boot/jointDebugList/index'
const emits = defineEmits(['onSubmit'])
const props = defineProps({
    debugId: {
        type: String,
        default: ''
    }
})
const dictData = useDictData()
const dialogFormVisible = ref(false)
const form: any = ref({})
const loading = ref(false)
const ruleFormRef: any = ref(null)
//定义切换form类型
const selectFormType = ref('')
selectFormType.value = '0'
const resetForm = () => {
    form.value = {
        reason: '', //填报人
        id: ''
    }
}
//初始化数据
resetForm()
//定义校验规则
const rules = ref({
    //基础信息
    reason: [
        {
            required: true,
            message: '请输入原因',
            trigger: 'blur'
        }
    ]
})
watch(
    () => form.value,
    (val, oldVal) => {},
    {
        deep: true,
        immediate: true
    }
)
const title = ref('')
const open = (text: any, row: any) => {
    dialogFormVisible.value = true
    title.value = text
    form.value.id = row.id
    form.value.reason = row.reason
}
const close = () => {
    //重置表单内容
    //取消表单校验状态
    ruleFormRef.value && ruleFormRef.value?.resetFields()
    dialogFormVisible.value = false
    emits('onSubmit')
    resetForm()
}
onMounted(() => {
    console.log()
})

//设置工程投产日期时间选择范围不能<今天
const disabledDate = time => {
    return time.getTime() < Date.now() - 8.64e7 // 8.64e7 毫秒数代表一天
}
//提交
const confirmForm = async (flag: boolean) => {
    loading.value = true
    if (flag) {
        // 提交监测点信息

        //提交监测点联调信息
        form.value.saveOrCheckflag = '1'
        await addMointorPointTempLinedebug(form.value).then(res => {
            ruleFormRef.value.resetFields()
            ElMessage.success('保存成功！')
            resetForm()
            close()
        })
    } else {
        // 提交监测点信息
        ruleFormRef.value.validate(async (valid) => {
            if (valid) {
                //提交监测点联调信息
                form.value.saveOrCheckflag = '2'
                if (!(title.value == '重新发起' || title.value == '编辑')) {
                    await addMointorPointTempLinedebug(form.value).then(res => {
                        ruleFormRef.value.resetFields()
                        ElMessage.success('申请联调成功！')
                        resetForm()
                        close()
                    })
                } else {
                    await updateMointorPointTempLinedebug(form.value).then(res => {
                        ruleFormRef.value.resetFields()
                        ElMessage.success('重新发起成功！')
                        resetForm()
                        close()
                    })
                }
            } else {
                console.log('表单验证失败')
                return false
            }
        })
    }
    await setTimeout(() => {
        loading.value = false
    }, 0)
}
defineExpose({ open })
</script>
