<template>
  <div style="background-color:#f7f7f7;padding:10px 10px;">
    <div class="couponPage">
      <div style="height:58px;line-height:58px;">
        <div style="color:#0006"> <span>平台活动</span>   <span style="padding:0 5px;">></span>  <span style="color:#000000e6">优惠券活动</span></div>
      </div>
      <!-- 搜索 -->
      <div class="formSearch">
        <!-- 搜索条件 -->
        <el-form :inline="true" :model="query" class="demo-form-inline">
          <el-form-item label="活动名称">
            <el-input v-model="query.activityName" placeholder="请输入活动名称" />
          </el-form-item>
          <el-form-item label="活动状态">
            <el-select v-model="query.state" placeholder="请选择活动状态">
              <el-option
                v-for="item in activityStatusSelect"
                :key="item.index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div>
          <el-button style="background-color: #3F9B6A;color: #fff" @click="search">查询</el-button>
          <el-button  class="buttonHover"
              style="color: #606266;border: 1px solid #dddfe5;background-color: #fff;" @click="clear">重置</el-button>
        </div>
      </div>
      <div style="margin: 0  0 20px 0;">
        <el-button icon="el-icon-circle-plus-outline"
          style="background-color: #3F9B6A;color: #fff;" @click="addActivity">新建活动</el-button>
      </div>
      <!-- 表格 -->
      <div class="tableBox">
        <el-table
          ref="multipleTable"
          :data="tableData"
         :header-cell-style="{fontSize: '14px',color:'#0009',fontWeight: 'normal',backgroundColor:'#F2F3F5'}"
          tooltip-effect="dark"
        >
          <el-table-column label="活动名称" width="220">
            <template slot-scope="scope">{{ scope.row.activityName }}</template>
          </el-table-column>
          <el-table-column label="活动状态">
            <template slot-scope="scope">
              <span v-if="scope.row.state == 0">报名未开始</span>
              <span v-if="scope.row.state == 1">报名进行中</span>
              <span v-if="scope.row.state == 2">活动待开始</span>
              <span v-if="scope.row.state == 3">活动进行中</span>
              <span v-if="scope.row.state == 4">活动已结束</span>
            </template>
          </el-table-column>
          <el-table-column prop="discountMode" label="营销方式">
            <template slot-scope="scope">
              <span v-if="scope.row.discountMode == 1">满减</span>
              <span v-if="scope.row.discountMode == 2">优惠券</span>
            </template>
          </el-table-column>
          <el-table-column prop="discountMode" label="启动积分兑换">
            <template slot-scope="scope">
              <span v-if="scope.row.ifCredit == 1">是</span>
              <span v-if="scope.row.ifCredit == 0">否</span>
            </template>
          </el-table-column>
          <el-table-column prop="credit" label="所需积分" />
          <el-table-column prop="shopNumber" label="商家数" />
          <el-table-column prop="productNumber" label="商品数量" />
          <el-table-column label="操作" show-overflow-tooltip>
            <template slot-scope="scope">
              <div class="btnList">
                <div class="tableBtn greens" @click="details(scope.row)">详情</div>
                <div v-if="scope.row.state != 4" class="tableBtn greens" @click="editActivity(scope.row)">编辑</div>
                <div v-if="scope.row.state != 4" class="tableBtn greens" @click="endActivity(scope.row)">结束</div>
                <div v-if="scope.row.state == 4" class="tableBtn greens" @click="delActivity(scope.row)">删除</div>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="fenye">
          <el-pagination
            :current-page="query.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="10"
           background
           small
           layout="prev, pager, next,total"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>

      <el-dialog
        :title="editForm ? '修改优惠券活动' : '新增优惠券活动'"
        :visible.sync="activityVisible"
        width="70%"
        center
        :close-on-click-modal="false"
        @close="closeModal"
      >
        <div class="couponDialogBox">
          <AddActive
            :activity-form="activityForm"
            :is-edit="editForm"
            @cancel="activityVisible = false"
            @refersh="refersh('activityVisible')"
          />
        </div>
      </el-dialog>
      <el-dialog
        :title="'优惠券活动详情'"
        :visible.sync="activityDetailVisible"
        width="74%"
        center
        :close-on-click-modal="false"
        @close="cleanForm('activityDetailVisible')"
      >
        <ActivityDetail :activity-id="activityForm.activityId" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import AddActive from '@/views/active/couponlist/couponAdd.vue'
