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,
    is_no_plus: 1,

    is_retail_price:0

  },
  properties: {
    // 这⾥定义了innerText属性，属性值可以在组件使⽤时指定
  },
  ready: function () {

    var th=this;
    getApp().getConfig2(function (e) {
      var swithc_list = e.switch_list;
      var sw_arr = JSON.parse(swithc_list);
      if(sw_arr.is_retail_price){
        th.setData({is_retail_price:1});
      }
    },1);

  },
  pageLifetimes: {
    //要处理一下，游客登录后的界面的变化,主要还该是改变会员
    show: function () {

      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;
            getApp().getConfig2(function (e) {
              var swithc_list = e.switch_list;
              var sw_arr = JSON.parse(swithc_list);
              ut.get_plus_name_price(sw_arr,th);
            })

          }
        }
      })
      //调用接口判断商家plus有没有过期
      getApp().request.promiseGet("/store/storemoduleendtime/page?store_id=" + os.stoid + "&type=3", {}).then(res => {
        if (res.data.code == 0) {
          var arr = res.data.data.pageData;
          if (arr.length > 0) {
            var item = arr[0];
            if (item.is_sy == 0) {
              var now = Date.parse(new Date()); now = now / 1000;
              if (item.end_time < now) {
                th.setData({ is_no_plus: 0 })
              }
            }
          }
        }
      })
    },

    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/goods/page?isnewwhere=1', {
        isShowLoading: false,
        data: {
          is_mainshow: 1,
          isonsale: 1,
          is_recommend: 1,
          is_on_sale: 1,
          store_id: o.stoid,
          page: curPage,
          pageSize: 6,
          orderField: "sort",
          orderType: 'asc',
          user_id: user_id,
        },
        success: function (res) {
          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
            });
          }
        }
      })
    },
    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: function (e) {
      var url = e.currentTarget.dataset.url;
      getApp().goto(url);
    }

  },
})