<template>
  <div class="mobile-home">
    <!-- 顶部Banner -->
    <div class="mobile-home__banner">
      <div class="mobile-home__banner-bg">
        <div class="mobile-home__banner-content">
          <div class="mobile-home__brand">
            <div class="mobile-home__brand-logo">
              <img src="@/assets/imgs/logo.png" alt="Logo" class="mobile-home__logo-icon" />
              <div class="mobile-home__logo-text">
                <div class="mobile-home__logo-title">亚为MOM</div>
                <div class="mobile-home__logo-subtitle">Manufacturing Operations Management</div>
              </div>
            </div>
            <div class="mobile-home__brand-desc">
              <div class="mobile-home__brand-title">智能制造一体化管理平台</div>
              <div class="mobile-home__brand-tags">
                <span class="tag">私有化部署</span>
                <span class="tag">可定制</span>
                <span class="tag">安全可靠</span>
              </div>
            </div>
          </div>
        </div>
        <!-- 装饰元素 -->
        <div class="mobile-home__banner-circles">
          <div class="circle circle-1"></div>
          <div class="circle circle-2"></div>
          <div class="circle circle-3"></div>
        </div>
      </div>
    </div>

    <!-- 常用功能区 -->
    <div class="mobile-home__section">
      <div class="mobile-home__section-header">
        <span class="mobile-home__section-title">常用功能</span>
        <span class="mobile-home__section-more">全部 ></span>
      </div>

      <div class="mobile-home__grid" v-loading="loading">
        <div
          v-for="item in shortcut"
          :key="item.name"
          class="mobile-home__grid-item"
          @click="handleShortcutClick(item.url)"
        >
          <div class="mobile-home__grid-icon" :style="{ background: item.color }">
            <Icon :icon="item.icon" />
          </div>
          <span class="mobile-home__grid-label">{{ item.name }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useUserStore } from '@/store/modules/user'
import { usePermissionStore } from '@/store/modules/permission'
import type { Shortcut } from './types'
import { useRouter } from 'vue-router'

defineOptions({ name: 'Index' })

const { t } = useI18n()
const router = useRouter()
const userStore = useUserStore()
const permissionStore = usePermissionStore()
const loading = ref(true)
const avatar = userStore.getUser.avatar
const username = userStore.getUser.nickname

// 预设颜色列表
const colorList = [
  '#1fdaca', '#ff6b6b', '#7c3aed', '#3fb27f', '#4daf1bc9', '#1a73e8',
  '#f59e0b', '#10b981', '#6366f1', '#ef4444', '#8b5cf6', '#06b6d4',
  '#84cc16', '#f97316', '#ec4899', '#14b8a6', '#22c55e', '#6b7280',
  '#3b82f6', '#fbbf24'
]

// 获取快捷入口
let shortcut = reactive<Shortcut[]>([])

// 递归提取所有叶子节点路由
const extractLeafRoutes = (routes: any[], result: Shortcut[] = [], colorIndex = { value: 0 }, parentPath = ''): Shortcut[] => {
  routes.forEach((route) => {
    if (route.meta?.hidden) return
    let fullPath = route.path || ''
    if (fullPath && !fullPath.startsWith('/') && !fullPath.startsWith('http')) {
      fullPath = parentPath.endsWith('/') ? `${parentPath}${fullPath}` : `${parentPath}/${fullPath}`
    }
    if (fullPath && !fullPath.startsWith('/') && !fullPath.startsWith('http')) {
      fullPath = `/${fullPath}`
    }
    if (route.children && route.children.length > 0) {
      extractLeafRoutes(route.children, result, colorIndex, fullPath)
    } else {
      if (fullPath) {
        result.push({
          name: route.meta?.title || route.name || '未命名',
          icon: route.meta?.icon || 'mdi:application',
          url: fullPath,
          color: colorList[colorIndex.value % colorList.length]
        })
        colorIndex.value++
      }
    }
  })
  return result
}

const getShortcut = async () => {
  const routes = permissionStore.getRouters
  const leafRoutes = extractLeafRoutes(routes)
  shortcut.splice(0, shortcut.length, ...leafRoutes)
}

