import dayjs from 'dayjs';
import {
  couponsData
} from './mock';
import {
  fetchCouponList
} from '~/services/coupon/index';
const emptyCouponImg = `https://cdn-we-retail.ym.tencent.com/miniapp/coupon/ordersure-coupon-newempty.png`;

Component({
  properties: {
    storeId: String,
    promotionGoodsList: {
      type: Array,
      value: [],
    },
    orderSureCouponList: {
      type: Array,
      value: [],
    },
    couponsShow: {
      type: Boolean,
      value: false,
      observer(couponsShow) {
        if (couponsShow) {
          const {
            promotionGoodsList,
            orderSureCouponList,
            storeId
          } =
          this.data;
          const products =
            promotionGoodsList &&
            promotionGoodsList.map((goods) => {
              this.storeId = goods.storeId;
              return {
                skuId: goods.skuId,
                spuId: goods.spuId,
                storeId: goods.storeId,
                selected: true,
                quantity: goods.num,
                prices: {
                  sale: goods.settlePrice,
                },
              };
            });
          const selectedCoupons =
            orderSureCouponList &&
            orderSureCouponList.map((ele) => {
              return {
                promotionId: ele.promotionId,
                storeId: ele.storeId,
                couponId: ele.couponId,
              };
            });
          this.setData({
            products,
          });
          this.coupons({
            products,
            selectedCoupons,
            storeId,
          }).then((res) => {
            // console.log(res,'res来咯');
            // this.initData(res);
          });
        }
      },
    },
  },
  data: {
    emptyCouponImg,
    goodsList: [],
    selectedList: [],
    couponsList: [],
    orderSureCouponList: [],
    promotionGoodsList: [],
  },
  methods: {
    initData(data = {}) {
      const {
        couponResultList = [], reduce = 0
      } = data;
      console.log(data,'data来咯');
      const selectedList = [];
      let selectedNum = 0;
      const couponsList =
        couponResultList &&
        couponResultList.map((coupon) => {
          console.log(coupon,'优惠价');
          if (coupon.status === 1) {
            selectedNum++;
            selectedList.push({
              couponId: coupon.key,
              // promotionId: ruleId,
              storeId: this.storeId,
            });
          }
          return {
            shopGuid: coupon.shopGuid,
            key: coupon.key,
            title: coupon.title,
            isSelected: false,
            timeLimit: coupon.timeLimit,
            value: coupon.value,
            status: coupon.status === 1 ? 'default' : 'useless',
            desc: coupon.desc,
            type : coupon.type,
            tag: '',
          };
        });
      this.setData({
        selectedList,
        couponsList,
        reduce,
        selectedNum,
      });
    },
    selectCoupon(e) {
      const {
        key
      } = e.currentTarget.dataset;
      const {
        couponsList,
        selectedList
      } = this.data;
      couponsList.forEach((coupon) => {
        if (coupon.key === key) {
          coupon.isSelected = !coupon.isSelected;
        }
      });

      const couponSelected = couponsList.filter(
        (coupon) => coupon.isSelected === true,
      );

      this.setData({
        selectedList: [...selectedList, ...couponSelected],
        couponsList: [...couponsList],
      });

      this.triggerEvent('sure', {
        selectedList: [...selectedList, ...couponSelected],
      });
    },
    hide() {
      this.setData({
        couponsShow: false,
      });
    },
    coupons(coupon = {}) {
      return new Promise((resolve, reject) => {
        console.log(coupon,'coupon里有什么');
        const {
          storeId
        } =
        this.data;
        console.log(storeId);
        if (coupon?.selectedCoupons) {
          let data = {
            couponResultList: [],
            reduce: 1000,
          }
          // 优惠券接口
            fetchCouponList({
              shopGuid: storeId,
              status: 1
            }).then((res) => {
              data.couponResultList = res
              this.initData(data);
            });

          resolve({
            couponResultList: data,
            reduce: 1000,
          });

        }
        return reject({
          couponResultList: [],
          reduce: undefined,
        });
      });
    },
  },
});