<template>
  <app-container>
    <!--筛选条件-->
    <search-area size="small" @search="search">
      <search-item>
        <el-input
          v-model="listQuery.devcode"
          size="small"
          type="text"
          placeholder="设备编号"
          clearable
        />
      </search-item>
    </search-area>
    <!--查询结果Table显示-->
    <normal-table ref="table" :data="list" :total="total" :query="listQuery" :columns="columns" :list-loading="listLoading" :pagination="true" :options="tableOptions" size="small" @change="changePage">
      <template slot="btns">
        <el-upload
          v-if="hasPerm('/device/simManage/batchimport')"
          :limit="1"
          :show-file-list="false"
          :http-request="uploadFile"
          :file-list="fileList"
          action="string"
          accept=".xls,.xlsx"
          style="display: inline-block"
        >
          <el-button slot="trigger" size="small" class="edit_btn">
            批量导入
          </el-button>
        </el-upload>
        <el-button v-if="hasPerm('/device/simManage/add')" size="small" class="edit_btn" @click="add">
          新增
        </el-button>
      </template>
      <template slot="columns">
        <el-table-column v-if="hasPerm('/device/simManage/update') || hasPerm('/device/simManage/delete')" label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button v-if="hasPerm('/device/simManage/update')" type="text" @click="edit(scope.row)">
              编辑
            </el-button>
            <el-button v-if="hasPerm('/device/simManage/delete')" size="small" type="text" @click="del(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <edit-device ref="editDevice" @watchChild="fetchData" />
  </app-container>
</template>

<script>
import editDevice from '@/views/deviceManage/deviceImei/components/editDeviceImei'
import { getDeviceImeiList, delDeviceImei, batchImportDevice } from '@/api/device/deviceImei'

export default {
  name: 'DeviceImei',
  components: { editDevice },
  data() {
    return {
      listQuery: {
        devcode: '',
        offset: 1,
        limit: 20,
        sort: 'id',
        order: 'asc'
      }, // 查询条件
      columns: [
        { text: '设备编号', value: 'devcode', align: 'center' },
        { text: 'IMEI', value: 'imei', align: 'center' },
        { text: 'ICCID', value: 'iccid', align: 'center' },
        { text: '记录时间', value: 'logtime', align: 'center' }
      ], // 显示列
      tableOptions: {
        needIndex: false, // 是否需要序号列
        border: true // 是否需要上方边框
      }, // 配置项
      fileList: [],
      multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      total: 0,
      listLoading: true // 加载动画
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 数据列表
    fetchData() {
      this.listLoading = true
      getDeviceImeiList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // table表格每行多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 检查选择情况
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        return false
      } else {
        return true
      }
    },
    // 删除设备
    del() {
      if (this.checkSelection()) {
        const deviceIds = []
        const deviceIndex = []
        this.multipleSelection.forEach(function(value, index) {
          deviceIds.push(value.id)
          deviceIndex.push(index)
        })
        this.$confirm(
          '确定要删除所选设备吗？',
          '确认操作',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          delDeviceImei(deviceIds).then(response => {
            if (response.code === 200) {
              this.$message.success('删除成功')
              this.fetchData()
            }
          })
        })
      } else {
        this.$message.error('至少选中一项')
      }
    },
    // 详情
    detail(row) {
      this.$refs.editDevice.initDialog('detail', row)
    },
    // 新增设备
    add() {
      this.$refs.editDevice.initDialog('create')
    },
    // 编辑设备信息
    edit(row) {
      this.$refs.editDevice.initDialog('update', row)
    },
    // 批量导入集中器
    uploadFile(param) {
      // 判断文件大小是否符合要求
      const _file = param.file
      const isLt5M = _file.size / 1024 / 1024 < 5
      if (!isLt5M) {
        this.$message.error('请上传5M以下的excel文件')
        return false
      }
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '导入中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      // 发起导入请求
      batchImportDevice(_file).then(res => {
        loading.close() // 关闭加载动画
        if (res.code === 200) {
          this.$message.success('导入成功')
          this.fetchData()
        } else {
          this.$message.error(res.message)
        }
      }).catch(() => {
        loading.close() // 关闭加载动画
      })
      this.fileList = []
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    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>
  $tableTitleHeight:46px;
  .table{
    margin-bottom: 20px;
  }
  .pagination-container{
    margin-bottom: 50px;
  }
  .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: 0px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
</style>
