<template>
    <el-dialog
        v-model="dialogFormVisible"
        :title="titleButton"
        width="65%"
        :append-to-body="true"
        :before-close="close"
        :close-on-click-modal="false"
        draggable
    >
        <!-- 用户档案录入 新建1 -->
        <el-form
            :model="form"
            :validate-on-rule-change="false"
            :scroll-to-error="true"
            ref="ruleFormRef"
            label-width="auto"
            class="form-two"
        >
            <el-divider content-position="left">干扰源用户基本信息</el-divider>
            <el-form-item label="用户性质：" prop="userType">
                <el-select v-model="form.userType" placeholder="请选择用户性质" :disabled="openType == 'detail'">
                    <el-option
                        v-for="(item, index) in userTypeList"
                        :label="item.label"
                        :value="item.value"
                        :key="index"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="所属地市：" prop="city">
                <el-select v-model="form.city" clearable placeholder="请选择所属地市" :disabled="openType == 'detail'">
                    <el-option
                        v-for="item in areaOptionList"
                        :key="item.id"
                        :label="item.name"
                        :value="item.name"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="归口管理部门：" prop="responsibleDepartment">
                <el-input
                    v-model="form.responsibleDepartment"
                    autocomplete="off"
                    placeholder="请输入归口管理部门"
                    :disabled="openType == 'detail'"
                />
            </el-form-item>
           <el-form-item label="工程名称：" prop="projectName">
                <el-input
                    v-model="form.projectName"
                    autocomplete="off"
                    placeholder="请输入工程名称"
                    :disabled="openType == 'detail'"
                />
            </el-form-item>
           <el-form-item label="用户状态：" prop="userStatus">
                <el-select v-model="form.userStatus" placeholder="请选择用户状态" :disabled="openType == 'detail'">
                    <el-option
                        v-for="(item, index) in userStateList"
                        :label="item.label"
                        :value="item.value"
                        :disabled="item.label != '可研'"
                        :key="index"
                    />
                </el-select>
            </el-form-item>
           <el-form-item label="变电站：" prop="substation">
                <el-input
                    v-model="form.substation"
                    autocomplete="off"
                    placeholder="请输入变电站"
                    :disabled="openType == 'detail'"
                />
            </el-form-item>
           <el-form-item v-if="form.userType == '0' || form.userType == '1'" label="电压等级：" prop="voltageLevel">
                <el-select
                    v-model="form.voltageLevel"
                    placeholder="请选择电压等级"
                    node-key="id"
                    :disabled="openType == 'detail'"
                >
                    <el-option
                        v-for="(item, index) in voltageLevelList"
                        :label="item.name"
                        :value="item.id"
                        :key="index"
                    />
                </el-select>
            </el-form-item>
           <el-form-item
                v-if="form.userType == '0' || form.userType == '1'"
                label="预测评估单位："
                prop="evaluationDept"
            >
                <el-select
                    v-model="form.evaluationDept"
                    placeholder="请选择预测评估单位"
                    :disabled="openType == 'detail'"
                >
                    <el-option
                        v-for="(item, index) in evaluationDeptList"
                        :label="item.name"
                        :value="item.name"
                        :key="index"
                    />
                </el-select>
            </el-form-item>
            <el-form-item v-if="form.userReportSubstationPO" label="是否需要治理：" prop="needGovernance">
                <el-radio-group v-model="form.userReportSubstationPO.needGovernance" :disabled="openType == 'detail'">
                    <el-radio :value="1">是</el-radio>
                    <el-radio :value="0">否</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item
                v-if="form.userReportSubstationPO"
                label="是否开展背景测试："
                prop="backgroundTestPerformed"
            >
                <el-radio-group
                    v-model="form.userReportSubstationPO.backgroundTestPerformed"
                    :disabled="openType == 'detail'"
                >
                    <el-radio :value="1">是</el-radio>
                    <el-radio :value="0">否</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-divider content-position="left">
                {{ bussType == 0 ? '入网设计方案审查报告' : '治理工程验收报告' }}
            </el-divider>
            <el-form-item label="填报人：" prop="reporter">
                <el-input v-model="form.reporter" autocomplete="off" :disabled="true" place-holder="请输入填报人" />
            </el-form-item>
            <el-form-item label="填报日期：" prop="reportDate">
                <el-date-picker
                    :disabled="true"
                    style="width: 100%"
                    v-model="form.reportDate"
                    type="date"
                    placeholder="请选择填报日期"
                />
            </el-form-item>
            <el-form-item label="填报部门：" prop="orgId">
                <el-input v-model="form.orgId" :disabled="true" autocomplete="off" />
            </el-form-item>
           <el-form-item
                class="uploadFile"
                :label="bussType == 0 ? '入网设计方案审查报告：' : '治理工程验收报告：'"
                prop="goToNetReport"
            >
                <el-upload
                    v-model:file-list="form.goToNetReport"
                    ref="uploadRef"
                    action=""
                    accept=".doc,.docx,.xlsx,.xls,.pdf"
                    :limit="1"
                    :on-exceed="handleExceed"
                    :on-change="choose"
                    :auto-upload="false"
                >
                    <template #trigger>
                        <el-button type="primary">上传文件</el-button>
                    </template>
                </el-upload>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="close()">取消</el-button>
                <el-button type="primary" @click="confirmForm()">确定</el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { ref, reactive, defineExpose, defineProps, defineEmits } from 'vue'
