<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form size="mini" inline>
      <el-form-item label="版本号：">
        <el-input v-model="form.lastVersion" placeholder="请输入版本号" clearable />
      </el-form-item>
      <el-form-item label="升级系统：">
        <el-select v-model="form.appType" placeholder="请选择升级系统" clearable>
          <el-option v-for="item in systemOptions" :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="['push:update:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="升级标题" prop="updateTitle" align="center" show-overflow-tooltip />
      <el-table-column label="版本号" prop="lastVersion" align="center" />
      <el-table-column label="下载路径" prop="downUrl" align="center" show-overflow-tooltip />
      <el-table-column label="是否强制升级" prop="isForceUpdate" align="center" :formatter="typeFormatter" />
      <el-table-column label="升级系统" prop="appType" align="center" :formatter="appTypeFormatter" />
      <el-table-column label="更新时间" prop="updateTime" 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 type="text" @click="getDetail(scope.row.id)" v-hasPermi="['push:update:edit']">编辑</el-button>
          <el-button type="delete" @click="getDelete(scope.row)" v-hasPermi="['push:update: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, remove } from '@/api/push/update'
import { systemMixin } from '@/mixin'
export default {
  mixins: [systemMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 列表
      tableData: [],
      // 总数据
      total: 0,
      // 是否强制
      typeOptions: [{
        value: 0,
        label: '否'
      }, {
        value: 1,
        label: '是'
      }]
    }
  },
  mounted() {
    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: '/push/update/detail',
        query: {
          id: id,
          boolean: boolean
        }
      })
    },

    // 删除
    getDelete(row) {
      this.$confirm(`是否删除${row.updateTitle}?`, '提示', {
        type: 'warning'
      }).then(() => {
        remove(row.id, 1).then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      }).catch(() => { })
    },

    // 字典翻译
    typeFormatter(row) {
      return this.selectDictLabel(this.typeOptions, row.isForceUpdate)
    },

    appTypeFormatter(row) {
      return this.selectDictLabel(this.systemOptions, row.appType)
    }
  }
}
</script>

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