<template>
  <div class="menu-previewer">
    <!-- 微信公众号头部 -->
    <div class="wechat-header">
      <div class="wechat-title">微信公众号</div>
    </div>
    
    <!-- 菜单容器 -->
    <div class="menu-container">
      <!-- 一级菜单 -->
      <div class="menu-level-1">
        <div
          v-for="(menu, index) in menuList"
          :key="menu.id || index"
          class="menu-item"
          :class="{
            'active': isMenuActive(index),
            'has-children': menu.children && menu.children.length > 0
          }"
          @click="handleMenuClick(menu, index)"
        >
          <span class="menu-text">{{ menu.name || '菜单' }}</span>
        </div>
        
        <!-- 填充空白菜单项 -->
        <div
          v-for="i in (3 - menuList.length)"
          :key="`empty-${i}`"
          class="menu-item empty"
          @click="handleAddMenu"
        >
          <span class="menu-text">+</span>
        </div>
      </div>
      
      <!-- 二级菜单 -->
      <div 
        v-if="showSubMenu && currentParentMenu && currentParentMenu.children"
        class="menu-level-2"
      >
        <div
          v-for="(submenu, subIndex) in currentParentMenu.children"
          :key="submenu.id || subIndex"
          class="submenu-item"
          :class="{ 'active': isSubMenuActive(parentIndex, subIndex) }"
          @click="handleSubMenuClick(submenu, parentIndex, subIndex)"
        >
          <span class="submenu-text">{{ submenu.name || '子菜单' }}</span>
        </div>
        
        <!-- 填充空白子菜单项 -->
        <div
          v-for="i in (5 - currentParentMenu.children.length)"
          :key="`empty-sub-${i}`"
          class="submenu-item empty"
          @click="handleAddSubMenu"
        >
          <span class="submenu-text">+</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'

defineOptions({
  name: 'MenuPreviewer'
})

// Props
const props = defineProps({
  modelValue: {
    type: Array,
    default: () => []
  },
  activeIndex: {
    type: String,
    default: ''
  },
  parentIndex: {
    type: Number,
    default: -1
  }
})

// Emits
const emit = defineEmits(['update:modelValue', 'menu-clicked', 'submenu-clicked', 'add-menu', 'add-submenu'])

// 响应式数据
const menuList = computed({
  get: () => props.modelValue || [],
  set: (value) => emit('update:modelValue', value)
})

const showSubMenu = ref(false)
const currentParentMenu = ref(null)

// 监听parentIndex变化
watch(() => props.parentIndex, (newIndex) => {
  if (newIndex >= 0 && newIndex < menuList.value.length) {
    currentParentMenu.value = menuList.value[newIndex]
    showSubMenu.value = !!(currentParentMenu.value && currentParentMenu.value.children && currentParentMenu.value.children.length > 0)
  } else {
    showSubMenu.value = false
    currentParentMenu.value = null
  }
}, { immediate: true })

// 判断菜单是否激活
const isMenuActive = (index) => {
  if (!props.activeIndex) return false
  const activeIndexStr = String(index)
  return props.activeIndex === activeIndexStr || props.activeIndex.startsWith(activeIndexStr + '-')
}

// 判断子菜单是否激活
const isSubMenuActive = (parentIdx, subIdx) => {
  return props.activeIndex === `${parentIdx}-${subIdx}`
}

// 处理一级菜单点击
const handleMenuClick = (menu, index) => {
  emit('menu-clicked', menu, index)
}

// 处理二级菜单点击
const handleSubMenuClick = (submenu, parentIdx, subIdx) => {
  emit('submenu-clicked', submenu, parentIdx, subIdx)
}

// 处理添加菜单
const handleAddMenu = () => {
  emit('add-menu')
}

// 处理添加子菜单
const handleAddSubMenu = () => {
  emit('add-submenu')
}
</script>

<style scoped>
.menu-previewer {
  width: 300px;
  background: #f5f5f5;
  border: 1px solid #e6e6e6;
  border-radius: 8px;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;
}

.wechat-header {
  background: #1aad19;
  color: white;
  text-align: center;
  padding: 12px 0;
  font-size: 16px;
  font-weight: 500;
}

.wechat-title {
  margin: 0;
}

.menu-container {
  background: white;
  position: relative;
}

.menu-level-1 {
  display: flex;
  border-bottom: 1px solid #e6e6e6;
}

.menu-item {
  flex: 1;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-right: 1px solid #e6e6e6;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  background: white;
}

.menu-item:last-child {
  border-right: none;
}

.menu-item:hover {
  background: #f0f0f0;
}

.menu-item.active {
  background: #1aad19;
  color: white;
}

.menu-item.has-children::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-bottom: 6px solid #e6e6e6;
}

.menu-item.active.has-children::after {
  border-bottom-color: #1aad19;
}

.menu-item.empty {
  background: #fafafa;
  color: #999;
  border: 2px dashed #ddd;
  margin: 5px;
  border-radius: 4px;
  height: 40px;
}

.menu-item.empty:hover {
  background: #f0f0f0;
  border-color: #1aad19;
  color: #1aad19;
}

.menu-text {
  font-size: 14px;
  max-width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
}

.menu-level-2 {
  background: white;
  border-bottom: 1px solid #e6e6e6;
  max-height: 250px;
  overflow-y: auto;
}

.submenu-item {
  height: 44px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: all 0.3s ease;
}

.submenu-item:last-child {
  border-bottom: none;
}

.submenu-item:hover {
  background: #f8f8f8;
}

.submenu-item.active {
  background: #e8f5e8;
  color: #1aad19;
}

.submenu-item.empty {
  background: #fafafa;
  color: #999;
  border: 2px dashed #ddd;
  margin: 5px 10px;
  border-radius: 4px;
  height: 34px;
  justify-content: center;
}

.submenu-item.empty:hover {
  background: #f0f0f0;
  border-color: #1aad19;
  color: #1aad19;
}

.submenu-text {
  font-size: 13px;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
