<template>
  <div class="grantStatistics" v-loading="loading">
    <div class="el-card__header">
      <span>优惠券统计</span>
      <div style="display: flex;flex-direction: row;align-items: center">
        <el-button type="primary" class="el-button--small" @click="Export">导出全部</el-button>
      </div>
    </div>
    <div class="content">
      <div style="display: flex;flex-direction: row;align-items: center">
        <el-select class="w100" @change="getList" style="margin-right: 10px;" v-model="msg.PlatformType" size="small" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.Id"
            :label="item.Name"
            :value="item.Id">
          </el-option>
        </el-select>
        <el-select class="w100" style="margin-right: 10px;"  size="small" v-model="Coupon" placeholder="请选择">
          <el-option
            v-for="item in options2"
            :key="item.Id"
            :label="item.Name"
            :value="item.Id">
          </el-option>
        </el-select>
        <div class="block">
          <el-date-picker
            style="padding: 3px 10px;width: 380px;height: 32px"
            v-model="value"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            @change="change"
            align="right">
          </el-date-picker>
        </div>
        <el-tabs v-model="activeName" @tab-click="handleClick" style="margin-left: 20px">
          <el-tab-pane label="7日" name="first"></el-tab-pane>
          <el-tab-pane label="30日" name="second"></el-tab-pane>

        </el-tabs>

        <span style="color: #92959B;margin-left: 35px;cursor: pointer;font-size: 15px;" @click="empty">清空筛选</span>

      </div>
    </div>
    <div style="margin: 10px 0;">
      <div class="zong">
        总成交
      </div>
      <div class="zongshow">
        <div class="num-info">
          <div class="num-info-item">
            <div>{{Totaldata.IsSued!=null ?Totaldata.IsSued:0 }}</div>
            <div class="info-item-name">发放总数</div>
          </div>
          <div class="num-info-item">
            <div>{{Totaldata.Used!=null ?Totaldata.Used:0 }}</div>
            <div class="info-item-name">已使用总数</div>
          </div>
          <div class="num-info-item">
            <div>{{Totaldata.UnUsed!=null ?Totaldata.UnUsed:0 }}</div>
            <div class="info-item-name">未使用总数</div>
          </div>
          <div class="num-info-item">
            <div>{{Totaldata.Voksi!=null ?Totaldata.Voksi:0 }}</div>
            <div class="info-item-name">已失效总数</div>
          </div>
        </div>
      </div>
    </div>

    <div style="padding: 20px;background: #fff;margin-top: 10px">
      <el-table
        :data="tableData"
         @sort-change='getSort'
        header-cell-class-name="headClass"
        style="width: 1698px;"
        :default-sort = "{prop: 'date', order: 'descending'}"

      >

        <el-table-column
          prop="CreateDate"
          label="日期"
          width="283">
        </el-table-column>
        <el-table-column
          prop="Name"
          label="优惠券名称"
          width="283">
        </el-table-column>
        <el-table-column
          prop="IsSued"
          sortable
          label="发放数量"
          width="283">
        </el-table-column>
        <el-table-column
          prop="Used"
          sortable
          label="已使用数量"
          width="283">
        </el-table-column>
        <el-table-column
          prop="UnUsed"
          sortable
          label="未使用数量"
          width="283">
        </el-table-column>
        <el-table-column
          prop="Voksi"
          sortable
          label="已失效数量"
        >
        </el-table-column>
      </el-table>
      <el-pagination style="text-align:right"
            background
            @current-change="handleCurrentChange"
            :page-size="msg.pageSize"
            layout="prev, pager, next"
            :current-page.sync="msg.pageIndex"
            :total="count">
      </el-pagination>
    </div>
  </div>
</template>

