<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="addProductAttrCate()" size="mini">添加</el-button>
    </el-card>

    <!-- 数据表格区域 -->
    <div class="table-container">
      <el-table
        ref="productAttrCateTable"
        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="200" align="center">
          <template slot-scope="scope">
            {{ scope.row.attributeCount == null ? 0 : scope.row.attributeCount }}
          </template>
        </el-table-column>

        <!-- 列：参数数量 -->
        <el-table-column label="参数数量" width="200" align="center">
          <template slot-scope="scope">
            {{ scope.row.paramCount == null ? 0 : scope.row.paramCount }}
          </template>
        </el-table-column>

        <!-- 列：设置 -->
        <el-table-column label="设置" width="200" align="center">
          <template slot-scope="scope">
            <!-- 属性列表按钮 -->
            <el-button size="mini" @click="getAttrList(scope.$index, scope.row)">
              属性列表
            </el-button>
            <!-- 参数列表按钮 -->
            <el-button size="mini" @click="getParamList(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>

    <!-- 弹出框：添加/编辑商品属性分类 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      width="30%"
    >
      <!-- 表单：输入类型名称 -->
      <el-form ref="productAttrCatForm" :model="productAttrCate" :rules="rules" label-width="120px">
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="productAttrCate.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <!-- 弹窗底部操作按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm('productAttrCatForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
/**
 * 导入商品属性分类相关的 API
 */
import {
  fetchList,
  createProductAttrCate,
  deleteProductAttrCate,
  updateProductAttrCate,
} from '@/api/productAttrCate';

export default {
  name: 'productAttrCateList', // 组件名称
  data() {
    return {
      list: null, // 数据列表
      total: null, // 数据总条数
      listLoading: true, // 加载状态
      listQuery: {
        pageNum: 1,
        pageSize: 5,
      },
      dialogVisible: false, // 控制弹窗的显示
      dialogTitle: '', // 弹窗标题
      productAttrCate: {
        name: '',
        id: null,
      },
      rules: {
        name: [{ required: true, message: '请输入类型名称', trigger: 'blur' }],
      },
    };
  },
  created() {
    // 页面加载时获取数据列表
    this.getList();
  },
  methods: {
    // 获取数据列表
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },

    // 添加商品属性分类
    addProductAttrCate() {
      this.dialogVisible = true;
      this.dialogTitle = '添加类型';
      this.productAttrCate = { name: '', id: null };
    },

    // 处理分页大小变化
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },

    // 处理页码变化
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },

    // 删除商品属性分类
    handleDelete(index, row) {
      this.$confirm('是否要删除该分类？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteProductAttrCate(row.id).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000,
          });
          this.getList();
        });
      });
    },

    // 编辑商品属性分类
    handleUpdate(index, row) {
      this.dialogVisible = true;
      this.dialogTitle = '编辑类型';
      this.productAttrCate.name = row.name;
      this.productAttrCate.id = row.id;
    },

    // 跳转到属性列表
    getAttrList(index, row) {
      this.$router.push({
        path: '/pms/productAttrList',
        query: { cid: row.id, cname: row.name, type: 0 },
      });
    },

    // 跳转到参数列表
    getParamList(index, row) {
      this.$router.push({
        path: '/pms/productAttrList',
        query: { cid: row.id, cname: row.name, type: 1 },
      });
    },

    // 确认弹窗提交
    handleConfirm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = new URLSearchParams();
          data.append('name', this.productAttrCate.name);

          if (this.dialogTitle === '添加类型') {
            // 添加类型
            createProductAttrCate(data).then(() => {
              this.$message({ message: '添加成功', type: 'success', duration: 1000 });
              this.dialogVisible = false;
              this.getList();
            });
          } else {
            // 修改类型
            updateProductAttrCate(this.productAttrCate.id, data).then(() => {
              this.$message({ message: '修改成功', type: 'success', duration: 1000 });
              this.dialogVisible = false;
              this.getList();
            });
          }
        }
      });
    },

    // 关闭弹窗时清除表单校验
    handleClose() {
      if (this.$refs.productAttrCatForm) {
        this.$refs.productAttrCatForm.clearValidate();
      }
    },
  },
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>
