var t = getApp(),
  a = t.request,
  o = t.globalData.setting,
  os = o,
  i = require("../../utils/util.js"),
  ut = i,
  s = require("../../utils/common.js");
Component({
  data: {
    url: o.imghost,
    object: null,
    curPage: 1,
    is_no_more: 1, //加载完所有数据的控制器
    load_complete: 0, //加载完成,
    rank_switch: false,
    card_field: "",
    card_name: "",
    max_card_field: "",
    card_list: null
  },
  properties: {
    // 这⾥定义了innerText属性，属性值可以在组件使⽤时指定
  },
  ready: function () {},
  pageLifetimes: {
    //要处理一下，游客登录后的界面的变化,主要还该是改变会员
    show: function () {
			console.log('page-show');
      this.init();
      if(getApp().globalData.login_back==1){
          getApp().globalData.login_back==0;
          this.data.curPage=1;
          this.setData({ recommend: [] });
          this.get_list();
      }
    }
  },

  methods: {
    init: function () {
      var th = this;
      if (!getApp().globalData.user_id) return false;
      getApp().request.get("/api/weshop/users/get/" + os.stoid + "/" + getApp().globalData.user_id, {
        isShowLoading: false,
        success: function (e) {
          if (e.data.code == 0 && e.data && e.data.data) {
            getApp().globalData.userInfo = e.data.data;
          }
        }
      })
    },
    get_list: function () {
      var that = this;
      if (that.data.is_no_more == 0) return false;
      var user_id=getApp().globalData.user_id;
      if(!user_id) user_id=0;

      var curPage = that.data.curPage;


      getApp().request.get('/api/weshop/serviceCard/page', {
        isShowLoading: false,
        data: {
          is_recommend: 1,
          store_id: o.stoid,
        },
        success: function (res) {
					console.log('获取卡项推荐', res.data.data);
          var data = res.data;
          var total = data.data.total;
          if (total <= curPage * 6) {
            that.setData({
              is_no_more: 0
            });
          } else {
            that.data.curPage++;
          }
          //加载完成
          if (data.data.pageData) {

            for(let i in data.data.pageData){
            	let item=data.data.pageData[i];
            	if(item.user_price)
            	   item.prom_price=item.user_price;
            }

            that.setData({
              load_complete: 1
            });
          }
          if (that.data.recommend != null) {
            var ra = that.data.recommend.concat(data.data.pageData);
            that.setData({
              recommend: ra
            });
          } else {
            that.setData({
              recommend: data.data.pageData
            });
          }
					
					console.log('卡项推荐按~~~~~~', that.data.recommend)
        }
      })
    },
    bind_bnerr_xc: function (e) {
      var _errImg = e.target.dataset.errorimg;
      var _errurl = e.target.dataset.url;
      var _errObj = {};
      _errObj[_errImg] = "/miniapp/images/default_g_img.gif";
      this.setData(_errObj) //注意这⾥的赋值⽅式,只是将数据列表中的此项图⽚路径值替换掉 ; 
    },
    reset: function () {
      curPage = 1;
    },
    //--- 获取卡类列表 ---
    getPlusCardType: function (func) {
      var storid = o.stoid;
      var th = this;
      getApp().request.promiseGet("/api/weshop/plus/vip/mem/bership/list?" + "storeId=" + storid, {}).then(res => {

        if (res.data.code != 0 || !res.data.data) {
          var ob = {
            "card_list": [],
            "name_map": ""
          };
          func(ob);
          return false;
        }

        var plusCard = res.data.data;
        var arr = [1219, 2089, 3031];
        var new_arr = new Array();
        var card_name_map = new Map();

        var user = getApp().globalData.userInfo;
        if (plusCard) {
          for (var i = 0; i < plusCard.length; i++) {
            if ((!user || user.card_field == null || user.card_field == "") && (plusCard[i].IsStopBuy == true)) {
              continue;
            }
            var name = "card" + plusCard[i].CorrPrice.toLowerCase();
            card_name_map.set(name, plusCard[i].CardName);
            new_arr.push(plusCard[i]);

          }
        }
        var ob = {
          "card_list": new_arr,
          "name_map": card_name_map
        };
        func(ob);
      })
    },

      go_url:async function (e) {
        var gid = e.currentTarget.dataset.gid;
        var rurl = '/api/weshop/activitylist/listGoodActInfo2New';
        var req_d = {
          "store_id": os.stoid,
          "goods_id": gid,
          "user_id": getApp().globalData.user_id,
          "goods_type":1
        }
        var res= await getApp().request.promiseGet(rurl, {data: req_d});

        var url = "/packageH/pages/goodsInfo/goodsInfo?goods_id=" + gid;
        if (res.data.code == 0 && res.data.data && res.data.data.length > 0) {
          var arr = res.data.data;
          let times = new Date().getTime();
          //-- 预热也要计算 --
          var arr2 = arr.filter(function (e) {
            return e.s_time < ut.gettimestamp() || (e.warm_uptime && e.warm_uptime < ut.gettimestamp())
          })
          if(arr2.length==1) {
            switch(arr2[0].prom_type){
              case 1:
                url+="&prom_type=1&prom_id="+arr2[0].act_id;
                break;
              case 2:
                url="/packageG/pages/group_buy/goodsInfo/goodsInfo?goods_id="+gid+"&prom_id="+arr2[0].act_id;
                break;
              case 6:
                url = `/packageA/pages/serviceCard_pd/goodsInfo/goodsInfo?goods_id=${gid}&prom_type=${arr2[0].prom_type}&prom_id=${arr2[0].act_id}`;
                break;
              case 8:
                url = `/packageC/pages/presell/cardInfo/goodsInfo?goods_id=${gid}&prom_id=${arr2[0].act_id}`;
                break;
            }
          }

        }


        getApp().goto(url);
      }

  },
})