<!--待办事项列表-->
<template>
    <div>
        <TableHeader showExport :showReset="false" showCustomColumn>
            <template v-slot:select>
                <el-form-item label="任务名称">
                    <el-input
                        v-model="tableStore.table.params.searchValue"
                        placeholder="请输入任务名称"
                        clearable
                        maxlength="32"
                        show-word-limit
                    ></el-input>
                </el-form-item>
            </template>
        </TableHeader>
        <!--表格-->
        <Table ref="tableRef"></Table>
    </div>
</template>

<script setup lang="ts">
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import { onMounted, provide, ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { getById } from '@/api/supervision-boot/leaflet'
import { getRunningDeviceById } from '@/api/supervision-boot/device/quitRunningDev'
const props = defineProps({
    height: [String, Number]
})
const VITE_FLAG = import.meta.env.VITE_NAME == 'jibei'
const router = useRouter() // 路由对象
defineOptions({
    name: 'businessUser'
})

const { push, options, currentRoute } = useRouter()
const flag = ref(false)

const tableStore = new TableStore({
    url: '/bpm-boot/bpm/task/allTodoList',
    method: 'POST',
    publicHeight: 65,
    isWebPaging: true,
    filename: '待办任务',
    column: [
        {
            field: 'index',
            title: '序号',
            width: '80',
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        { title: '任务名称', field: 'taskName', minWidth: 200 },
        { title: '任务内容', field: 'taskContent', minWidth: 300 },
        { title: '任务来源', field: 'startUser', minWidth: 120 },
        { title: '发起时间', field: 'taskCreateTime', minWidth: 180 },
        {
            field: 'source',
            title: '流程状态',
            render: 'tag',
            minWidth: 130,
            custom: {
                1: 'primary',
                2: 'danger',
                3: 'primary'
            },
            replaceValue: {
                1: '待审批',
                2: '审批不通过',
                3: '待处理'
            }
        },
        {
             title: '操作',fixed: 'right',
            align: 'center',
            minWidth: 100,
            render: 'buttons',
            buttons: [
                {
                    name: 'productSetting',
                    title: '办理',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    disabled: row => {
                        return row.source == 2 || row.source == 3
                    },
                    click: row => {
                        flag.value = true
                        handleAudit(row.processInstanceId, row.historyInstanceId)
                    }
                },
                {
                    name: 'productSetting',
                    title: '流程详情',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    disabled: row => {
                        return row.source == 1 || row.source == 3
                    },
                    click: row => {
                        flag.value = true
                        handleAudit(row.processInstanceId, row.historyInstanceId)
                    }
                },
                {
                    name: 'productSetting',
                    title: '重新发起',
                    type: 'warning',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    disabled: row => {
                        return row.source == 1 || row.source == 3
                    },
                    click: async row => {
                        // 页面内tab切换
                        let type = 1
                        let key = row.businessKey
                        // 特殊处理预告警单
                        if (key == 'warn_leaflet') {
                            await getById(row.id).then(res => {
                                if (res.code == 'A0000') {
                                    if (res.data.leafletType == 1) {
                                        type = 0
                                    } else {
                                        type = 1
                                    }
                                }
                            })
                        }

                        if (key == 'quit_running_device') {
                            await getRunningDeviceById(row.id).then(res => {
                                if (res.code == 'A0000') {
                                    if (res.data.deviceType == 1) {
                                        type = 0
                                    } else {
                                        type = 1
                                    }
                                }
                            })
                        }

                        // 特殊处理终端监测点状态切换
                        if (
                            // 干扰源建档
                            key == 'build_user_info' ||
                            // 终端入网检测
                            key == 'device_info_add' ||
                            // 技术监督计划
                            key == 'sup_plan_add' ||
                            // 台账管理 干扰源用户台账
                            key == 'build_user_info_archivist'
                        ) {
                            type = 1
                        } else if (
                            // 干扰源用户常态化管理
                            key == 'user_report_update' ||
                            // 入网方案申请
                            key == 'user_go_net' ||
                            // 治理工程审核
                            key == 'user_treat_check' ||
                            // 监测点台账录入
                            key == 'line_info_add' ||
                            // 技术监督计划测试
                            key == 'survey_test'
                        ) {
                            type = 2
                        } else if (
                            // 监测点联调
                            key == 'temp_line_debug'
                        ) {
                            type = 3
                        } else if (
                            // 终端周期检测
                            key == 'timing_check_device'
                        ) {
                            type = 4
                        }
                        // 业务记录id
                        let id = row.id
                        router.push({
                            name: row.routePath,
                            query: {
                                type,
                                id,
                                key,
                                t: Date.now()
                            }
                        })
                    }
                },
                {
                    name: 'productSetting',
                    title: '办理',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    disabled: row => {
                        return row.source == 1 || row.source == 2
                    },
                    click: row => {
                        // 页面内tab切换
                        let type = row.tabValue
                        let key = '3'
                        let name = row.routePath
                        if (VITE_FLAG) {
                            let key = row.routePath?.split('?')[0].slice(-1)
                            const isNumber = /^\d$/.test(key)
                            if (!isNumber) {
                                name = row.routePath + type
                            }
                        }

                        // 业务记录id
                        let id = row.id
                        router.push({
                            name: name,
                            query: {
                                type,
                                id,
                                key,
                                t: Date.now()
                            }
                        })
                    }
                }
            ]
        }
    ],
    beforeSearchFun: () => {
        for (let key in tableStore.table.params) {
            if (tableStore.table.params[key] === '' && key != 'searchValue') {
                delete tableStore.table.params[key]
            }
        }
    },
    loadCallback: () => {
        if (tableStore.table.params.searchValue) {
            const searchProps = ['taskName', 'taskContent', 'startUser', 'taskCreateTime']
            const rest = tableStore.table.data.filter(item =>
                searchProps.some(
                    key => String(item[key]).toLowerCase().indexOf(tableStore.table.params.searchValue) > -1
                )
            )
            tableStore.table.data = rest.map(row => {
                const item = Object.assign({}, row)

                return item
            })
        } else {
            tableStore.table.data = tableStore.table.data
        }
    },
    exportProcessingData: () => {
        tableStore.table.allData = tableStore.table.allData.filter(item => {
            item.source = item.source == 1 ? '待审批' : item.source == 2 ? '审批不通过' : '待处理'

            return item
        })
    }
})
tableStore.table.params.searchValue = ''

provide('tableStore', tableStore)
onMounted(() => {
    // 加载数据
    tableStore.index()
    setTimeout(() => {
        if (props.height) {
            tableStore.table.height = props.height
        }
    }, 0)
})
/** 处理审批按钮 */
const handleAudit = (instanceId: string, historyInstanceId: string) => {
    push({
        name: 'BpmProcessInstanceDetail',
        state: {
            id: instanceId,
            todo: 'todo',
            historyInstanceId
        }
    })
}

watch(
    () => currentRoute.value.path,
    () => {
        if (flag.value && options.history.state.forward?.split('/')[1] == 'bpm') {
            tableStore.index()
            flag.value = false
        }
    },
    {
        deep: true
    }
)
</script>
