<template>
    <TableHeader area datePicker nextFlag theCurrentTime ref="TableHeaderRef" showCustomColumn>
        <!--        <template #operation>-->
        <!--            <el-button icon="el-icon-Plus" type="primary" @click="add">新增</el-button>-->
        <!--            <el-button icon="el-icon-Delete" type="primary">删除</el-button>-->
        <!--        </template>-->
    </TableHeader>
    <Table ref="tableRef" />
    <!--弹框-->
    <feedback-popup ref="feedbackPopup" />
</template>
<script setup lang="ts">
import { ref, onMounted, provide, nextTick } from 'vue'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import FeedbackPopup from '@/views/pqs/supervise/technology/feedbackPopup.vue'
import { useRouter } from 'vue-router'
import { cancelFeedback } from '@/api/supervision-boot/leaflet'
import { useAdminInfo } from '@/stores/adminInfo'
import { useDictData } from '@/stores/dictData'
import { getFileNameAndFilePath } from '@/api/system-boot/file'
const dictData = useDictData()
//获取登陆用户姓名和部门
const adminInfo = useAdminInfo()
const { push } = useRouter()
const tableRef = ref()
const TableHeaderRef = ref()
const feedbackPopup = ref()
const tableStore = new TableStore({
    url: '/supervision-boot/warningLeaflet/allPageData',
    publicHeight: 65,
    method: 'POST',
    column: [
        {
            title: '序号',
          
            align: 'center',
            width: 80,
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },

        {
            field: 'problemType',
            title: '预告警单问题来源',
            minWidth: '150',
            render: 'tag',
            custom: {
                1: 'warning',
                2: 'warning',
                3: 'warning',
                4: 'warning'
            },
            replaceValue: {
                1: '技术监督计划',
                2: '在线监测超标问题',
                3: '用户投诉问题',
                4: '试运行监测点问题'
            }
        },
        { field: 'dutyOrgName', title: '负责单位', minWidth: '150' },
        {
            field: 'leafletType',
            title: '告警类型',
            render: 'tag',
            minWidth: '150',
            custom: {
                1: 'warning',
                2: 'danger'
            },
            replaceValue: {
                1: '预警单',
                2: '告警单'
            }
            // formatter: (row: any) => {
            //     return row.row.leafletType == 1 ? '预警单' : '告警单'
            // }
        },
        { field: 'reformAdvice', title: '整改意见', minWidth: '200' },
        { field: 'leafletName', title: '单据名称', minWidth: '200' },
        {
            field: 'status',
            title: '流程状态',
            minWidth: '150',
            render: 'tag',
            custom: {
                0: 'warning',
                1: 'primary',
                2: 'success',
                3: 'danger',
                4: 'warning',
                5: 'primary'
            },
            replaceValue: {
                0: '待提交审批',
                1: '审批中',
                2: '审批通过',
                3: '审批不通过',
                4: '已取消',
                5: '新增'
            }
        },

        { field: 'createTime', title: '创建时间', minWidth: '150' },
        {
            field: 'createBy',
            title: '填报人',
            minWidth: 80,
            formatter: (row: any) => {
                return dictData.state.userList.filter(item => item.id == row.cellValue)[0]?.name
            }
        },
        {
             title: '操作',fixed: 'right',
            minWidth: '220',
            align: 'center',
            render: 'buttons',
          
            buttons: [
                // {
                //     name: 'productSetting',
                //     title: '发送督办单',
                //     type: 'primary',
                //     icon: 'el-icon-EditPen',
                //     render: 'basicButton',
                //     click: row => {
                //         // handleAudit(row.processInstanceId)
                //         ElMessage.warning('待打通生成管理系统接口！')
                //     }
                // },
                {
                    name: 'productSetting',
                    title: '问题反馈',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    disabled: row => {
                        return row.dutyOrgId != adminInfo.$state.deptId || row.status !== 5
                    },
                    click: row => {
                        feedbackPopup.value.open(
                            '填报反馈单',
                            row.id,
                            row.status,
                            row.issueDetail,
                            row.problemPath,
                            row.supervisionReport,
                            row.reformAdvice
                        )
                    }
                },

                {
                    name: 'productSetting',
                    title: '流程详情',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    disabled: row => {
                        return !row.processInstanceId
                    },
                    click: row => {
                        handleAudit(row.processInstanceId, row.historyInstanceId)
                    }
                },
                {
                    name: 'edit',
                    title: '编辑',
                    type: 'primary',
                    icon: 'el-icon-Open',
                    render: 'basicButton',
                    showDisabled: row => {
                        return row.dutyOrgId != adminInfo.$state.deptId || !(row.status == 0)
                    },
                    disabled: row => {
                        return !(row.status == 0)
                    },
                    click: row => {
                        feedbackPopup.value.open(
                            '编辑',
                            row.id,
                            row.status,
                            row.issueDetail,
                            row.problemPath,
                            row.supervisionReport,
                            row.reformAdvice,
                            row.takeStep,
                            row.reportPath
                        )
                    }
                },
                {
                    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 => {
                        // deviceQuitPopup.value.open('重新发起', row)
                        feedbackPopup.value.open(
                            '重新发起告警单',
                            row.id,
                            row.status,
                            row.issueDetail,
                            row.problemPath,
                            row.supervisionReport,
                            row.reformAdvice,
                            row.takeStep,
                            row.reportPath,
                            row.reformAdvice
                        )
                    }
                },
                {
                    name: 'productSetting',
                    title: '报告下载',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    disabled: row => {
                        return row.problemType != 2
                    },
                    click: row => {
                        window.open(row.filePath)
                    }
                },
                {
                    name: 'cancel',
                    title: '取消',
                    type: 'danger',
                    icon: 'el-icon-Open',
                    render: 'basicButton',
                    disabled: row => {
                        return row.createBy != adminInfo.$state.id || row.status != 1
                    },
                    click: row => {
                        cancelLeave(row)
                    }
                }
            ]
        }
    ],
    beforeSearchFun: () => {
        tableStore.table.params.currentPage = tableStore.table.params.pageNum
    }
})

provide('tableStore', tableStore)

onMounted(() => {
    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.id,
        processInstanceId: row.processInstanceId,
        reason: value
    }
    await cancelFeedback(data)
    ElMessage.success('取消成功')
    // 加载数据
    tableStore.index()
}
/** 流程实例详情 */
const handleAudit = (instanceId: any, historyInstanceId: any) => {
    push({
        name: 'BpmProcessInstanceDetail',
        query: {
            id: instanceId,
            historyInstanceId
        }
    })
}
</script>

<style scoped lang="scss"></style>
