<template>
  <div class="app-container">
    <div class="filter-container">
      <router-link to="/goods/category/create"><el-button class="filter-item" type="primary" icon="el-icon-circle-plus-outline">添加分类</el-button></router-link>
      <el-button class="filter-item" type="primary" icon="el-icon-sort" @click="batchSaveSort">批量更新排序</el-button>
    </div>
    <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 align="center" label="序号" width="50px">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="分类名称" align="center" prop="classifyName" />
      <el-table-column label="分类图标" align="center" prop="classifyImage">
        <template slot-scope="{row}">
          <img :src="row.classifyImage" alt="" width="50">
        </template>
      </el-table-column>
      <el-table-column label="排序值" align="center" prop="sort">
        <template slot-scope="{row}">
          <el-input v-model="row.sort" style="width:60px;" size="small" />
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="状态" align="center" prop="status">
        <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)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column align="center" label="添加时间" prop="addTime">
        <template slot-scope="{row}">
          <i class="el-icon-time" />
          <span>{{ row.addTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <router-link :to="'/goods/category/edit/' + row.id "><el-button type="primary" size="small">编辑</el-button></router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :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) {
      switchClassify({ id: id }).then(() => {
        this.getList()
      }).catch(() => {
        this.getList()
      })
    },
    batchSaveSort() {
      const sorts = []
      this.list.map(item => {
        sorts.push(item.id + '-' + item.sort)
      })
      saveCategorySort({ newProducSorttList: sorts.join(',') }).then(() => {
        this.getList()
      }).catch(() => {
        this.getList()
      })
    }
  }
}

</script>
