 
<template>
  <div>
    <div class="heior_box">
      <el-button size="small" type="primary" icon="el-icon-plus" @click="add">新建优惠券</el-button>
      <div class="search-box">
        <!-- <el-select v-model="value" placeholder="请选择" size="small">
          <el-option label="全部状态" value="0"></el-option>
          <el-option label="未开始" value="1"></el-option>
          <el-option label="进行中" value="2"></el-option>
          <el-option label="已结束" value="3"></el-option>
          <el-option label="已失效" value="4"></el-option>
        </el-select>-->
        <el-input v-model="searchModel.keyWord" placeholder="请输入套餐名称/标题" clearable size="small">
          <el-button slot="append" icon="el-icon-search" @click="get"></el-button>
        </el-input>
      </div>
    </div>
    <el-table :data="retData.Data" row-key="Id" border size="medium" :height="$getHeight(110)">
      <el-table-column prop="Name" label="优惠券名称"></el-table-column>
      <el-table-column prop="CouponType" label="类型">
        <template slot-scope="scope">{{scope.row.CouponType==1?"满减券":"折扣券"}}</template>
      </el-table-column>
      <el-table-column prop="CouponValue" label="优惠内容">
        <template
          slot-scope="scope"
        >{{scope.row.CouponType==1?"减"+scope.row.CouponValue+"元":scope.row.CouponValue+"折"}}</template>
      </el-table-column>
      <el-table-column label="已领取">
        <template slot-scope="scope">{{scope.row.GrantCount}}</template>
      </el-table-column>
      <!-- <el-table-column prop="UseCount" label="已使用" width="80"></el-table-column> -->
      <el-table-column label="使用日期" width="240">
        <template slot-scope="scope">
          <div
            v-if="scope.row.DateRule==1&&scope.row.StartDate&&scope.row.EndDate"
          >{{scope.row.StartDate.DateFormat('yyyy-MM-dd')}}至{{scope.row.EndDate.DateFormat('yyyy-MM-dd')}}</div>
          <div v-else-if="scope.row.DateRule==2">领取当天起{{scope.row.ValidDay}}天内有效</div>
          <div v-else>领取次日起{{scope.row.ValidDay}}天内有效</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="220">
        <template slot-scope="scope">
          <span class="handel cursor edit" @click="give(scope.row)">发放</span>
          <span class="handel cursor edit" @click="edit(scope.row)">编辑</span>
           <router-link :to="{name:'couponused',query:{couponId:scope.row.Id,couponName:scope.row.Name}}">
                <span class="handel cursor edit">明细</span>
            </router-link>
          <!-- <span class="handel cursor edit"  v-if="scope.row.Disabled==0" @click="disabled(scope.row)">使失效</span>   -->
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        v-if="retData.Data.length>0"
        @current-change="get"
        :page-size="50"
        layout="total,prev, pager, next"
        :total="retData.TotalCount"
      ></el-pagination>
    </div>
    <el-dialog title="优惠券维护" :visible.sync="showDialog" width="680px">
      <el-form :model="currentModel" :rules="rules" ref="inputForm" label-width="100px">
        <el-form-item prop="CouponType" label="优惠券类型">
          <el-radio-group v-model="currentModel.CouponType">
            <el-radio :label="1">满减券</el-radio>
            <el-radio :label="2">折扣券</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="Name" label="优惠券名称">
          <el-input v-model="currentModel.Name" size="small"></el-input>
        </el-form-item>
        <el-form-item prop="CouponValue" label="优惠内容">
          <div v-if="currentModel.CouponType==1">
            <el-input v-model="currentModel.CouponValue" placeholder="请输入优惠券金额" size="small">
              <el-button slot="append">元</el-button>
            </el-input>
          </div>
          <div class="discount-box" v-else>
            <el-input v-model="currentModel.CouponValue" placeholder="请输入优惠券折扣（1-10）" size="small">
              <el-button slot="append">折</el-button>
            </el-input>
          </div>
        </el-form-item>
        <el-form-item prop="isLimitAmount" label="使用门槛">
          <el-radio-group v-model="isLimitAmount">
            <el-radio :label="0">无门槛</el-radio>
            <el-radio :label="1">
              <el-input
                v-model="currentModel.AllowMinAmount"
                size="small"
                placeholder="请输入满xx元后方可使用"
              ></el-input>
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item prop="DateRule" label="使用时间">
          <el-radio-group v-model="currentModel.DateRule">
            <el-radio :label="1">
              特定时间段
              <el-date-picker
                size="small"
                v-model="dateRange"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-radio>
            <el-radio :label="2">
              领取当日起
              <el-input v-model="currentModel.ValidDay" size="small" />天内可用
            </el-radio>
            <el-radio :label="3">
              领取次日起
              <el-input v-model="currentModel.ValidDay" size="small" />天内可用
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm">取 消</el-button>
        <el-button type="primary" @click="save()">确定</el-button>
      </span>
    </el-dialog>
      <userDrawer ref="userDrawer" @select="onSelect"></userDrawer>
  </div>
