<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.categoryId" placeholder="请选择设备分类" clearable>
          <el-option v-for="item in categoryOptions" :key="item.value" :value="item.value" :label="item.label" />
        </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 yesOrNoOptions" :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="getDetail()"
          v-hasPermi="['device:class:add']">新增</el-button>
        <el-button type="primary" :disabled="ids === ''" @click="getChange(ids, 1, '批量上架')"
          v-hasPermi="['device:class:up']">批量上架</el-button>
        <el-button type="primary" :disabled="ids === ''" @click="getChange(ids, 2, '批量下架')"
          v-hasPermi="['device:class:down']">批量下架</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading" @selection-change="handleSelection">
      <el-table-column type="selection" align="center" />
      <el-table-column label="大类Id" prop="id" align="center" show-overflow-tooltip />
      <el-table-column label="大类名称" prop="name" align="center" />
      <el-table-column label="设备图片" align="center" width="100px">
        <template slot-scope="scope"><el-image :src="scope.row.img" /></template>
      </el-table-column>
      <el-table-column label="设备分类" prop="categoryId" align="center" :formatter="categoryIdFormatter" />
      <el-table-column label="设备类型" prop="type" align="center" :formatter="typeFormatter" />
      <el-table-column label="是否热门" prop="isHot" align="center" :formatter="isHotFormatter" />
      <el-table-column label="关联设备" prop="deviceIds" align="center" :formatter="deviceFormatter" show-overflow-tooltip />
      <el-table-column label="当前状态" prop="status" align="center" :formatter="statusFormatter" />
      <el-table-column label="更新时间" prop="updateTime" align="center" />
      <el-table-column label="创建时间" prop="createTime" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDetail(scope.row.id, true)">查看</el-button>
          <el-button v-if="scope.row.status === 1" type="text" @click="getChange(scope.row.id, 2, '下架')"
            v-hasPermi="['device:class:down']">下架</el-button>
          <span v-else style="margin-left: 10px;">
            <el-button type="text" @click="getDetail(scope.row.id)" v-hasPermi="['device:class:edit']">编辑</el-button>
            <el-button type="text" @click="getChange(scope.row.id, 1, '上架')"
              v-hasPermi="['device:class:up']">上架</el-button>
            <el-button type="delete" @click="getDelete(scope.row)" v-hasPermi="['device:class:delete']">删除</el-button>
          </span>
        </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, change } from '@/api/device/class'
import { devCategoryMixin, devMixin, onOrOffMixin, devTypeMixin, yesOrNoMixin } from '@/mixin/index'
export default {
  mixins: [devCategoryMixin, devMixin, onOrOffMixin, devTypeMixin, yesOrNoMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 总数据
      total: 0,
      // 列表
      tableData: [],
      // 批量上下架
      ids: ''
    }
  },
  mounted() {
    this.getCateGory(1)
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      list(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

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

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

    // 新增
    getDetail(id, boolean) {
      this.$router.push({
        path: '/device/class/detail',
        query: {
          id: id,
          boolean: boolean
        }
      })
    },

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

    // 上架 下架 删除
    getChange(ids, status, title) {
      change(ids, status).then(res => {
        if (res.code === 0) {
          this.$message.success(`${title}成功！`)
          this.getList()
        }
      })
    },

    // 删除
    getDelete(e) {
      this.$confirm(`是否删除${e.name}?`, '提示', {
        type: 'warning'
      }).then(() => {
        this.getChange(e.id, 0, '删除')
      })
    },

    // 字典翻译
    categoryIdFormatter(row) {
      return this.selectDictLabel(this.categoryOptions, row.categoryId)
    },
    typeFormatter(row) {
      return this.selectDictLabel(this.devTypeOptions, row.type)
    },
    isHotFormatter(row) {
      return this.selectDictLabel(this.yesOrNoOptions, row.isHot)
    },
    deviceFormatter(row) {
      return row.deviceIds.split(',').map(i => this.selectDictLabel(this.devOptions, i))
    },
    statusFormatter(row) {
      return this.selectDictLabel(this.onOrOffOptions, row.status)
    }
  }
}
</script>