<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>
</template>

<script setup>
import { ref, computed, onMounted } 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 expressOperations = ref([
    {
        name: "中转\n\r签收",
        path: "/pages/index/express/transfer-sign",
        type: "primary",
    },
    {
        name: "到仓\n\r签收",
        path: "/pages/index/express/warehouse-sign",
        type: "warning",
    },
    {
        name: "重量\n\r修改",
        path: "/pages/index/express/weight-modify",
        type: "primary",
    },
    {
        name: "快递\n\r验收",
        path: "/pages/index/express/quick-check",
        type: "warning",
    },
    {
        name: "路由签收\n\r异常",
        path: "/pages/index/express/route-exception",
        type: "primary",
    },
    {
        name: "快递\n\r拆包",
        path: "/pages/index/express/quick-unpack",
        type: "warning",
    },
]);

// 审核操作列表
const auditOperations = ref([
    {
        name: "确认\n\r收货",
        path: "/pages/index/audit/confirm-receipt",
        type: "primary",
    },
    {
        name: "扫书\n\r查单",
        path: "/pages/index/audit/scan-order",
        type: "warning",
    },
    {
        name: "根据快递单\n\r或订单",
        path: "/pages/index/audit/express-order",
        type: "warning",
    },
    {
        name: "根据\n\r发件人",
        path: "/pages/index/audit/sender",
        type: "primary",
    },
]);

// WMS操作列表
const wmsOperations = ref([
    {
        name: "中等\n\r入库",
        path: "/pages/index/wms/medium-in",
        type: "primary",
    },
    {
        name: "良品\n\r入库",
        path: "/pages/index/wms/good-in",
        type: "warning",
    },
    {
        name: "次品\n\r入库",
        path: "/pages/index/wms/secondary-in",
        type: "primary",
    },
    {
        name: "不良\n\r入库",
        path: "/pages/index/wms/bad-in",
        type: "warning",
    },
    {
        name: "不良\n\r出库",
        path: "/pages/index/wms/bad-out",
        type: "primary",
    },
    {
        name: "不良\n\r下架",
        path: "/pages/index/wms/bad-off",
        type: "warning",
    },
    {
        name: "订单\n\r查询",
        path: "/pages/index/wms/order-query",
        type: "primary",
    },
    {
        name: "库位\n\r订单",
        path: "/pages/index/wms/location-order",
        type: "warning",
    },
    {
        name: "快速盘点",
        path: "/pages/index/wms/speedy-check",
        type: "primary",
        span: 24,
    },
]);

// 统计操作列表
const statisticsOperations = ref([
    {
        name: "审核统计",
        path: "/pages/index/statistic/audit",
        type: "warning",
        span: 24,
    },
    {
        name: "售后\n\r统计",
        path: "/pages/index/statistic/after-sale",
        type: "primary",
    },
    {
        name: "打包\n\r统计",
        path: "/pages/index/statistic/package",
        type: "warning",
    },
]);
//线下核单
const offlineOperations = ref([
    {
        name: "线下\n\r核单",
        path: "/pages/index/offline/check-order",
        type: "primary",
    },
    {
        name: "核单\n\r记录",
        path: "/pages/index/offline/check-record",
        type: "warning",
    },
]);

//录入信息
const entryOperations = ref([
    {
        name: "扫码\n\r查书",
        path: "/pages/index/entry/scan-book",
        type: "primary",
    },
    {
        name: "录入\n\r书籍重量",
        path: "/pages/index/entry/book-weight",
        type: "warning",
    },
    {
        name: "商品档案扫码",
        path: "/pages/index/entry/commodity-scan",
        type: "primary",
        span: 24,
    },
]);

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