// pages/shopList/shopList.js
const app = getApp();
const user = require('../../../utils/cache.js');
// import StoreApi from "../../../api/storeList.js";


import staff from '../../../api/StaffApi'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        phoneIcon: 'https://cdn1.overseasdragon.com.cn/xcx-smdd/shop_list_phone.png',
        page: 1,
        pageSize: 5,
        longitude: "",
        latitude: "",
        keyword: "",
        storeList: [],
        selectStoreInfo: null,
        id: 0,
        isClicked: false, //是否已经点击过了，默认没有
        pageSource: "",
        way: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _self = this;
        let way = options.way
        console.log('way' + way)

        if (way == 1) {
            _self.setData({
                way: way
            })
        }

        console.log(options)

        if (options.id) {
            _self.setData({
                id: options.id
            })
        }
        if (options.pageSource) {
            _self.setData({
                pageSource: options.pageSource
            })
        }
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let _self = this;
        //获取最近的经纬度
        // let locationPosition = user.getNearLocation()
        // if (locationPosition != null) {
        //     _self.setData({
        //         longitude: locationPosition.longitude,
        //         latitude: locationPosition.latitude
        //     })
        // }
        _self.getNearbyStoreList();
    },

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

    },

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

    },

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

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let _self = this;
        _self.setData({
            page: _self.data.page + 1
        })
        //获取门店列表
        _self.getNearbyStoreList('pull')
    },

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

    },

    /**
     * 获取门店列表
     */
    async getNearbyStoreList(step = 'init') {
        console.log("进入方法执行")
        let _self = this;
        let page = 0
        if (this.data.keyword != null && this.data.keyword != '') {
            page = 1
        } else {
            page = this.data.page
        }
        try {
            let storeRes = await staff.staffShopList({
                mobile:user.getMobile()
            });
            if (storeRes.code == 200 && storeRes.msg == 'success') {
                let list = storeRes.data;
                if (list.length != 0) {
                    list.map((item, index) => {
                        if (item.distance !== null) {
                            let distance = parseFloat(item.distance);
                            item.distance = distance.toFixed(2);
                        }

                        if (item.id == _self.data.id && _self.data.isClicked == false) {
                            item.active = true;
                            _self.setData({
                                selectStoreInfo: item,
                            })
                        } else {
                            item.active = false;
                        }
                    })
                }
                if (step == 'init') { //初始化
                    _self.setData({
                        storeList: list
                    })
                } else {
                    if (list.length == 0) {
                        _self.setData({
                            page: _self.data.page - 1
                        })
                    } else {
                        //分页
                        _self.setData({
                            storeList: _self.data.storeList.concat(list),
                        })
                    }

                }
            }
        } catch (error) {
            console.log(error);
        }
    },
    //打电话
    callPhone(e) {
        //   console.log(e);
        let _self = this;
        let phone = e.currentTarget.dataset.phone;
        wx.makePhoneCall({
            phoneNumber: phone
        })
    },
    /**
     * 选择门店
     */
    selectStore(e) {
        let _self = this;
        let idx = e.currentTarget.dataset.idx;
        let storeList = _self.data.storeList;
        _self.setData({
            isClicked: true,
        })
        for (let i = 0; i < storeList.length; i++) {
            if (idx == i) {
                storeList[i].active = true;
                _self.setData({
                    selectStoreInfo: storeList[i]
                })
            } else {
                storeList[i].active = false;
            }
        }
        _self.setData({
            storeList
        })
    },
    /**
     * 选择门店
     */
    sureStore() {
        let _self = this;
        //存储选中的地址
        if (this.data.way == 1) {
            user.setChooseStore(_self.data.selectStoreInfo);
            wx.redirectTo({
                url: '../goodsManagements/goodsManagements',
            })
        }


    },

    listenInput: function (e) {
        console.log("进入确定")
        let keyword = e.detail.value;
        this.data.lock = false;
        this.setData({
            keyword,
        })
        this.getNearbyStoreList();
    },
})