<template>
  <view class="couponStyle" :style="{ height: contentHeight }">
    <u-tabs
      :list="list"
      :is-scroll="false"
      :current="msg.UseState"
      @change="change"
      :active-color="mainColor"
      :show-bar="showBar"
      height="100"
    ></u-tabs>
    <u-empty v-if="g.length == 0" text="暂无相关优惠券" mode="coupon"></u-empty>

    <view
      v-if="g.length > 0"
      style="
        height: calc(100vh - 50px);
        width: calc(100vw);
        overflow: hidden;
        padding-bottom: 50px;
      "
    >
      <scroll-view
        :scroll-y="true"
        :enable-back-to-top="true"
        :enable-flex="true"
        @scrolltolower="lower"
        :style="{ height: '100%' }"
      >
        <view class="couponList">
          <view class="item" v-for="(x, i) in g" :key="i" @click="godetails(x)">
            <view :class="msg.UseState == 0 ? 'ticket' : 'ticket2'">
              <view class="miane">
                <view class="money">
                  <!-- <text style="font-size: 44px;">{{x.sub_price.toString().split('.')[0]}}</text>
		          <text  v-if="x.coupon.type==1">折</text> -->
                  <text class="small" v-if="x.CouponType == 1">￥</text>
                  <Text style="font-size: 44px"
                    >{{ Number(x.DiscountsPrice) }}
                  </Text>
                  <Text class="small">{{ x.CouponType == 2 ? "折" : "" }}</Text>
                </view>
                <view class="tiaojian"
                  >满{{ Number(x.MinConsumePrice) }}元可用</view
                >
              </view>
              <view
                style="
                  width: 10px;
                  height: 10px;
                  border-radius: 5px;
                  background: #f3f4f6;
                  position: absolute;
                  right: -5px;
                  top: -5px;
                "
              ></view>
              <view
                style="
                  width: 10px;
                  height: 10px;
                  border-radius: 5px;
                  background: #f3f4f6;
                  position: absolute;
                  right: -5px;
                  bottom: -5px;
                "
              ></view>
            </view>
            <view class="detail">
              <view class="coupond">
                <view class="name">{{ x.Name }}</view>
                <view class="date">{{ x.StartDate + "-" + x.EndDate }}</view>

                <Text class="rule" v-if="x.UseTypeStr == 1">限商品类别</Text>
                <Text class="rule" v-if="x.UseTypeStr == 2">限商品</Text>
                <Text class="rule" v-if="x.UseTypeStr == 3">全场通用</Text>
                <Text class="rule" v-if="x.UseTypeStr == 4">当面付</Text>
              </view>
            </view>
          </view>
        </view>

        <u-loadmore
          :status="status"
          :load-text="loadText"
          :font-size="24"
          :margin-top="20"
          :margin-bottom="20"
          bg-color="#f3f4f6"
        />
      </scroll-view>
    </view>
    <view
      style="
        width: 100%;
        height: 50px;
        background: #ffffff;
        display: flex;
        align-items: center;
        justify-content: center;
        position: absolute;
        left: 0;
        bottom: 0;
      "
      @click="goUrl"
    >
      <Text>去领券</Text>
    </view>
    <!-- 加载中 -->
    <view class="loading" v-if="loading">
      <u-loading mode="flower" size="48">></u-loading>
      <Text style="color: #fff; margin-top: 10rpx">加载中...</Text>
    </view>
    <!-- 取消提示 -->
    <u-toast ref="uToast" />
    <auth
      v-if="showAuth"
      @changeuserinfo="reloadUserinfo"
      @gbAuth="gbAuth"
    ></auth>
  </view>
</template>

