<!---技术监督计划-->
<template>
    <div class="default-main">
        <TableHeader area datePicker ref="TableHeaderRef" showCustomColumn>
            <template #select>
                <el-form-item label="流程状态">
                    <el-select v-model="tableStore.table.params.status" clearable placeholder="请选择流程状态" filterable>
                        <el-option
                            v-for="item in statusSelect"
                            :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 { ElMessage } from 'element-plus'
import { ElMessageBox } from 'element-plus/es'
const dictData = useDictData()
const statusSelect = dictData.statusSelect()
import addForm from './components/addForm.vue'
import effectProblem from './components/effectProblem/index.vue'
import { planDetailsAudit, sendAlarm } from '@/api/supervision-boot/plan/index'
import { useAdminInfo } from '@/stores/adminInfo'
import { cancelPlanFormData } from '@/api/supervision-boot/plan/index'
//获取登陆用户姓名和部门
const VITE_FLAG = import.meta.env.VITE_NAME == 'jibei'
const adminInfo = useAdminInfo()
defineOptions({
    name: 'plan'
})
const flag = ref(false)
// const layout = mainHeight(120) as any

const { push, options, currentRoute } = 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: '序号',
            width: 80,
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        {
            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' ? '在运站' : '新（改、扩）建站'
            }
        },
        {
            field: 'createBy',
            title: '填报人',
            minWidth: 80,
            formatter: (row: any) => {
                return dictData.state.userList.filter(item => item.id == row.cellValue)[0]?.name
            }
        },
        {
             title: '操作',fixed: 'right',
            minWidth: 300,
          
            render: 'buttons',
            buttons: [
                {
                    name: 'productSetting',
                    title: '流程详情',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        flag.value = true
                        handleDetails(row.processInstanceId, row.historyInstanceId)
                    },
                    disabled: row => {
                        return !row.processInstanceId
                    }
                },
                {
                    name: 'productSetting',
                    title: '实施问题',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    disabled: row => {
                        return row.createBy != adminInfo.$state.id || row.status != 3
                    },
                    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: 'edit',
                    title: '重新发起',
                    type: 'warning',
                    icon: 'el-icon-Open',
                    render: 'basicButton',
                    disabled: row => {
                        return row.createBy != adminInfo.$state.id || !(row.status == 3 || row.status == 4)
                    },
                    click: row => {
                        addForms.value.open({
                            title: '重新发起',
                            row: row
                        })
                    }
                },
                {
                    name: 'cancel',
                    title: '取消',
                    type: 'danger',
                    icon: 'el-icon-Open',
                    render: 'basicButton',
                    disabled: row => {
                        // return row.createBy != adminInfo.$state.id || row.status != 1 || !row.processInstanceId
                        return row.createBy != adminInfo.$state.id || row.status != 1
                    },
                    click: row => {
                        cancelLeave(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' + (VITE_FLAG ? '4' : ''),
                            query: {
                                type: 4
                            }
                        })
                    }
                }
            ]
        }
    ],
    beforeSearchFun: () => {
        tableStore.table.params.orgNo = tableStore.table.params.deptIndex
        // tableStore.table.params.relationUserName = tableStore.table.params.userName
    }
})
tableStore.table.params.status = ''
// tableStore.table.params.loadType = ''
// tableStore.table.params.userName = ''
// tableStore.table.params.fileUploadflag = ''
provide('tableStore', tableStore)
const addForms = ref()
const addFormModel = () => {
    setTimeout(() => {
        addForms.value.open({
            title: '技术监督计划'
        })
    }, 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, historyInstanceId: any) => {
    push({
        name: 'BpmProcessInstanceDetail',
        state: {
            id: id,
            historyInstanceId
        }
    })
}
/** 提交审核按钮 */
const handleAudit = (row: any) => {
    planDetailsAudit({ id: row.planId }).then(res => {
        ElMessage.success('提交审核成功！')
        tableStore.index()
    })
}
//发起告警单
const handleAlarmForm = (row: any) => {
    sendAlarm({ id: row.planId }).then(res => {
        ElMessage.success('发起告警单成功！')
        tableStore.index()
    })
}
/**取消流程操作*/
const cancelLeave = async (row: any) => {
    // 二次确认
    const { value } = await ElMessageBox.prompt('请输入取消原因', '取消流程', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: 'textarea',
        inputPattern: /^[\s\S]*.*\S[\s\S]*$/, // 判断非空，且非空格
        inputErrorMessage: '取消原因不能为空'
    })
    // 发起取消
    let data = {
        id: row.planId,
        processInstanceId: row.processInstanceId,
        reason: value
    }
    await cancelPlanFormData(data)
    ElMessage.success('取消成功')
    // 加载数据
    tableStore.index()
}
// 实施问题按钮
const effectProblemList = ref()
const effectProblemForm: any = ref({})
const handleEffectProblem = (row: any) => {
    // effectProblemForm.value = row
    // effectProblemList.value.open()
    push({
        name: 'PlanEffectProblem',
        query: {
            id: row.planId
        }
    })
}
watch(
    () => currentRoute.value.path,
    () => {
        if (flag.value && options.history.state.forward?.split('/')[1] == 'bpm') {
            tableStore.index()
            flag.value = false
        }
    },
    {
        deep: true
    }
)
</script>
