"use strict";
$(function () {

    $('a').click(function () {
        if ($(this).attr('href') && $(this).attr('target') !== '_blank' && $(this).attr('href') !== 'javascript:;' && $(this).attr('href').indexOf('tel:') == -1) {
            $.loading();
        }
    });
    $('.dropdown-exit').click(function () {


        $.clientPost('shop/client/member/out-login', {}, function (res
        ) {
            if (res.error === 0) {
                sessionStorage.removeItem('login_member');
                location.href = window.settings.rootUrl + 'index.html';
            }
        })

    });


    $.setFloatPosition = function () {
        var left = $(".footer-inner").offset().left + 1200 + 30;
        $('.component-customer').css('left', left - 210 + 'px');
        $('.component-float').css('left', left + 'px').show();
    };

    $.setFloatPosition();
    $('.component-float .float-top').click(function () {
        $("body, html").stop().animate({
            scrollTop: 0
        });

    });

    $('.component-float .btn-item-customer').click(function () {

        if ($('.preview-customer').is(":hidden")) {
            var left = parseFloat($('.component-float').css('left').replace('px', '')) - 210;
            var top = parseFloat($('.component-float').css('top').replace('px', ''));
            $('.component-customer').css({
                'left': left + 'px',
                'top': top + 'px'
            });
            $('.preview-customer').show();
        } else {
            $('.preview-customer').hide();
        }

    });
    // $(document).click(function(e){
    //
    //
    //     if($(e.target).closest('.btn-item-customer').length<=0 && $(e.target).closest('.component-customer').length<=0) {
    //         $('.preview-customer').hide();
    //     }
    // });
    window.addEventListener('resize', function () {
        $.setFloatPosition();
    });
    window.addEventListener('scroll', function () {

        if ($(window).scrollTop() > 0) {
            $('.component-float .float-top').show();
        } else {
            $('.component-float .float-top').hide();
        }

    });

    var url = $.getWapUrl('', {})
    $('.mobile-qrcode').qrcode({
        render: "canvas", //也可以替换为table
        width: 146,
        height: 146,
        text: url
    });

    $('.header-login-link').click(function(){
        $.checkLogin();
    })

    //多门店

    if (window.settings.hasStore) {

        // //定位
        // $.initMap();

        new Vue({

            el: '#storeApp',
            delimiters: ['vue{', '}}'], //更改解析格式\
            data(){
                return {

                    page: 1,
                    pagesize: 10,
                    list:[],
                    keywords:'',
                    count:0,
                    location: {},
                    lastSelect:null,
                    listShow:false,
                    loaded:false,
                    loadMoreText:'加载更多',
                    loadMoreShow: true,
                    loadEnd:false,
                    loading:false
                }
            },
            mounted(){
                var _this = this;
                if(window.settings.hasStore) {
                    this.initMap();

                }
            },
            computed:{
                canSwitch(){
                    return Number(window.settings.storeSettings.store_switch) === 0;
                }
            },
            methods:{
                showList(){

                    if(Number(window.settings.storeSettings.store_switch) === 1){
                        return;
                    }
                    if(!this.loaded){
                        $.loading();
                        this.getList(1);
                        return;
                    }

                    this.listShow = true;
                    this.initClose();

                },
                search(){
                    this.getList(1);
                },
                initClose(){

                    var _this = this;
                    $(document.body).click(function(e){
                        if ($(e.target).closest('.store-list').length <= 0 && $(e.target).closest('.left-store').length <= 0 && !$('.store-list').is(":hidden")) {
                            _this.listShow = false;
                        }
                    })
                },
                //定位
                initMap() {

                    var _this = this;

                    var loc = sessionStorage.getItem('pc_location') || '';
                    if(loc){
                        _this.location = JSON.parse(loc);
                        if (!window.settings.storeId) {
                            _this.selectStore(_this.location);
                        } else {
                            $.removeLoading();
                        }
                        return;
                    }

                    $.loading()

                    // 腾讯地图定位
                    var geolocation = new qq.maps.Geolocation("TEIBZ-F2KC4-NLZUV-DMCRG-327JJ-PKBTZ", "pcshop");
                    geolocation.getIpLocation(
                        (position) => {
                            var loc =  {
                                lat: position.lat,
                                lng: position.lng,
                                province: position.province,
                                city_code: '',
                                area_code: position.adcode,
                                address: position.addr,
                                city: position.city,
                                area: position.district,
                            };
                            loc.city_code = Number((position.adcode+'').substr(0,4)+'00');
                            _this.location = loc;
                            sessionStorage.setItem('pc_location',JSON.stringify(loc) );

                            if (!window.settings.storeId ) {
                                _this.selectStore(_this.location);
                            } else {
                                $.removeLoading();
                            }
                        },
                        (err) => {
                            // 定位出错
                            if (window.sessionStorage.getItem('selectStore')!==null){
                                $.removeLoading();
                                return;
                            }
                            this.loadingShow = false
                            this.loadingText = ''
                            $.tip('获取您的位置信息失败，已自动选择默认门店');
                            window.storeLocation = {
                                lat: '',
                                lng: '',
                                address: '无法获取当前位置',
                                province: '无法获取当前位置',
                                city: '无法获取当前位置',
                                area: '无法获取当前位置'
                            }
                            _this.selectStore({})
                        },
                    );

/*                    var map = new AMap.Map('iCenter', {
                        resizeEnable: true
                    })

                    map.plugin('AMap.Geolocation', function () {
                        var geolocation = new AMap.Geolocation({
                            // 是否使用高精度定位，默认：true
                            enableHighAccuracy: true
                        })

                        // 获取当前位置信息
                        geolocation.getCurrentPosition();
                        // 监听获取位置信息成功的回调函数
                        AMap.event.addListener(geolocation, 'complete', onComplete);
                        // 监听获取位置信息错误的回调函数
                        AMap.event.addListener(geolocation, 'error', onError);

                        function onComplete(data) {

                            var  loc =  {
                                lat: data.position.lat,
                                lng: data.position.lng,
                                province: data.addressComponent.province,
                                city_code: data.addressComponent.citycode,
                                area_code: data.addressComponent.adcode,
                                address: data.formattedAddress,
                                city: data.addressComponent.city,
                                area: data.addressComponent.district,
                            };

                            _this.location = loc;
                            sessionStorage.setItem('pc_location',JSON.stringify(loc) );

                            if (!window.settings.storeId ) {
                                _this.selectStore(_this.location);
                            } else {
                                $.removeLoading();
                            }
                        }

                        function onError(error) {
                            // 定位出错
                            if (window.sessionStorage.getItem('selectStore')!==null){
                                $.removeLoading();
                                return;
                            }
                            this.loadingShow = false
                            this.loadingText = ''
                            $.tip('获取您的位置信息失败，已自动选择默认门店');
                            window.storeLocation = {
                                lat: '',
                                lng: '',
                                address: '无法获取当前位置',
                                province: '无法获取当前位置',
                                city: '无法获取当前位置',
                                area: '无法获取当前位置'
                            }
                            _this.selectStore({})
                        }
                    })*/
                },

                //定位门店
                selectStore(location) {

                    $.clientGet("/shop/apps/store/client/index/select-store", {
                        lng: location.lng || '',
                        lat: location.lat || '',
                        province: location.province || '',
                        city_code: location.city_code || '',
                        area_code: location.area_code || '',
                    }, function (res) {

                        if (res.error === 0) {
                            $.removeLoading();
                            if(!window.settings.storeId) {
                                window.location.reload();
                            }
                        }

                    }, function (error) {
                        $.removeLoading();
                    });
                },
                //切换门店
                switchStore (storeId) {


                    $.loading();
                    $.clientPost('shop/apps/pc/client/store/switch', {
                        store_id: storeId,
                    }, function () {
                        window.sessionStorage.setItem('selectStore',storeId)
                        location.reload();
                    }, function () {
                        $.tip('切换门店失败');
                    })
                },

                //门店列表
                getList(page, pageSize, callback) {


                    var _this =this;
                    this.page = page || this.page;
                    this.pagesize = pageSize || this.pagesize;
                    _this.loadMoreText = '加载中...';
                    _this.loading = true;
                    $.clientGet("/shop/apps/store/client/index/store-list", {
                        lng: this.location && this.location.lng,
                        lat: this.location && this.location.lat,
                        keywords: this.keywords.trim(),
                        page: this.page,
                        pagesize: this.pagesize
                    }, function (res) {


                        $.removeLoading();
                        _this.loading = false;
                        if (res.error === 0) {
                            _this.loadMoreText = '加载更多';
                            var list = [];
                            var selected = null;
                            res.list.forEach(v=>{
                                if(Number(v.id) === Number(window.settings.storeId)){
                                    selected = v;
                                    selected.active = true;
                                }else{
                                    list.push(v );
                                }
                            });
                            if(selected){
                                list = [selected].concat( list );
                            }
                            if(_this.page === 1){
                                _this.list = list;
                            }else{
                                _this.list = _this.list.concat(list);
                            }


                            _this.lastSelect = res.last_select&&res.last_select.id?[res.last_select]:[];
                            _this.count = res.count;
                            _this.loaded = true;
                            _this.listShow = true;
                            _this.initClose();
                            if (_this.page * res.pageSize >= res.count) {
                                if( res.list.length<=0){
                                    _this.loadMoreText = '没查找到任何门店';
                                }else{
                                    _this.loadMoreText = '已经加载完了';
                                    _this.loadEnd = true;
                                }
                            }


                        } else {
                            $.tip(res.message)
                        }
                    }, function (error) {
                    });
                },
                loadMore(){
                    if( this.loadEnd){
                        return;
                    }
                    if( this.loading ){
                        return;
                    }
                    this.page++;

                    this.getList(this.page);
                }

            }
        })

    }
});
//UkNTV0ctVDFWQUItWUhWQUUtRDFXUEwtUVpYWUw=[EWEISHOP] Copyright(C) 2017 Phonixnest Inc.//2024-03-28 11:10:07