var t = getApp(), a = t.request, o = t.globalData.setting, os = o,
  i = require("../../../../utils/util.js"), ut = i, s = require("../../../../utils/common.js");
var regeneratorRuntime = require('../../../../utils/runtime.js');


Page({
  data: {
    ylburl: "https://mshopimg.yolipai.net/",//有礼派图片地址
    pt_act: null,//拼单数据
    baseUrl: "/api/weshop/teamgroup/page?1=1",
    resourceUrl: t.globalData.setting.resourceUrl,
    currentPage: 1,
    requestData: null,
    store_id: 1,
    team_id:0,
    requestUrl: "",
    teamgroup:null,
  },

  onLoad: function (t) {

    wx.setNavigationBarTitle({ title: "开团列表", })

    var th=this;
    //初始化加載更多
    th.data.requestData=new Array();  
    var team_id=t.team_id;
    var teamlist=null;
    th.data.team_id = team_id;

   //获取活动表的ct_num
    getApp().request.promiseGet("/api/weshop/teamlist/get/" + os.stoid + "/" + team_id, {
      data: {}
    }).then(res => {
      if (res.data.code == 0 && res.data.data) {
        teamlist = res.data.data;

        //是不是限定连接了
        if(teamlist.is_share){
          getApp().confirmBox('该活动已经限定链接参团',()=>{
            getApp().goto('/pages/index/index/index');
          })
          return false;
        }
        var jt_team_num=teamlist.ct_rylist;
        if (jt_team_num != "" && jt_team_num != null && jt_team_num!= undefined) {
          var team_num_arr = JSON.parse(jt_team_num);
          var max_num = 0;
          team_num_arr.forEach(function (val, ind) {
            if (val.rynum > max_num) max_num = val.rynum;
          })
          teamlist.max_num = max_num;
        }

         th.setData({ pt_act: teamlist });
         th.init(team_id);
      }
    })
  },

  //加载更多的函数
  async init(team_id) {
         
     var teamgroup_arr = [];//活動从表
     var teamgroup = null,//活動从表
      user = null,
      ee = this, th=ee,
      pt_status = 1;//已支付
	 	  
	  var req_data={
		 store_id: os.stoid, pageSize:6,page:ee.data.currentPage,
		 state: 2, team_id: team_id,ordernum:1
	  }
	  
	  if(this.data.pt_act.kttype==3 && this.data.pt_act.max_num){
		 req_data.max_num=this.data.pt_act.max_num; 
	  }
	  

    //获取活动从表信息team_id
    await getApp().request.promiseGet("/api/weshop/teamgroup/page", {
      data: req_data
    }).then(res => {
      if(ut.ajax_ok(res)){
        teamgroup = res.data.data.pageData;
        console.log(teamgroup);
        ee.data.currentPage++;
      }

    })

    //获取订单的总数这个接口不能用
    for (i = 0; i < teamgroup.length; i++) {
      await getApp().request.promiseGet("/api/weshop/rechargeServicelist/page", {
        data: { store_id: os.stoid, pt_status: 1, pt_listno: teamgroup[i].listno }
      }).then(res => {
        if(ut.ajax_ok(res)){
          var order = res.data.data.pageData;
          teamgroup[i].open_num = order.length;
        }
      })

      if(!teamgroup[i].open_num) continue;

      await getApp().request.promiseGet("/api/weshop/users/get/" + os.stoid + "/" + teamgroup[i].openvipid, {
      }).then(res => {
        var user = res.data.data;
        teamgroup[i].user = user;
      })

      teamgroup_arr.push(teamgroup[i]);
    }
    // //设置值
    // for(var ii=0;ii<teamgroup.length;ii++){
    //   th.data.requestData.push(teamgroup[ii]);
    // }
    if(teamgroup_arr.length){
      th.setData({ teamgroup: teamgroup_arr});
      ee.countDown2();
    }
  },
   //滚动到底部
  onReachBottom: function () {
    this.init(th.data.team_id);
  },

  resetData: function () {
    n.resetConfig(), this.data.teamgroup = null, this.data.currentPage = 1;
  },

  //---小于10的格式化函数----
  timeFormat(param) {
    return param < 10 ? '0' + param : param;
  },

  //----拼单倒计时函数-----
  countDown2() {
    var th = this;
    // 获取当前时间，同时得到活动结束时间数组
    var newTime = ut.gettimestamp();
    var List = this.data.teamgroup;
	if(!List || List.length<=0) return false;

    for (var j = 0; j < List.length; j++) {
      // 对结束时间进行处理渲染到页面
      var endTime = List[j].kt_end_time;
      let obj = null;
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        let time = (endTime - newTime);
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let hou = parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: this.timeFormat(day),
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        }
      } else {
        //活动已结束，全部设置为'00'
        obj = {
          day: '00',
          hou: '00',
          min: '00',
          sec: '00'
        }
      }
      var txt = "teamgroup[" + j + "].djs";
      th.setData({
        [txt]: obj
      });
    }

    setTimeout(th.countDown2, 1000);
  },

  //--跳转到加载更多--
  go_team_show:function (e) {

    var th=this;
    var tid=e.currentTarget.dataset.tid;
    var team_id=e.currentTarget.dataset.team_id;
    var oo=getApp().globalData;

    getApp().request.get("/api/weshop/rechargeServicelist/page", {
      data: {
        prom_id: team_id,
        user_id: oo.user_id,
        store_id: os.stoid,
        pageSize: 1,
        page: 1
      },
      success: function(e) {
        //--跳转到已经购买的情况--
        if (e.data.code == 0 && e.data.data.pageData.length > 0) {
          var odr = e.data.data.pageData[0];
          //还未支付
          if (odr.pt_status == 0 && odr.order_status == 1) {
            wx.navigateTo({
              url: "/packageA/pages/details_serviceCard/details_serviceCard/details_serviceCard?order_id=" + odr.order_id,
            });
          }else if (odr.pt_status == 1 && odr.order_status == 1){
            wx.navigateTo({
              url: "/packageA/pages/serviceCard_pd/team_success/team_success?ordersn=" + odr.order_sn,
            });
          }
        }
        //--跳转到参团--
        else if(e.data.code == 0){
          wx.navigateTo({
            url: "/packageA/pages/serviceCard_pd/team_show/team_show?tg_id="+tid+"&goods_id="+th.data.pt_act.goods_id,
          });
        }

      }
    });

  }
 
})