<template>
  <div class="payreturnPage">
    <!-- 搜索 -->
    <div class="formSearch">
      <!-- 搜索条件 -->
      <el-form :inline="true" :model="query" class="demo-form-inline">
        <el-form-item label="爆品名称">
          <el-input v-model="query.categoryName" maxlength="20" placeholder="分类名称" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" plain @click="search">查询</el-button>
          <el-button plain @click="clear">重置</el-button>
          <el-button type="primary" plain @click="handleAddCategory()">新增爆品分类</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <div class="tableBox">
      <el-table ref="multipleTable" :data="tableData" border :header-cell-style="{ background: '#EEF3FF', color: '#333333' }" tooltip-effect="dark" style="width: 100%">
        <el-table-column prop="categoryName" label="分类名称" align="center" width="220" />
        <el-table-column prop="categoryImage" label="分类图片" align="center">
          <template slot-scope="scope">
            <el-image :preview-src-list="[scope.row.categoryImage]" style="width: 80px; height: 80px; border-radius: 10px;e" :src="scope.row.categoryImage"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="categorySort" label="排序值" align="center" width="220" />
        <el-table-column prop="createTime" label="创建时间" align="center" width="220" />

        <el-table-column label="操作" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <div class="btnList">
              <el-button type="text" @click="handleAddCategory(scope.row)">编辑</el-button>
              <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
              <!--  <el-button type="text" @click="deleteHotGoods(scope.row)">删除</el-button>
              <el-button type="text" @click="editGift(scope.row)">编辑配销产品</el-button> -->
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye">
        <el-pagination
          :current-page="query.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 新增，编辑 -->
    <EditCategory @refresh="search" ref="editCategoryRef"></EditCategory>
  </div>
</template>

<script>
import { getExplosiveProductCategory, deleteExplosiveProductCategory } from '@/api/explosiveProduct/category'
import EditCategory from './EditCategory.vue'

export default {
  components: { EditCategory },
  data() {
    return {
      query: { page: 1, pageSize: 10, categoryName: '' },
      total: 1,
      tableData: []
    }
  },
  created() {
    this.getAll()
  },
  methods: {
    async getAll() {
      const res = await getExplosiveProductCategory(this.query)
      this.tableData = res.data.list
      this.total = res.data.total
    },
    handleSizeChange(val) {
      this.query.pageSize = val
      this.getAll()
    },
    handleCurrentChange(val) {
      this.query.page = val
      this.getAll()
    },
    search() {
      this.total = 1
      this.query.page = 1
      this.getAll()
    },
    clear() {
      this.query = {
        page: 1,
        pageSize: 10
      }
    },

    // 新增，编辑分类
    handleAddCategory(row) {
      this.$refs.editCategoryRef.open(row)
    },

    // 删除
    handleDelete(row) {
      this.$confirm(`是否删除【${row.categoryName}】这项分类?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await deleteExplosiveProductCategory({ categoryId: row.id })
          if (res.code == '200') {
            this.$message.success('删除成功')
            this.search()
          }
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.payreturnPage {
  padding: 30px;

  .tableBox {
    .fenye {
      margin: 20px;
    }
  }
}
</style>
