<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form inline label-width="100px" size="mini" @submit.native.prevent>
      <el-form-item label="设备名称：">
        <el-input v-model="form.name" placeholder="请输入设备名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getSearch">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="getRefresh">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" plain @click="getChange()">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="设备ID" prop="id" align="center" />
      <el-table-column label="设备型号" prop="clientType" align="center" />
      <el-table-column label="设备名称" prop="name" align="center" />
      <el-table-column label="设备图片" prop="img" align="center" width="100">
        <template slot-scope="scope">
          <el-image :src="scope.row.img" :preview-src-list="[scope.row.img]" fit="scale-down" />
        </template>
      </el-table-column>
      <el-table-column label="使用分类" prop="deviceType" align="center" :formatter="deviceTypeFormatter" />
      <el-table-column label="升级方式" prop="upgradeTypeText" align="center" />
      <el-table-column label="创建时间" prop="createTimeText" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getChange(scope.row.id, '查看')">查看</el-button>
          <el-button type="text" @click="getChange(scope.row.id)">编辑</el-button>
          <el-button type="delete" @click="getDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { listPage, deviceDelete } from '@/api/device/list'
import mixin from './mixin/index'
export default {
  mixins: [mixin],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 表格
      tableData: [],
      // 总数据
      total: 0,
      // 表单
      form: {
        name: '',
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      listPage(this.form).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
        this.loading = false
      })
    },

    // 搜索
    getSearch() {
      this.form.pageNum = 1
      this.form.pageSize = 10
      this.getList()
    },

    // 重置
    getRefresh() {
      this.form = {
        name: '',
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },

    // 新增 / 编辑 / 查看
    getChange(id, key) {
      this.$router.push({
        path: `/device/list/detail`,
        query: {
          id: id,
          key: key
        }
      })
    },

    // 删除
    getDelete(id) {
      this.$confirm('确定要删除?', {
        type: 'warning'
      }).then(() => {
        deviceDelete(id).then(res => {
          if (res.data.code === 0) {
            this.getList()
            this.$message.success('删除成功！')
          }
        })
      }).catch(() => { })
    },

    // 字典翻译
    deviceTypeFormatter(row) {
      return this.selectDictLabel(this.deviceTypeOptions, row.deviceType);
    }
  }
}
</script>

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