<template>
  <div class="navi-layout">
    <!-- 顶部状态栏 -->
    <StatusBar :title="robotName" />

    <!-- 主内容区 -->
    <div class="layout-main">
      <div class="navi-page">
        <!-- 动态背景层 -->
        <div class="bg-layer">
          <div class="bg-orb orb-1"></div>
          <div class="bg-orb orb-2"></div>
          <div class="bg-orb orb-3"></div>
          <div class="bg-grid-overlay"></div>
        </div>

        <!-- 背景模糊遮罩（让主内容区更清晰） -->
        <div class="content-shade"></div>

        <!-- 内容区 -->
        <div class="navi-content">
          <!-- 标题区 -->
          <div class="navi-hero">
            <h1 class="page-title">路线引导</h1>
            <p class="page-subtitle">请选择您要去的地方</p>
          </div>

          <!-- 2×2 点位卡片区 -->
          <div
            class="destinations-grid"
            @touchstart="onTouchStart"
            @touchend="onTouchEnd"
          >
            <div
              v-for="dest in pagedDestinations"
              :key="dest.destinationId"
              class="destination-card"
              @click="selectDestination(dest)"
            >
              <div class="dest-icon" :style="{ background: getCategoryColor(dest.category) }">
                <component :is="getCategoryIcon(dest.category)" />
              </div>
              <div class="dest-info">
                <h3>{{ dest.name }}</h3>
                <p>{{ dest.floor }} · {{ dest.description }}</p>
                <div class="dest-time">
                  <span>约{{ dest.estimatedTime }}</span>
                </div>
              </div>
            </div>
          </div>

          <!-- 分页控件 -->
          <div v-if="totalPages > 1" class="pager">
            <button
              class="pager-btn"
              :disabled="currentPage === 0"
              @click="prevPage"
            >
              <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
                <path d="M15 18l-6-6 6-6" />
              </svg>
              上一页
            </button>
            <div class="pager-dots">
              <span
                v-for="i in totalPages"
                :key="i"
                class="pager-dot"
                :class="{ active: i - 1 === currentPage }"
                @click="goToPage(i - 1)"
              ></span>
            </div>
            <button
              class="pager-btn"
              :disabled="currentPage >= totalPages - 1"
              @click="nextPage"
            >
              下一页
              <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
                <path d="M9 18l6-6-6-6" />
              </svg>
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 左下角返回菜单按钮 -->
    <button class="btn-back" @click="goMenu">
      <svg class="back-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
        <path d="M15 18l-6-6 6-6" />
      </svg>
      <span>返回菜单</span>
    </button>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, h } from 'vue'
import { useRouter } from 'vue-router'
import { useNavigationStore } from '@/stores/navigation'
import { useScreenStore } from '@/stores/screen'
import StatusBar from '@/components/StatusBar.vue'

const router = useRouter()
const navigationStore = useNavigationStore()
const screenStore = useScreenStore()

const robotName = computed(() => screenStore.robotName || '智能迎宾机器人')

const destinations = ref([])
const pageSize = 4
const currentPage = ref(0)

const totalPages = computed(() => Math.ceil(destinations.value.length / pageSize))
const pagedDestinations = computed(() => {
  const start = currentPage.value * pageSize
  return destinations.value.slice(start, start + pageSize)
})

const prevPage = () => {
  if (currentPage.value > 0) currentPage.value--
}

const nextPage = () => {
  if (currentPage.value < totalPages.value - 1) currentPage.value++
}

const goToPage = (i) => {
  currentPage.value = i
}

const goMenu = () => {
  router.push('/menu')
}

// 触摸滑动
let touchStartX = 0
const onTouchStart = (e) => {
  touchStartX = e.touches[0].clientX
}
const onTouchEnd = (e) => {
  const delta = e.changedTouches[0].clientX - touchStartX
  if (Math.abs(delta) > 50) {
    if (delta < 0) nextPage()
    else prevPage()
  }
}

// 图标组件
const ReceptionIcon = () => h('svg', { viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', 'stroke-width': '2' }, [
  h('rect', { x: '2', y: '3', width: '20', height: '18', rx: '2' }),
  h('line', { x1: '8', y1: '12', x2: '16', y2: '12' })
])

const LobbyIcon = () => h('svg', { viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', 'stroke-width': '2' }, [
  h('path', { d: 'M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z' }),
  h('polyline', { points: '9 22 9 12 15 12 15 22' })
])

const MeetingIcon = () => h('svg', { viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', 'stroke-width': '2' }, [
  h('path', { d: 'M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2' }),
  h('circle', { cx: '9', cy: '7', r: '4' }),
  h('path', { d: 'M23 21v-2a4 4 0 0 0-3-3.87' }),
  h('path', { d: 'M16 3.13a4 4 0 0 1 0 7.75' })
])

const ExhibitionIcon = () => h('svg', { viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', 'stroke-width': '2' }, [
  h('rect', { x: '3', y: '3', width: '18', height: '18', rx: '2' }),
  h('circle', { cx: '8.5', cy: '8.5', r: '1.5' }),
  h('polyline', { points: '21 15 16 10 5 21' })
])