import type {  UploadProps, UploadRawFile } from 'element-plus'
import { genFileId, ElMessage } from 'element-plus'
import { useDictData } from '@/stores/dictData'
import { useAdminInfo } from '@/stores/adminInfo'
import { uploadFile } from '@/api/system-boot/file'
import { submitGoNet } from '@/api/supervision-boot/interfere/index'
import { getUserReportById } from '@/api/supervision-boot/userReport/form'

const props = defineProps({
    openType: {
        type: String,
        default: 'create'
    },
    id: {
        type: String
    },
    bussType:{
        type: Number
    },
    titleButton:{
        type: String
    }
})
const emits = defineEmits(['onSubmit'])
const dictData = useDictData()
const dialogFormVisible = ref(false)

const form: any = ref({})
const ruleFormRef: any = ref(null)
//字典获取所属地市
const areaOptionList = dictData.getBasicData('jibei_area')
//字典电压等级
const voltageLevelList = dictData.getBasicData('Dev_Voltage_Stand')
//字典预测评估单位
const evaluationDeptList = dictData.getBasicData('evaluation_dept')

//用户性质数组
const userTypeList = reactive([
    {
        label: '新建电网工程',
        value: 0
    },
    {
        label: '扩建电网工程',
        value: 1
    },
    {
        label: '新建非线性负荷用户',
        value: 2
    },
    {
        label: '扩建非线性负荷用户',
        value: 3
    },
    {
        label: '新建新能源发电站',
        value: 4
    },
    {
        label: '扩建新能源发电站',
        value: 5
    },
    {
        label: '敏感及重要用户',
        value: 6
    }
])
//用户状态数组
const userStateList = reactive([
    {
        label: '可研',
        value: 0
    },
    {
        label: '建设',
        value: 1
    },
    {
        label: '运行',
        value: 2
    },
    {
        label: '退运',
        value: 3
    }
])
//获取登陆用户姓名和部门
const adminInfo = useAdminInfo()
const resetForm = () => {
    form.value = {
        reporter: '', //填报人  
        reportDate: new Date(), //填报日期  
        orgId: '', //填报部门  
        goToNetReport:[],//报告 
        userType: 0, //用户性质 
        city: areaOptionList[0].name, //所属地市 
        responsibleDepartment: '', //归口管理部门 
        projectName: '', //工程名称 
        userStatus: userStateList[0].value, //用户状态 
        substation: '', //变电站 
        backgroundTestPerformed: 0, //是否开展背景测试 
        antiInterferenceTest: 0, //是否开展抗扰度测试 
        voltageLevel: voltageLevelList[0].id, //电压等级 
        evaluationDept: evaluationDeptList[0].name, //预测评估单位 
   }
    form.value.reporter = adminInfo.$state.name
    form.value.orgId = adminInfo.$state.deptName
}
//初始化数据
resetForm()

const detailLoading = ref(false) // 表单的加载中
const getInfo = async () => {
    detailLoading.value = true
    try {
        await getUserReportById(props.id).then(res => {
            form.value = res.data
            form.value.reporter = adminInfo.$state.name
            form.value.orgId = adminInfo.$state.deptName
            form.value.reportDate = new Date()
        })
    } finally {
        detailLoading.value = false
    }
}




const open = () => {
    dialogFormVisible.value = true
    resetForm()
    getInfo()

}
const close = () => {
    //重置表单内容
    resetForm()
    dialogFormVisible.value = false

}



// 上传报告
const uploadRef = ref()
const handleExceed: UploadProps['onExceed'] = files => {
    uploadRef.value!.clearFiles()
    const file = files[0] as UploadRawFile
    file.uid = genFileId()
    uploadRef.value!.handleStart(file)
}

// 治理工程验收报告数组
const goToNetReport = ref('')

const choose = (e: any) => {
    uploadFile(e.raw, '/supervision/').then(res => {
        //治理工程验收报告
        goToNetReport.value = res.data.name
    })
}


//提交
const confirmForm = () => {
    if(goToNetReport.value ==null ||goToNetReport.value ==''){
        return ElMessage({
            message: '请上传报告',
            type: 'warning'
        })
    }
   let data = {
        type: props.bussType,
        userReportId: props.id,
        reportUrl: goToNetReport.value
    }
    submitGoNet(data).then((res:any) => {
        if(res.code==='A0000'){
            ElMessage({
                message: '新建流程成功',
                type: 'success'
            })
            emits("onSubmit")
            close()
       }
    })
}
defineExpose({ open })
</script>
