<template>
  <app-container>
    <!--筛选条件-->
    <search-area size="small" @search="search">
      <search-item>
        <el-select v-model="listQuery.deviceType" size="small" placeholder="设备类型" clearable>
          <el-option
            v-for="item in deviceTypeList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.communication" size="small" placeholder="通信方式" clearable>
          <el-option
            v-for="item in commuList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </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-button class="edit_btn" size="small" @click="add">
          新增
        </el-button>
      </template>
      <template slot="columns">
        <el-table-column label="操作" align="center" width="160">
          <template v-slot="scope">
            <el-button type="text" size="mini" @click="edit(scope.row)">
              编辑
            </el-button>
            <el-button type="text" size="mini" @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/deviceModel/components/editDeviceModel'
import { getDeviceModelList, delDeviceModel } from '@/api/device/deviceModel'
import { getDictByCode } from '@/api/system/dict'
import { getDeviceType } from '@/api/device/device'

export default {
  name: 'DeviceModel',
  components: { editDevice },
  data() {
    return {
      listQuery: {
        deviceType: '', // 设备类型
        communication: '', // 通信方式
        offset: 1,
        limit: 30,
        sort: 'id',
        order: 'asc'
      }, // 查询条件
      total: 0, // 数据总数
      commuList: [],
      deviceTypeList: [],
      columns: [
        // {
        //   text: '设备型号编号',
        //   value: 'id',
        //   align: 'center',
        //   width: '70px'
        // },
        {
          text: '设备类型',
          value: 'deviceTypeName',
          align: 'center'
        },
        {
          text: '设备型号',
          value: 'modelName',
          align: 'center'
        },
        {
          text: '通信方式',
          value: 'communNameGroup',
          align: 'center'
        }
      ], // 显示列
      list: [], // 列表数据
      listLoading: true // 加载动画
    }
  },
  created() {
    this.fetchDeviceType()
    this.fetchCommunType()
    this.fetchData()
  },
  methods: {
    // 获取通信方式列表
    fetchCommunType() {
      getDictByCode('communicationType').then(response => {
        this.commuList = response.data
      })
    },
    // 获取设备类型
    fetchDeviceType() {
      getDeviceType(this.listQuery).then(response => {
        this.deviceTypeList = response.data
      })
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 数据列表
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getDeviceModelList(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        this.listLoading = false
      })
    },
    // 删除设备
    del(row) {
      this.$confirm(
        '确定要删除所选设备型号吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delDeviceModel(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    // 详情
    detail(row) {
      this.$refs.editDevice.initDialog('detail', row)
    },
    // 新增设备
    add() {
      this.$refs.editDevice.initDialog('create')
    },
    // 编辑设备信息
    edit(row) {
      this.$refs.editDevice.initDialog('update', row)
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    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>
