<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="ID" prop="ID">
          <template slot-scope="scope">
            <router-link :to="`/article/detail/${scope.row.ID}`" target="_blank" class="islide">
              <span>{{ scope.row.ID }}</span>
            </router-link>
          </template>
        </el-table-column>
        <el-table-column label="Title" prop="TITLE">
          <template slot-scope="scope">
            <router-link :to="`/article/detail/${scope.row.ID}`" target="_blank" class="islide">
              <span>{{ scope.row.TITLE }}</span>
            </router-link>
          </template>
        </el-table-column><!--标题-->
        <el-table-column label="Category"><!--分类-->
          <template slot-scope="scope">
            {{allData.allCategory[scope.row.CID].CATE_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="Sort" 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="Creation Time"><!--创建时间-->
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.CREATED_AT)}}
          </template>
        </el-table-column>
        <el-table-column label="Status"> <!-- 状态 -->
          <template slot-scope="scope">
            <div v-if="scope.row.STATUS === '1'">Show</div>
            <div v-else>Hide</div>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="Action" width="180"><!--操作-->
          <template slot-scope="scope">
            <el-dropdown size="small" trigger="click">
              <el-button type="primary" size="small" @click.stop="">
                Action<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="edit" @click.native="handleEdit(scope.row)">Edit</el-dropdown-item>
                <el-dropdown-item command="delete" @click.native="handleDelete(scope.row)">Delete</el-dropdown-item>
                <el-dropdown-item command="hide" @click.native="handleHide(scope.row)">Hide</el-dropdown-item>
                <el-dropdown-item command="un-hide" @click.native="handleUnHide(scope.row)">Unhide</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">
            Selected data<!--所选数据--><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()">Delete</el-dropdown-item>
            <el-dropdown-item command="hide" @click.native="handleMultiHide()">Hide</el-dropdown-item>
            <el-dropdown-item command="un-hide" @click.native="handleMultiUnHide()">Unhide</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-button type="primary" size="small" @click="handleAdd">New article</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_index',
  mounted () {
    this.getData()
  },
  data () {
    return {
      allData: null,
      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/add`})
    },
    handleEdit (row) {
      this.$router.push({path: `/article/edit/${row.ID}`})
    },
    handleDelete (row) {
      this.delData(row.ID)
    },
    handleHide (row) {
      this.hideData(row.ID)
    },
    handleUnHide(row) {
      this.unHideData(row.ID)
    },
    handleMuliDel () {
      this.delData()
    },
    handleMultiHide () {
      this.hideData()
    },
    handleMultiUnHide () {
      this.unHideData()
    },
    handleChangeSort (row, sort) {
      network.getData('/article/sort', {id: row.ID, sort: sort}).then(_ => {
        this.getData(this.currentPage, this.pageSize)
      })
    },
    getData (page, pageSize) {
      let obj = this
      network.getPageData(this, 'article/index', page, pageSize, this.filterData, function (response) {
        obj.allData = response
      })
    },
    delData (id = null) {
      let obj = this
      obj.$confirm('Are you sure to delete the selected data？', 'Hint', {// 确定删除选定的数据
        confirmButtonText: 'confirm', // 确定
        cancelButtonText: 'cancel', // 取消
        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/article-delete`, {
          selected: selectedIds
        })
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        obj.getData(obj.currentPage, obj.pageSize)
      }).catch(response => {

      })
    },
    hideData (id = null) {
      let obj = this
      obj.$confirm('Are you sure to hide the selected data？', 'Notice', { // 确定删除选定的数据？
        confirmButtonText: 'confirm', // 确定
        cancelButtonText: 'cancel', // 取消
        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/article-hide`, {
          selected: selectedIds
        })
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        obj.getData(obj.currentPage, obj.pageSize)
      }).catch(response => {

      })
    },
    unHideData (id = null) {
      let obj = this
      obj.$confirm('Are you sure to un-hide the selected data？', 'Notice', { // 确定删除选定的数据？
        confirmButtonText: 'confirm', // 确定
        cancelButtonText: 'cancel', // 取消
        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/article-un-hide`, {
          selected: selectedIds
        })
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        obj.getData(obj.currentPage, obj.pageSize)
      }).catch(response => {

      })
    },
  }
}

</script>

<style scoped>

</style>
