var auth = require("../../../utils/auth.js"),
	rq = require("../../../utils/request.js");
var e = getApp(),
	app = e,
	s = e.globalData.setting,
	os = s,
	app_d = e.globalData;
var regeneratorRuntime = require('../../../utils/runtime.js');
var ut = require("../../../utils/util.js");

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		iurl: s.imghost,
		is_show: 0, //显示弹框
		user: app_d.userInfo,
		zl_act: null, //助力活动
		tasking: 0, //助力任务ID
		userId: 0, //任务用户的ID
		s_num: 0, //分享的人数
		taskId:0, //活动从表ID
    helpId:0,
    invalidTime:0, //任务失效时间戳
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
    console.log('助力——————————————————————————————');
    console.log(options);
		var th = this;

		var nav_b = th.selectComponent("#nav_b"); //组件的id
		nav_b.set_name("助力", "/pages/user/assistance/assistance");

		if (options.tasking) this.data.tasking = options.tasking;
		if (options.userId) this.data.userId = options.userId;
		//任务是不是为空
		if (this.data.tasking == 0 || this.data.tasking == undefined) {
      this.data.tasking = decodeURIComponent(options.scene);
		}

	},

	//  跳转到助力首页,我也要参加
	participate_activity: function() {
		wx.navigateTo({
			url: '/pages/user/assistance/assistance',
		})
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
        getApp().check_can_share();
		//--先判断会员状态--
		var user_info = getApp().globalData.userInfo;
		if (user_info == null || user_info.mobile == undefined || user_info.mobile == "" || user_info.mobile == null) {
			getApp().goto('/packageE/pages/togoin/togoin');
			return false;
		}


		var th=this;
		if (!th.data.user) th.setData({ user: getApp().globalData.userInfo});
		if (this.data.tasking != null && this.data.tasking != 'undefined' && this.data.tasking != "" && this.data.tasking !=
			0) {
			getApp().request.promiseGet("/api/weshop/marketing/help/help/tasking/get", {
				data: {
					taskingId: th.data.tasking,
					storeId: os.stoid
				}
			}).then(res => {
        console.log('任务时间-------------------------');
        console.log(res);
				if (res.data.code == 0) {

					th.data.userId = res.data.data.userId;
					th.data.taskId = res.data.data.helpTaskId;
          th.data.helpId=res.data.data.helpFormId;
          // th.invalidTime = res.data.data.invalidTime
          // th.setData({
          //   invalidTime:1662138310
          // })
          let openTime = res.data.data.openTime
          th.getInvalidTime(openTime)
					//获取活动的时间
					return getApp().request.promiseGet("/api/weshop/marketing/help/help/act/get", {
						data: {
							helpId: res.data.data.helpFormId,
							storeId: os.stoid
						}
					})
				}
				return ut.null_promise();
			}).then(res => {
				if (res.data.code == 0) {
					th.setData({
						zl_act: res.data.data
					})
					//获取活动的参与的人数
					return getApp().request.promiseGet("/api/weshop/marketing/help/involve/help/act/people/count", {
						data: {
							helpId: res.data.data.id,
							storeId: os.stoid,
							taskId:th.data.taskId,
						}
					})
				}
				return ut.null_promise();
			}).then(res => {
				if (res && res.data && res.data.code == 0) {
					th.setData({
						s_num: res.data.data.countAll
					});
				}
			})

		}
  },
  getInvalidTime(openTime=0){
    let th = this
    rq.get("/api/weshop/marketing/giftbag/helpinfo/get", {
      data: {
        storeId: os.stoid,
        // userId: user_id,
        taskId: th.data.taskId,
        helpId:th.data.helpId
      },
      success: function (res) {
        console.log('获取失效时间------');
        console.log(res);
        if (res.data.code == 0) {
          let validTime=res.data.data.validTime
          let invalidTime =0
          if (openTime && validTime > 0) {
            invalidTime = openTime*1 + validTime*60*60
          }
          th.setData({
            invalidTime
          });
        }
      }
    })
  },
	//点击开启礼盒
	cklie_button: function() {
    //判断任务是否失效
    let invalidTime = this.data.invalidTime
    let nowTime = parseInt((new Date().getTime())/1000)  
    if (invalidTime > 0) {
      let disTime = invalidTime - nowTime
      if (disTime <=0) {
        getApp().showWarning("该任务已失效");
        return
      }
    }
    
		var th = this;
		//--先判断会员状态--
		var user_info = getApp().globalData.userInfo;
		if (user_info == null || user_info.mobile == undefined || user_info.mobile == "" || user_info.mobile == null) {
			wx.navigateTo({
				url: '/packageE/pages/togoin/togoin',
			})
			return false;
		}
		var t_user_id = app_d.user_id;
		var data = {
			id: this.data.tasking,
			storeId: os.stoid,
			userId: this.data.userId,
			zlUserId: t_user_id,
			isWeappUser:1,   //是不是小程序的入口，不需要判断
		};
		console.log(data);
		getApp().request.json_post("/api/weshop/marketing/help/help/task/involve/insert",
			data, //入参
			function(res) { //成功
				if (res.data.code == 0)
					th.setData({
						is_show: 1
					});
				else {
					getApp().my_warnning(res.data.msg, 0, th)
				}
			},
			function() { //失败

			}
		)

	},
	//点击关闭礼盒
	clike_none: function() {
		this.setData({is_show: 0});
		getApp().goto("/pages/user/assistance/assistance_success?helpid="+this.data.helpId+"&taskId="+this.data.taskId);
	},
	//导航球
	close: function() {
		var th = this;
		var nav_b = th.selectComponent("#nav_b"); //组件的id
		nav_b.close_box();
    nav_b.set_name("助力", "/pages/user/assistance/assistance");
	},


	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {
        getApp().globalData.no_clear=1;
	},

})
