<template>
  <div class="app-container">
    <!--筛选条件-->
    <div class="search-div">
      <el-form
        ref="selectForm"
        :inline="true"
        :model="listQuery"
        class="form-container"
      >
        <el-row>
          <el-form-item class="selectForm-container-item">
            <el-input
              v-model="listQuery.devcode"
              type="text"
              placeholder="设备编号"
              clearable
            />
          </el-form-item>
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="search"
          >
            搜索
          </el-button>
        </el-row>
      </el-form>
    </div>
    <!--查询结果Table显示-->
    <div>
      <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-upload
            v-if="hasPerm('/device/batchImport')"
            :limit="1"
            :show-file-list="false"
            :http-request="uploadFile"
            :file-list="fileList"
            action="string"
            accept=".xls,.xlsx"
            class="edit_btn"
          >
            <el-button slot="trigger" size="small">
              批量导入
            </el-button>
          </el-upload>
          <!-- v-if="hasPerm('/device/delete')" -->
          <el-button
            class="edit_btn"
            size="small"
            @click="del"
          >
            删除
          </el-button>
          <el-button
            class="edit_btn"
            size="small"
            @click="add"
          >
            新增
          </el-button>
        </el-col>
      </el-row>
      <el-table
        v-loading="listLoading"
        :data="list"
        class="table"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          align="center"
          type="selection"
          width="55"
        />
        <!-- <el-table-column
          align="center"
          type="index"
          label="序号"
          width="55"
        /> -->
        <el-table-column
          v-for="column in columns"
          :key="column.value"
          :label="column.text"
          :width="column.width"
          :align="column.align"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{ scope.row[column.value] }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="160"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="edit(scope.row)">
              编辑
            </el-button>
            <!-- <el-button type="text" @click="detail(scope.row)">
              详情
            </el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <edit-device ref="editDevice" @watchChild="fetchData" />
  </div>
</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,
        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'
        }
      ], // 显示列
      fileList: [],
      multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      listLoading: true // 加载动画
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 数据列表
    fetchData() {
      this.listLoading = true
      getDeviceImeiList(this.listQuery).then(response => {
        this.list = response.data
        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 = []
    }
  }
}
</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:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
</style>
