<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-select
              v-model="listQuery.modelName"
              placeholder="设备类型"
              clearable
            >
              <el-option
                v-for="item in deviceModelList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item class="selectForm-container-item">
            <el-select
              v-model="listQuery.communication"
              placeholder="通信方式"
              clearable
            >
              <el-option
                v-for="item in commuList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </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">
          <!-- 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/deviceModel/components/editDeviceModel'
import { getDeviceModelList, getDevice, delDeviceModel } from '@/api/device/deviceModel'

export default {
  name: 'DeviceModel',
  components: { editDevice },
  data() {
    return {
      listQuery: {
        modelName: '',
        communication: '',
        offset: 1,
        sort: 'id',
        order: 'asc'
      }, // 查询条件
      commuList: [
        {
          label: 'LORA',
          value: '0'
        },
        {
          label: '3G',
          value: '1'
        },
        {
          label: '4G',
          value: '2'
        },
        {
          label: '联通移动NB',
          value: '3'
        },
        {
          label: '电信NB',
          value: '4'
        }
      ],
      columns: [
        {
          text: '设备型号编号',
          value: 'id',
          align: 'center'
        },
        {
          text: '型号',
          value: 'modelName',
          align: 'center'
        },
        {
          text: '设备类型',
          value: 'deviceTypeName',
          align: 'center'
        },
        {
          text: '通信方式',
          value: 'communication',
          align: 'center'
        }
      ], // 显示列
      multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      deviceModelList: [], // 设备类型列表
      listLoading: true // 加载动画
    }
  },
  created() {
    this.fetchDeviceType()
    this.fetchData()
  },
  methods: {
    // 获取设备类型
    fetchDeviceType() {
      getDevice(this.listQuery).then(response => {
        this.deviceModelList = response.data
      })
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 数据列表
    fetchData() {
      this.listLoading = true
      getDeviceModelList(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(() => {
          delDeviceModel(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)
    }
  }
}
</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>
