<template>
  <div class="table-container">
    <el-row>
      <el-form :model="listQuery" :inline="true" label-width="auto">
        <el-form-item label="设备IMEI号:">
          <el-input v-model="listQuery.keywords" clearable/>
        </el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
      </el-form>
    </el-row>
    <el-row class="table-title">
      <el-col :span="6"><div class="title-header"><i class="el-icon-menu"/>数据列表</div></el-col>
      <el-col :span="12" :offset="6" class="edit_btns">
        <el-button v-if="hasPerm('/userDevice/add')" class="edit_btn" size="small" @click="add">新增</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="listLoading" ref="table" :data="list" class="table" border>
      <el-table-column :index="indexMethod" align="center" type="index" />
      <el-table-column v-for="column in tableColumns" :key="column.value" :label="column.text" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row[column.value] }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="hasPerm('/userDevice/update')||hasPerm('/userDevice/delete')" label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button v-if="hasPerm('/userDevice/update')" class="table-button" type="text" @click="edit(scope.row)">编辑</el-button>
          <el-button v-if="hasPerm('/userDevice/delete')" class="table-button" type="text" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        :current-page="listQuery.offset"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        :total="total"
        align="center"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
    <edit-user-device ref="editUserDevice" @watchChild="fetchData"/>
  </div>
</template>

<script>
import { getUserDeviceList, deleteUserDevice } from '@/api/busAdmin/userDevice'
import EditUserDevice from '@/views/busAdmin/userDevice/editUserDevice.vue'

export default {
  name: 'UserDevice',
  components: { EditUserDevice },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        keywords: '',
        limit: 10,
        offset: 1,
        sort: 'id',
        order: 'asc'
      },
      tableColumns: [
        {
          text: '设备名称',
          value: 'deviceName'
        },
        {
          text: '设备IMEI号',
          value: 'imei'
        },
        {
          text: '设备入网号',
          value: 'networkNumber'
        },
        {
          text: '用户名称',
          value: 'name'
        },
        {
          text: '设备最后在线时间',
          value: 'lastTimeFmt'
        }
      ]
    }
  },
  activated() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      this.listLoading = true
      const res = await getUserDeviceList(this.listQuery)
      this.list = res.data.rows
      this.total = parseInt(res.data.total)
      this.listLoading = false
    },
    search() {
      this.listQuery.offset = 1
      this.fetchData()
    },
    add() {
      console.log(this.$refs.editUserDevice)
      this.$refs.editUserDevice.initDialog(true, 'create')
    },
    edit(row) {
      this.$refs.editUserDevice.initDialog(true, 'update', row)
    },
    del(row) {
      this.$confirm(
        '确定要删除所选类别吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        deleteUserDevice(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    indexMethod(index) {
      return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      this.fetchData()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight:46px;
  .table{
    margin-bottom: 20px;
  }
  .table-container{
    margin: 20px;
  }
  .table-title{
    background-color:rgba(243, 243, 243, 1);
    height: $tableTitleHeight;
    .title-header{
      line-height:$tableTitleHeight;
      color: #606266;
      font-size: 15px;
      i{
        margin-left: 5px;
        margin-right: 5px;
      }
    }
  }
  .edit_btns{
    .edit_btn{
      float:right;
      margin:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
  .table-button{
    padding: 10px;
    height: 23px;
  }
</style>