const RestIcon = () => h('svg', { viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', 'stroke-width': '2' }, [
  h('path', { d: 'M18 8h1a4 4 0 0 1 0 8h-1' }),
  h('path', { d: 'M2 8h16v9a4 4 0 0 1-4 4H6a4 4 0 0 1-4-4V8z' }),
  h('line', { x1: '6', y1: '1', x2: '6', y2: '4' }),
  h('line', { x1: '10', y1: '1', x2: '10', y2: '4' }),
  h('line', { x1: '14', y1: '1', x2: '14', y2: '4' })
])

const ToiletIcon = () => h('svg', { viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', 'stroke-width': '2' }, [
  h('circle', { cx: '12', cy: '4', r: '2' }),
  h('path', { d: 'M12 6v6' }),
  h('path', { d: 'M8 22v-6a4 4 0 0 1 8 0v6' })
])

const OtherIcon = () => h('svg', { viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', 'stroke-width': '2' }, [
  h('circle', { cx: '12', cy: '12', r: '10' }),
  h('line', { x1: '12', y1: '8', x2: '12', y2: '12' }),
  h('line', { x1: '12', y1: '16', x2: '12.01', y2: '16' })
])

const getCategoryIcon = (category) => {
  const icons = {
    '服务': ReceptionIcon,
    '公共': LobbyIcon,
    '会议': MeetingIcon,
    '展示': ExhibitionIcon,
    '设施': RestIcon,
    '卫生间': ToiletIcon
  }
  return icons[category] || OtherIcon
}

const getCategoryColor = (category) => {
  const colors = {
    '服务': 'linear-gradient(135deg, #2f8ee5 0%, #1a6fc9 100%)',
    '公共': 'linear-gradient(135deg, #20b7c7 0%, #0ea5a5 100%)',
    '会议': 'linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%)',
    '展示': 'linear-gradient(135deg, #f59e0b 0%, #d97706 100%)',
    '设施': 'linear-gradient(135deg, #10b981 0%, #059669 100%)',
    '卫生间': 'linear-gradient(135deg, #ec4899 0%, #db2777 100%)'
  }
  return colors[category] || colors['公共']
}

const selectDestination = async (dest) => {
  try {
    console.log('[Navigation] select destination:', dest)
    await navigationStore.startNavigation(dest)
    console.log('[Navigation] startNavigation success:', navigationStore.currentTask)
    router.push('/navigation/status')
  } catch (error) {
    console.error('[Navigation] startNavigation failed:', error)
    if (import.meta.env.DEV) {
      console.warn('[Navigation] DEV: creating mock navigation task')
      navigationStore.setMockNavigationTask(dest)
      router.push('/navigation/status')
      return
    }
    alert('路线引导功能暂不可用，请稍后再试')
  }
}

onMounted(async () => {
  await navigationStore.fetchDestinations()
  destinations.value = navigationStore.destinations
})
</script>

<style scoped>
/* ===== 布局结构 ===== */
.navi-layout {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: linear-gradient(155deg, #e8f4fd 0%, #dbeafe 40%, #eff6ff 100%);
}

.layout-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.navi-page {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

/* ===== 动态背景层 ===== */
.bg-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 0;
}

.bg-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.45;
}

.orb-1 {
  width: 480px;
  height: 480px;
  background: rgba(47, 142, 229, 0.28);
  top: -180px;
  left: -140px;
  animation: float1 12s ease-in-out infinite;
}

.orb-2 {
  width: 360px;
  height: 360px;
  background: rgba(32, 183, 199, 0.22);
  bottom: -100px;
  right: -80px;
  animation: float2 15s ease-in-out infinite;
}

.orb-3 {
  width: 280px;
  height: 280px;
  background: rgba(139, 92, 246, 0.18);
  top: 50%;
  right: 15%;
  animation: float3 18s ease-in-out infinite;
}

@keyframes float1 {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-40%, -60%) scale(1.08); }
}

@keyframes float2 {
  0%, 100% { transform: translate(50%, 50%) scale(1); }
  50% { transform: translate(60%, 40%) scale(1.06); }
}

@keyframes float3 {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(-5%, 8%) scale(1.1); }
}

.bg-grid-overlay {
  position: absolute;
  inset: 0;
  background-image: radial-gradient(circle, rgba(47, 142, 229, 0.08) 1px, transparent 1px);
  background-size: 32px 32px;
}

.content-shade {
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.18);
  z-index: 0;
  pointer-events: none;
}

/* ===== 内容区 ===== */
.navi-content {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 36px 16px;
  z-index: 1;
  overflow: hidden;
}

/* ===== 标题区 ===== */
.navi-hero {
  text-align: center;
  margin: 46px 0 28px;
  flex-shrink: 0;
}

.page-title {
  font-size: 42px;
  font-weight: 900;
  color: #111827;
  margin: 0;
  letter-spacing: 2px;
  line-height: 1.2;
}

