<template>
  <div class="mod--fetch">
    <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
          v-if="isAuth('platform:fetch:save')"
          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 === 4" size="small" type="info">审核失败</el-tag>
        <el-tag v-else-if="scope.row.status === 3" size="success">审核中</el-tag>
        <el-tag v-else-if="scope.row.status === 2" type="warning">平台关闭</el-tag>
        <el-tag v-else-if="scope.row.status === 1" size="small">营业</el-tag>
        <el-tag v-else size="small" type="danger">关闭</el-tag>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          v-if="isAuth('platform:fetch:update')"
          icon="el-icon-edit"
          size="small"
          type="text"
          @click="addOrUpdateHandle(scope.row.fetchId)"
        >查看信息</el-button>
        <el-button
          type="text"
          icon="el-icon-bottom"
          size="small"
          v-if="isAuth('platform:fetch: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:fetch:update') && scope.row.status > 1"
          @click="fetchAuditHandle(scope.row.fetchId)"
        >{{scope.row.status === 2?'违规下架':scope.row.status === 3?'待审核':'审核失败'}}</el-button>
        <!-- <el-button
          v-if="isAuth('platform:fetch:delete')"
          type="text"
          style="color:red"
          icon="el-icon-delete"
          size="small"
          @click="deleteHandle(scope.row.fetchId)"
        >删除</el-button>-->
      </template>
    </avue-crud>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
    <!-- 商品审核弹窗 -->
    <OfflineAudit
      v-if="fetchAuditVisible"
      ref="offlineAudit"
      selectUrl="/platform/fetch/getOfflineHandleEventByFetchId"
      updateUrl="/platform/fetch/auditFetch"
      @refreshDataList="refreshChange"
    ></OfflineAudit>
  </div>
</template>

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