<template>
  <div class="mod-seckill-seckill">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
    >
      <template slot-scope="scope" slot="menuLeft">
        <el-button icon="el-icon-plus" type="primary" size="small" @click="addOrUpdateHandle()">新增</el-button>
      </template>
      <template slot-scope="scope" slot="status">
        <el-tag v-if="scope.row.status === 0" size="small" type="warning">关闭</el-tag>
        <el-tag v-else-if="scope.row.status === 1" size="small">开启</el-tag>
        <el-tag v-if="scope.row.status === 2" size="small" type="danger">违规下线</el-tag>
        <el-tag v-if="scope.row.status === 3" size="small" type="warning">待审核</el-tag>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          icon="el-icon-view"
          size="small"
          type="primary"
          @click="addOrUpdateHandle(scope.row.seckillId)"
        >查看</el-button>
        <el-button
          type="danger"
          icon="el-icon-bottom"
          size="small"
          v-if="scope.row.status ===  1"
          @click="invalidHandle(scope.row.seckillId)"
        >关闭</el-button>
        <el-button
          type="primary"
          icon="el-icon-document-checked"
          size="small"
          v-if="scope.row.status > 1"
          @click="auditEventHandle(scope.row.seckillId)"
        >{{scope.row.status === 2?'申请上架':'等待审核'}}</el-button>
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="small"
          :disabled="scope.row.status > 1"
          @click="deleteHandle(scope.row.seckillId)"
        >删除</el-button>
      </template>
    </avue-crud>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
    <!-- 下线管理弹窗-->
    <offlineEventHandle
      v-if="offlineEventHandleVisible"
      selectUrl="/seckill/seckill/getOfflineHandleEventBySeckillId"
      applyUrl="/seckill/seckill/auditApply"
      ref="offlineEvent"
      @refreshDataList="refreshChange"
    ></offlineEventHandle>
  </div>
</template>

<script>
import { tableOption } from '@/crud/seckill/seckill'
import AddOrUpdate from './seckill-add-or-update'
import OfflineEventHandle from '@/components/offline-event-handle'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {},
      dataListLoading: false,
      tableOption: tableOption,
      offlineEventHandleVisible: false,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate,
    OfflineEventHandle
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/seckill/seckill/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize
            },
            this.searchForm
          )
        )
      }).then(({ data }) => {
        data.records.forEach(element => {
          if (element.maxNum === -1) {
            element.maxNum = '不限'
          }
        })
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    deleteHandle (id) {
      this.$confirm('确定进行删除操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/seckill/seckill/' + id),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      }).catch(() => { })
    },
    invalidHandle (id) {
      this.$confirm('确定使活动失效?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/seckill/seckill/invalid/' + id),
          method: 'put',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    // 刷新回调用
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.searchForm = params
      this.getDataList(this.page)
    },
    // 下线管理
    auditEventHandle (id) {
      this.offlineEventHandleVisible = true
      this.$nextTick(() => {
        this.$refs.offlineEvent.init(id)
      })
    }
  }
}
</script>
<style lang="scss">
.mod-seckill-seckill {
}
</style>
