const navigationBarHeight = (getApp().statusBarHeight + 50) + "px"
const Http = require("../utils/HttpBasics");
const imgurl = require("../utils/imgurl");
const config = require("../config/config");
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationBarHeight,
    navLineHeight: ((app.statusBarHeight + app.statusBarHeight) + 50) + "px",
    navImgHeight:(((app.statusBarHeight + app.statusBarHeight) + 34)/2) + "px",
    lists: [],
    merchantVoList: [],
    indexId: 0,
    businessId: 0,
    windowHeight: "",
    page: 1,
    isFirstPage: false,
    isLastPage: false,
    teljpgUrl: imgurl.teljpg.url,
    map: imgurl.map.url, //商场地图
    mapUrl: '/pages/marketAtlas/marketAtlas', //跳转地图的路径
    mouldType:0,
  },
  getFemgMap() { //判断时候对接封了地图
    Http.get({
      url: config.api.ifFengMap
    }).then(res => {
      if (res.data) { //
        let initMpa = {
          appName: res.data.appName,
          key: res.data.appKey,
          defaultThemeName: res.data.themeId,
          tenantId: res.data.tenantId,
          mapId: res.data.mapId
        }
        console.log(initMpa, "initMpa")
        this.setData({
          mapUrl: `/pages2/fengMap/fengMap?initMap=${JSON.stringify(initMpa)}`
        })
      } else {
        this.setData({
          mapUrl: "/pages/marketAtlas/marketAtlas"
        })

      }
    }).catch(err => {
      this.setData({
        mapUrl: "/pages/marketAtlas/marketAtlas"
      })
    })
  },
  //跳转到地图
  goMap() {
    tt.navigateTo({
      url: this.data.mapUrl,
    })
  },
  // 左侧点击事件
  jumpIndex(e) {
    let that = this;
    let index = e.currentTarget.dataset.menuindex;
    that.setData({
      indexId: index,
      businessId: index
    });
    that.setData({
      page: 1
    })
    that.getList(1, that.data.businessId);
  },
  goSearchBar() {
    tt.navigateTo({
      url: '/pages/searchbar/searchbar',
    })
  },
  /**
   * 跳转到门店列表的详情页面
   */
  gotoDetail(e) {
    tt.navigateTo({
      url: `/pages/index/searchbar/detail/index?id=${e.currentTarget.dataset.id}`
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      mouldType: app.globalData.mouldType,
    })
    let that = this;
    that.getBussiness();
    that.getList(1, 0);
    tt.showLoading({
      title: '加载中...',
    })
  },
  onShow() {
    this.setData({
      mouldType: app.globalData.mouldType,
    })
    this.getFemgMap()
    let that = this;
    if (typeof this.getTabBar === 'function' &&
      that.getTabBar()) {
      that.getTabBar().setData({
        selected: 1
      })
    };
    tt.getSystemInfo({
      success: function(res) {
        that.setData({
          windowHeight: res.windowHeight
        })
      },
    })
  },
  getBussiness: function() {
    let that = this;
    Http.get({
        url: config.api.businessList,
        data: {
          filter: 1
        }
      }).then(res => {
        that.setData({
          lists: [{
            id: 0,
            title: "全部",
            type: 1
          }].concat(res.data)
        });
      })
      .catch(err => {
        tt.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
  },
  getList: function(page, businessId) {
    let that = this;
    let data;
    if (businessId == 0) {
      data = {
        pageNum: page,
        pageSize: 8
      }
    } else {
      data = {
        pageNum: page,
        pageSize: 8,
        businessId: businessId
      }
    }
    Http.get({
        url: config.api.merchantList,
        data: data
      }).then(res => {
        tt.hideLoading();
        if (page == 1) {
          that.setData({
            merchantVoList: [],
          })
        }
        var tmpArr = that.data.merchantVoList;
        tmpArr.push.apply(tmpArr, res.data.list);
        // console.log(tmpArr)
        that.setData({
          merchantVoList: tmpArr,
          isFirstPage: res.data.isFirstPage,
          isLastPage: res.data.isLastPage
        })
        // console.log(that.data.merchantVoList)
      })
      .catch(err => {
        tt.hideLoading();
        tt.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
  },
  phone: function(e) {
    let that = this;
    if (e.currentTarget.dataset.merchantlinkphone) {
      tt.makePhoneCall({
        phoneNumber: e.currentTarget.dataset.merchantlinkphone
      })
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let that = this;
    that.data.page++;
    that.setData({
      page: that.data.page
    });
    console.log(that.data.page);
    // 如果是最后一页
    //就不发送请求
    if (!that.data.isLastPage || that.data.isFirstPage && that.data.isLastPage) {
      that.getList(that.data.page, that.data.businessId);
    }
  }
})