<template>
  <!-- 页面容器 -->
  <div class="app-container">
    <!-- 筛选搜索区域 -->
    <el-card class="filter-container" shadow="never">
      <div>
        <!-- 搜索标题和按钮 -->
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="searchBrandList()"
          type="primary"
          size="small">
          查询结果
        </el-button>
      </div>
      <!-- 筛选条件 -->
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="输入搜索：">
            <el-input
              style="width: 203px"
              v-model="listQuery.keyword"
              placeholder="品牌名称/关键字">
            </el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <!-- 操作区域 -->
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <!-- 添加品牌按钮 -->
      <el-button
        class="btn-add"
        @click="addBrand()"
        size="mini">
        添加
      </el-button>
    </el-card>

    <!-- 数据表格区域 -->
    <div class="table-container">
      <el-table
        ref="brandTable"
        :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="品牌名称" 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.firstLetter }}</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="100" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleFactoryStatusChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.factoryStatus">
            </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="220" align="center">
          <template slot-scope="scope">
            <span>商品：</span>
            <el-button
              size="mini"
              type="text"
              @click="getProductList(scope.$index, scope.row)">
              100
            </el-button>
            <span>评价：</span>
            <el-button
              size="mini"
              type="text"
              @click="getProductCommentList(scope.$index, scope.row)">
              1000
            </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="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"
        @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>
import { fetchList, updateShowStatus, updateFactoryStatus, deleteBrand } from "@/api/brand";

export default {
  name: "brandList", // 组件名称
  data() {
    return {
      operates: [
        { label: "显示品牌", value: "showBrand" },
        { label: "隐藏品牌", value: "hideBrand" },
      ],
      operateType: null, // 批量操作类型
      listQuery: {
        keyword: null,
        pageNum: 1,
        pageSize: 10,
      },
      list: null, // 品牌数据列表
      total: null, // 数据总数
      listLoading: true, // 加载状态
      multipleSelection: [], // 多选数据
    };
  },
  created() {
    this.getList(); // 初始化加载列表数据
  },
  methods: {
    // 获取品牌列表
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    // 查询品牌
    searchBrandList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    // 批量操作
    handleBatchOperate() {
      console.log(this.multipleSelection);
    },
    // 添加品牌
    addBrand() {
      this.$router.push({ path: "/pms/addBrand" });
    },
    // 更新品牌状态
    handleUpdate(index, row) {
      this.$router.push({ path: "/pms/updateBrand", query: { id: row.id } });
    },
    // 删除品牌
    handleDelete(index, row) {
      deleteBrand(row.id).then(() => {
        this.$message({ message: "删除成功", type: "success" });
        this.getList();
      });
    },
    // 分页事件
    handleSizeChange(val) {
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
/* 自定义样式 */
</style>
