<template>
  <app-container>
    <search-area @search="search">
      <search-item>
        <el-input v-model="listQuery.keywords" size="small" placeholder="设备IMEI号" clearable/>
      </search-item>
    </search-area>
    <normal-table ref="table" :data="list" :columns="tableColumns" :total="total" :query="listQuery" :list-loading="listLoading" @change="changePage" @selection-change="handleSelectionChange">
      <template slot="btns">
        <el-button v-if="hasPerm('/userDevice/add')" size="small" @click="add">新增</el-button>
      </template>
      <template slot="columns">
        <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')" size="small" type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button v-if="hasPerm('/userDevice/delete')" size="small" type="text" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <edit-user-device ref="editUserDevice" @watchChild="fetchData"/>
  </app-container>
</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'
        }
      ]
    }
  },
  created() {
    this.fetchData()
  },
  activated() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getUserDeviceList(this.listQuery).then(res => {
        if (res.code === 200) {
          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()
          }
        })
      })
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
