// pages/shop/shop.js
var app = getApp();
var WXAPI = require('../../utils/server.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    list: [],
    mch_arr:[], //店铺分类列表
    mch_data:[], //店铺分类总数据
    i_showFl:0,//选择的分类
    latitude: 31.772752,
    longitude: 119.946973,
    index: 0,
    cid: ''
  },
  fl_cid(e) {
    console.log(e)
    // 显示当前点击的分类店铺 
    var index = e.currentTarget.dataset.index
    if(index > 0){
      var cid = this.data.mch_data[index-1].id
      this.setData({
        i_showFl: index,
        cid: cid
      })
    } else {
      this.setData({
        i_showFl: index,
        cid: ''
      })
    }   
    this.getShopList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getMchClass();
    this.getShopList();
  },
  getMchClass() {
    var that = this
    WXAPI.mchClass({
      accessId: app.globalData.accessId,
      storeId: 239,
      storeType: 1,
    }).then(res => {
      if (res.code == 200) {
        var data = res.data
        //分类的名称存入数组
        var mch_arr = []
        data.list.forEach((item,index)=>{
          mch_arr.push(item.name)
        })
        //返回的分类列表
        that.setData({
          mch_data: data.list,
          mch_arr: mch_arr
        })
      }      
    });
  },
  getShopList() {
    var that = this
    WXAPI.shopList({
      accessId: app.globalData.accessId,
      storeId: 239,
      storeType: 1,
      latitude: that.data.latitude,
      longitude: that.data.longitude,
      cid: that.data.cid,
    }).then(res => {
      if (res.code == 200) {
        var data = res.data
        that.setData({
          list: data.list,
        })
      }      
    });
  },
  navToStore: function (e) {
    var info = e.currentTarget.dataset.info
    wx.navigateTo({
      url: '/pages/store/store?shopId=' + info.shop_id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})