<template>
  <div class="app-container">
    <el-form size="mini">
      <el-form-item>
        <el-button type="primary" icon="el-icon-plus" @click="getDetail()"
          v-hasPermi="['registration:lottery:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData">
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="活动名称" prop="name" align="center" />
      <el-table-column label="活动时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.startTime }} - {{ scope.row.endTime }}
        </template>
      </el-table-column>
      <el-table-column label="活动状态" prop="currentStatus" align="center" :formatter="currentFormatter" />
      <el-table-column label="当前状态" prop="status" align="center" :formatter="statusFormatter" />
      <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)"
              v-hasPermi="['registration:lottery:edit']">编辑</el-button>
            <el-button type="text" @click="getChange(scope.row, 0)"
              v-hasPermi="['registration:lottery:up']">上架</el-button>
            <el-button type="delete" @click="getDelete(scope.row)"
              v-hasPermi="['registration:lottery:delete']">删除</el-button>
          </span>
          <el-button v-else type="text" @click="getChange(scope.row, 1)"
            v-hasPermi="['registration:lottery:down']">下架</el-button>
          <el-button type="text" @click="getCopy(scope.row.id)" v-hasPermi="[`registration:lottery:copy`]">复制</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { list, change, remove } from '@/api/registration/lottery'
import { disabledMixin, currentMixin } from '@/mixin/index'
export default {
  mixins: [disabledMixin, currentMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 总数据
      total: 0,
      // 列表
      tableData: []
    }
  },
  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/lotteryConfig/detail`,
        query: {
          id: id,
          disabled: boolean
        }
      })
    },

    // 上下架
    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(() => { })
    },

    // 复制
    getCopy(id) {
      this.$router.push({
        path: `/registration/lotteryConfig/detail`,
        query: {
          copyId: id
        }
      })
    },

    // 字典翻译
    statusFormatter(row) {
      return this.selectDictLabel(this.disabledOptions, row.status)
    },
    currentFormatter(row) {
      return this.selectDictLabel(this.currentOptions, row.currentStatus)
    }
  }
}
</script>