import {
  get,
  post
} from '../../utils/http'
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    total: 0,
    page: 1,
    list: [],
    isSelectProduct: false, //是否是预约时跳转过来
    ids:'',//预约项目ID
    lat1:'',
    lng1:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    let defaultStore = {}
    if (options.defaultStore) {
      defaultStore = JSON.parse(options.defaultStore)
    }
    if (Object.keys(defaultStore).length == 0) {
      defaultStore = app.globalData.storeData
    }
    let isSelectProduct = false
    if (options.isSelectProduct) {
      isSelectProduct = true
    }
    this.setData({
      defaultStore: defaultStore,
      isSelectProduct,
      ids:options.project_id?JSON.parse(options.project_id):'',
      lat1:app.address.latitude,
      lng1:app.address.longitude
    })
    // 是否是预约店铺切换
    if(this.data.isSelectProduct){
      this.getSelectStore()
    }else{
      this.getStore()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page * 10 < this.data.total) {
      this.getStore(++this.data.page)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  /**
   * 清除搜索文本
   */
  onClear() {
    this.setData({
      value: ''
    })
  },

  /**
   * 选中店铺
   */
  onChangeStore(e) {
    let pages = getCurrentPages()
    let prevPage = pages[pages.length - 2]
    let storeId = e.currentTarget.dataset.item.id
    prevPage.getStoreInfo(storeId)
    if (this.data.isSelectProduct) {
      wx.navigateBack({
        delta: 1
      })
      return
    }
    get(
      'api/store/info', {
        store_id: storeId
      },
      (res) => {
        wx.setStorageSync('store_id', String(storeId))
        app.globalData.storeData = res.data
        wx.navigateBack({
          delta: 1
        })
      }
    )
  },

  /**
   * 监听键盘输入
   */
  onChange(e) {
    this.setData({
      value: e.detail.value
    })
  },

  /**
   * 搜索店铺
   */
  onConfirm() {
    this.getStore(1)
  },

  /**
   * 获取店铺接口
   * /api/store
   */
  getStore(_page) {
    let {
      list,
      current,
      page
    } = this.data
    get(
      'api/store', {
        page: _page || page,
        limit: 10,
        keyword: this.data.value
      },
      (res) => {
        if (_page == 1 || page == 1) {
          list = []
          this.data.page = 1
        }
        list.push(...res.data.list)
        this.setData({
          list,
          total: res.data.total
        })
        console.log(res, '重复')
      }
    )
  },
  // 预约店铺选择店铺获取店铺列表
  getSelectStore(_page) {
    let {
      list,
      current,
      page
    } = this.data
    get(
      'v2/api/order/store', {
        page: _page || page,
        limit: 10,
        project_id:this.data.ids.toString()||''
      },
      (res) => {
        if (_page == 1 || page == 1) {
          list = []
          this.data.page = 1
        }
        list.push(...res.data)
        this.setData({
          list,
        })
        console.log(res, '重复')
      }
    )
  },
  /**
   * 打卡地图
   */
  onOpenLocation(e) {
    let store = e.currentTarget.dataset.storeinfo
    let latitude = parseFloat(store.latitude)
    let longitude = parseFloat(store.longitude)
    wx.openLocation({
      latitude: parseFloat(store.latitude), // 纬度，范围为-90~90，负数表示南纬
      longitude: parseFloat(store.longitude), // 经度，范围为-180~180，负数表示西经
      name: store.belong_region
    })
  }
})