<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item label="当前状态：">
        <el-select v-model="form.status" placeholder="请选择当前状态" clearable>
          <el-option v-for="item in currentOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="跳转页面：">
        <el-select v-model="form.forwardType" placeholder="请选择跳转页面" clearable>
          <el-option v-for="item in typeOptions" :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="getDetail"
          v-hasPermi="['operation:startPage:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData">
      <el-table-column label="名称" prop="name" align="center" />
      <el-table-column label="图片" align="center" width="50px">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic" :previewSrcList="[scope.row.pic]" />
        </template>
      </el-table-column>
      <el-table-column label="生效期" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <div>{{ scope.row.startTime }} 至 {{ scope.row.endTime }}</div>
        </template>
      </el-table-column>
      <el-table-column label="跳转" prop="forwardType" align="center" :formatter="forwardFomatter" />
      <el-table-column label="当前状态" prop="currentStatus" align="center" :formatter="currentFormatter" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDetail(scope.row, true)">查看</el-button>
          <el-button type="text" @click="getDetail(scope.row)" v-hasPermi="['operation:startPage:edit']">编辑</el-button>
          <el-button type="delete" @click="getDelete(scope.row)"
            v-hasPermi="['operation:startPage: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 { page, remove } from '@/api/operation/startPage'
import { currentMixin } from '@/mixin/index'
export default {
  mixins: [currentMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 列表
      tableData: [],
      total: 0,
      // 跳转类型
      typeOptions: [{
        value: 0,
        label: '指定页面'
      }, {
        value: 1,
        label: '指定URL'
      }]
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 搜索
    getSearch() {
      this.form.pageNum = 1
      this.form.pageSize = 10
      this.getList()
    },

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

    // 列表
    getList() {
      this.loading = true
      page(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

    // 新增 编辑
    getDetail(row, boolean) {
      this.$router.push({
        path: '/operation/startPage/detail',
        query: {
          id: row.id,
          boolean: boolean
        }
      })
    },

    // 删除
    getDelete(row) {
      this.$confirm(`是否删除${row.name}`, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove({
          id: row.id
        }).then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      }).catch(() => {
        this.$message.info('取消删除！')
      })
    },

    // 字典翻译
    forwardFomatter(row) {
      return this.selectDictLabel(this.typeOptions, row.forwardType)
    },
    currentFormatter(row) {
      return this.selectDictLabel(this.currentOptions, row.currentStatus)
    }
  }
}
</script>