<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form inline size="mini">
      <el-form-item label="设备名称：">
        <el-input v-model="form.name" placeholder="请输入设备名称" clearable />
      </el-form-item>
      <el-form-item label="设备型号：">
        <el-select v-model="form.clientTypeId" placeholder="请选择设备型号" filterable clearable>
          <el-option v-for="item in devOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备分类：">
        <el-select v-model="form.categoryId" placeholder="请选择设备分类" clearable>
          <el-option v-for="item in categoryOptions" :key="item.id" :value="item.id" :label="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item label="当前状态：">
        <el-select v-model="form.status" placeholder="请选择当前状态" clearable>
          <el-option v-for="item in onOrOffOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否热门：">
        <el-select v-model="form.isHot" placeholder="请选择是否热门" clearable>
          <el-option v-for="item in hotOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </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="bluetoothName" 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="deviceType" align="center" :formatter="deviceTypeFormatter" />
      <el-table-column label="是否热门" prop="isHot" align="center" :formatter="hotFormatter" />
      <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="getChange(scope.row.id)"
            v-hasPermi="['device:list:edit']">
            编辑
          </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 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 { List, Delete, upDown } from '@/api/device/list'
import { devMixin, onOrOffMixin } from '@/mixin/index'
import { categoryList } from "@/api/device/category";
export default {
  mixins: [devMixin, onOrOffMixin],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 表格
      tableData: [],
      // 总数据
      total: 0,
      // 搜索表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 批量上下架
      changeForm: {
        ids: '',
        type: null
      },
      // 是否热门
      hotOptions: [{
        value: 0,
        label: '否'
      }, {
        value: 1,
        label: '是'
      }],
      deviceTypeOptions: [{
        value: 0,
        label: '音响'
      }, {
        value: 1,
        label: '耳机'
      }, {
        value: 2,
        label: '穿戴'
      }],
      // 设备分类
      categoryOptions: []
    }
  },
  mounted() {
    this.getList()
    this.getCategory()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      List(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
    },

    // 设备分类
    getCategory() {
      categoryList().then(res => {
        this.categoryOptions = res.data
      })
    },

    // 上下架
    getShelves(key, e) {
      if (e) {
        this.changeForm.ids = e
      }
      let title = key === 1 ? '上架' : '下架'
      this.changeForm.type = key
      upDown(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(() => {
        Delete(id).then(res => {
          if (res.code === 0) {
            this.getList()
            this.$message.success('删除成功！')
          }
        })
      }).catch(() => { })
    },

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

    deviceTypeFormatter(row) {
      return this.selectDictLabel(this.deviceTypeOptions, row.deviceType)
    }
  }
}
</script>