<template>
  <!-- 页面主容器 -->
  <div class="app-container">
    <!-- 操作栏，包含“添加”按钮 -->
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" style="margin-top: 5px"></i>
      <span style="margin-top: 5px">数据列表</span>
      <el-button
        class="btn-add"
        @click="handleAddProductCate"
        size="mini">
        添加
      </el-button>
    </el-card>

    <!-- 表格数据容器 -->
    <div class="table-container">
      <el-table
        ref="productCateTable"
        style="width: 100%"
        :data="list"
        v-loading="listLoading"
        border>

        <!-- 列：编号 -->
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>

        <!-- 列：分类名称 -->
        <el-table-column label="分类名称" align="center">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>

        <!-- 列：级别 -->
        <el-table-column label="级别" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.level | levelFilter }}</template>
        </el-table-column>

        <!-- 列：商品数量 -->
        <el-table-column label="商品数量" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.productCount }}</template>
        </el-table-column>

        <!-- 列：数量单位 -->
        <el-table-column label="数量单位" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.productUnit }}</template>
        </el-table-column>

        <!-- 列：导航栏显示状态 -->
        <el-table-column label="导航栏" width="100" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleNavStatusChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.navStatus">
            </el-switch>
          </template>
        </el-table-column>

        <!-- 列：是否显示 -->
        <el-table-column label="是否显示" width="100" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleShowStatusChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.showStatus">
            </el-switch>
          </template>
        </el-table-column>

        <!-- 列：排序 -->
        <el-table-column label="排序" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.sort }}</template>
        </el-table-column>

        <!-- 列：设置操作 -->
        <el-table-column label="设置" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              :disabled="scope.row.level | disableNextLevel"
              @click="handleShowNextLevel(scope.$index, scope.row)">查看下级
            </el-button>
            <el-button
              size="mini"
              @click="handleTransferProduct(scope.$index, scope.row)">转移商品
            </el-button>
          </template>
        </el-table-column>

        <!-- 列：操作 -->
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleUpdate(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页组件 -->
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  // API 引入
  import {
    fetchList,
    deleteProductCate,
    updateShowStatus,
    updateNavStatus
  } from '@/api/productCate';

  export default {
    name: "productCateList",
    data() {
      return {
        list: null, // 数据列表
        total: null, // 数据总数
        listLoading: true, // 加载状态
        listQuery: { // 查询条件
          pageNum: 1,
          pageSize: 5
        },
        parentId: 0 // 当前父级分类ID
      };
    },
    created() {
      this.resetParentId();
      this.getList();
    },
    watch: {
      // 监听路由变化，重新获取列表
      $route() {
        this.resetParentId();
        this.getList();
      }
    },
    methods: {
      // 重置父级分类ID
      resetParentId() {
        this.listQuery.pageNum = 1;
        this.parentId = this.$route.query.parentId || 0;
      },
      // 获取数据列表
      getList() {
        this.listLoading = true;
        fetchList(this.parentId, this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        });
      },
      // 添加分类
      handleAddProductCate() {
        this.$router.push('/pms/addProductCate');
      },
      // 修改导航栏状态
      handleNavStatusChange(index, row) {
        let data = new URLSearchParams();
        data.append('ids', row.id);
        data.append('navStatus', row.navStatus);
        updateNavStatus(data).then(() => {
          this.$message.success('修改成功');
        });
      },
      // 修改显示状态
      handleShowStatusChange(index, row) {
        let data = new URLSearchParams();
        data.append('ids', row.id);
        data.append('showStatus', row.showStatus);
        updateShowStatus(data).then(() => {
          this.$message.success('修改成功');
        });
      },
      // 查看下级分类
      handleShowNextLevel(index, row) {
        this.$router.push({ path: '/pms/productCate', query: { parentId: row.id } });
      },
      // 编辑分类
      handleUpdate(index, row) {
        this.$router.push({ path: '/pms/updateProductCate', query: { id: row.id } });
      },
      // 删除分类
      handleDelete(index, row) {
        this.$confirm('是否要删除该分类？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteProductCate(row.id).then(() => {
            this.$message.success('删除成功');
            this.getList();
          });
        });
      },
      // 分页大小变更
      handleSizeChange(val) {
        this.listQuery.pageSize = val;
        this.getList();
      },
      // 当前页码变更
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      // 商品转移操作
      handleTransferProduct() {
        this.$message.info('功能待实现');
      }
    },
    filters: {
      // 级别过滤器
      levelFilter(value) {
        return value === 0 ? '一级' : '二级';
      },
      // 禁用下级按钮过滤器
      disableNextLevel(value) {
        return value !== 0;
      }
    }
  };
</script>

<style scoped>
/* 样式留空，按需添加 */
</style>
