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

Page({
  data: {
    url: os.imghost,
    temp_id: 0,
    goodsGroupArr: [],
    //满屏顶部导航参数------------
    is_full_screen_navigation: false, //是否满屏
    is_full_screen_navigation_index: -1, //广告轮播满屏下标
    nav_title: '首页',
    nav_frontColor: "#000",
    old_nav_frontColor: "",
    nav_backgroundColor: "#fff",
    nav_type: 1,  //导航类型
    searchbox_transparent: 1, //搜索框背景是否透明

    nav_but_color: '#32ACF9', //导航按钮颜色
    sele_pickup: null, //默认门店
    // nav_bg_color:'#e823e8', //导航按钮颜色
    //----------------
    is_show_top: 0, //是否置顶显示
    is_fixed_nav:0,//是否显示固定底部导航
  },

  onLoad: function (e) {
    var th = this;
    this.data.temp_id = e.scene;
    if (!this.data.temp_id)
      this.data.temp_id = e.sence;
    if (!this.data.temp_id) wx.showModal({ title: "未读取到模板ID", })

    var first_leader = e.first_leader;
    if (first_leader) {
      //--  user_id代过来免登录 --
      getApp().globalData.first_leader = first_leader;
      //调用接口判断是不是会员
      getApp().request.promiseGet("/api/weshop/shoppingGuide/get/" + os.stoid + "/" + first_leader, {}).then(res => {
        if (res.data.code == 0) {
          getApp().globalData.guide_id = res.data.data.id;
          getApp().globalData.guide_pick_id = res.data.data.pickup_id
        }
      })

    }

    getApp().getConfig(function (e) {
      //--首页的问题--
      if (getApp().globalData.config && getApp().globalData.config.store_name != undefined && getApp().globalData.config.store_name != null) {
        wx.setNavigationBarTitle({
          title: getApp().globalData.config.store_name,
        });
        // th.setData({
        //   nav_title: getApp().globalData.config.store_name
        // })
        th.setData({
          nav_title: th.data.share_title 
        })
      } else {
        wx.setNavigationBarTitle({
          title: getApp().globalData.setting.appName,
        });
        // th.setData({
        //   nav_title: getApp().globalData.setting.appName,
        // })
        th.setData({
          nav_title: th.data.share_title 
        })
      };
    });



  },

  async onShow() {
    getApp().check_can_share();
    var th = this;
    await this.init_load();
    //如果是自定义模板
    if (this.data.isTemplate) {

    }
  },
  //获取商品模块实例
  newGoodsGroup() {
    let arr = []
    if (this.data.template_arr.length == 0) {
      return
    }
    this.data.template_arr.map((item, index) => {
      if (item.ename == 'goodsGroup') {
        arr.push(`goodsGroup${index}`)
      }
      if (item.ename == 'goodsGroupNew') {
        arr.push(`goodsGroup${index}`)
      }
    })
    if (arr.length > 0) {
      this.setData({
        goodsGroupArr: arr
      })
      arr.map(item => {
        let a = '.' + item
        this[item] = this.selectComponent(a);
      })
    }
  },
  //当隐藏的时候就关闭计时器
  onHide: function () {

  },
  //同步初始加载
  async init_load() {
    var th = this;
    //因为营销版本的功能包含了自定义模板的功能，是同时的
    //读取全局是否有弄自定义模板
    await getApp().request.promiseGet("/api/weshop/store_module/get/" + th.data.temp_id + "/4", { 1: 1 }).then(res => {
      var data = res.data.data;
      if (data) {
        var temp_data = data;
        wx.setNavigationBarTitle({
          title: temp_data.page_title,
        });
        th.setData({
          nav_title: temp_data.page_title,
        })
        var t_arr = JSON.parse(temp_data.json_str);
        //增加判断是否是满屏
        let is_full_screen_navigation = false
        let is_full_screen_navigation_index = -1
        let nav_but_color = "#32ACF9"
        let left_arr = [] //左边悬浮
        let right_arr=[] //右边悬浮
        let floatw=null //悬浮窗口
        if (t_arr) {
          t_arr.map((item, index) => {
            if (item && item.content && item.content.full_screen == 1 && !is_full_screen_navigation) {
              is_full_screen_navigation = true
              is_full_screen_navigation_index = index
              try {
                nav_but_color = item.content.data[0].but_color
              } catch (error) { }
            }
            if (item.ename=='floatingWindow') {
              floatw = item
            }
            if (item.ename=='backTop' || item.ename=='onlineService' || item.ename=='scan' || item.ename=='floatingWindow') {
              right_arr.push(item)
            }
          })
        }
        if (floatw && floatw.content) {
          let fi = right_arr.findIndex(ite=>ite.ename=='floatingWindow')
          let datas={}
          if (!floatw.content.data) { //兼容旧的悬浮窗口
            datas.AppId= floatw.content.AppId
           datas.feedId= floatw.content.feedId
           datas.finderUserName= floatw.content.finderUserName
           datas.img=floatw.content.img
           datas.video_type= floatw.content.video_type
           datas.wxapp_url= floatw.content.wxapp_url
           datas.wxapp_url_title=floatw.content.wxapp_url_title
           floatw.content.data=[datas]
          }
          if (floatw.content.loaction_index==1) {
            right_arr.splice(fi,1,floatw)
            // right_arr.unshift(floatw)
          }else{
            left_arr.unshift(floatw)
            right_arr.splice(fi,1)
          }
        }
        if (left_arr.length>0) {
          let obj={
            ename:'floatingBox',
            content:left_arr,
            loaction_index:0
          }
          t_arr.push(obj)
        }
        if (right_arr.length>0) {
          let obj={
            ename:'floatingBox',
            content:right_arr,
            loaction_index:1
          }
          t_arr.push(obj)
        }
 
        th.setData({
          nav_but_color,
          is_full_screen_navigation,
          is_full_screen_navigation_index,
          share_title: temp_data.page_title,
          template_arr: t_arr,
          isTemplate: 1,
          bgcolor_t: temp_data.bkcolor,
          is_fixed_nav:temp_data.is_fixed_nav
        });
        setTimeout(() => {
          th.newGoodsGroup()
        }, 1000)

        //---如果有设定顶部的颜色的时候--
        if (temp_data.top_color && temp_data.top_color != 'null') {

          var top_w_color = "#ffffff";
          if (temp_data.top_word_color && temp_data.top_word_color == 'black') {
            top_w_color = "#000000";
          }

          wx.setNavigationBarColor({
            frontColor: top_w_color, // 必写项
            backgroundColor: temp_data.top_color, // 必写项
          })
          th.setData({
            nav_frontColor: top_w_color, // 必写项
            nav_backgroundColor: temp_data.top_color, // 必写项
          })
        }

      }
    })



  },
  //广告轮播满屏按钮颜色传递方法
  bindbut_color_event_fn(even) {
    let nav_but_color = even.detail.but_color
    this.setData({
      nav_but_color
    })
  },
  onPageScroll: function (e) {
    //满屏模式监听 
    let num = e.scrollTop
    if (!this.data.old_nav_frontColor) {
      this.setData({
        old_nav_frontColor:this.data.nav_frontColor
      })
    }
    if (this.data.is_full_screen_navigation) {
      let nav_type = this.data.nav_type
      if (num >= 100 && nav_type) {
        this.setData({
          nav_type: 0,
          searchbox_transparent: 0,
          nav_frontColor: '#000'
        })
        // this.data.nav_type = 0
      }
      if (num < 100 && !nav_type) {
        this.setData({
          nav_type: 1,
          searchbox_transparent: 1,
          nav_frontColor: this.data.old_nav_frontColor
        })
        // this.data.nav_type = 1
      }
    }
    //-----------end-------
    //是否显示置顶
    if (num > 400) {
      this.setData({
        is_show_top: 1
      })
    } else {
      this.setData({
        is_show_top: 0
      })
    }
    //----end------

    // this.data.scrollTop = e.scrollTop;
    // var t = getCurrentPages();
    // "pages/index/index/index" == t[t.length - 1].route;

  },
  onPullDownRefresh: function (e) {

  },


  onShareAppMessage: async function (e) {
    var th = this;
    getApp().globalData.no_clear = 1;
    getApp().getConfig(await function (e) {
      th.setData({
        store_config: e
      })
    }.bind(this))

    var url = `pages/template/index?sence=` + this.data.temp_id;
    var userInfo = getApp().globalData.userInfo;
    //是分销商才带
    if (userInfo) {
      url += "&first_leader=" + userInfo.user_id;
    }

    var ob = {
      title: this.data.share_title,
      path: url,
      //imageUrl:this.data.url+this.data.store_config.store_logo,
    };
    return ob;
  },

  /**
  * 用户分享朋友圈
  */
  async onShareTimeline() {

    var th = this;
    getApp().globalData.no_clear = 1;
    getApp().getConfig(await function (e) {
      th.setData({
        store_config: e
      })
    }.bind(this))
    var { share_title, temp_id, store_config } = this.data;
    var url = `sence=${temp_id}`;

    var userInfo = getApp().globalData.userInfo;
    //是分销商才带
    if (userInfo) {
      url += "&first_leader=" + userInfo.user_id;
    }

    var ob = {
      title: share_title,
      query: url,
      //imageUrl:this.data.url+store_config.store_logo,
    };
    console.log(ob);
    return ob;
  },
  //---加载更多是靠这个函数----
  onReachBottom: function () {
    var th=this;
    if (getApp().globalData.func_list) {
      for (let i in getApp().globalData.func_list) {
        let item = getApp().globalData.func_list[i];
        item.re_show();
      }
    }

    //自定义时商品自动加载更多
    let goodsGroupArr = this.data.goodsGroupArr
    if (this.data.isTemplate && goodsGroupArr.length > 0) {
      goodsGroupArr.map(item => {
        if(th[item])
            th[item].automore()
      })
    }
  },



});