<script>
    export default {
        name: "grantStatistics",
      data(){
        return{
          msg:{
            pageIndex:1,
            pageSize:20,
            PlatformType:0,
            StartDate:'',
            EndDate:'',
            OrderBy:'',
          },
          options:[],
          options2:[{Name: "优惠券", Id: 0}],
          Coupon:0,
          tableData:[],
          count:0,
          value:'',
          activeName:'',
          loading:false,
          Totaldata:{
            IsSued: "0",
            UnUsed: "0",
            Used: "0",
            Voksi: "0",
          },
        }
      },
      created(){
        this.getplat();
        this.getIntegraStatisticslList()
      },
      methods:{
        getplat(){
          this.apipost("/api/User/GetMemberUserSourceEnumList", {}, res => {
            if(res.data.resultCode==1){
              this.options = res.data.data;
              this.options.unshift({Name: "全部平台", Id: 0})
            }

          })
        },
        change(val){
          this.activeName=''
          this.msg.pageIndex=1
          this.getIntegraStatisticslList()
        },
        getIntegraStatisticslList(){
          this.loading=true;
          if(this.value!='' && this.value !=null ){
            this.msg.StartDate = this.value[0];
            this.msg.EndDate = this.value[1];
          }else{
            this.msg.StartDate = '';
            this.msg.EndDate = '';
          }
          this.apipost("/api/Coupon/GetCouponStatisticslList", this.msg, res => {
            this.loading=false;
            if(res.data.resultCode==1){
              this.tableData = res.data.data.pageData;
              this.count = res.data.data.count;
            }else {
              this.Info(res.data.message);
            }

          })
          let Totalmsg = {
            PlatformType:this.msg.PlatformType,
            StartDate :this.msg.StartDate ,
            EndDate :this.msg.EndDate,
          }

          this.apipost("/api/Coupon/GetTotalCoupon", Totalmsg, res => {
            this.loading=false;
            if(res.data.resultCode==1){
              this.Totaldata = res.data.data;
            }else {
              this.Info(res.data.message);
            }

          })

        },

        handleCurrentChange(val) {
          this.msg.pageIndex = val;
          this.getIntegraStatisticslList();
        },
        Export(){
          let msg = {
            PlatformType:this.msg.PlatformType,
            StartDate :this.msg.StartDate ,
            EndDate :this.msg.EndDate,
          }
          msg = JSON.parse(JSON.stringify(msg));
          this.GetLocalFile(
            "/api/Coupon/GetCouponListToExcel",
            msg,
            "优惠券-发布统计.xls"
          );
        },
        empty(){
          this.msg.PlatformType=0;
          this.msg.StartDate='';
          this.msg.EndDate='';
          this.value='';
          this.activeName='';
        },
        handleClick(tab, event) {

          if(tab.name=='first'){
            this.fun_date(7)
          }else if(tab.name=='second'){
            this.fun_date(30)
          }
        },
        fun_date(num){
          let that= this
          let date1 = new Date();
          //今天时间
          let time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate()
          let date2 = new Date(date1);
          date2.setDate(date1.getDate() - num);
          //num是正数表示之后的时间，num负数表示之前的时间，0表示今天
          let time2 = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
          let data = []
          data.push(time2)
          data.push(time1)
          that.value = data;
          this.msg.pageIndex=1
          this.getIntegraStatisticslList()

        },
        getList(){
          this.msg.pageIndex=1
          this.getIntegraStatisticslList()
        },
        getSort(val){
           if(val.prop == 'IsSued'){
            if(val.order=='ascending'){
                  this.msg.OrderBy='IsSued asc'   
              }else if(val.order=='descending'){
                  this.msg.OrderBy='IsSued desc'
              }else{
                  this.msg.OrderBy=''
              }
          }else if(val.prop == 'Used'){
             if(val.order=='ascending'){
                  this.msg.OrderBy='Used asc'   
              }else if(val.order=='descending'){
                  this.msg.OrderBy='Used desc'
              }else{
                  this.msg.OrderBy=''
              }
          }else if(val.prop == 'UnUsed'){
             if(val.order=='ascending'){
                  this.msg.OrderBy='UnUsed asc'   
              }else if(val.order=='descending'){
                  this.msg.OrderBy='UnUsed desc'
              }else{
                  this.msg.OrderBy=''
              }
          }else if(val.prop == 'Voksi'){
             if(val.order=='ascending'){
                  this.msg.OrderBy='Voksi asc'   
              }else if(val.order=='descending'){
                  this.msg.OrderBy='Voksi desc'
              }else{
                  this.msg.OrderBy=''
              }
          }
        },
      },
      mounted(){

      }
    }
</script>

<style >

  .grantStatistics .el-card__header{
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    background: #fff;
  }
  .grantStatistics .el-button--small{
    padding: 9px 15px;
  }
  .grantStatistics .content .searchInput{
    border: 1px solid #DCDFE6;
    border-radius: 4px;
  }
  .grantStatistics .content .searchInput .el-input__inner{
    border:none;outline:none;
    height: 30px;
    line-height: 30px;
  }
  .grantStatistics .content .searchInput{
    line-height: normal;
    display: inline-table;
    border-collapse: separate;
    border-spacing: 0;
    width:250px;
    margin-right: 20px;
  }
  .grantStatistics .content{
    background: #fff;
    margin-top:10px;
    padding: 15px;
    box-sizing: border-box;
  }
  .grantStatistics .el-icon-date{
    line-height: 24px;
  }
  .grantStatistics .el-range-separator{
    line-height: 24px;
  }
  .grantStatistics .zong{
    width: 100%;
    height: 57px;
    padding: 18px 20px;
    border-bottom: 1px solid #EBEEF5;
    background: white;
  }
  .grantStatistics .zongshow{
    padding: 20px;
    background: white;
  }
  .grantStatistics .num-info {
    display: flex;
    width: 100%;
    height: 60px;
    font-size: 24px;
    color: #303133;
    margin: 20px 0;
  }
  .grantStatistics .num-info .num-info-item {
    text-align: center;
    flex-grow: 1;
  }
  .grantStatistics .info-item-name {
    font-size: 14px;
    color: #92959B;
  }
</style>
