<template>
  <div class="mod-discount">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :option="tableOption"
      @search-change="searchChange"
      @selection-change="selectionChange"
      @refresh-change="refreshChange"
      @on-load="getDataList"
    >
      <template slot="menuLeft"></template>

      <template slot-scope="scope" slot="status">
        <el-tag v-if="scope.row.status === 0" size="small">关闭</el-tag>
        <el-tag v-else-if="scope.row.status === 1" type="success" 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
          type="text"
          icon="el-icon-edit"
          size="small"
          @click="showDiscountInfoHandle(scope.row.discountId)"
        >查看活动</el-button>
        <el-button
          type="text"
          icon="el-icon-bottom"
          size="small"
          v-if="isAuth('platform:discount:update') && scope.row.status<2"
          @click="offlineDiscountHandle(scope.row)"
        >下架活动</el-button>
        <el-button
          type="text"
          icon="el-icon-document-checked"
          size="small"
          v-if="isAuth('platform:discount:update') && scope.row.status > 1"
          @click="discountAuditHandle(scope.row.discountId)"
        >{{scope.row.status === 2?'违规下架':'待审核'}}</el-button>
        <el-button
          type="text"
          icon="el-icon-view"
          size="small"
          @click="showProds(scope.row.discountId)"
        >查看商品</el-button>
        <el-button
          type="text"
          icon="el-icon-delete"
          size="small"
          style="color:red"
          @click="deleteHandle(scope.row.discountId)"
        >删除</el-button>
      </template>
    </avue-crud>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate"></add-or-update>
    <discount-info
      v-if="discountInfoVisible"
      ref="discountInfo"
      @refreshSelectProds="refreshChange"
    ></discount-info>
    <!-- 商品审核弹窗 -->
    <DiscountAudit
      v-if="discountAuditVisible"
      ref="discountAudit"
      selectUrl="/platform/discount/getOfflineHandleEventByDiscountId"
      updateUrl="/platform/discount/auditDiscount"
      @refreshDataList="refreshChange"
    ></DiscountAudit>
  </div>
</template>

<script>
import AddOrUpdate from './discountProd-add-or-update'
import DiscountInfo from './discountInfo'
import { tableOption } from '@/crud/marketing/discount'
import DiscountAudit from '@/components/offline-audit'
export default {
  data () {
    return {
      dataForm: {
        discountName: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {},
      dataListLoading: false,
      discountAuditVisible: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      discountInfoVisible: false,
      tableOption: tableOption

    }
  },
  components: {
    AddOrUpdate,
    DiscountInfo,
    DiscountAudit
  },
  methods: {
    // 获取数据列表
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/platform/discount/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
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增
    addOrUpdateHandle (id) {
      this.$router.push({ path: '/discountInfo', query: { discountId: id } })
    },
    // 查看商品
    showProds (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.showProd(id, 1)
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(`确定进行[删除]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl(`/platform/discount/${id}`),
            method: 'delete',
            data: this.$http.adornData({}, false)
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList(this.page)
              }
            })
          })
        })
        .catch(() => { })
    },
    // 条件查询
    searchChange (params) {
      this.searchForm = params
      this.getDataList(this.page)
    },
    // 多选变化
    selectionChange (val) {
      this.dataListSelections = val
    },
    showDiscountInfoHandle (id) {
      this.discountInfoVisible = true
      this.$nextTick(() => {
        this.$refs.discountInfo.init(id)
      })
    },
    // 更新数据
    refreshChange () {
      this.getDataList()
    },
    // 下架活动
    offlineDiscountHandle (row) {
      this.$prompt('确定对活动[' + row.discountName + ']进行下架操作？如是，请填写下架理由', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then((obj) => {
        if (!obj.value) {
          this.$message.error('下架原因不能为空')
          return
        }
        this.$http({
          url: this.$http.adornUrl(`/platform/discount/offline`),
          method: 'post',
          data: this.$http.adornData({
            handleId: row.discountId,
            offlineReason: obj.value || null
          })
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      })
    },
    // 弹窗处理
    discountAuditHandle (id) {
      this.discountAuditVisible = true
      this.$nextTick(() => {
        this.$refs.discountAudit.init(id)
      })
    }
  }
}
</script>
