const navigationBarHeight = (getApp().statusBarHeight + 50)  +'px';
const config = require("../../config/config");
const Http = require("../../utils/HttpBasics");
const imgurl = require("../../utils/imgurl");
Page({
  data: {
    result: [],
    page: 1,
    navigationBarHeight,
    multiArray: [
      [], []
    ],
    searchText: "请输入",
    multiIndex: 0,
    buildingFloor: [],
    building: null,
    floor: null,
    teljpgUrl: imgurl.teljpg.url
  },
  // lastSearch: Date.now(),
  onShow(){
    this.getbuildingfloorlist();
    if (this.data.focus) {
      this.setData({
        searchState: true
      });
    }
  },
  phone: function (e) {
    let that = this;
    if (e.currentTarget.dataset.merchantlinkphone) {
      tt.makePhoneCall({
        phoneNumber: e.currentTarget.dataset.merchantlinkphone
      })
    }
  },
  gotoDetail(e) {
    tt.navigateTo({
      url: `/pages/index/searchbar/detail/index?id=${e.currentTarget.dataset.id}`
    })
  },
  bindMultiPickerColumnChange: function (e) {
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    console.log(e)
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        data.multiArray[1] = this.data.buildingFloor[e.detail.value].floors;
        break;
    }
    this.setData({
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    })
  },
  bindMultiPickerChange: function (e) {
    // 楼座 building
    // 楼层 floor
    this.setData({
      multiIndex: e.detail.value,
      building: this.data.multiArray[0][e.detail.value[0]],
      floor: this.data.multiArray[1][e.detail.value[1]],
    })
    this.getList(this.data.building.id, this.data.floor.id, 1);
    this.setData({
      page:1
    })
    this.setData({
      searchText: this.data.building.name + this.data.floor.floorName
    })
  },
  //获取楼层楼座
  getbuildingfloorlist() {
    Http.get({
      url: config.api.getbuildingfloorlist,
      data: {
      }
    }).then(res => {
      if (res.data.length > 0) {
        res.data.map(file => {
          if (file.floors.length > 0) {
            file.floors.map(ever => {
              ever.name = ever.floorName
            })
          }
        })
      }
      this.setData({
        buildingFloor: res.data
      })
      this.setData({
        multiArray: [[...this.data.buildingFloor], [...this.data.buildingFloor[0].floors]]
      })

    })
      .catch(err => {
        tt.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
  },
  getList: function (building, floor, page) {
    let that = this;
    let data = {
      pageNum: page,
      pageSize: 10,
      building: building ? building : null,
      floor: floor ? floor : null,
    };
    Http.get({
      url: config.api.merchantList,
      data: data
    }).then(res => {
      if (page == 1) {
        that.setData({
          merchantVoList: [],
        })
      }
      var tmpArr = that.data.merchantVoList;
      tmpArr.push.apply(tmpArr, res.data.list);
      that.setData({
        merchantVoList: tmpArr,
        isFirstPage: res.data.isFirstPage,
        isLastPage: res.data.isLastPage
      })
    })
      .catch(err => {
        tt.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
  },
  clearInput: function clearInput() {
    this.setData({
      value: ''
    });
    this.triggerEvent('clear');
  },
  inputFocus: function inputFocus(e) {
    this.triggerEvent('focus', e.detail);
  },
  inputBlur: function inputBlur(e) {
    this.setData({
      focus: false
    });
    this.triggerEvent('blur', e.detail);
  },
  showInput: function showInput() {
    this.setData({
      focus: true,
      searchState: true
    });
  },
  hideInput: function hideInput() {
    this.setData({
      searchState: false
    });
    this.setData({
      searchText: "请输入",
      merchantVoList: []
    })
  },
  selectResult: function selectResult(e) {
    var index = e.currentTarget.dataset.index;
    var item = this.data.result[index];
    this.triggerEvent('selectresult', {
      index: index,
      item: item
    });
  },
  /**
    * 页面上拉触底事件的处理函数
    */
  onReachBottom: function () {
    let that = this;
    that.data.page++;
    that.setData({
      page: that.data.page
    });
    // 如果是最后一页
    //就不发送请求
    if (!that.data.isLastPage || that.data.isFirstPage && that.data.isLastPage) {
      this.getList(this.data.building.id, this.data.floor.id, that.data.page)
    }
  }
})