<template>
  <view class="page" :style="{'height':windowHeight}">

        <view class="flex-row">
            <view class="flex-col justify-center" @click="noUseChange">
                <u-icon v-if="noUse" name="checkmark-circle-fill" color="#38db38" size="25" ></u-icon>
                <u-icon v-else name="/static/order/ud9.png" color="green" size="25"></u-icon>
            </view>
            <view class="noUse">
                <text>不使用优惠券</text>
            </view>
        </view>
        <view  :style="{'height':scrollViewHeight}">
            <scroll-view class="scroll " :style="{'height':scrollViewHeight}" scroll-y>
                <view  class="row-list flex-row justify-start" :class="{'grayscale' : item.discountsPrice*1 >standardPrice*1 }" v-for="(item,index) in countsList" @click="clickCoupons(item,index)" :key="index">
                    <view class="h-text flex-col justify-center ">
                        <view class="money">
                            <text>￥{{item.discountsPrice}}</text>
                        </view>
                        <view class="desc">
                            <text>{{item.name}}</text>
                        </view>
                    </view>

                    <view class="h-center-content flex-col ">
                        <view class="h-value">
                            <text>门店通用</text>
                        </view>
                        <view class="title">
                            使用平台：<text v-if="item.strategyType == '0'">全平台</text>
                            <text v-else-if="item.strategyType == '1'">个人优惠卷</text>
                        </view>
                        <view class="title">
                            使用次数：{{item.degree}}
                        </view>
                        <view class="title">
                            到期时间：{{item.endTime.substring(0,10)}}
                        </view>
                    </view>

                    <view class="h-right-content flex-col justify-center">

                        <view class="flex-row justify-center" v-if="item.discountsPrice*1 >standardPrice*1 ">
                            <view class="h-btn-img">
                                <image class="h-btn-img" src="/static/coupon/u2110.png" mode=""></image>
                            </view>
                        </view>

                        <view class="flex-row justify-center" v-else>
                            <u-icon v-if="item.isUse " name="checkmark-circle-fill" color="#38db38" size="25"></u-icon>
                            <u-icon v-else name="/static/order/ud9.png" color="green" size="25"></u-icon>
                        </view>

                    </view>
                </view>
            </scroll-view>
        </view>
        <view>
            <view class="btn" @click="useCoupons">
                <text>确定</text>
            </view>
        </view>


  </view>
</template>
 <script>
export default {
  data() {
    return {
        countsList:[],
        noUse:true,
        windowHeight:'',
        scrollViewHeight:'',
        standardPrice:0,
    };
  },
  onLoad(e) {
      let sysInfo = uni.getSystemInfoSync()
      this.windowHeight =sysInfo.windowHeight +'px'
      this.scrollViewHeight =sysInfo.windowHeight -(sysInfo.windowWidth/750)*252 +'px'
      this.getMyCouponList()

      if (e.standardPrice){
          console.log('++++++++++++++', e.standardPrice)
          this.standardPrice = e.standardPrice
      }

  },
  computed: {

  },
  methods: {

      useCoupons(){
          let  useCoupons = []
          for (const useCoupon of this.countsList) {
              if (useCoupon.isUse){
                  useCoupons.push(useCoupon)
              }
          }
          uni.$emit('getUseCoupons',useCoupons)
          uni.navigateBack({
              data:1
          })
      },

      noUseChange(){
          this.noUse = !this.noUse
          if (this.noUse){
             this.countsList.forEach(item=>{
                 item.isUse = false
             })
          }
      },
      clickCoupons(item,index){

          if (item.discountsPrice*1 > this.standardPrice*1){
              return
          }

          //关闭多选开始
          this.countsList.forEach(item=>{
              item.isUse = false
          })
          //关闭多选结束

          console.log(item)
          this.noUse = false
          item.isUse = !item.isUse
          this.$set(this.countsList,index,item)

          let flag = true
          this.countsList.forEach(item=>{
             if (item.isUse){
                 flag = false
             }
          })
          this.noUse = flag

      },


      // 查询可领取的优惠劵
      getMyCouponList(){
          let that = this;
          // 用户绑定门店
          this.$api.getMyCouponList({
              type:0,
              pageNum: 1,
              pageSize: 1000,
          }).then((res)=>{
              console.log(res)
              this.countsList = res.data.rows;
          }).catch(() =>{
              uni.showToast({
                  title: "操作失败"
              })
          })
      },

  },
};
</script>
 <style lang="scss" scoped>
     @import '/common/css/common.css';
     @import './index.rpx.scss';
</style>
