<template>
    <div>
        <TableHeader area datePicker ref="TableHeaderRef">
            <template #operation>
                <el-button icon="el-icon-Plus" type="primary" @click="add">新增</el-button>
            </template>
        </TableHeader>
        <Table ref="tableRef" />
    </div>
    <!-- 新增编辑 -->
    <el-dialog draggable :title="title" v-model="userAdd" width="500px">
        <Equipment ref="EquipmentRef" @onSubmit="onSubmit" />
        <div style="display: flex; justify-content: center; margin-top: 30px">
            <el-button type="primary" class="ml20" @click="EquipmentRef.config()">保存</el-button>

            <el-button class="ml20" @click="userAdd = false">取消</el-button>
        </div>
    </el-dialog>
</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 Equipment from './equipment.vue'
import { ElMessage } from 'element-plus'
import { deleteIssues } from '@/api/process-boot/electricitymanagement'
import { createCheckflow, getFileUrl } from '@/api/process-boot/retire'

import { useDictData } from '@/stores/dictData'
const userAdd = ref(false)
const TableHeaderRef = ref()
const EquipmentRef = ref()
const title: any = ref('')

defineOptions({
    name: '/Processsupervision/retire'
})
const tableStore: any = new TableStore({
    url: '/process-boot/rFlowProcess/getFlowProcess',
    method: 'POST',
    publicHeight: 65,
    column: [
        // { width: '60', type: 'checkbox' },
        {
            field: 'index',
            title: '序号',
            width: '60',
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        { field: 'orgName', title: '所属单位' },
        {
            field: 'applyForm',
            title: '设备编号',
            formatter: (row: any) => {
                return row.cellValue.assetNumber
            }
        },
        {
            field: 'applyForm',
            title: '资产编号',
            formatter: (row: any) => {
                return row.cellValue.devNumber
            }
        },
        { field: 'updateTime', title: '创建时间' },
        { field: 'checkerName', title: '审核人' },
        {
            field: 'processStatus',
            title: '流程状态',
            width: 150,
            render: 'tag',
            custom: {
                0: '',
                1: '',
                2: 'warning',
                3: 'danger',
                4: 'success',
                5: 'success'
            },
            replaceValue: {
                0: '',
                1: '新建',
                2: '待审核',
                3: '驳回',
                4: '已审核',
                5: '归档'
            }
        },

        {
            title: '操作',
            width: '200',
            render: 'buttons',
            buttons: [
                {
                    name: 'edit',
                    title: '编辑',
                    type: 'primary',
                    disabled: row => {
                        return !(row.processStatus == 1 || row.processStatus == 3)
                    },
                    icon: 'el-icon-Plus',
                    render: 'basicButton',
                    click: async row => {
                        userAdd.value = true
                        title.value = '编辑设备退役申请单'
                        nextTick(() => {
                            EquipmentRef.value.open('编辑设备退役申请单', row)
                        })
                    }
                },

                {
                    name: 'edit',
                    title: '提交审核',
                    disabled: row => {
                        return !(row.processStatus == 1 || row.processStatus == 3)
                    },
                    type: 'primary',
                    icon: 'el-icon-Plus',
                    render: 'confirmButton',
                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        title: '是否提交审核？'
                    },
                    click: row => {
                        createCheckflow({ id: row.id }).then(() => {
                            ElMessage.success('提交成功！')
                            tableStore.index()
                        })
                    }
                },
                {
                    name: 'edit',
                    title: '下载文件',
                    type: 'primary',
                    icon: 'el-icon-Plus',
                    render: 'basicButton',
                    click: async row => {
                        getFileUrl({ filePath: row.filePath }).then(async res => {
                            let response = await fetch(res.data)
                            let blob = await response.blob()
                            let a = document.createElement('a')
                            a.href = window.URL.createObjectURL(blob)
                            a.download = row.fileName
                            a.click()
                            a.remove()
                        })
                    }
                },

                {
                    name: 'del',
                    text: '删除',
                    type: 'danger',
                    icon: 'el-icon-Delete',
                    render: 'confirmButton',
                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        confirmButtonType: 'danger',
                        title: '确定删除？'
                    },

                    click: row => {
                        deleteIssues(row.powerQualityProblemNo).then(() => {
                            ElMessage.success('删除成功')
                            tableStore.index()
                        })
                    }
                }
            ]
        }
    ],
    beforeSearchFun: () => {
        tableStore.table.params.orgNo = tableStore.table.params.deptIndex
        tableStore.table.params.pageType = '1'
        tableStore.table.params.type = 5
        tableStore.table.params.applyType = 4
    }
})

tableStore.table.params.problemName = ''
tableStore.table.params.problemSources = ''
tableStore.table.params.reportProcess = ''
tableStore.table.params.reportProcessStatus = ''

provide('tableStore', tableStore)
onMounted(() => {
    TableHeaderRef.value.setDatePicker([
        { label: '年', value: 1 },
        { label: '季', value: 2 },
        { label: '月', value: 3 }
    ])

    tableStore.index()
})
// 新增
const add = () => {
    userAdd.value = true
    title.value = '新增设备退役申请单'
    nextTick(() => {
        EquipmentRef.value.open('新增设备退役申请单')
    })
}
// 关闭
const onSubmit = () => {
    tableStore.index()
    userAdd.value = false
}
</script>
