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

    <!-- 数据表格区域 -->
    <div class="table-container">
      <el-table
        ref="productAttrTable"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <!-- 多选框 -->
        <el-table-column type="selection" width="60" align="center"></el-table-column>

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

        <!-- 属性名称列 -->
        <el-table-column label="属性名称" width="140" align="center">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>

        <!-- 商品类型列 -->
        <el-table-column label="商品类型" width="140" align="center">
          <template slot-scope="scope">{{ $route.query.cname }}</template>
        </el-table-column>

        <!-- 属性是否可选列 -->
        <el-table-column label="属性是否可选" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.selectType | selectTypeFilter }}</template>
        </el-table-column>

        <!-- 属性值录入方式列 -->
        <el-table-column label="属性值的录入方式" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.inputType | inputTypeFilter }}</template>
        </el-table-column>

        <!-- 可选值列表列 -->
        <el-table-column label="可选值列表" align="center">
          <template slot-scope="scope">{{ scope.row.inputList }}</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" @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="batch-operate-container">
      <el-select size="small" v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operates"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small"
      >
        确定
      </el-button>
    </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, deleteProductAttr } from '@/api/productAttr';

export default {
  name: 'productAttrList', // 组件名称
  data() {
    return {
      list: null, // 商品属性数据列表
      total: null, // 数据总条数
      listLoading: true, // 加载状态
      listQuery: {
        pageNum: 1,
        pageSize: 5,
        type: this.$route.query.type, // 查询参数，来源于路由
      },
      operateType: null, // 批量操作类型
      multipleSelection: [], // 多选的属性集合
      operates: [
        { label: '删除', value: 'deleteProductAttr' }, // 批量操作选项
      ],
    };
  },
  created() {
    this.getList(); // 页面加载时获取数据列表
  },
  methods: {
    // 获取商品属性列表
    getList() {
      this.listLoading = true;
      fetchList(this.$route.query.cid, this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },

    // 添加商品属性
    addProductAttr() {
      this.$router.push({
        path: '/pms/addProductAttr',
        query: { cid: this.$route.query.cid, type: this.$route.query.type },
      });
    },

    // 处理多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    // 批量操作处理
    handleBatchOperate() {
      if (this.multipleSelection.length < 1) {
        this.$message({
          message: '请选择至少一条记录',
          type: 'warning',
          duration: 1000,
        });
        return;
      }
      if (this.operateType !== 'deleteProductAttr') {
        this.$message({
          message: '请选择批量操作类型',
          type: 'warning',
          duration: 1000,
        });
        return;
      }

      const ids = this.multipleSelection.map((item) => item.id);
      this.handleDeleteProductAttr(ids);
    },

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

    // 页码切换
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },

    // 跳转到编辑页面
    handleUpdate(index, row) {
      this.$router.push({ path: '/pms/updateProductAttr', query: { id: row.id } });
    },

    // 删除商品属性
    handleDeleteProductAttr(ids) {
      this.$confirm('是否要删除选中的属性？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        const data = new URLSearchParams();
        data.append('ids', ids);
        deleteProductAttr(data).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000,
          });
          this.getList();
        });
      });
    },

    // 删除单个商品属性
    handleDelete(index, row) {
      const ids = [row.id];
      this.handleDeleteProductAttr(ids);
    },
  },
  filters: {
    // 过滤器：属性值的录入方式
    inputTypeFilter(value) {
      return value === 1 ? '从列表中选取' : '手工录入';
    },
    // 过滤器：属性是否可选
    selectTypeFilter(value) {
      if (value === 1) return '单选';
      if (value === 2) return '多选';
      return '唯一';
    },
  },
};
</script>

<style scoped>
/* 自定义样式区域 */
.app-container {
  padding: 20px;
}
</style>
