<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="菜单名" prop="title">
                <el-input v-model="queryParams.title" placeholder="请输入菜单名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
            </el-col> -->
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table
            v-loading="loading"
            :data="treeData"
            row-key="id"
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
            <el-table-column prop="title" label="菜单名称" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="orderNum" label="排序"></el-table-column>
            <el-table-column label="操作" align="center" width="210" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button v-if="scope.row.type === 1" link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                        >删除</el-button
                    >
                </template>
            </el-table-column>
        </el-table>

        <!-- 添加或修改南洋数字图书馆菜单管理对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="LibraryMenuRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="父菜单" prop="parentId" v-if="form.parentId !== 1">
                    <el-select v-model="form.parentId" placeholder="请选择父菜单" size="large" style="width: 240px">
                        <el-option v-for="item in firstOrderList" :key="item.id" :label="item.title" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="菜单名" prop="title">
                    <el-input v-model="form.title" placeholder="请输入菜单名" />
                </el-form-item>
                <el-form-item label="显示顺序" prop="orderNum">
                    <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="LibraryMenu">
import {
    listLibraryMenu,
    listFirstOrder,
    getLibraryMenu,
    delLibraryMenu,
    addLibraryMenu,
    updateLibraryMenu
} from '@/api/VRdemo/libraryMenu'

const { proxy } = getCurrentInstance()

const LibraryMenuList = ref([])
const firstOrderList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')
const treeData = ref([])

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        module: null,
        type: null,
        parentId: null,
        orderNum: null
    },
    rules: {}
})

const { queryParams, form, rules } = toRefs(data)

/** 查询南洋数字图书馆菜单管理列表 */
function getList() {
    loading.value = true
    listLibraryMenu(queryParams.value).then(response => {
        LibraryMenuList.value = response.rows
        total.value = response.total
        loading.value = false
        treeData.value = convertToTree(LibraryMenuList.value)
    })
    listFirstOrder().then(res => {
        firstOrderList.value = res.data
    })
}

function convertToTree(data) {
    const map = new Map()
    const result = []

    data.forEach(item => {
        map.set(item.id, { ...item, children: [] })
    })

    data.forEach(item => {
        const node = map.get(item.id)
        const parent = map.get(item.parentId)

        if (parent) {
            parent.children.push(node)
        } else {
            result.push(node)
        }
    })

    return result
}

// 取消按钮
function cancel() {
    open.value = false
    reset()
}

// 表单重置
function reset() {
    form.value = {
        id: null,
        title: null,
        module: null,
        parentId: null,
        orderNum: null,
        type: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
    }
    proxy.resetForm('LibraryMenuRef')
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1
    getList()
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm('queryRef')
    handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id)
    single.value = selection.length != 1
    multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
    reset()
    open.value = true
    title.value = '添加南洋数字图书馆菜单管理'
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset()
    const _id = row.id || ids.value
    getLibraryMenu(_id).then(response => {
        form.value = response.data
        open.value = true
        title.value = '修改南洋数字图书馆菜单管理'
    })
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs['LibraryMenuRef'].validate(valid => {
        if (valid) {
            if (form.value.id != null) {
                updateLibraryMenu(form.value).then(response => {
                    proxy.$modal.msgSuccess('修改成功')
                    open.value = false
                    getList()
                })
            } else {
                addLibraryMenu(form.value).then(response => {
                    proxy.$modal.msgSuccess('新增成功')
                    open.value = false
                    getList()
                })
            }
        }
    })
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _ids = row.id || ids.value
    proxy.$modal
        .confirm('是否确认删除南洋数字图书馆菜单管理编号为"' + _ids + '"的数据项？')
        .then(function () {
            return delLibraryMenu(_ids)
        })
        .then(() => {
            getList()
            proxy.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download(
        'VRdemo/LibraryMenu/export',
        {
            ...queryParams.value
        },
        `LibraryMenu_${new Date().getTime()}.xlsx`
    )
}

getList()
</script>