const getAllApi = async () => {
  await getShortcut()
  loading.value = false
}

const handleShortcutClick = (url: string) => {
  if (url.startsWith('http://') || url.startsWith('https://')) {
    window.open(url, '_blank')
  } else {
    router.push(url)
  }
}

getAllApi()
</script>

<style lang="scss" scoped>
.mobile-home {
  background: linear-gradient(180deg, #f0f4f8 0%, #f5f7fa 100%);
  min-height: 100vh;
  padding-bottom: 20px;
}

/* Banner 区域 */
.mobile-home__banner {
  margin-bottom: 16px;
}

.mobile-home__banner-bg {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 0 0 24px 24px;
  padding: 24px 16px 32px;
  position: relative;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(102, 126, 234, 0.3);
}

.mobile-home__banner-content {
  position: relative;
  z-index: 2;
}

.mobile-home__brand {
  color: #fff;
}

.mobile-home__brand-logo {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

.mobile-home__logo-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 700;
  color: #fff;
  border: 2px solid rgba(255, 255, 255, 0.3);
  object-fit: contain;
  padding: 6px;
}

.mobile-home__logo-text {
  flex: 1;
}

.mobile-home__logo-title {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 2px;
  letter-spacing: 0.5px;
}

.mobile-home__logo-subtitle {
  font-size: 10px;
  opacity: 0.85;
  letter-spacing: 0.3px;
  text-transform: uppercase;
}

.mobile-home__brand-desc {
  margin-top: 4px;
}

.mobile-home__brand-title {
  font-size: 15px;
  font-weight: 500;
  margin-bottom: 10px;
  opacity: 0.95;
}

.mobile-home__brand-tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  
  .tag {
    padding: 4px 12px;
    background: rgba(255, 255, 255, 0.2);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    font-size: 11px;
    font-weight: 500;
    border: 1px solid rgba(255, 255, 255, 0.3);
  }
}

/* Banner 装饰圆圈 */
.mobile-home__banner-circles {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  z-index: 1;
  
  .circle {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    
    &.circle-1 {
      width: 120px;
      height: 120px;
      top: -40px;
      right: -20px;
    }
    
    &.circle-2 {
      width: 80px;
      height: 80px;
      bottom: -20px;
      right: 60px;
      background: rgba(255, 255, 255, 0.08);
    }
    
    &.circle-3 {
      width: 60px;
      height: 60px;
      top: 50%;
      right: -10px;
      background: rgba(255, 255, 255, 0.06);
    }
  }
}

/* 功能区 */
.mobile-home__section {
  margin: 0 12px;
  background: #fff;
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.mobile-home__section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}

.mobile-home__section-title {
  font-size: 17px;
  font-weight: 600;
  color: #1a1a1a;
}

.mobile-home__section-more {
  font-size: 13px;
  color: #909399;
  cursor: pointer;
  
  &:active {
    opacity: 0.7;
  }
}

/* 功能网格 */
.mobile-home__grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px 12px;
  min-height: 100px;
}

.mobile-home__grid-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  
  &:active .mobile-home__grid-icon {
    transform: scale(0.9);
  }
}

.mobile-home__grid-icon {
  width: 56px;
  height: 56px;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  color: #fff;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  position: relative;
  
  &::before {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: 16px;
    background: linear-gradient(135deg, rgba(255,255,255,0.3) 0%, rgba(255,255,255,0) 100%);
    pointer-events: none;
  }
}

.mobile-home__grid-label {
  font-size: 12px;
  color: #303133;
  text-align: center;
  line-height: 1.3;
  max-width: 70px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  white-space: normal;
  word-break: break-all;
}

/* 响应式调整 */
@media (max-width: 375px) {
  .mobile-home__grid {
    grid-template-columns: repeat(4, 1fr);
    gap: 12px 8px;
  }
  
  .mobile-home__grid-icon {
    width: 52px;
    height: 52px;
    font-size: 26px;
  }
  
  .mobile-home__grid-label {
    font-size: 11px;
  }
}
</style>
