<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-table :data="tableData" stripe style="width: 100%;" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="分类名称" prop="CATE_NAME"></el-table-column>
        <el-table-column label="排序" width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.SORT" min="0" max="99"
                      @change="handleChangeSort(scope.row, scope.row.SORT)"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.CREATED_AT)}}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope">
            <el-dropdown size="small" trigger="click">
              <el-button type="primary" size="small" @click.stop="">
                操作该数据<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="edit" @click.native="handleDelete(scope.row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <el-dropdown size="small" trigger="click">
          <el-button type="primary" size="small">
            所选数据<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="delete" @click.native="handleMuliDel()">删除</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-button type="primary" size="small" @click="handleAdd">新建分类</el-button>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[20, 50, 100, 200]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import network from '@/utils/network'
import tool from '@/utils/tool'
import store from '@/utils/vuexStore'

export default {
  name: 'article_category',
  mounted () {
    this.getData()
  },
  data () {
    return {
      tableData: null,
      loading: true,
      multipleSelection: [],
      currentPage: 1,
      totalPages: 1,
      totalCount: 1,
      pageSize: 20,
      tool: tool,
    }
  },
  methods: {
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleCurrentChange (page) {
      this.getData(page, this.pageSize)
    },
    handleSizeChange (pageSize) {
      this.getData(this.currentPage, pageSize)
    },
    handleAdd () {
      this.$router.push({path: `/article/category-add`})
    },
    handleDelete (row) {
      this.delData(row.ID)
    },
    handleMuliDel () {
      this.delData()
    },
    handleChangeSort (row, sort) {
      network.getData('/article/category-sort', {id: row.ID, sort: sort}).then(response => {
        this.getData(this.currentPage, this.pageSize)
      }).catch(response => {
      })
    },
    getData (page, pageSize) {
      network.getPageData(this, 'article/category', page, pageSize, this.filterData, function (response) {
      })
    },
    delData (id = null) {
      let obj = this
      obj.$confirm('确定删除选定的数据？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let selectedIds = []
        if (id === null) {
          for (let val of obj.multipleSelection) {
            selectedIds.push(val.ID)
          }
        } else {
          selectedIds.push(id)
        }

        return network.postData(`article/category-delete`, {
          selected: selectedIds
        })
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        obj.getData(obj.currentPage, obj.pageSize)
      }).catch(response => {

      })
    },
  }
}

</script>

<style scoped>

</style>