import ActivityDetail from '@/views/active/couponlist/couponDetail.vue'
import {
  getCouponData,
  endCoupon,
  delCoupon
} from '@/api/active/active_coupon.js'
export default {
  components: {
    AddActive,
    ActivityDetail
  },
  data () {
    return {
      query: {
        activityName: '', // 活动名称
        // 活动状态 0-报名未开始 1-报名进行中 2-活动待开始 3-活动进行中 4-活动已结束
        state: '',
        page: 1,
        pageSize: 10
      },
      total: 1,
      tableData: [],
      activityStatusSelect: [
        {
          index: 0,
          label: '报名未开始',
          value: 0
        },
        {
          index: 1,
          label: '报名进行中',
          value: 1
        },
        {
          index: 2,
          label: '活动待开始',
          value: 2
        },
        {
          index: 3,
          label: '活动进行中',
          value: 3
        },
        {
          index: 4,
          label: '活动已结束',
          value: 4
        }
      ],
      activityVisible: false,
      editForm: false,
      activityDetailVisible: false,
      activityForm: {}
    }
  },
  created () {
    this.getAll()
  },
  methods: {
    // 关闭弹窗
    closeModal () {},
    async getAll () {
      const res = await getCouponData(this.query)
      this.tableData = res.data.list
      this.total = res.data.total
    },
    handleSizeChange (val) {
      this.query.pageSize = val
      this.getAll()
    },
    handleCurrentChange (val) {
      this.query.page = val
      this.getAll()
    },
    search () {
      this.total = 1
      this.query.page = 1
      this.getAll()
    },
    // 重置
    clear () {
      this.query = {
        activityName: '',
        state: '',
        page: 1,
        pageSize: 10
      }
      this.getAll()
    },
    // 活动详情
    details (row) {
      this.activityForm = row
      this.activityDetailVisible = true
    },
    // 添加活动
    addActivity () {
      this.editForm = false
      this.activityVisible = true
    },
    // 编辑
    editActivity (row) {
      this.editForm = true
      this.activityForm = row
      this.activityVisible = true
    },
    async endActivity (row) {
      const res = await endCoupon({ activityId: row.activityId })
      if (res.code === '') {
        this.$message({
          message: '结束成功',
          type: 'success'
        })
      } else {
        this.$message({
          message: res.message,
          type: 'error'
        })
      }
      this.getAll()
    },
    async delActivity (row) {
      const res = await delCoupon({ activityId: row.activityId })
      if (res.code === '') {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      } else {
        this.$message({
          message: res.message,
          type: 'error'
        })
      }
      this.getAll()
    },
    cleanForm (attr) {
      attr = false
    },
    refersh (attr) {
      this[attr] = false
      this.getAll()
    }
  }
}
</script>

<style lang="scss" scoped>
.couponPage{

   padding: 0  20px 20px 20px;
   min-height: calc(100vh - 50px - 20px);
   background-color: #Fff;


  .tableBox{
    .fenye{
      margin: 20px;
    }
  }
}
.couponDialogBox {
  max-height: 600px;
  overflow-y: auto;
}
.formSearch{

    display: flex;
    width: 100%;
    font-size: 14px;
    justify-content: space-between;
    padding-bottom: 10px;
    align-items: center;

}
  

.greens {
    color: #3F9B6A;
  }
  ::v-deep .buttonHover:hover{
    color:#3f9b6a !important;
    border-color: #c5e1d2 !important;
    background-color: #ecf5f0 !important;
    outline: none;
  }
  ::v-deep .el-pagination__total {
    position: absolute;
    left: 10px;
  }
  ::v-deep .el-pagination.is-background .el-pager li:not(.disabled).active{
    background-color:#3f9b6a;
  }
</style>
