<script setup lang="ts">
/**
 * 工作台设置 / 菜单编辑器
 *
 * 展示分组列表，支持点击编辑和添加分组。
 * Design: #1E74FF NavBar, #F4F7F8 bg, white cards border-radius 10px.
 */
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

/** 分组列表 */
interface GroupItem {
  id: number
  name: string
}

const groups = ref<GroupItem[]>([])

/**
 * 模拟初始化数据
 */
onMounted(() => {
  groups.value = [
    { id: 1, name: '常用功能' },
    { id: 2, name: '管网管理' },
    { id: 3, name: '巡检管理' },
    { id: 4, name: '报表统计' },
  ]
})

/**
 * 跳转添加分组
 */
function toAddGroup() {
  router.push({
    name: 'addGroup',
    params: { title: '添加' },
  })
}

/**
 * 跳转编辑分组
 */
function toEditGroup(item: GroupItem) {
  router.push({
    name: 'addGroup',
    params: { title: '编辑' },
    query: {
      name: item.name,
      groupId: item.id,
    },
  })
}
</script>

<template>
  <div class="menu-edit-page">
    <!-- NavBar — brand blue -->
    <van-nav-bar
      title="工作台设置"
      left-text="返回"
      left-arrow
      fixed
      placeholder
      @click-left="router.back()"
    />

    <div class="menu-edit-content">
      <!-- 添加分组按钮 -->
      <div class="add-group-btn" @click="toAddGroup">
        <van-icon name="plus" size="18" />
        <span>添加分组</span>
      </div>

      <!-- 分组列表 -->
      <div class="group-list" v-if="groups.length > 0">
        <div
          class="group-item"
          v-for="item in groups"
          :key="item.id"
          @click="toEditGroup(item)"
        >
          <div class="group-item__icon">
            <van-icon name="apps-o" size="20" color="#1E74FF" />
          </div>
          <div class="group-item__name">{{ item.name }}</div>
          <div class="group-item__arrow">
            <van-icon name="arrow" size="14" color="#C8C9CC" />
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <van-empty v-else image="search" description="暂无分组，点击上方按钮添加" />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.menu-edit-page {
  min-height: 100vh;
  background: #F4F7F8;

  :deep(.van-nav-bar) { background: #1E74FF; }
  :deep(.van-nav-bar__title) { color: #fff; font-size: 18px; font-weight: 600; }
  :deep(.van-nav-bar__text),
  :deep(.van-nav-bar__arrow) { color: #fff; }
  :deep(.van-nav-bar .van-icon) { color: #fff; }
}

.menu-edit-content {
  width: 100%;
}

.add-group-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 44px;
  margin: 12px;
  font-size: 15px;
  font-weight: 500;
  color: #1E74FF;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  cursor: pointer;
  transition: background 0.15s ease;

  &:active {
    background: var(--color-primary-bg);
  }
}

.group-list {
  padding: 0 12px;
}

.group-item {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 56px;
  padding: 0 16px;
  background: #fff;
  border-radius: 10px;
  margin-bottom: 10px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  cursor: pointer;
  transition: box-shadow 0.15s ease;

  &:active {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  }

  &__icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: var(--color-primary-bg);
    border-radius: 8px;
    flex-shrink: 0;
  }

  &__name {
    flex: 1;
    font-size: 15px;
    font-weight: 500;
    color: var(--color-text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  &__arrow {
    flex-shrink: 0;
  }
}
</style>
