<template>
  <div class="couponManage">
    <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="addRecharge">新增</el-button>
      </div>
    </div>
    <div class="content">
      <div style="display: flex;flex-direction: row;align-items: center">
        <div class="block">
          <div class="searchInput" style="width:250px">
            <el-input style="display:inline-block;width:225px;height:30px"
                      placeholder="请输入优惠券名称搜索"
                      v-model="msg.Name"
                      size="small"
                      @clear="getList"
                      @keyup.enter.native="getList"
                      clearable>
            </el-input>
            <span @click="getList" class="el-icon-search" style="color:#979dad;font-size:14px;position:relative;top:1px"></span>
          </div>
        </div>
      </div>
    </div>


    <div style="padding: 20px;background: #fff;">
      <el-table
        :data="tableData"
        header-cell-class-name="headClass"
        style="width: 100%"
        border
      >

        <el-table-column
          prop="ID"
          label="ID"
          width="100">
        </el-table-column>

        <el-table-column
          prop="Name"
          label="优惠券名称">
        </el-table-column>
        <el-table-column
          prop="SmallShopName"
          label="微店名称">
        </el-table-column>
        <el-table-column
          prop="MinConsumePrice"
          label="最低消费金额（元）"
          width="100">
        </el-table-column>
        <el-table-column
          label="优惠方式"
          width="150">
          <template slot-scope="scope">
            <div v-if="scope.row.CouponType==1">优惠:{{scope.row.DiscountsPrice}}元</div>
            <div v-if="scope.row.CouponType==2">
              <div>{{scope.row.DiscountsPrice}}折</div>
              <div v-if="scope.row.MaxDiscountsPrice!=0">优惠上限:{{scope.row.MaxDiscountsPrice}}元</div>
              <div v-if="scope.row.MaxDiscountsPrice==0">优惠上限:无上限</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="UseTypeStr"
          label="使用范围"
          width="120">
        </el-table-column>

        <el-table-column
          label="有效时间"
          width="180">
          <template slot-scope="scope">
            <div v-if="scope.row.IndateType==1">领取后{{scope.row.IndateDay}}天内有效</div>
            <div v-if="scope.row.IndateType==2">开始时间：{{scope.row.StartDate}}</div>
            <div v-if="scope.row.IndateType==2">结束时间：{{scope.row.EndDate}}</div>
          </template>
        </el-table-column>

        <el-table-column
          label="数量"
          width="150">
          <template slot-scope="scope">
            <div>总数量：{{scope.row.TotalNum ==-1? '无限制领取':scope.row.TotalNum+scope.row.ResidueNum}}</div>
            <div v-if="scope.row.TotalNum !=-1">剩余发放数：{{scope.row.TotalNum}}</div>
          </template>
        </el-table-column>

        <el-table-column
          label="加入领券中心 "
          width="80">
          <!--PickupType-->
          <template slot-scope="scope">
            <el-switch
              @change="dianswitch(scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.PickupCenter"
              >
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column
          fixed="right"
          label="操作"
          width="180"
        >
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="发放优惠券" placement="top" >
              <img src="../../assets/img/setup/send.png" alt="" class="imgstyle" @click="grant(scope.row)">
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="编辑" placement="top" >
              <img src="../../assets/img/setup/edit.png" alt="" class="imgstyle" @click="Edit(scope.row)">
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top" >
              <img src="../../assets/img/setup/del.png" alt="" class="imgstyle" @click="delete_b(scope.row)">
            </el-tooltip>
          </template>
        </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: "couponManage",
      data(){
        return{
          msg:{
            pageIndex:1,
            pageSize:20,
            Name:'',
          },
          tableData:[],
          count:0,
          loading:false
        }
      },
      created(){
        this.getDateList();

      },
      methods:{
        getDateList(){
          this.loading=true;
          this.apipost("/api/Coupon/GetDiscountCouponPageList", 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);
            }

          })
        },
        addRecharge(){
          this.$router.push('/addCoupon');

        },
        grant(row){
          var arr=JSON.stringify(row)
          this.$router.push({
            name: 'grantCouPon',
            query: {
              row
            }
          });
        },
        Edit(row){
          this.$router.push({
            name: 'addCoupon',
            query: {
              ID:row.ID,
              blank: "y"
            }
          });
        },
        delete_b(row){
          let that=this;
          that.Confirm("是否删除?", function () {
            that.apipost(
              "/api/Coupon/DelDiscountCoupon",
              {Id:row.ID},
              res => {
                if (res.data.resultCode == 1) {
                  that.Success(res.data.message);
                  that.getDateList();
                } else {
                  that.Error(res.data.message);
                }
              },
            );
          });
        },
        SetUp(){
          this.$router.push('/rechargeSet');
        },
        getList(){
          this.msg.pageIndex = 1
          this.getDateList()
        },
        dianswitch(row){
          this.apipost("/api/Coupon/UpdatePickupType", {'Id':row.ID}, res => {
            if(res.data.resultCode==1){
              this.Success(res.data.message);
            }else {
              this.Info(res.data.message);
            }

          })
        },
        handleCurrentChange(val) {
          this.msg.pageIndex = val;
          this.getDateList();
        },
      },
    }
</script>

<style >
  .couponManage .el-card__header{
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    background: #fff;

  }
  .couponManage .el-button--small{
    padding: 9px 15px;
  }
  .couponManage .content .searchInput{
    border: 1px solid #DCDFE6;
    border-radius: 4px;
    margin-left: 20px;
  }
  .couponManage .content .searchInput .el-input__inner{
    border:none;outline:none;
    height: 30px;
    line-height: 30px;
  }
  .couponManage .content .searchInput{
    line-height: normal;
    display: inline-table;
    border-collapse: separate;
    border-spacing: 0;
    width:250px;
  }
  .couponManage .content{
    background: #fff;
    margin-top:10px;
    padding: 15px;
    box-sizing: border-box;
  }
</style>
