<template>
  <view class="admin-page-container">
    <view class="page-card">
      <!-- 搜索栏 -->
      <view class="search-wrap">
        <view class="search-item">
          <text class="label">按钮名称：</text>
          <input class="input" placeholder="请输入按钮名称" v-model="searchKey" />
        </view>
        <button class="btn btn-primary" @click="onSearch">查询</button>
      </view>

      <!-- 表格区域 -->
      <view class="table-wrap">
        <view class="table-header">
          <view class="th" style="flex: 4; text-align: left; padding-left: 20px;">按钮名称</view>
          <view class="th" style="flex: 3;">类型</view>
          <view class="th" style="flex: 2;">排序</view>
          <view class="th" style="flex: 2;">是否显示</view>
          <view class="th" style="flex: 2;">操作</view>
        </view>
        <view class="table-body">
          <view v-for="item in permissionList" :key="item.id" class="tr">
            <view class="td" style="flex: 4; text-align: left; padding-left: 20px;">
              <text v-if="item.hasChildren" class="expand-icon">▶</text>
              <text class="menu-name">{{ item.name }}</text>
            </view>
            <view class="td" style="flex: 3;">{{ item.type }}</view>
            <view class="td" style="flex: 2;">{{ item.sort }}</view>
            <view class="td" style="flex: 2;">
              <switch :checked="item.isshow" color="#1890ff" @change="onToggleShow(item)" />
            </view>
            <view class="td" style="flex: 2;">
              <text class="action-btn" @click="onEdit(item)">编辑</text>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="uts">
import { ref, reactive } from 'vue'

const searchKey = ref('')

type PermissionItem = {
  id: number
  name: string
  type: string
  sort: number
  isshow: boolean
  hasChildren: boolean
}

const permissionList = reactive<PermissionItem[]>([
  { id: 1, name: '主页', type: '菜单：/admin/index', sort: 127, isshow: true, hasChildren: false },
  { id: 2, name: '用户', type: '菜单：/admin/user', sort: 125, isshow: true, hasChildren: true },
  { id: 3, name: '订单', type: '菜单：/admin/order', sort: 120, isshow: true, hasChildren: true },
  { id: 4, name: '商品', type: '菜单：/admin/product', sort: 115, isshow: true, hasChildren: true },
  { id: 5, name: '营销', type: '菜单：/admin/marketing', sort: 110, isshow: true, hasChildren: true }
])

function onSearch() {
  console.log('Search:', searchKey.value)
}

function onToggleShow(item: PermissionItem) {
  item.isshow = !item.isshow
}

function onEdit(item: PermissionItem) {
  console.log('Edit:', item.name)
}
</script>

<style scoped>
.admin-page-container {
  /* 使用 Layout 的背景和内边距 */
  padding: 0;
  background-color: transparent;
  min-height: auto;
}

.page-card {
  background-color: #fff;
  border-radius: 4px;
  padding: 20px;
}

.search-wrap {
  display: flex;
  flex-direction: row;
  align-items: center;
  padding-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 20px;
}

.search-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-right: 20px;
}

.label {
  font-size: 14px;
  color: #606266;
  margin-right: 8px;
}

.input {
  width: 200px;
  height: 32px;
  padding: 0 10px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
  color: #606266;
}

.btn {
  height: 32px;
  line-height: 30px;
  padding: 0 15px;
  font-size: 14px;
  border-radius: 4px;
  border: none;
}

.btn-primary {
  background-color: #1890ff;
  color: #fff;
}

.table-wrap {
  width: 100%;
  border: 1px solid #f0f0f0;
}

.table-header {
  display: flex;
  flex-direction: row;
  background-color: #f8f8f9;
}

.th {
  padding: 12px 10px;
  font-size: 14px;
  font-weight: bold;
  color: #515a6e;
  border-bottom: 1px solid #f0f0f0;
  text-align: center;
}

.tr {
  display: flex;
  flex-direction: row;
  border-bottom: 1px solid #f0f0f0;
}

.td {
  padding: 12px 10px;
  font-size: 14px;
  color: #515a6e;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.expand-icon {
  font-size: 12px;
  color: #999;
  margin-right: 5px;
}

.menu-name {
  font-size: 14px;
}

.action-btn {
  color: #1890ff;
  font-size: 13px;
  cursor: pointer;
}
</style>
