var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp();

Page({
    /**
     * 页面的初始数据
     */
    data: {
        // 中心点纬度、经度
        latitude: "",
        longitude: "",
        // 标记点 当前位置
        markers: [],
        nearStoreList: [],
        isShow: false,
        storeId: "",
        storeName: "",
        storeAddress: "",
        distance: "",
        merchSn: ""
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        this.mapCtx = wx.createMapContext('myMap');
        this.mapCtx.moveToLocation();

        let nearStoreList = wx.getStorageSync('nearStoreList') ? JSON.parse(wx.getStorageSync('nearStoreList')) : [];
        this.setData({
            nearStoreList: nearStoreList
        })

        let that = this;
        util.getLocation((lng, lat) => {
            util.request(api.NearbyList, { longitude: lng, latitude: lat }).then((res) => {
            let markers = [];
        let points = [];
        let nlist = res.data;
        for (var i = 0; i < nlist.length; i++) {
            let marker = {
                iconPath: "/static/images/address.png",
                id: i,
                latitude: nlist[i].latitude,
                longitude: nlist[i].longitude,
                width: 35,
                height: 35,
                clickable: true,
                storeId: nlist[i].id,
                storeName: nlist[i].storeName,
                storeAddress: nlist[i].storeAddress,
                distance: nlist[i].distance,
                merchSn: nlist[i].merchSn
            };
            markers.push(marker);

            let point = {
                latitude: nlist[i].latitude,
                longitude: nlist[i].longitude,
            };
            points.push(point);
        }

        this.mapCtx.includePoints({
            points: points,
        })

        that.setData({
            latitude: lat,
            longitude: lng,
            markers: markers
        })

    })
    })
    },
    onReady: function () {
        // 页面渲染完成
        wx.create
    },
    onShow: function () {
        // 页面显示

    },
    onHide: function () {
        // 页面隐藏

    },
    onUnload: function () {
        // 页面关闭

    },
    markersClick: function(res) {
        // console.log(res);
        let that = this;
        let marker = that.data.markers[res.markerId];
        that.setData({
            isShow: true,
            storeId: marker.storeId,
            merchSn: marker.merchSn,
            storeName: marker.storeName,
            storeAddress: marker.storeAddress,
            distance: marker.distance
        });
    },
    chooseStore(e) {
        // console.log(e.currentTarget.dataset.id)
        // console.log(e.currentTarget.dataset.merchSn)
        let storeId = e.currentTarget.dataset.id;
        let merchSn = e.currentTarget.dataset.merchSn;

        let that = this;
        util.request(api.ChooseStoreId, { storeId: storeId, merchSn: merchSn }, 'POST').then(function (res) {
            if (res.errno === 0) {
                wx.removeStorageSync('nearStoreList');
                wx.removeStorageSync('storeId');
                wx.removeStorageSync('storeVo');
                wx.removeStorageSync('currentCategory');
                wx.setStorageSync('storeId', storeId);
                var item = "";
                for (var i = 0; i < that.data.nearStoreList.length; i++) {
                    if (storeId == that.data.nearStoreList[i].id) {
                        item = that.data.nearStoreList[i];
                        wx.setStorageSync('storeVo', JSON.stringify(item));
                        break;
                    }
                }

                var pages = getCurrentPages();
                var currPage = pages[pages.length - 1];  //当前页面
                var prevPage = pages[pages.length - 2]; //上一个页面
                if (item == "") {
                    wx.removeStorageSync('nearStoreList');
                    wx.removeStorageSync('storeId');
                    wx.removeStorageSync('storeVo');
                    that.setData({
                        storeName: '附近暂无门店'
                    })
                } else {
                    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
                    prevPage.setData({
                        storeName: item.storeName
                    })
                    // that.setData({
                    //   storeName: item.storeName
                    // })
                }
                // wx.switchTab({
                //   url: '/pages/index/index'
                // });

                prevPage.getIndexData();
                prevPage.enableActivity();
                prevPage.getGroupData();
                wx.navigateBack()
            }
        })
    }
})