<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" >{{scope.row[tableHeader.index].value}}</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/bind-edit`)||permission.hasPermission(`user/bind-delete`)">
              <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="handleUpdate(scope.row)" v-show="permission.hasPermission(`user/bind-edit`)">编辑绑定关系</el-dropdown-item>
                <el-dropdown-item command="delete" @click.native="delData(scope.row.ID)" v-show="permission.hasPermission(`user/bind-delete`)">撤销绑定关系</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/bind-delete`)">
          <el-button type="primary" size="small">
            Selected data<!--所选数据--><i class="el-icon-arrow-down el-icon&#45;&#45;right"></i>
            <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/bind-add`)">添加点位绑定</el-button>
        <el-button type="success" size="small" @click="handleExport" v-show="permission.hasPermission(`user/bind-export`)">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_bind',
  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: null,
      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/bind-add`})
    },
    handleUpdate (row) {
      this.$router.push({path: `/user/bind-edit/${row.ID}`})
    },
    handleFilterUser(filterData){
      filterHelper.handleFilterUser(this, filterData)
    },
    handleFilter(){
      this.getData()
    },
    getData (page, pageSize, isLoading = true) {
      network.getPageData(this, 'user/bind', page, pageSize, this.filterModel, response => {
        this.filterTypes = response.filterTypes
      }, isLoading)
    },
    delData (id = null) {
      let obj = this
      this.$confirm('确定撤销选定的数据？', '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(`user/bind-delete`, {
          selected: selectedIds
        })
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        obj.getData(obj.currentPage, obj.pageSize)
      }).catch(response => {

      })
    },
    handleExport(){
      this.$confirm(`Are you sure you want to export the current data？`, 'Hint', {
        confirmButtonText: 'confirm', // 确定
        cancelButtonText: 'cancel', // 取消
        type: 'warning'
      }).then(() => {
        return network.getData(`user/bind-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>
