<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="请输入设备名称" clearable />
      </el-form-item>
      <el-form-item label="设备型号：">
        <el-input v-model="form.clientType" placeholder="请输入设备型号" clearable />
      </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()"
          v-hasPermi="['device:list:add']">
          新增
        </el-button>
        <el-button type="primary" icon="el-icon-top" @click="getShelves(1)" :disabled="isDisabled()"
          v-hasPermi="['device:list:up']">
          批量上架
        </el-button>
        <el-button type="primary" icon="el-icon-bottom" @click="getShelves(2)" :disabled="isDisabled()"
          v-hasPermi="['device:list:down']">
          批量下架
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="tableData" v-loading="loading" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" />
      <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" />
        </template>
      </el-table-column>
      <el-table-column label="设备类型" prop="categoryName" align="center" />
      <el-table-column label="是否热门" prop="isHot" align="center" :formatter="hotFormatter" />
      <el-table-column label="升级方式" prop="upgradeType" align="center" :formatter="upgradeTypeFormatter" />
      <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 v-if="scope.row.status === 2" type="text" @click="getShelves(1, scope.row.id)" v-hasPermi="['device:list:up']">
            上架
          </el-button>
          <el-button v-else type="text" @click="getShelves(2, scope.row.id)" v-hasPermi="['device:list:down']">
            下架
          </el-button>
          <el-button type="text" @click="getChange(scope.row.id)" v-hasPermi="['device:list:edit']">
            编辑
          </el-button>
          <el-button v-if="scope.row.status === 2" type="delete" @click="getDelete(scope.row.id)" v-hasPermi="['device:list:delete']">
            删除
          </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, batchChange } from '@/api/device/list'
export default {
  dicts: ['hot_yes_no', 'dev_list_version'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 表格
      tableData: [],
      // 总数据
      total: 0,
      // 搜索表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 批量上下架
      changeForm: {
        ids: '',
        type: null
      }
    }
  },
  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()
    },

    // 禁用上下架功能
    isDisabled() {
      return this.changeForm.ids === '' ? true : false
    },

    // 上下架
    getShelves(key, e) {
      if (e) {
        this.changeForm.ids = e
      }
      let title = key === 1 ? '上架' : '下架'
      this.changeForm.type = key
      batchChange(this.changeForm).then(res => {
        if (res.code === 0) {
          this.$message.success(`${title}成功！`)
          this.getList()
        }
      })
    },

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

    // 多选
    handleSelectionChange(e) {
      if (e.length > 0) {
        let arr = []
        e.filter(i => {
          arr.push(i.id)
          this.changeForm.ids = arr.join(',')
        })
      } else {
        this.changeForm.ids = ''
      }
    },

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

    // 字典翻译
    hotFormatter(row) {
      return this.selectDictLabel(this.dict.type.hot_yes_no, row.isHot)
    },

    upgradeTypeFormatter(row) {
      return this.selectDictLabel(this.dict.type.dev_list_version, row.upgradeType)
    }
  }
}
</script>

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