<!---技术监督计划-->
<template>
    <div class="default-main">
        <TableHeader area datePicker ref="TableHeaderRef">
            <!-- <template #select>
            <el-form-item label="工程名称">
                <el-input v-model="tableStore.table.params.searchValue" clearable></el-input>
            </el-form-item>
            <el-form-item label="所属地市">
                <el-select v-model="tableStore.table.params.loadType" clearable placeholder="请选择所属地市">
                    <el-option
                        v-for="item in areaOptionList"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>
        </template> -->
            <template #operation>
                <el-button icon="el-icon-Plus" type="primary" @click="addFormModel">新增</el-button>
                <!-- <el-button icon="el-icon-Download" @click="exportEvent" type="primary">导出</el-button> -->
            </template>
        </TableHeader>
        <Table ref="tableRef" />
        <!-- 新增弹框 -->
        <addForm ref="addForms" @onSubmit="tableStore.index()"></addForm>
        <!-- 实施问题弹框 -->
        <effectProblem
            ref="effectProblemList"
            @onSubmit="tableStore.index()"
            :effectProblemForm="effectProblemForm"
        ></effectProblem>
    </div>
</template>
<script setup lang="ts">
import { ref, onMounted, provide, nextTick } from 'vue'
import { mainHeight } from '@/utils/layout'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import { useRouter } from 'vue-router'
import { getLoadTypeUserList } from '@/api/process-boot/interference'
import { useDictData } from '@/stores/dictData'
import addForm from './components/addForm.vue'
import effectProblem from './components/effectProblem/index.vue'
import { planDetailsAudit ,sendAlarm} from '@/api/supervision-boot/plan/index'
import { ElMessage } from 'element-plus'
defineOptions({
    name: '/admin/plan'
})
// const layout = mainHeight(120) as any
const dictData = useDictData()
const { push } = useRouter()
const router = useRouter() // 路由对象
const TableHeaderRef = ref()
const tableRef = ref()
//字典获取监督类型
const supvTypeList = dictData.getBasicData('supv_type')
//字典获取监督阶段
const supvStageList = dictData.getBasicData('supv_stage')
//字典获取监督对象类型
const supvObjTypeList = dictData.getBasicData('supv_obj_type')

const tableStore = new TableStore({
    url: '/supervision-boot/superPlan/pagePlan',
    // publicHeight: 65,
    method: 'POST',
    column: [
        { title: '序号', type: 'seq', width: 80 },
        {
            field: 'province',
            title: '所属省份',
            minWidth: 170,
            formatter: (row: any) => {
                return row.cellValue ? row.cellValue : '/'
            }
        },
        {
            field: 'city',
            title: '所属地市',
            minWidth: 170,
            formatter: (row: any) => {
                return row.cellValue ? row.cellValue : '/'
            }
        },
        {
            field: 'county',
            title: '所属县区',
            minWidth: 170,
            formatter: (row: any) => {
                return row.cellValue ? row.cellValue : '/'
            }
        },
        {
            field: 'supvOrgName',
            title: '监督单位',
            minWidth: 170
        },
        { field: 'workPlanName', title: '计划名称', minWidth: 170 },
        {
            field: 'planSupvDate',
            title: '计划监督时间',
            minWidth: 170,
            formatter: (row: any) => {
                return row.cellValue.replace('T', ' ')
            }
        },
        {
            field: 'status',
            title: '审核状态',
            minWidth: 100,
            render: 'tag',
            custom: {
                1: 'primary',
                2: 'success',
                3: 'danger',
                4: 'warning'
            },
            replaceValue: {
                1: '审批中',
                2: '审批通过',
                3: '审批不通过',
                4: '已取消'
            }
        },
        {
            field: 'supvType',
            title: '监督类型',
            minWidth: 170,
            formatter: (row: any) => {
                return supvTypeList.filter(item => item.id === row.cellValue)[0]?.name
            }
        },
        {
            field: 'supvStage',
            title: '监督阶段',
            minWidth: 170,
            formatter: (row: any) => {
                return supvStageList.filter(item => item.id === row.cellValue)[0]?.name
            }
        },
        {
            field: 'supvObjType',
            title: '监督对象类型',
            minWidth: 170,
            formatter: (row: any) => {
                return supvObjTypeList.filter(item => item.id === row.cellValue)[0]?.name
            }
        },
        {
            field: 'objType',
            title: '监督对象属性',
            minWidth: 170,
            formatter: (row: any) => {
                return row.cellValue == '1' ? '在运站' : '新（改、扩）建站'
            }
        },
        {
            title: '操作',
            minWidth: 300,
            fixed: 'right',
            render: 'buttons',
            buttons: [
                {
                    name: 'productSetting',
                    title: '流程详情',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        handleDetails(row.processInstanceId)
                    },
                    disabled: row => {
                        return !row.processInstanceId
                    }
                },
                {
                    name: 'productSetting',
                    title: '实施问题',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        handleEffectProblem(row)
                    }
                },
                {
                    name: 'productSetting',
                    title: '提交审核',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    disabled: row => {
                        return row.processInstanceId
                    },
                    click: row => {
                        handleAudit(row)
                    }
                },
                {
                    name: 'productSetting',
                    title: '发起告警单',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    disabled: row => {
                        return row.isUploadHead == 1
                    },
                    click: row => {
                        handleAlarmForm(row)
                    }
                },
                {
                    name: 'edit',
                    title: '查看告警单',
                    type: 'primary',
                    icon: 'el-icon-Open',
                    render: 'basicButton',
                    disabled: row => {
                        return row.isUploadHead == 0
                    },
                    click: row => {
                        router.push({
                            name: 'supervision/supervision/manage'
                        })
                    }
                }
            ]
        }
    ],

    beforeSearchFun: () => {
        tableStore.table.params.orgNo = tableStore.table.params.deptIndex
        // tableStore.table.params.relationUserName = tableStore.table.params.userName
    }
})