<script>
import auth from "../../../components/auth/index.vue";
export default {
  components: {
    auth,
  },
  data() {
    return {
      pageTitle: "我的优惠券",
      showAuth: false,
      U: {},
      list: [{ name: "未使用" }, { name: "已使用" }, { name: "已过期" }],
      mainColor: "",
      contentHeight: 0,

      msg: {
        pageIndex: 1,
        pageSize: 10,
        UseState: 0,
      },
      page_count: 1,
      g: [],
      loading: false,
      status: "loadmore",
      loadText: {
        loadmore: "轻轻上拉，加载更多",
        loading: "努力加载中",
        nomore: "没有更多了",
      },
      showBar: true,
    };
  },
  created() {
    this.contentHeight = this.$utils.calcContentHeight(-40) + "px";
    this.mainColor = this.$uiConfig.mainColor;
    // #ifdef MP-ALIPAY
    this.showBar = false;
    // #endif
  },
  mounted() {
    let currentPages = getCurrentPages();
    let u = "/" + currentPages[currentPages.length - 1].route;
    let pages = wx.getStorageSync("basedata")
      ? wx.getStorageSync("basedata").bar_title
      : [];
    pages.forEach((x) => {
      if (x.value == u) {
        this.pageTitle = x.new_name ? x.new_name : x.name;
      }
    });
    uni.setNavigationBarTitle({
      title: this.pageTitle,
    });
  },
  onLoad: function (option) {
    this.u = uni.getStorageSync("mall_UserInfo");
    if (!this.u) {
      this.u = {
        nickName: "未登录",
        avatarUrl: "",
      };
      this.showAuth = true;
    } else {
      this.init();
    }
  },
  methods: {
    change(index) {
      this.msg.UseState = index;
      this.msg.pageIndex = 1;
      this.g = [];
      this.loading = true;
      this.init();
    },
    init() {
      this.request2(
        {
          url: "/api/AppletUser/GetMemberCouponPageList",
          data: this.msg,
        },
        (res) => {
          if (res.resultCode == 1) {
            this.loading = false;
            this.g = this.g.concat(res.data.pageData);

            this.page_count = res.data.pageCount;
            if (this.page_count == 1) {
              this.status = "nomore";
            }
          }
        }
      );
    },
    lower(e) {
      if (this.msg.pageIndex < this.page_count) {
        this.msg.pageIndex++;
        this.status = "loading";
        this.init();
      } else {
        this.status = "nomore";
      }
    },
    reloadUserinfo() {
      this.u = uni.getStorageSync("mall_UserInfo");
      // this.showAuth=false;
      this.init();
    },
    //关闭登录窗口
    gbAuth() {
      uni.navigateBack();
    },
    godetails(item) {
      if (this.msg.UseState == 0) {
        uni.navigateTo({
          url:
            "/pages/coupon/details/details?item=" +
            encodeURIComponent(JSON.stringify(item)) +
            "&type=1",
        });
      }
    },
    goUrl() {
      uni.navigateTo({
        url: "/pages/coupon/list/list",
      });
    },
  },
};
</script>
<style>
.couponStyle {
  width: 100%;
  height: 100%;
  background: #f3f4f6;
  position: relative;
}

.couponStyle .loading {
  width: 180rpx;
  height: 180rpx;
  background: #000000;
  opacity: 0.7;
  border-radius: 10rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: fixed;
  left: 50%;
  top: 30%;
  margin-left: -100rpx;
  z-index: 999;
}
.couponStyle .couponList {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.couponStyle .couponitem {
  width: 94%;
  display: flex;
  align-items: center;
  height: 180rpx;
  background: #f2ebe4;
  margin-top: 20rpx;
}
.couponStyle .couponitem .couponitem_l {
  width: 180rpx;
  height: 180rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
  padding: 20rpx;
  color: #fff;
}
.couponStyle .couponitem .couponitem_r {
  width: calc(100vw - 220rpx);
  height: 180rpx;
  display: flex;
  flex-direction: column;
  padding: 20rpx;
  justify-content: space-around;
}

.couponStyle .item {
  display: flex;
  margin-top: 10px;
  align-items: flex-start;
  background: #fff;
  width: 94%;
  border-radius: 12rpx;
  overflow: hidden;
}
.couponStyle .item .ticket {
  position: relative;
  background-image: linear-gradient(
    to bottom right,
    rgb(117, 174, 246),
    rgb(56, 106, 244)
  );
  width: 125px;
  height: 100px;
  display: flex;
  align-items: center;
  color: #fff;
}
.couponStyle .item .ticket2 {
  position: relative;
  background: #c3c3c3;
  width: 125px;
  height: 100px;
  display: flex;
  align-items: center;
  color: #fff;
}
.couponStyle .item .miane .money {
  font-family: "oswald";
  font-weight: bold;
  size: 64px;
  width: 125px;
  text-align: center;
}
.couponStyle .item .miane .money .small {
  font-size: 20px;
}
.couponStyle .item .miane .tiaojian {
  font-size: 14px;
  width: 125px;
  text-align: center;
}

.couponStyle .item .detail {
  width: 100%;
  height: 100px;
  background: #fff;
  padding: 10px;
  display: flex;
}
.couponStyle .item .detail .coupond {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}
.couponStyle .item .detail .coupond .name {
  font-size: 16px;
  font-weight: 600;
  color: #000;
}
.couponStyle .item .detail .coupond .date {
  font-family: "23";
  margin-top: 5px;
  font-size: 12px;
  color: #777;
}
.couponStyle .item .detail .coupond .rule {
  font-size: 12px;
  color: #777;
  margin-top: 5 px;
}
.couponStyle .item .detail .chosen {
  width: 20px;
}
</style>
