const navigationBarHeight = (getApp().statusBarHeight + 50) + 'px'
var config = require("../../../config/config.js");
var app = getApp();
const Http = require("../../../utils/HttpBasics");
const util = require("../../../utils/util");
const imgurl = require("../../../utils/imgurl");
Page({
  data: {
    couponIds:"",
    couponChannelId:"",
    showPage:false,
    data:{},
    spuIdObj:{},
    priceAndStockObj:{},
    validStartDate:"",
    validEndDate:"",

  },
  onLoad: function (options) {
    console.log(options.couponChannelId);
    this.getDetail(options.couponChannelId)

  },
  getDetail: function (couponChannelId, flag) {
    let that = this;
    let data = {};

    // that.getCouponPriceAndStock(couponChannelId);//获取券价格和库存
    if (that.data.couponIds) {
      data.couponId = couponChannelId
    } else {
      data.couponChannelId = couponChannelId
    }
    var parmer = {
      url: config.api.newCouponDetail,
      data: data
    };
    Http.get(parmer).then(res => {

        that.setData({
          couponId: res.data.couponId
        })
        /**
         * activityStatus==0 活动未开始
         * activityStatus==1 活动已开始
         * flag == endclock 说明倒计时已经结束
         */
        if (res.data.endTime && res.data.beginTime) {
          that.setData({
            begin_time: res.data.beginTime,
            end_time: res.data.endTime,
            activityStatus: res.data.actStatus ? res.data.actStatus : ''
          })
          if (res.data.actStatus == 0 && flag != 'endclock') {
            that.countdown(res.data.beginTime);
          } else if (res.data.actStatus != 0 && flag != 'endclock') {
            that.countdown(res.data.endTime);
          } else {
            clearInterval(that.data.setInterval)
          }
          if (res.data.actStatus == 0) {
            var beginTime = util.formatTime(res.data.beginTime, "yyyy-MM-dd hh:mm:ss");
            if (util.timechuo(beginTime).indexOf('-') == 0) {
              that.setData({
                beginTime: "活动已结束",
              });
            } else {
              that.setData({
                beginTime: util.timechuo(beginTime)
              });
            }
          } else {
            var endTime = util.formatTime(res.data.endTime, "yyyy-MM-dd hh:mm:ss");
            if (util.timechuo(endTime).indexOf('-') == 0) {
              that.setData({
                endtime: "活动已结束",
              });
            } else {
              that.setData({
                endtime: util.timechuo(endTime)
              });
            }
          }
        }
        tt.hideLoading();
        that.setData({
          data: res.data,
        });
        if (res.data.type == 10) {
          that.setData({
            salePriceStr: res.data.salePriceStr,
            pickEndDate: util.formatTime(res.data.pickEndDate, "yyyy-MM-dd"),
            pickStartDate: util.formatTime(res.data.pickStartDate, "yyyy-MM-dd"),
            priceStr: res.data.priceStr,
            tailPriceStr: res.data.tailPriceStr,
            origPriceStr: res.data.origPriceStr
          })
        }
        if (res.data.validType == 1) {
          that.setData({
            validStartDate: util.formatTime(res.data.validStartDate, "yyyy-MM-dd"),
            validEndDate: util.formatTime(res.data.validEndDate, "yyyy-MM-dd"),
            pickStartDate: util.formatTime(res.data.pickStartDate, "yyyy-MM-dd"),
            pickEndDate: util.formatTime(res.data.pickEndDate, "yyyy-MM-dd"),
          });
        } else {
          if (res.data.validDays) {
            that.setData({
              validDays: res.data.validDays
            });
          }
        }

        if (res.data.itemGroup) {
          let tempObj = this.data.data
          tempObj.itemGroup = JSON.parse(tempObj.itemGroup)
          this.setData({
            data: tempObj
          })
          console.log(this.data.data, "data");
        }

        if (res.data.productAttrs && res.data.skuAttrs) {
          let tempObj = this.data.data
          tempObj.productAttrs = JSON.parse(tempObj.productAttrs)
          tempObj.skuAttrs = JSON.parse(tempObj.skuAttrs)
          tempObj.productAttrs.map(item => {

            if (item.key == "appointment") {
              let tempAppoinObj = JSON.parse(item.data)
              if (tempAppoinObj.need_appointment) { 
                let subscribeSing = "";
                if (tempAppoinObj.ahead_time_type == 1) {
                  subscribeSing = `需提前${tempAppoinObj.ahead_day_num}天致电商家预约`
                } else if (tempAppoinObj.ahead_time_type == 2) {
                  subscribeSing = `需提前${tempAppoinObj.ahead_hour_num}小时致电商家预约`
                } else if (tempAppoinObj.ahead_time_type == 3) {
                  subscribeSing = `需提前${tempAppoinObj.ahead_minute_num}分钟致电商家预约`
                }
                tempObj.subscribeSing = subscribeSing
              }

            }

            if (item.key == "Notification") {
              if (item.data != '') {
                tempObj.curLsit = JSON.parse(item.data)
              }
            }
            if (item.key == "bring_out_meal") {
              tempObj.besides = item.data
            }
            if (item.key == "free_pack") {
              tempObj.pack = item.data
            }

            if (item.key == "superimposed_discounts") {
              tempObj.superimposed_discounts = item.data
            }
            if (item.key == "private_room") {
              tempObj.private_room = item.data
            }
            if (item.key == "rec_person_num_max") {
              tempObj.rec_person_num_max = item.data
            }


            if (item.key == 'Description') {
              tempObj.Description = JSON.parse(item.data)
            }
            if (item.key == "can_no_use_date") {
              tempObj.can_no_use_date = JSON.parse(item.data)
            }
          })

          tempObj.skuAttrs.map(item => {
            if (item.key == "commodity") {
              if (item.data != '') {
                tempObj.itemGroup = JSON.parse(item.data)
              }
            }
          })
          this.setData({
            data: tempObj
          })
          console.log(this.data.data, "data");
        }
      }).catch(err => {
        tt.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
  },
})