'use strict';

angular.module('app').controller('HomeController', ['$scope', '$state', '$timeout', function($scope, $state, $timeout) {
    $scope.getAddSuccess = true;

    $scope.$on('$viewContentLoaded', function() {
        //加载轮播
        var swiper = new Swiper('.swiper-container', {
            pagination: '.swiper-pagination',
            paginationClickable: true,
            loop: true,
            autoplayDisableOnInteraction: false,
            autoplay: 5000,
            effect: 'coverflow',
            slidesPerView: 'auto',
            centeredSlides: true,
            spaceBetween: -55,
            coverflow: {
                rotate: 30,
                stretch: 0,
                depth: 60,
                modifier: 1,
                slideShadows: false
            }
        });
        //加载字体适应
        var clientWidth = document.documentElement.clientWidth || window.innerWidth;
        var innerWidth = Math.max(Math.min(clientWidth, 480), 320);
        console.log(innerWidth);
        if (innerWidth < 350) {
            angular.element(".explain").removeClass("font-15");
            angular.element(".explain").addClass("font-12");
        } else if (innerWidth > 400) {
            angular.element(".explain").removeClass("font-12");
            angular.element(".explain").addClass("font-15");
        }
        //加载sdk
        ysf.on({
            'onload': function() {
                $scope.sdk = true;
            }
        });
        //加载地图，调用浏览器定位服务
        var map, geolocation, marker, geocoder, regeocoder;
        map = new AMap.Map('', {
            resizeEnable: true,
            zoom: 17
        });
        map.plugin('AMap.Geolocation', function() {
            geolocation = new AMap.Geolocation({
                enableHighAccuracy: true, //是否使用高精度定位，默认:true
                timeout: 10000, //超过10秒后停止定位，默认：无穷大
                maximumAge: 0, //定位结果缓存0毫秒，默认：0
                convert: true, //自动偏移坐标，偏移后的坐标为高德坐标，默认：true
                showButton: true, //显示定位按钮，默认：true
                buttonPosition: 'RB', //定位按钮停靠位置，默认：'LB'，左下角
                buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
                showMarker: true, //定位成功后在定位到的位置显示点标记，默认：true
                showCircle: true, //定位成功后用圆圈表示定位精度范围，默认：true
                panToLocation: true, //定位成功后将定位到的位置作为地图中心点，默认：true
                zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
                useNative: true
            });
            map.addControl(geolocation);
            geolocation.getCurrentPosition();
            AMap.event.addListener(geolocation, 'complete', onComplete); //返回定位信息
            AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息

            function onComplete(data) {
                $scope.getAddSuccess = true;
                var geocoder = new AMap.Geocoder({
                    radius: 1000,
                    extensions: "all"
                });
                console.log("获取地址");
                geocoder.getAddress(data.position, function(status, result) {
                    if (status === 'complete' && result.info === 'OK') {
                        $scope.addr = result;
                        $timeout();
                    }
                });
            };

            function onError(data) {
                $scope.getAddSuccess = false;
            };
        });
    });

    $scope.onOnlineClick = function(e) {
        console.log($scope.sdk);
        $scope.mobile = "";
        var activeClick = $(e.target);
        //console.log(activeClick);
        if (!$scope.getAddSuccess) {
            alert("获取地址失败，请开启手机GPS定位功能，并允许获取地理位置授权");
            window.location.reload();
        } else if (!$scope.addr && $scope.getAddSuccess) {
            alert("地理位置获取中，请稍后");
        } else if ($scope.addr.regeocode.addressComponent.district === '莲都区') {
            //activeClick[0].parentElement.parentElement.dataset.target = "#phoneLoginModal";
            activeClick[0].parentElement.dataset.target = "#phoneLoginModal";
        } else {
            //activeClick[0].parentElement.parentElement.dataset.target = "#errorLoginModal";
            activeClick[0].parentElement.dataset.target = "#errorLoginModal";
        }
    };

    $scope.onlineService = function() {
        //console.log($scope.mobile, $scope.sdk);
        if ($scope.mobile.length == 11 && $scope.sdk) {
            ysf.config({
                uid: $scope.mobile, // 用户Id
                name: '微信用户', // 用户名称
                email: $scope.addr.regeocode.formattedAddress, // 用户邮箱
                mobile: $scope.mobile, // 用户电话
                success: function() { // 成功回调
                    ysf.open();
                    console.log('success');
                },
                error: function() { // 错误回调
                    // handle error
                    //ysf.open();
                    alert('error!!!');
                }
            });
        } else {
            alert("请正确输入手机号码");
        }
    };
}]);