// pages/spellDetail/index.js
const navigationBarHeight = (getApp().statusBarHeight + 50) + 'px'
var config = require("../../config/config.js");
const Http = require("../../utils/HttpBasics");
const imgurl = require("../../utils/imgurl");
const { spellStatus } = require("../../utils/spell");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationBarHeight,
    orangeImg: imgurl.orange.url,
    blueImg: imgurl.blue.url,
    grayImg: imgurl.gray.url,
    tuanzhang: imgurl.tuanzhang.url,
    tuanzhang: imgurl.tuanzhang.url,
    spellBg: imgurl.spellBg.url,
    position: imgurl.position.url,
    close03: imgurl.close03.url,
    home: imgurl.wmhome.url,
    paramData: null,
    data: null,
    clock: "00",
    day: "0",
    hour: "00",
    min: "00",
    sec: "00",
    spellStatusList: spellStatus,
    isMyself: false,
    showAlert: false,
    goHomeUrl:"",
  },
  gotoAgain() {
    let data = this.data.data;
    wx.navigateTo({
      url: `/pages/spellGroup/mySpellGroup/index?couponId=${data.couponId}&couponChannelId=${data.couponChannelId}`,
    })
  },
  getUserInfo: function () {
    let that = this;
    // 获取用户信息
    Http.get({
      url: config.api.getScore,
      data: {}
    })
      .then(res => {
        console.log(res)
        that.setData({
          nickName: res.data.nickName,
          avatarUrl: res.data.avatarUrl
        })
      })
  },
  gotoIndex(){
    let this_ = this
    wx.switchTab({
      url: this_.data.goHomeUrl,
    })
  },
  gotoshare() {
    wx.navigateTo({
      url: `/pages/spellGroup/mySpellGroup/index?orderId=${this.data.paramData.orderId}&orderGroupId=${this.data.paramData.orderGroupId}&couponChannelId=${this.data.paramData.couponChannelId}`,
    })
  },
  gotoSearch() {
    wx.navigateTo({
      url: `/pages/spellGroup/spellGroup`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.spellStatusList, options, 7777777)
    this.setData({
      paramData: options,
      goHomeUrl: app.globalData.goHomeUrl,
    })
    this.getUserInfo();
    if (options.orderGroupId){
      if (options.orderId && options.couponId){
        this.getDetail(options);
      }
      this.checkUser(options);
    }

    // 关闭来自于左上角的分享
    wx.hideShareMenu()
  },
  /**
   * 判断用户是否已经在团中
   */
  checkUser(options) {
    let that = this;
    Http.get({
      url: config.api.queryAttendStatus,
      data: {
        id: options.orderGroupId,
      }
    }).then(res => {
      wx.stopPullDownRefresh();
      console.log(res.data, 3333333333)
      that.setData({
        isMyself: res.data.attend
      })
    });
  },
  /**
   * 去使用
   */
  gotoUse() {
    wx.navigateTo({
      url: `/pages/couponorder/index/index`
    });
  },
  /**
   * 拼团状态字段转换
   */
  changeSatus(status) {
    console.log(spellStatus, 8888888)
    return spellStatus.filter(item => item.value == status)[0].name;
  },
  // 时间格式化输出，如11:03 25:19 每1s都会调用一次
  dateformat(micro_second) {
    // 总秒数
    var second = Math.floor(micro_second / 1000);
    // 天数
    var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
    // 小时
    var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
    // 分钟
    var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
    // 秒
    var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);

    // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
    return {
      a1: day,
      b1: hr,
      c1: min,
      d1: sec
    }
  },
  countdown(time) {
    let that = this;
    var EndTime = time;
    if (that.data.data != null && that.data.data.status==10) {
      EndTime = that.data.data.expiredDate;
    }
    var NowTime = new Date().getTime();
    var total_micro_second = EndTime - NowTime || [];
    // 渲染倒计时时钟
    let obj = that.dateformat(total_micro_second);
    console.log(obj)
    if (total_micro_second > 0) {
      that.setData({
        clock: obj,
        day: obj.a1,
        hour: obj.b1,
        min: obj.c1,
        sec: obj.d1,
      })
    } else {
      that.setData({
        // data:null,
        clock: "00",
        day: "00",
        hour: "00",
        min: "00",
        sec: "00",
      })
      // that.getOneSpell(that.data.couponId)
    }
    if (that.data.showTime) {
      setTimeout(function () {
        total_micro_second -= 1000;
        that.countdown();
      }, 1000)
    }
  },
  //关闭弹框
  gotoClose() {
    this.setData({
      showAlert: false
    })
  },
  //跳转拼团列表
  gotoSpellList() {
    wx.navigateTo({
      url: '/pages/spellGroup/spellGroup',
    })
  },
  getDetail() {
    let that = this;
    Http.get({
      url: config.api.queryOrderGroupStatus,
      data: {
        orderId: this.data.paramData.orderId,
        couponId: this.data.paramData.couponId,
        id: this.data.paramData.orderGroupId
      }
    }).then(res => {
      wx.stopPullDownRefresh();
      let data = res.data;
      data.statustext = that.changeSatus(data.status);
      if (data.status ==10) {
        that.countdown(data.expiredDate);
      }
      data.salePrice = (data.salePrice / 100).toFixed(2)
      for (let i = 0; i < data.remainPeople; i++) {
        let a = {};
        data.userList.push(a)
      }
      if (res.data.couponStatus != 0) {
        that.setData({
          showAlert: true
        })
      }
      console.log(data,3333333333333)
      that.setData({
        data: data
      })
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      showTime: true
    })
    if (this.data.data != null&&this.data.data.status ==10) {
      this.countdown()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      showTime: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.setData({
      showTime: false
    })
  },  

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getDetail(this.data.paramData);
    this.checkUser(this.data.paramData)
  },
  /**
   * 用户点击右上角分享 
   */
  // paramData
  onShareAppMessage: function (res) { 
    let _this = this;
    app.globalData.previewFlag = true
    if (res.from === 'button') {
      console.log(`/pages/index/index?couponId=${_this.data.data.couponId}&orderGroupId=${_this.data.data.orderGroupId}&couponChannelId=${_this.data.paramData.couponChannelId}&orderId=${_this.data.data.orderId}&avatarUrl=${_this.data.avatarUrl}&nickName=${_this.data.nickName}`,)
      // 来自页面内转发按钮
      
      return {
        title: _this.data.nickName + '超值推荐的' + _this.data.data.title,
        path: `/pages/index/index?couponId=${_this.data.data.couponId}&orderGroupId=${_this.data.data.orderGroupId}&couponChannelId=${_this.data.paramData.couponChannelId}&orderId=${_this.data.data.orderId}&avatarUrl=${_this.data.avatarUrl}&nickName=${_this.data.nickName}`,
        imageUrl: _this.data.data.coverImg,
        success: function (res) {
          // 转发成功
        },
        fail: function (res) {
          // 转发失败
        }
      }
    } else {
      console.log(res, 444444444444444444)
    }
  }
})