<template>
    <div class="table-page">
        <div class="search-part" v-show="isSearch">
            <div class="search-cell">
                <span class="label">菜单名称</span>
                <el-input v-model="queryParams.menuName" placeholder="请输入菜单名称"></el-input>
            </div>
            <el-button type="primary" icon="Search" @click="queryData(queryParams)">查询</el-button>
            <el-button icon="Refresh" @click="queryParams = {}">重置</el-button>
        </div>
        <div class="button-part" style="justify-content: space-between;">
            <div>
                <el-button type="primary" icon="FirstAidKit" @click="addData">新增</el-button>
                <el-button @click="expandTable">展开/折叠</el-button>
            </div>
            <TableAbility :isDownload="false" @searchBtn="isSearch = !isSearch" @refreshBtn="initData()"></TableAbility>
        </div>
        <div class="table-part">
            <el-table ref="tableRef" :data="tableData" row-key="id" height="100%" border show-overflow-tooltip
                @row-click="tableRowClick">
                <el-table-column property="menuName" label="菜单名称" width="180" align="center" />
                <el-table-column property="order" label="排序" width="80" align="center" />
                <el-table-column label="图标" width="80" align="center">
                    <template #default="scope"><i class="main-color f20" :class="scope.row.icon"></i></template>
                </el-table-column>
                <el-table-column property="route" label="路由" width="260" align="center" />
                <el-table-column property="type" label="类型" width="180" align="center" />
                <el-table-column label="操作" align="center">
                    <template #default="scope">
                        <span @click.stop>
                            <el-button link icon="EditPen" @click="editData(scope.row)">修改</el-button>
                            <el-button link icon="Delete" @click="removeData(scope.row)">删除</el-button>
                        </span>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="pagination-part">
            <CommonPagination :total="100" @paginationChange="paginationChange" />
        </div>
    </div>
    <el-dialog v-model="isFormDialog" :title="formDialogTitle" width="30%">
        <MenuForm ref="doctorFormRef" :type="formDialogTitle === '添加' ? 'add' : 'edit'" @close="isFormDialog = false" />
    </el-dialog>
</template>

<script lang='ts' setup>
import { onMounted, reactive, ref, toRefs, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { tableRemoveRow, exportData } from '@/utils/table-util'
import CommonPagination from '@/components/common-pagination.vue'
import MenuForm from './form/menu-form.vue'

let isExpand = false // 表格展开状态

const tableRef = ref()
const doctorFormRef = ref()
const isSearch = ref(true)
const loading = ref(true)
const isFormDialog = ref(false)
const formDialogTitle = ref('')
const queryParams = ref({
    menuName: ''
} as any)
const tableData = ref([] as any)

initData()

function initData() {
    loading.value = true
    tableData.value = []
    setTimeout(() => {
        while (tableData.value.length < 10) {
            tableData.value.push({
                id: tableData.value.length,
                menuName: '权限管理',
                order: tableData.value.length + 1,
                icon: 'icon-shouye-zhihui',
                route: '/permissions-manage',
                type: '菜单',
                children: [
                    {
                        id: tableData.value.length + '-1',
                        menuName: '权限管理',
                        order: tableData.value.length + 1,
                        icon: 'icon-shouye-zhihui',
                        route: '/permissions-manage',
                        type: '菜单',
                    }
                ]
            })
        }
        loading.value = false
    }, 200);
}

const queryData = (e: any) => {
    loading.value = true
    tableData.value = []
    setTimeout(() => {
        while (tableData.value.length < 10) {
            tableData.value.push({
                id: tableData.value.length,
                menuName: e.menuName,
                order: tableData.value.length + 1,
                icon: 'icon-shouye-zhihui',
                route: '/permissions-manage',
                type: '菜单',
            })
        }
        loading.value = false
    }, 200);
}
const addData = () => {
    isFormDialog.value = true
    formDialogTitle.value = '添加'
    setTimeout(() => {
        doctorFormRef.value.resetData()
    }, 0)
}
const expandTable = () => {
    isExpand = !isExpand
    tableData.value.forEach((row: any) => {
        tableRef.value.toggleRowExpansion(row, isExpand)
    })
}
const removeData = (e?: any) => {
    const selectRow = e || tableRef.value.getSelectionRows()
    tableRemoveRow({ data: selectRow }, (res: boolean) => {
        if (res) {
            console.log('调用删除', selectRow)
        }
    })
}
const editData = (e: any) => {
    isFormDialog.value = true
    formDialogTitle.value = '修改'
    setTimeout(() => {
        doctorFormRef.value.resetData()
        doctorFormRef.value.formData = JSON.parse(JSON.stringify(e))
    }, 0)
}
const tableRowClick = (row: any) => {
    tableRef.value.toggleRowExpansion(row)
}
const paginationChange = (page: number, size: number) => {

}
</script>

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