<template>
  <div v-loading="loading">
    <div class="white-box">
      <div class="filter-box">
        <filter-user :filter-types="filterTypes" @select-value="handleFilterUser"></filter-user>
      </div>

      <el-table :data="tableData" stripe style="width: 100%;" @selection-change="handleSelectionChange" :height="tool.getTableHeight()">
        <el-table-column type="selection" width="55" v-if="tableHeaders"></el-table-column>
        <el-table-column v-for="(tableHeader, key) in tableHeaders" :key="key" :label="tableHeader.header" :width="tableHeader.other.width ? tableHeader.other.width : ''">
          <template slot-scope="scope">
            <template v-if="scope.row[tableHeader.index].other.tag" >
              <el-tag :type="scope.row[tableHeader.index].other.tag.type ? scope.row[tableHeader.index].other.tag.type : null" :size="scope.row[tableHeader.index].other.tag.size ? scope.row[tableHeader.index].other.tag.size : null" :class="scope.row[tableHeader.index].other.tag.class ? scope.row[tableHeader.index].other.tag.class : null" ><div v-html="scope.row[tableHeader.index].value"></div></el-tag>
            </template>
            <template v-else>
              <div v-html="scope.row[tableHeader.index].value"></div>
            </template>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180" v-if="tableHeaders">
          <template slot-scope="scope">
            <el-dropdown size="small" trigger="click" v-show="permission.hasPermission(`user/system-edit`)||permission.hasPermission(`user/system-delete`)||permission.hasPermission(`user/system-renew`)">
              <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="handleUpdate(scope.row)" v-show="permission.hasPermission(`user/system-edit`)">编辑资料</el-dropdown-item>
                <el-dropdown-item v-show="scope.row.IS_DEL!=='1' && permission.hasPermission(`user/system-delete`)" command="delete" @click.native="delData(scope.row.ID)">撤销</el-dropdown-item>
                <el-dropdown-item v-show="scope.row.IS_DEL==='1' && permission.hasPermission(`user/system-renew`)" command="renew" @click.native="handleRenew(scope.row.ID)" >恢复</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" @command="handleMuliDel" v-show="permission.hasPermission(`user/system-delete`)">
          <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">撤销</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-button type="primary" size="small" @click="handleAdd" icon="el-icon-plus" v-show="permission.hasPermission(`user/system-add`)">添加体系</el-button>
        <el-button type="success" size="small" @click="handleExport" v-show="permission.hasPermission(`user/system-export`)">导出Excel</el-button>
        <pagination :total="totalCount" :page_size="pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange"></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'
import FilterUser from '@/components/FilterUser'
import permission from '@/utils/permission'
import Pagination from '@/components/Pagination'
import filterHelper from '../../utils/filterHelper'

export default {
  name: 'user_system',
  components: {FilterUser,Pagination},
  created () {

  },
  mounted () {
    this.getData()
    store.state.socket.onMessageCallback = this.onMessageCallback
  },
  data () {
    return {
      tableHeaders: null,
      tableData: null,
      loading: true,
      multipleSelection: [],
      currentPage: 1,
      totalPages: 1,
      totalCount: 1,
      pageSize: 20,
      tool: tool,
      permission: permission,
      filterTypes: {},
      filterModel: {},
    }
  },
  methods: {
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleCurrentChange (page) {
      this.getData(page, this.pageSize)
    },
    handleSizeChange (pageSize) {
      this.getData(this.currentPage, pageSize)
    },
    handleMuliDel (command) {
      if (command === 'delete') {
        this.delData()
      }
    },
    handleAdd () {
      this.$router.push({path: `/user/system-add`})
    },
    handleUpdate (row) {
      this.$router.push({path: `/user/system-edit/${row.ID}`})
    },
    handleRenew (id){
      let obj = this
      this.$confirm('确定恢复该体系？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return network.postData(`user/system-renew`, {
          id: id
        })
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        this.getData(obj.currentPage, obj.pageSize)
      }).catch(response => {

      })
    },
    handleFilterUser(filterData){
      filterHelper.handleFilterUser(this, filterData)
    },
    handleFilter(){
      this.getData()
    },
    getData (page, pageSize, isLoading = true) {
      network.getPageData(this, 'user/system', page, pageSize, this.filterModel, response => {
        this.filterTypes = response.filterTypes
      }, isLoading)
    },
    delData (id = null) {
      let obj = this
      this.$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(`user/system-delete`, {
          selected: selectedIds
        })
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        this.getData(obj.currentPage, obj.pageSize)
      }).catch(response => {

      })
    },
    handleExport(){
      this.$confirm(`确定要导出当前数据吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return network.getData(`user/system-export`, this.filterModel)
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
      }).catch(response => {

      })
    },
    onMessageCallback(data) {
      //this.getData(this.currentPage, this.pageSize, false)
    },
  }
}

</script>

<style scoped>

</style>
