<template>
    <view class="operation-container" @click="playGlobalSound">

        <!-- 直接循环 -->
        <view class="section" v-for="item in menuData" :key="item.path">
            <view class="section-title">{{ item.meta.title }}</view>
            <view class="grid-container">
                <view
                    class="grid-item"
                    v-for="child in item.children"
                    :style="{ gridColumn: child.query.span ? 'span 2' : 'span 1' }"
                    :key="child.path"
                    :class="child.query.type"
                    @click="handleNavigation(child.path)"
                >
                    {{ formatName(child.meta.title) }}
                </view>
            </view>
        </view>

        <!-- 工单操作列表 -->
        <view class="section" v-if="workOrderOperations.length > 0">
            <view class="section-title">工单</view>
            <view class="grid-container">
                <view
                    class="grid-item"
                    v-for="op in workOrderOperations"
                    :key="op.path"
                    @click="handleNavigation(op.path)"
                    :class="op.type"
                >
                    {{ op.name }}
                </view>
            </view>
        </view>
    </view>
</template>

<script setup>
import { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";

//点击全局音效
function playGlobalSound(){
    uni.$u.playClickSound()
}

// 菜单数据和加载状态
const menuData = ref([]);
let menuPromise = null;

//获取权限菜单 - 只调用一次
async function getMenuList() {
    // 如果已经有数据，直接返回
    if (menuData.value && menuData.value.length > 0) {
        return menuData.value;
    }

    // 如果已有一个正在进行的请求，则返回该Promise
    if (menuPromise) {
        return menuPromise;
    }

    // 创建新的请求
    menuPromise = uni.$u.http
        .get("/app/appUser/getRouters")
        .then((res) => {
            const data = res.data;
            menuData.value = data.map((item) => {
                item.children = item.children.map((child) => {
                    child.query = child.query ? JSON.parse(child.query) : {};
                    return child;
                });
                return item;
            });
            return data;
        })
        .catch((err) => {
            // 处理错误情况
            console.error("获取菜单数据失败", err);
            return [];
        })
        .finally(() => {
            // 请求完成后，重置Promise
            menuPromise = null;
        });

    return menuPromise;
}

//获取按钮权限
function getButtonList() {
    uni.$u.http.get("/app/appUser/getInfo").then((res) => {
        if (res.code == 200) {
            // 保存到本地存储
            uni.setStorageSync("buttonList", res.permissions);
        }
    });
}

//获取音频信息配置
function getAudioConfig() {
    uni.$u.http.get("/app/appConf/getAudioConfig").then((res) => {
        if (res.code == 200) {
            uni.setStorageSync("audioConfig", res.data);
        }
    });
}

// 页面加载时初始化数据
onLoad(() => {
    // 显示加载中提示
    uni.showLoading({
        title: '加载中...',
        mask: true
    });
    
    // 初始化菜单和按钮数据
    Promise.all([getMenuList(), getButtonList()])
        .finally(() => {
            // 数据加载完成后，隐藏加载提示
            uni.hideLoading();
        });

    getAudioConfig();
});

const formatName = (name) => {
    if (!name) return "";

    let result = name;

    if (result.includes("\\n\\r")) {
        result = result.replace(/\\n\\r/g, "\n");
    }

    return result;
};






// 工单操作列表
const workOrderOperations = ref([
    {
        name: "回收工单",
        path: "/pages/index/work-order/recycle",
        type: "primary",
    },
    {
        name: "卖书工单",
        path: "/pages/index/work-order/mall",
        type: "warning",
    },
]);

// 页面跳转方法
const handleNavigation = (path) => {
    uni.navigateTo({
        url: path,
        fail: () => {
            uni.showToast({
                title: "页面跳转失败",
                icon: "none",
            });
        },
    });
    playGlobalSound();
};
</script>

<style lang="scss" scoped>
.operation-container {
    padding: 20rpx;
    box-sizing: border-box;
    /* #ifdef H5 */
    padding-bottom: 100rpx;
    /* #endif */
}

.section {
    margin-bottom: 30rpx;
    background: #ffffff;
    border-radius: 16rpx;
    padding: 20rpx;
    padding-right: 0;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);

    .section-title {
        font-size: 50rpx;
        font-weight: 600;
        color: #333;
        padding: 20rpx;
        padding-top: 0;
        border-bottom: 2rpx solid #f0f0f0;
        margin-bottom: 20rpx;
    }

    .grid-container {
        display: flex;
        flex-wrap: wrap;
        padding: 10rpx;

        .grid-item {
            width: calc(50% - 20rpx);
            min-height: 148rpx;
            display: flex;
            align-items: center;
            justify-content: center;
            text-align: center;
            padding: 20rpx 30rpx;
            line-height: 60rpx;
            font-size: 50rpx;
            border-radius: 12rpx;
            color: #ffffff;
            transition: all 0.3s;
            white-space: pre-wrap;
            box-sizing: border-box;
            margin-right: 20rpx;
            margin-bottom: 20rpx;

            &[style*="grid-column: span 2"] {
                width: calc(100% - 20rpx);
                margin-right: 20rpx;
            }

            &.primary {
                background: linear-gradient(135deg, #4cd964, #3ac555);

                &:active {
                    background: linear-gradient(135deg, #3ac555, #2fb548);
                }
            }

            &.warning {
                background: linear-gradient(135deg, #ff9500, #ff8000);

                &:active {
                    background: linear-gradient(135deg, #ff8000, #e67300);
                }
            }

            &:active {
                transform: scale(0.98);
            }
        }
    }
}
</style>
