<template>
    <div class="default-main">
        <div class="custom-table-header">
            <div class="title">部门列表</div>
            <el-input
                v-model="tableStore.table.params.searchValue"
                style="width: 240px"
                placeholder="请输入部门名称"
                class="ml10"
                clearable
                @input="search"
                maxlength="32"
                show-word-limit
            />
        </div>
        <Table
            :row-config="{ isCurrent: true, isHover: true, keyField: 'id' }"
            :tree-config="{ children: 'children', reserve: true }"
            :scroll-y="{ enabled: false }"
        />
        <PopupForm ref="popupFormRef" @init="tableStore.index()"></PopupForm>
        <PopupPoint ref="popupPointRef"></PopupPoint>
    </div>
</template>
<script setup lang="ts">
import { ref, nextTick, inject, provide, watch, onMounted } from 'vue'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import PopupForm from './popupForm.vue'
import PopupPoint from './popupPoint.vue'
import { deleteDept, selectPid } from '@/api/user-boot/dept'
import { ElMessage, ElMessageBox } from 'element-plus'

defineOptions({
    name: 'auth/department'
})

const popupFormRef = ref()
const popupPointRef = ref()
let originData: any[]
const tableStore = new TableStore({
    publicHeight: 60,
    showPage: false,
    url: '/user-boot/dept/deptTree',
    method: 'POST',
    column: [
        {
            title: '名称',
            field: 'name',
            align: 'left',
            width: 350,
            treeNode: true
        },
        { title: '区域', field: 'areaName', align: 'center' },
        {
            title: '类型',
            field: 'type',
            align: 'center',
            render: 'renderFormatter',
            renderFormatter: data => {
                const typeArr = ['非自定义', 'web自定义', 'App自定义', 'Web测试']
                return typeArr[data.type] || '/'
            }
        },
        {
            title: '描述',
            field: 'remark',
            align: 'center',
            render: 'renderFormatter',
            renderFormatter: data => {
                return data.remark || '/'
            }
        },
        {
            title: '状态',
            field: 'state',
            align: 'center',
            render: 'renderFormatter',
            renderFormatter: data => {
                return data.state === 1 ? '正常' : '删除'
            }
        },
        {
            title: '操作',
            fixed: 'right',
            align: 'center',
            width: '180',
            render: 'buttons',
            buttons: [
                {
                    name: 'edit',
                    text: '新增',
                    type: 'primary',
                    icon: 'el-icon-Plus',
                    render: 'basicButton',
                    click: row => {
                        popupFormRef.value.open('新增部门', {
                            pid: row.id,
                            pArea: row.area
                        })
                    }
                },
                {
                    name: 'edit',
                    text: '编辑',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        popupFormRef.value.open('编辑部门', {
                            ...row
                        })
                    }
                },
                {
                    name: 'del',
                    text: '删除',
                    type: 'danger',
                    icon: 'el-icon-Delete',
                    render: 'basicButton',
                    click: (row: any) => {
                        if (row.children.length > 0) {
                            ElMessageBox.confirm('该部门有子部门，是否确认删除', '提示', {
                                confirmButtonText: '确认删除',
                                cancelButtonText: '取消',
                                type: 'warning'
                            }).then(() => {
                                deleteDept([row.id]).then(response => {
                                    ElMessage.success('删除成功')
                                    tableStore.index()
                                })
                            })
                        } else {
                            ElMessageBox.confirm('是否确认删除该部门', '提示', {
                                confirmButtonText: '确认删除',
                                cancelButtonText: '取消',
                                type: 'warning'
                            }).then(() => {
                                deleteDept([row.id]).then(response => {
                                    ElMessage.success('删除成功')
                                    tableStore.index()
                                })
                            })
                        }
                    }
                },
                {
                    name: 'edit',
                    text: '绑定监测点',
                    type: 'primary',
                    icon: 'el-icon-Plus',
                    render: 'basicButton',
                    disabled: (row: any) => {
                        return row.children.length > 0
                    },
                    click: row => {
                        popupPointRef.value.open(row)
                    }
                }
            ]
        }
    ],
    loadCallback: () => {
        originData = JSON.parse(JSON.stringify(tableStore.table.data))
        tableStore.table.data.forEach((item: any) => {
            item.stateName = item.state === 1 ? '正常' : '删除'
            item.remark = item.remark || '/'
        })
        nextTick(() => {
            tableStore.table.ref?.setAllTreeExpand(true)
        })
    }
})
provide('tableStore', tableStore)
onMounted(() => {
    tableStore.index()
})
const addMenu = () => {}
const search = () => {
    tableStore.table.data = filterData(JSON.parse(JSON.stringify(originData)))
    nextTick(() => {
        tableStore.table.ref?.setAllTreeExpand(true)
    })
}

// 过滤
const filterData = (arr: treeData[]): treeData[] => {
    if (!tableStore.table.params.searchValue) {
        return arr
    }
    return arr.filter((item: treeData) => {
        if (item.name!.includes(tableStore.table.params.searchValue)) {
            return true
        } else if (item.children?.length > 0) {
            item.children = filterData(item.children)
            return item.children.length > 0
        } else {
            return false
        }
    })
}
</script>
