<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form :model="form" inline size="mini">
      <el-form-item label="升级标题：">
        <el-input v-model="form.title" 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>
        <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:version:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column prop="id" label="型号ID" align="center" />
      <el-table-column prop="title" label="升级标题" align="center" show-overflow-tooltip />
      <el-table-column prop="deviceTypeName" label="设备名称" align="center" show-overflow-tooltip />
      <el-table-column prop="listType" label="设备类型" align="center" :formatter="typeFormatter" />
      <el-table-column prop="version" label="版本号" align="center" />
      <el-table-column label="下载路径" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link :href="scope.row.url" :underline="false">{{ scope.row.url }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="userTypeValue" label="升级对象" align="center" />
      <el-table-column prop="enforcementValue" label="APP强制升级" align="center" />
      <el-table-column prop="deviceIsEnforcement" label="设备强制升级" align="center" />
      <el-table-column prop="updateTimeStr" label="更新时间" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getChange(scope.row.id)" v-hasPermi="['device:version:edit']">
            编辑
          </el-button>
          <el-button type="delete" @click="getDelete(scope.row.id)" v-hasPermi="['device:version:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="total>0" :total="total" :page.sync="form.pageIndex" :limit.sync="form.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { getPageList, getVersionDelete } from '@/api/device/version'
import { devMixin, devTypeMixin } from '@/mixin/index'
export default {
  mixins: [devMixin, devTypeMixin],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 表单
      form: {
        pageIndex: 1,
        pageSize: 10
      },
      // 列表
      tableData: [],
      // 总数
      total: 0,
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      getPageList(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = []
          res.data.records.forEach(i => {
            this.tableData.push({
              ...i,
              deviceIsEnforcement: i.deviceIsEnforcement ? i.deviceIsEnforcement : '否',
              enforcementValue: i.enforcementValue ? i.enforcementValue : '否'
            })
          })
          this.total = res.data.total
          this.loading = false
        }
      })
    },
    // 搜索
    getSearch() {
      this.form.pageIndex = 1
      this.getList()
    },
    // 重置
    getRefresh() {
      this.form = {
        pageIndex: 1,
        pageSize: 10
      }
      this.getList()
    },
    // 新增
    getChange(id) {
      this.$router.push({
        path: `/device/version/detail`,
        query: {
          id: id
        }
      })
    },
    // 删除
    getDelete(id) {
      this.loading = true
      getVersionDelete({ id: id }).then(res => {
        if (res.code === 0) {
          this.$message.success('删除成功！')
          this.getList()
          this.loading = false
        }
      })
    },

    // 字典翻译
    typeFormatter(row) {
      return row.listType.map(i => this.selectDictLabel(this.typeOptions, i)).join(',')
    }
  }
}
</script>