import { default as storeApi} from "../../api/store"
import { default as commonApi} from "../../api/commonApi"
import { default as requestApi} from "../../utils/request"
import { default as productApi} from "../../api/product"

Page({
  data: {
    shopList: [{
      id: 1,
      imgurl: "../../images/shop.jpg",
      shopname: "大华店",
      stars: 4,
      mark: true,
      new: true,
      discount: false,
      marktext: "新店促销",
      shop_address: "上海市宝山区大华虎城A座10L",
      shop_distance: "6.5KM"
    }],
    tabs: [],
    activeTab: 0,
    productHeight: 0,
    productList: [{
        id: "1",
        title: "葛优躺（北京躺）电影足疗",
        price: "299",
        desc: "全店通用",
        open: true,
        imgurl: "../../images/product01.jpg"
      },
      {
        id: "2",
        title: "中式古法推拿",
        price: "299",
        desc: "暂未开放",
        open: false,
        imgurl: "../../images/product02.jpg"
      },
      {
        id: "3",
        title: "中式古法推拿",
        price: "299",
        desc: "暂未开放",
        open: false,
        imgurl: "../../images/product02.jpg"
      },
      {
        id: "4",
        title: "葛优躺（北京躺）电影足疗",
        price: "299",
        desc: "暂未开放",
        open: false,
        imgurl: "../../images/product01.jpg"
      }
    ],
    isLocation:false

  },


  onLoad: async function () {
    const self = this
    const tokenData = await commonApi.getToken();
      if(tokenData.token){
        wx.setStorageSync('token', tokenData.token)
        requestApi.HEAD.token = tokenData.token
      }
    wx.getSetting({
      success: (res) => {
        let authSetting = res.authSetting
        if (authSetting['scope.userLocation']) {
          console.log('已授权地理位置')
          // 已授权
          self.getLocation();
        } else {
          // 未授权
          console.log('未授权地理位置');
        }
      }
    })
    var tabs = await this.getProductList();
    this.setData({
      tabs
    });
    this.getProductHeight()
  },


  //动态生成产品的父盒子高度
  getProductHeight: function () {
    var h = wx.getSystemInfoSync().windowHeight * (750 / wx.getSystemInfoSync().windowWidth);
    var productHeight = h - 430;
    this.setData({
      productHeight
    })
  },

  getProductList: async function () {
    const self = this
    const productList = await productApi.getProductList(1,4)
    console.log()
    self.setData({
      productList: productList,
      page:1,
      pageSize:10
    })
  },
  requestAll(url, data, header = {}, method) {
    const _self = this
    wx.showLoading()
    return new Promise((resolve, reject) => {
      wx.request({
        url: url,
        data: data,
        header: header,
        dataType: 'json',
        method: method,
        success: (res => {
          wx.hideLoading()
          if (res.data.code === 1) {
            //200: 服务端业务处理正常结束
            resolve(res.data)
          } else {
            if (res.data.code === 0) {
              wx.showToast({
                title: res.data.message,
              })
            }
            if (res.data.code === 901) {
              console.log(res.data)
            }
            //其它错误，提示用户错误信息
            if (this._errorHandler != null) {
              //如果有统一的异常处理，就先调用统一异常处理函数对异常进行处理
              this._errorHandler(res)
            }
            reject(res)
          }
        }),
        fail: (res => {
          if (this._errorHandler != null) {
            this._errorHandler(res)
          }
          wx.showToast({
            title: '网络异常请,稍后再试~',
          })
          reject(res)
        })
      })
    })
  },
  onTabClick(e) {
    const index = e.detail.index
    this.setData({
      activeTab: index
    })
  },

  onChange(e) {
    const index = e.detail.index
    this.setData({
      activeTab: index
    })
  },
  handleClick(e) {
    wx.navigateTo({
      url: './webview',
    })
  },
  getLocation: async function(){
    const self = this
    wx.getLocation({
      type: 'wgs84',
      async success (res) {
        console.log(res)
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        const rs = await storeApi.getNearStore(latitude,longitude)
        console.log(rs.info);
        self.setData({
          locationFlag: true,
          storeInfo: rs.info
        })
      }
     })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('2222')
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log(111)
  },
})
