<template>
  <div class="mod-admin-groupActivity">
    <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="menu">
        <el-button
          icon="el-icon-edit"
          size="small"
          type="text"
          @click="addOrUpdateHandle(scope.row.groupActivityId)"
        >活动详情</el-button>
        <!-- manageGroupProdHandle(scope.row.groupActivityId) -->
        <el-button
          icon="el-icon-s-opportunity"
          size="small"
          type="text"
          @click="selectActivityProd(scope.row.groupActivityId)"
        >查看活动商品</el-button>
        <!-- <el-button icon="el-icon-folder-delete"
                   size="small"
                   type="text"
                   @click="invalidActivityHandle(scope.row)">失效活动
        
        <el-tooltip class="item"
                    effect="dark"
                    content="“使失效”即立即结束且不可再编辑，未成团订单将自动关闭并退款，已成团订单仍需及时处理。失效后的商品活动可删除。"
                    placement="right">
          <i class="el-icon-question"></i>
        </el-tooltip>
        </el-button>-->
        <el-button
          type="text"
          icon="el-icon-bottom"
          size="small"
          v-if="isAuth('group:activity:update') && scope.row.activityStatus<3"
          @click="offlineGroupHandle(scope.row)"
        >下架活动</el-button>
        <el-tooltip
          class="item"
          effect="dark"
          content="下架商家活动后，该活动的所有未拼团成功拼团团队将会拼团失败全部退款，商户需要重新申请成功后才能新建此商品的拼团活动，此操作可能会造成商家的经济损失，请谨慎操作！"
          placement="right"
        >
          <i class="el-icon-question"></i>
        </el-tooltip>
        <el-button
          type="text"
          icon="el-icon-document-checked"
          size="small"
          v-if="isAuth('group:activity:update') && scope.row.status > 2"
          @click="groupAuditHandle(scope.row.groupActivityId)"
        >{{scope.row.status === 3?'违规下架':'待审核'}}</el-button>
        <el-button
          type="text"
          :disabled="!(scope.row.activityStatus == 1 || scope.row.activityStatus == 3 || scope.row.activityStatus == 4)"
          icon="el-icon-delete"
          size="small"
          style="color:red"
          @click="deleteHandle(scope.row)"
        >删除</el-button>
      </template>
      <template slot-scope="scope" slot="groupNumber">
        <el-tag size="mini">{{scope.row.groupNumber}}人团</el-tag>
      </template>
      <template slot-scope="scope" slot="activityStatus">
        <el-tag
          type="info"
          v-if="scope.row.activityStatus === 1 && scope.row.status === 2"
          size="mini"
        >未启用</el-tag>
        <el-tag v-if="scope.row.activityStatus === 1 && scope.row.status !== 2" size="mini">未开始</el-tag>
        <el-tag type="success" v-if="scope.row.activityStatus === 2" size="mini">进行中</el-tag>
        <el-tag type="warning" v-if="scope.row.activityStatus === 3" size="mini">已结束</el-tag>
        <el-tag type="danger" v-if="scope.row.activityStatus === 4" size="mini">已失效</el-tag>
        <el-tag type="danger" v-if="scope.row.activityStatus === 5" size="mini">违规下架</el-tag>
        <el-tag type="warning" v-if="scope.row.activityStatus === 6" size="mini">平台审核</el-tag>
      </template>
    </avue-crud>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
    <groupProds v-if="groupProdsVisible" ref="groupProds" @refreshDataList="refreshChange"></groupProds>

    <!-- 商品审核弹窗 -->
    <groupAudit
      v-if="groupAuditVisible"
      ref="groupAudit"
      selectUrl="/platform/group/activity/getOfflineHandleEventByActivityId"
      updateUrl="/platform/group/activity/auditGroupActivity"
      @refreshDataList="refreshChange"
    ></groupAudit>
  </div>
</template>

<script>
import { tableOption } from '@/crud/marketing/groupActivity'
import AddOrUpdate from './groupActivity-add-or-update'
import GroupProds from './groupProd'
import groupAudit from '@/components/offline-audit'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {},
      dataListLoading: false,
      groupAuditVisible: false,
      groupProdsVisible: false,
      tableOption: tableOption,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate,
    groupAudit,
    GroupProds
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/platform/group/activity/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 (row) {
      this.$confirm('确定删除【' + row.activityName + '】活动吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl(`/platform/group/activity/${row.groupActivityId}`),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    /**
     * 刷新回调
     */
    refreshChange (isRouteToGroupProd, groupActivityId) {
      if (isRouteToGroupProd === 1) {
        this.$router.push({
          path: '/groupProds',
          query: { activityId: groupActivityId }
        })
      }
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.searchForm = params
      this.getDataList(this.page)
    },
    // 管理拼团活动商品
    manageGroupProdHandle (groupActivityId) {
      this.visible = false
      this.$router.push({
        path: '/groupProds',
        query: { activityId: groupActivityId }
      })
    },
    // 失效活动活动
    invalidActivityHandle (row) {
      this.$confirm('确定失效【' + row.activityName + '】活动吗？失效后如果活动开启了模拟成团，拼团中的团单仍需要处理', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(
        () => {
          this.$http({
            url: this.$http.adornUrl('/group/activity/invalid/' + row.groupActivityId),
            method: 'PUT',
            data: this.$http.adornData({})
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList(this.page)
              }
            })
          })
        }).catch(() => { })
    },
    // 下架活动
    offlineGroupHandle (row) {
      this.$prompt('确定对活动[' + row.activityName + ']进行下架操作？如是，请填写下架理由', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then((obj) => {
        if (!obj.value) {
          this.$message.error('下架原因不能为空')
          return
        }
        this.$http({
          url: this.$http.adornUrl(`/platform/group/activity/offline`),
          method: 'post',
          data: this.$http.adornData({
            handleId: row.groupActivityId,
            offlineReason: obj.value || null
          })
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      })
    },
    // 弹窗处理
    groupAuditHandle (id) {
      this.groupAuditVisible = true
      this.$nextTick(() => {
        this.$refs.groupAudit.init(id)
      })
    },
    // 弹窗处理
    selectActivityProd (id) {
      this.groupProdsVisible = true
      this.$nextTick(() => {
        this.$refs.groupProds.init(id)
      })
    }
  }
}
</script>
<style lang="scss">
.mod-admin-groupActivity {
}
</style>
