<template>
  <div class="category-page">
    <div class="category-container">
      <!-- 搜索区域（独立卡片） -->
      <div class="search-card">
        <el-form :model="queryParams" :inline="true">
          <el-form-item label="分类类型">
            <el-select v-model="queryParams.categoryType" placeholder="请选择分类类型" style="width: 200px">
              <el-option label="福礼分类" value="福礼分类" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            <el-button type="primary" icon="Plus" @click="handleAdd()">添加分类</el-button>
            <el-button type="info" @click="toggleExpandAll">
              <el-icon><ArrowDown v-if="!isExpandAll" /><ArrowUp v-else /></el-icon>
              {{ isExpandAll ? '全部收起' : '全部展开' }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 表格区域（独立卡片） -->
      <div class="table-card">
        <el-table
          v-loading="loading"
          :data="categoryList"
          row-key="id"
          border
          :default-expand-all="isExpandAll"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column label="分类名称" prop="name" min-width="200" />
          <el-table-column label="分类级别" align="center" prop="level" width="120" />
          <el-table-column label="是否显示" align="center" width="100">
            <template #default="scope">
              <el-link :type="scope.row.visible ? 'primary' : 'danger'">
                {{ scope.row.visible ? '是' : '否' }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column label="排序" align="center" prop="sort" width="100" />
          <el-table-column label="操作" align="center" width="280">
            <template #default="scope">
              <span class="action-link success" @click="handleAdd(scope.row)">+新增分类</span>
              <span class="action-link primary" @click="handleEdit(scope.row)">修改分类</span>
              <span class="action-link warning" @click="handleConfigProduct(scope.row)">配置商品</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <!-- 添加/编辑对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="上级分类" prop="parentId">
          <el-tree-select
            v-model="form.parentId"
            :data="categoryOptions"
            :props="{ value: 'id', label: 'name', children: 'children' }"
            value-key="id"
            placeholder="请选择上级分类"
            check-strictly
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="0" :max="999" controls-position="right" />
        </el-form-item>
        <el-form-item label="是否显示" prop="visible">
          <el-radio-group v-model="form.visible">
            <el-radio :value="true">是</el-radio>
            <el-radio :value="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="GiftCategory" lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { ArrowDown, ArrowUp } from '@element-plus/icons-vue';

// 查询参数
const queryParams = reactive({
  categoryType: '福礼分类'
});

// 是否全部展开
const isExpandAll = ref(false);

// 列表数据
const categoryList = ref([
  { id: '1', name: '家用电器', level: '1级', sort: 0, visible: true, children: [] },
  { id: '2', name: '食品饮料', level: '1级', sort: 0, visible: true, children: [] },
  { id: '3', name: '杯壶厨具', level: '1级', sort: 0, visible: true, children: [] },
  { id: '4', name: '家纺家居', level: '1级', sort: 0, visible: true, children: [] },
  { id: '5', name: '箱包服饰', level: '1级', sort: 0, visible: true, children: [] },
  { id: '6', name: '个护清洁', level: '1级', sort: 0, visible: true, children: [] },
  { id: '7', name: '运动户外', level: '1级', sort: 0, visible: true, children: [] },
  { id: '8', name: '通讯数码', level: '1级', sort: 0, visible: true, children: [] },
  { id: '9', name: '车载宠物', level: '1级', sort: 0, visible: true, children: [] },
  { id: '10', name: '母婴玩具', level: '1级', sort: 0, visible: true, children: [] },
  { id: '11', name: '卡册卡券', level: '1级', sort: 0, visible: true, children: [] },
  { id: '12', name: '文房收藏', level: '1级', sort: 0, visible: true, children: [] }
]);
const loading = ref(false);

// 分类选项（用于树形选择）
const categoryOptions = ref<any[]>([]);

// 对话框
const dialog = reactive({
  visible: false,
  title: ''
});

// 表单
const formRef = ref();
const initForm = {
  id: undefined as string | undefined,
  parentId: '0',
  name: '',
  sort: 0,
  visible: true
};
const form = ref({ ...initForm });

const rules = {
  name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
};

// 查询列表
const getList = () => {
  loading.value = true;
  setTimeout(() => {
    loading.value = false;
    // 构建分类选项
    categoryOptions.value = [{ id: '0', name: '顶级分类', children: categoryList.value }];
  }, 300);
};

// 搜索
const handleQuery = () => {
  getList();
};

// 重置
const resetQuery = () => {
  queryParams.categoryType = '福礼分类';
  handleQuery();
};

// 切换展开/收起
const toggleExpandAll = () => {
  isExpandAll.value = !isExpandAll.value;
  // 重新渲染表格
  const temp = categoryList.value;
  categoryList.value = [];
  setTimeout(() => {
    categoryList.value = temp;
  }, 0);
};

// 添加
const handleAdd = (row?: any) => {
  form.value = { ...initForm };
  if (row) {
    form.value.parentId = row.id;
  }
  dialog.title = '添加分类';
  dialog.visible = true;
};

// 编辑
const handleEdit = (row: any) => {
  form.value = { ...row, parentId: row.parentId || '0' };
  dialog.title = '修改分类';
  dialog.visible = true;
};

// 配置商品
const handleConfigProduct = (row: any) => {
  ElMessage.info(`配置商品：${row.name}`);
};

// 提交表单
const submitForm = () => {
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      ElMessage.success(form.value.id ? '修改成功' : '添加成功');
      dialog.visible = false;
      getList();
    }
  });
};

onMounted(() => {
  getList();
});
</script>

<style scoped lang="scss">
.category-page {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 20px;
}

.category-container {
  max-width: 1200px;
  margin: 0 auto;
}

.search-card {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  margin-bottom: 12px;
}

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

.action-link {
  cursor: pointer;
  margin: 0 6px;
  font-size: 13px;
  
  &.primary {
    color: #409eff;
    &:hover { color: #66b1ff; }
  }
  
  &.success {
    color: #67c23a;
    &:hover { color: #85ce61; }
  }
  
  &.warning {
    color: #e6a23c;
    &:hover { color: #ebb563; }
  }
}
</style>
