<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.productName" 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="handleAddHotGoods">新增爆品商品</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="productName" label="爆品名称" align="center" width="220" />
        <el-table-column prop="productImage" label="爆品图片" align="center">
          <template slot-scope="scope">
            <el-image :preview-src-list="[scope.row.productImage]" style="width: 80px; height: 80px; border-radius: 10px;e" :src="scope.row.productImage"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="productPrice" label="爆品单价" align="center" width="120">
          <template slot-scope="scope">
            <el-tag size="mini" type="success">{{ scope.row.productPrice }}元</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="isPut" label="是否上架" align="center" width="200">
          <template slot-scope="scope">
            <el-switch active-text="已上架" inactive-text="已下架" :value="!!scope.row.isPut" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <div class="btnList">
              <el-button type="text" @click="editHotGoods(scope.row)">编辑</el-button>
              <el-button type="text" @click="handleCHnageHootGoodsStatus(scope.row)">{{ scope.row.isPut ? '下架' : '上架' }}</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>

    <!-- 添加/ 编辑 爆款产品 -->
    <HotGoodsAdd @refresh="search" ref="hotGoodsAddRef"></HotGoodsAdd>

    <!-- 编辑配销产品 -->
    <ExplosiveProductsSubsidiary @refresh="search" ref="explosiveProductsSubsidiaryRef"></ExplosiveProductsSubsidiary>
  </div>
</template>

<script>
import { getHotGoodsList, deleteHotGoods, upDownExplosiveProducts } from '@/api/active/active_hot'
import HotGoodsAdd from './HotGoodsAdd.vue'
import ExplosiveProductsSubsidiary from './ExplosiveProductsSubsidiary.vue'

export default {
  components: {
    HotGoodsAdd,
    ExplosiveProductsSubsidiary
  },
  data() {
    return {
      query: { page: 1, pageSize: 10, productName: '' },
      total: 1,
      tableData: []
    }
  },
  created() {
    this.getAll()
  },
  methods: {
    async getAll() {
      const res = await getHotGoodsList(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
      }
    },
    handleAddHotGoods() {
      this.$refs.hotGoodsAddRef.open()
    },
    editHotGoods(row) {
      this.$refs.hotGoodsAddRef.open(row)
    },
    // 删除
    async deleteHotGoods(row) {
      this.$confirm(`是否删除【${row.productName}】这项爆品?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await deleteHotGoods({ explosiveId: row.id })
          if (res.code == '200') {
            this.$message.success('删除成功')
            this.getAll()
          }
        })
        .catch(() => {})
    },

    // 上下架
    handleCHnageHootGoodsStatus(row) {
      this.$confirm(`是否对爆品【${row.productName}】${row.isPut ? '下架' : '上架'}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await upDownExplosiveProducts({ explosiveId: row.id, isPut: row.isPut ? 0 : 1 })
          if (res.code == '200') {
            this.$message.success(row.isPut ? '下架' : '上架' + '成功')
            this.getAll()
          }
        })
        .catch(() => {})
    },

    // 编辑配销产品
    editGift(row = {}) {
      this.$refs.explosiveProductsSubsidiaryRef.open(row, this.query)
    }
  }
}
</script>

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

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