// tableStore.table.params.loadType = ''
// tableStore.table.params.userName = ''
// tableStore.table.params.fileUploadflag = ''

provide('tableStore', tableStore)

const addForms = ref()
const addFormModel = () => {
    setTimeout(() => {
        addForms.value.open()
    }, 0)
}

const exportEvent = () => {
    let form = JSON.parse(JSON.stringify(tableStore.table.params))
    form.pageNum = 1
    form.pageSize = tableStore.table.total
    getLoadTypeUserList(form).then(res => {
        tableRef.value.getRef().exportData({
            filename: '未建档非线性用户', // 文件名字
            sheetName: 'Sheet1',
            type: 'xlsx', //导出文件类型 xlsx 和 csv
            useStyle: true,
            data: res.data.records, // 数据源 // 过滤那个字段导出
            columnFilterMethod: function (column: any) {
                return !(column.$columnIndex === 0)
            }
        })
    })
}
onMounted(() => {
    tableStore.index()
})
//
const handleDetails = (id: any) => {
    push({
        name: 'BpmProcessInstanceDetail',
        query: {
            id: id
        }
    })
}
/** 提交审核按钮 */
const handleAudit = (row: any) => {
    planDetailsAudit({ id: row.planId }).then(res => {
        tableStore.index()
    })
}
//发起告警单
const handleAlarmForm=(row:any)=>{
    sendAlarm({ id: row.planId }).then(res => {
        ElMessage.success('发起告警单成功！')
        tableStore.index()
    })
}

// 实施问题按钮
const effectProblemList = ref()
const effectProblemForm: any = ref({})
const handleEffectProblem = (row: any) => {
    effectProblemForm.value = row
    effectProblemList.value.open()
}

/**获取用户性质*/
const getUserTypeName = (userType: any) => {
    if (userType === 0) {
        return '新建电网工程'
    }
    if (userType === 1) {
        return '扩建电网工程'
    }
    if (userType === 2) {
        return '新建非线性负荷用户'
    }
    if (userType === 3) {
        return '扩建非线性负荷用户'
    }
    if (userType === 4) {
        return '新建新能源发电站'
    }
    if (userType === 5) {
        return '扩建新能源发电站'
    }
    if (userType === 6) {
        return '敏感及重要用户'
    }
    return '新建电网工程'
}
</script>

<style scoped lang="scss">
// :deep(.el-upload-list__item) {
//     width: 400px;
// }

// ::v-deep .el-input__wrapper {
//     // width: 200px !important;
// }
// .bars_w {
//     width: 100%;
//     height: 500px;
// }
// :deep(.el-tabs__content) {
//     height: v-bind('layout.height');
//     overflow-y: auto;
// }
</style>
