<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.status" placeholder="请选择当前状态" clearable>
          <el-option v-for="item in disabledOptions" :key="item.value" :value="item.value"
            :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="兑换类型：">
        <el-select v-model="form.type" placeholder="请选择奖品类型" clearable>
          <el-option v-for="item in rewardOptions" :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" plain icon="el-icon-plus" @click="getDetail()">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="兑换标题" prop="name" align="center" />
      <el-table-column label="所需积分" prop="exchangePoint" align="center" />
      <el-table-column label="兑换图片" align="center" width="100px">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic" />
        </template>
      </el-table-column>
      <el-table-column label="兑换类型" prop="type" align="center" :formatter="typeFormatter" />
      <el-table-column label="当前状态" prop="status" align="center" :formatter="statusFormatter" />
      <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>
          <span v-if="scope.row.status === 1" style="margin: 0 10px">
            <el-button type="text" @click="getDetail(scope.row.id)">编辑</el-button>
            <el-button type="text" @click="getChange(scope.row, 0)">启用</el-button>
            <el-button type="delete" @click="getDelete(scope.row)">删除</el-button>
          </span>
          <el-button v-else type="delete" @click="getChange(scope.row, 1)">禁用</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 { disabledMixin } from '@/mixin/index'
import { list, change, remove } from '@/api/registration/exchange'
export default {
  mixins: [disabledMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 总数据
      total: 0,
      // 列表
      tableData: [],
      // 奖品类型
      rewardOptions: [{
        value: 0,
        label: '音乐套餐'
      }, {
        value: 1,
        label: '流量套餐'
      }, {
        value: 3,
        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
        }
      })
    },

    // 新增 编辑 查看
    getDetail(id, boolean) {
      this.$router.push({
        path: '/registration/exchangeConfig/detail',
        query: {
          id: id,
          disabled: boolean
        }
      })
    },

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

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

    // 上下架
    getChange(row, status) {
      let title = status === 0 ? '启用' : '禁用'
      this.$confirm(`是否${title}${row.name}?`, '提示', {
        type: 'warning'
      }).then(() => {
        change({
          id: row.id,
          status: status
        }).then(res => {
          if (res.code === 0) {
            this.$message.success(`${title}成功！`)
            this.getList()
          }
        })
      }).catch(() => { })
    },

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

    // 字典翻译
    statusFormatter(row) {
      return this.selectDictLabel(this.disabledOptions, row.status)
    },
    typeFormatter(row) {
      return this.selectDictLabel(this.rewardOptions, row.type)
    }
  }
}
</script>