</template>
<script>
var that = null;
export default {
  components: {
    userDrawer: () => import("@/components/common/userDrawer")
  },
  data() {
    return {
      showDialog: false,
      optype: 1, //1:新建 2：编辑
      retData: { TotalCount: 0, Data: [] },
      searchModel: {
        PageIndex: 1,
        keyWord: ""
      },
      currentModel: {
        CouponType: 1,
        DateRule: 1,
        ValidDay: 0,
        CouponValue: 0,
        AllowMinAmount: 0,
        StartDate: null,
        EndDate: null
      },
      giveModel:{
        CouponId:0,
        UserId:0
      },
      isLimitAmount: 0,
      dateRange: [],
      rules: {}
    };
  },
  created() {
    this.$store.commit("setCurrentNav", "优惠券管理");
    this.get(1);
  },
  methods: {
    get(page) {
      if (Number.isInteger(page)) {
        this.searchModel.PageIndex = page;
      }
      this.$api.get("sells/v1/coupon/Page", this.searchModel).then(res => {
        this.retData = res;
      });
    },
    add() {
      this.optype = 1;
      this.showDialog = true;
    },
    edit(item) {
      this.optype = 2;
      this.showDialog = true;
      this.currentModel = clone(item);
      if (item.AllowMinAmount > 0) this.isLimitAmount = 1;
      else this.isLimitAmount = 0;
    },
    save() {
      this.currentModel.StartDate = this.dateRange[0];
      this.currentModel.EndDate = this.dateRange[1];
      if (this.isLimitAmount == 0) this.currentModel.AllowMinAmount = 0;
      if (this.optype == 1) {
        this.$api.post("sells/v1/coupon/Post", this.currentModel).then(res => {
          this.resetForm();
          this.get();
        });
      } else {
        this.$api.post("sells/v1/coupon/put", this.currentModel).then(res => {
          this.resetForm();
          this.get();
        });
      }
    },
    resetForm() {
      this.$refs["inputForm"].resetFields();
      this.showDialog = false;
    },
    disabled(item) {
      this.$api.post("sells/v1/coupon/Disabled?id=" + item.Id).then(res => {
        this.get();
      });
    },
    give(row) {      
      this.giveModel.CouponId=row.Id;     
      this.$refs["userDrawer"].show();
    },
    onSelect(item) {
      this.giveModel.UserId=item.Id;
      this.$confirm("确定要赠送吗？").then(_ => {
        this.$api.post("sells/v1/coupon/give", this.giveModel).then(res => {
          this.get();
        })
      })
    }
  }
};
</script>

<style scoped>
.xeBtnDefault {
  outline: medium;
  color: #666;
  background: #fafbfc;
  border: 1px solid #e5e7eb;
}

.btnMid {
  display: inline-block;
  width: 90px;
  height: 36px;
  padding: 0;
  text-align: center;
  line-height: 36px;
  border-radius: 2px;
  cursor: pointer;
  font-size: 14px;
}
.el-radio {
  padding: 5px 0;
}
</style>
