<template>

  <div class="fatherTop">
    <el-button-group>

      <el-button
          class="no-active-btn"
          :class="{ 'active-btn': activeTab === 'coinConsumeDetail' }"
          @click="navigateTo('coinConsumeDetail')"
          v-if="hasDetail"
      >
        {{ $t('consume.coinConsumeDetail') }}
      </el-button>

      <el-button
          class="no-active-btn"
          :class="{ 'active-btn': activeTab === 'addCoinConsume' }"
          @click="navigateTo('addCoinConsume')"
          v-if="hasAdd"
      >
        {{ $t('consume.addCoinConsume') }}
      </el-button>
    </el-button-group>
  </div>
  <div>
    <router-view></router-view>
  </div>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { storeToRefs } from "pinia";
import { useAdminStore } from "@/store/index.js";
import { hasMenuPermission, permissionMapping } from "@/utils/menuTreePermission.js";
import { useI18n } from 'vue-i18n';

const router = useRouter();
const route = useRoute();
const adminStore = useAdminStore();
const { menuTree } = storeToRefs(adminStore);

const activeTab = ref('');
const hasAdd = ref(false);
const hasDetail = ref(false);
// 导航方法
const navigateTo = (name) => {
  activeTab.value = name;
  router.push({ name });
};


// 初始化权限状态
const initPermissions = () => {
  if (!menuTree.value || !menuTree.value.length) return;
  hasAdd.value = hasMenuPermission(menuTree.value, permissionMapping.add_gold_coin_consumption);
  hasDetail.value = hasMenuPermission(menuTree.value, permissionMapping.gold_coin_consumption_details);
};

// 默认跳转逻辑
const getDefaultAuditRoute = () => {
  initPermissions();
  if (hasDetail.value) return 'coinConsumeDetail';
  if (hasAdd.value) return 'addCoinConsume';
  return 'coinConsumeDetail';
};

// 监听路由变化更新标签状态
watch(() => route.name, (newName) => {
  initPermissions()
  if (newName === 'addCoinConsume' || newName === 'coinConsumeDetail') {
    activeTab.value = newName;
  } else if (newName === 'coinConsume') {
    // 每次访问 /coinConsume 都进行默认跳转
    const defaultRoute = getDefaultAuditRoute();
    navigateTo(defaultRoute);
  }
});

// 初始化逻辑
onMounted(() => {
  initPermissions()
  if (route.name === 'coinConsume') {
    const defaultRoute = getDefaultAuditRoute();
    navigateTo(defaultRoute);
  } else {
    // 非父路由初始化当前标签状态
    if (route.name === 'addCoinConsume' || route.name === 'coinConsumeDetail') {
      activeTab.value = route.name;
    }
  }
});
</script>
<style scoped lang="scss">

/* 自定义按钮组布局 */

.fatherTop {
  height: 4vh;
}

.custom-button-group {
  display: flex;
  margin-bottom: 16px;
  gap: 8px;
}
:deep(.el-button.custom-tab-button) {
  border-radius: 4px;
  transition: all 0.3s ease;
}
// 自定义tab按钮激活样式
:deep(.el-button.custom-tab-button.el-button--primary) {
  background-color: #2741DE !important;
  border-color: #2741DE !important;
  color: #F3FAFE !important;
}

/* 鼠标悬停效果 */
:deep(.el-button.custom-tab-button:hover:not(.is-disabled)) {
  opacity: 0.8;
}
</style>