.page-subtitle {
  font-size: 22px;
  color: #6b7280;
  margin: 6px 0 0;
}

/* ===== 2×2 点位卡片网格 ===== */
.destinations-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: 20px 24px;
  width: min(1240px, calc(100vw - 72px));
  max-height: 560px;
  min-height: 500px;
}

.destination-card {
  display: flex;
  align-items: center;
  gap: 26px;
  padding: 36px 42px;
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 28px;
  box-shadow: 0 8px 28px rgba(47, 142, 229, 0.1), 0 2px 8px rgba(0, 0, 0, 0.05);
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  box-sizing: border-box;
}

.destination-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 18px 40px rgba(47, 142, 229, 0.16), 0 4px 12px rgba(0, 0, 0, 0.08);
}

.destination-card:active {
  transform: scale(0.97);
}

.dest-icon {
  width: 92px;
  height: 92px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 24px;
  color: white;
  flex-shrink: 0;
}

.dest-icon svg {
  width: 46px;
  height: 46px;
}

.dest-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.dest-info h3 {
  font-size: 38px;
  font-weight: 900;
  color: #111827;
  margin: 0 0 6px;
  line-height: 1.15;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dest-info p {
  font-size: 23px;
  color: #6b7280;
  margin: 0 0 12px;
  line-height: 1.4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dest-time {
  margin-top: 0;
}

.dest-time span {
  display: inline-block;
  font-size: 20px;
  color: #2f8ee5;
  padding: 10px 16px;
  background: rgba(47, 142, 229, 0.1);
  border-radius: 999px;
  white-space: nowrap;
  font-weight: 700;
}

/* ===== 分页控件 ===== */
.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  margin-top: 14px;
  flex-shrink: 0;
  height: 66px;
}

.pager-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 64px;
  min-width: 132px;
  padding: 0 28px;
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(10px);
  border: 2px solid rgba(47, 142, 229, 0.2);
  border-radius: 999px;
  color: #2f8ee5;
  font-size: 22px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s ease;
  box-sizing: border-box;
}

.pager-btn svg {
  width: 24px;
  height: 24px;
}

.pager-btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.98);
  border-color: #2f8ee5;
  transform: translateY(-1px);
  box-shadow: 0 8px 24px rgba(47, 142, 229, 0.22);
}

.pager-btn:active:not(:disabled) {
  transform: scale(0.97);
}

.pager-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.pager-dots {
  display: flex;
  align-items: center;
  gap: 10px;
}

.pager-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: rgba(47, 142, 229, 0.2);
  cursor: pointer;
  transition: all 0.2s ease;
}

.pager-dot.active {
  background: #2f8ee5;
  width: 38px;
  border-radius: 999px;
}

.pager-dot:hover:not(.active) {
  background: rgba(47, 142, 229, 0.4);
}

/* ===== 左下角返回菜单按钮 ===== */
.btn-back {
  position: fixed;
  left: 24px;
  bottom: 28px;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 66px;
  padding: 0 28px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 2px solid rgba(148, 163, 184, 0.22);
  border-radius: 999px;
  color: #374151;
  font-size: 22px;
  font-weight: 800;
  cursor: pointer;
  letter-spacing: 1px;
  transition: all 0.2s ease;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.1);
  box-sizing: border-box;
}

.btn-back:hover,
.btn-back:focus-visible {
  color: #2f8ee5;
  border-color: rgba(47, 142, 229, 0.36);
  background: rgba(255, 255, 255, 0.98);
  transform: translateY(-2px);
  box-shadow: 0 10px 30px rgba(47, 142, 229, 0.18);
}

.btn-back:active {
  transform: scale(0.97);
}

.back-icon {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
}

/* ===== 响应式适配 ===== */
@media (max-height: 700px) {
  .navi-hero {
    margin: 8px 0 12px;
  }

  .page-title {
    font-size: 36px;
  }

  .page-subtitle {
    font-size: 18px;
    margin-top: 4px;
  }

  .destinations-grid {
    gap: 14px 18px;
    min-height: 460px;
    max-height: 490px;
  }

  .destination-card {
    padding: 26px 32px;
  }

  .dest-icon {
    width: 76px;
    height: 76px;
    border-radius: 20px;
  }

  .dest-icon svg {
    width: 38px;
    height: 38px;
  }

  .dest-info h3 {
    font-size: 30px;
  }

  .dest-info p {
    font-size: 18px;
    margin-bottom: 10px;
  }

  .dest-time span {
    font-size: 17px;
    padding: 8px 13px;
  }

  .pager {
    margin-top: 10px;
    gap: 14px;
    height: 60px;
  }

  .pager-btn {
    height: 58px;
    min-width: 118px;
    padding: 0 22px;
    font-size: 20px;
  }

  .pager-btn svg {
    width: 22px;
    height: 22px;
  }

  .pager-dot {
    width: 13px;
    height: 13px;
  }

  .pager-dot.active {
    width: 34px;
  }

  .btn-back {
    height: 58px;
    padding: 0 22px;
    font-size: 20px;
    left: 20px;
    bottom: 22px;
  }
}
</style>
