<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-control">
        <span>主题:</span>
        <el-input
          v-model="listQuery.productId"
          placeholder="请输入活动主题"
          clearable
          @input="e => (listQuery.productId= checkedInput(e,1))"
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>状态:</span>
        <el-select v-model="listQuery.status" style="width:120px;" clearable @change="getList">
          <el-option value="" label="请选择" />
          <el-option :value="0" label="已上线" />
          <el-option :value="1" label="已下线" />
        </el-select>
      </div>
      <div class="filter-control">
        <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
        <el-button type="primary" icon="el-icon-plus" @click="handleToEdit('add')">添加活动</el-button>
      </div>
    </div>
    <p style="margin: 0;line-height: 24px;font-size: 14px;color: #999;">注：专题仅支持一个已启用状态的活动。</p>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <el-table-column label="分类名称" align="center" prop="classifyName" />
      <el-table-column label="活动入口图片" align="center" prop="classifyImage">
        <template slot-scope="{row}">
          <el-popover
            placement="top-start"
            title=""
            width="530"
            trigger="hover"
          >
            <img :src="row.classifyImage" alt="" style="width:500px;height:200px;">
            <img slot="reference" :src="row.classifyImage" alt="" style="width:200px;height:50px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="排序值" align="center" prop="sort">
        <template slot-scope="{row}">
          <el-input-number v-model="row.sort" controls-position="right" :min="1" :max="999" :maxlength="3" size="mini" style="width:65px;" />
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="状态" align="center" prop="status" width="100">
        <template slot-scope="{row}">
          <el-tooltip :content="row.status*1 === 1 ? '停用' : '启用'" placement="top">
            <el-switch
              v-model="row.status"
              active-value="1"
              inactive-value="0"
              @change="changeStatus(row.id, row.status)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column align="center" label="添加时间" prop="addTime">
        <template slot-scope="{row}">
          <span>{{ row.addTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleToEdit('edit',row.id)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>20" :total="total" :page.sync="listQuery.index" :limit.sync="listQuery.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { getCategory, switchClassify, saveCategorySort } from '@/api/goods-classify'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        index: 1,
        pageSize: 20,
        organizeId: this.$store.getters.organizeId
      }
    }
  },
  computed: {
    organizeId() {
      return this.$store.getters.organizeId
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getCategory(this.listQuery).then(response => {
        this.list = response.data.results
        this.total = response.data.totalRecord
        this.listLoading = false
      })
    },
    changeStatus(id, status) {
      const confirmTxt = '确定要' + (status * 1 === 1 ? '启用' : '停用') + '该分类吗？'
      this.$confirm(confirmTxt).then(_ => {
        this.confirmChange(id)
      }).catch(() => {
        this.getList()
      })
    },
    confirmChange(id) {
      switchClassify({ id: id }).then(() => {
        this.getList()
      }).catch(() => {
        this.getList()
      })
    },
    batchSaveSort() {
      const sorts = []
      this.list.map(item => {
        if (item.sort) {
          sorts.push(item.id + '-' + Math.abs(item.sort))
        }
      })
      saveCategorySort({ newProducSorttList: sorts.join(',') }).then(response => {
        this.$message({ message: response.msg, type: 'success', center: true })
        this.getList()
      }).catch(() => {
        this.getList()
      })
    },
    // 跳转添加/编辑
    handleToEdit(type, id) {
      if (type === 'add') {
        this.$router.push({ path: '/campaign/special-edit', query: { type: type }})
      } else {
        this.$router.push({ path: '/campaign/special-edit', query: { type: type, id: id }})
      }
    }
  }
}

</script>
<style>
.el-input-number.is-controls-right .el-input__inner {
    padding-left: 5px;
    padding-right: 30px;
}
</style>
<style lang="scss" scoped>
.filter-title{
  float: left;
  font-size: 12px;
  line-height: 18px;
  text-align: left;
  color: #2E3748;
}
.filter-title p{
  margin:10px 0;
}
</style>
