<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"></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="text"
          @click="addOrUpdateHandle(scope.row.seckillId)"
        >查看详情</el-button>
        <!-- <el-button type="text"
                   style="color:red"
                   icon="el-icon-bottom"
                   size="small"
                   v-if="scope.row.status === 1"
        @click="invalidHandle(scope.row.seckillId)">下线</el-button>-->
        <el-button
          type="text"
          icon="el-icon-bottom"
          size="small"
          v-if="isAuth('platform:seckill:update') && scope.row.status<2"
          @click="offlineSeckillHandle(scope.row)"
        >下线活动</el-button>
        <el-button
          type="text"
          icon="el-icon-document-checked"
          size="small"
          v-if="isAuth('platform:seckill:update') && scope.row.status > 1"
          @click="seckillAuditHandle(scope.row.seckillId)"
        >{{scope.row.status === 2?'违规下架':'待审核'}}</el-button>
        <el-button
          type="text"
          icon="el-icon-delete"
          size="small"
          style="color:red"
          :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>

    <!-- 商品审核弹窗 -->
    <SeckillAudit
      v-if="seckillAuditVisible"
      ref="seckillAudit"
      selectUrl="/platform/seckill/getOfflineHandleEventBySeckillId"
      updateUrl="/platform/seckill/auditSeckill"
      @refreshDataList="refreshChange"
    ></SeckillAudit>
  </div>
</template>

<script>
import { tableOption } from '@/crud/marketing/seckill'
import AddOrUpdate from './seckill-add-or-update'
import SeckillAudit from '@/components/offline-audit'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {},
      seckillAuditVisible: false,
      dataListLoading: false,
      tableOption: tableOption,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate,
    SeckillAudit
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/platform/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('/platform/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('/platform/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)
    },
    // 下线活动
    offlineSeckillHandle (row) {
      this.$prompt('确定对活动[' + row.seckillName + ']进行下线操作？如是，请填写下线理由', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then((obj) => {
        if (!obj.value) {
          this.$message.error('下线原因不能为空')
          return
        }
        this.$http({
          url: this.$http.adornUrl(`/platform/seckill/offline`),
          method: 'post',
          data: this.$http.adornData({
            handleId: row.seckillId,
            offlineReason: obj.value || null
          })
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      })
    },
    // 弹窗处理
    seckillAuditHandle (id) {
      this.seckillAuditVisible = true
      this.$nextTick(() => {
        this.$refs.seckillAudit.init(id)
      })
    }
  }
}
</script>
<style lang="scss">
.mod-seckill-seckill {
}
</style>
