var network = require('../../../utils/network.js')
var dates = require('../../../utils/util.js')
var network = require('../../../utils/network.js')
const baseUrl = require('../../../api/index.js').config
const Method = require('../../../api/method.js').Method
const order = require('../../../api/order.js')
const member = require('../../../api/member.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    latitude: '',
    longitude: '',
    currentLo: null,
    currentLa: null,
    markers: null,
    scale: 12,

    mentionlist: [],
    trueindex: '',
    isScroll: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      latitude: options.lat,
      longitude: options.lon
    })
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getMention: function () {
    var that = this
    var storeid = wx.getStorageSync('storeid')
    order.getMention(
      baseUrl.waimaiUrl + '/cab/list',
      {
        storeId: storeid,
        lat: that.data.latitude,
        lon: that.data.longitude
      },
      {},
      '加载中',
      function (res) {
        if (res.code == 200) {
          var result = res.results
          for (var i = 0; i < result.length; i++) {
            result[i].selectmention = false
          }
          that.setData({
            mentionlist: result
          })
          let newData = []
          res.results.forEach((ele, index) => {
            newData.push({
              id: ele.cab_code,
              longitude: ele.longitude,
              latitude: ele.latitude,
              iconPath:
                'https://ybm-public.oss-cn-beijing.aliyuncs.com/image/cab.png',
              width: 35,
              height: 35
            })
          })
          that.setData({
            mentionlist: result,
            markers: newData,
            currentLo: result[0].longitude,
            currentLa: result[0].latitude,
            areadetail: result[0].area,
            citydetail: result[0].city
            // areaone: test[0].area,
          })
        } else {
          console.log(res.message)
        }
      },
      function (err) {
        wx.showToast({
          title: '失败'
        })
      }
    )
  },

  takeMark: function (e) {
    var that = this
    var v = ''
    var flag = e.markerId
    var test = that.data.mentionlist
    for (var i = 0; i < test.length; i++) {
      if (flag == test[i].cab_code) {
        v = test[0]
        test[0] = test[i]
        test[i] = v
      }
    }
    that.setData({
      mentionlist: test
    })
  },
  takeCondition: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    for (var i = 0; i < that.data.mentionlist.length; i++) {
      if (that.data.mentionlist[i].cab_code == id) {
        if (that.data.mentionlist[i].selectmention == true) {
          that.data.mentionlist[i].selectmention = false
        } else {
          that.data.mentionlist[i].selectmention = true
        }
      } else {
        that.data.mentionlist[i].selectmention = false
      }
    }
    that.setData({
      mentionlist: that.data.mentionlist,
      mentionid: id,
      mentionindex: index
    })
    // wx.setStorageSync('couponindex', index);
  },
  takeChoice: function (e) {
    var that = this
    if (that.data.mentionlist[that.data.mentionindex].selectmention == false) {
      that.setData({
        trueindex: 'fail',
        showMention: false
      })
    } else {
      that.setData({
        trueindex: that.data.mentionindex,
        showMention: false
      })
      wx.setStorageSync('cabinetflag', '3')
    }
    wx.setStorageSync('trueindex', that.data.trueindex)
    wx.setStorageSync('mentionlist', that.data.mentionlist[that.data.trueindex])
    wx.redirectTo({
      url: '../affirm/affirm'
    })
    // wx.navigateBack({
    //   delta: 1
    // })
  }
})
