var starter = angular.module('starter.controllers', [])
//登录页面控制器
starter.controller('loginCtrl',["$scope",'$state','$ionicLoading','$timeout','$ionicPopup',function($scope,$state,$ionicLoading,$timeout,$ionicPopup){
	$scope.vm = {
		mobile:'',
		password:''
	};
	$scope.login = function(){
		if($scope.vm.username == 'admin' && $scope.vm.password == 'admin'){

        	$state.go('home');

			// $ionicLoading.show({
            // template: '<div class="loader"><svg class="circular"><circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="2" stroke-miterlimit="10"/></svg></div>'
	        // });
	        // $timeout(function(){
	        // 	$ionicLoading.hide();
	        // },1500);
				
		}else{
			var alertPopup = $ionicPopup.alert({
            title: '账号密码不正确',
            template: '唯一账号密码:admin！！！',
            buttons: [
		       {
		         text: '<b>确定</b>',
		         type: 'button-calm',
		       },
		     ]
	        });

	        // $timeout(function() {
	        //     ionicMaterialInk.displayEffect();
	        // }, 0);
		}
	}	
}])
//主页面控制器
starter.controller('homeCtrl',["$scope",'$http','$timeout','$ionicLoading','$state',function($scope,$http,$timeout,$ionicLoading,$state){
	$http({
    		method:'get',
    		url:'http://172.31.40.29/list',
    	}).success(function(newItem){
    		$scope.excals = newItem
    	})
    
	$scope.doRefresh = function(){
		$ionicLoading.show({
            template: '加载中...',
		    animation: 'fade-in',
		    showBackdrop: true,
		    maxWidth: 200,
		    showDelay: 0 
        });
        	$http({
        		method:'get',
        		url:'http://172.31.40.29/list',
        	}).success(function(newItem){
		        $timeout(function(){
		        	$ionicLoading.hide();
		        	$scope.excals = newItem
		        });
        	})
            .finally(function() {
	              $scope.$broadcast('scroll.refreshComplete');
            });
		
	}
	

		$scope.getName = function(name){
			// name = this.$$watchers[0].last;
			$state.go('show',{item:name});
	}
}])
	
	

//数据展示控制器
starter.controller('showCtrl',["$scope",'$ionicHistory','$state','$timeout','$stateParams','$http',function($scope,$ionicHistory,$state,$timeout,$stateParams,$http){
    $scope.$on('$ionicView.beforeEnter', function () {
    	name = $stateParams.item;
    	$scope.name = name;
    	$ionicLoading.show({
            template: '加载中...',
		    animation: 'fade-in',
		    showBackdrop: true,
		    maxWidth: 200,
		    showDelay: 0 
        });
    	$http({
			method:'get',
			url:'http://172.31.40.29/data/'+name
		}).success(function(req){
			$timeout(function() {
		        $ionicLoading.hide();
				$scope.item= req.speed;
				$scope.data = [req.torque,req.v,req.p,req.a,req.efficiency];
				$scope.item1 = req.torque;
				$scope.data1 = [req.speed,req.v,req.p,req.a,req.efficiency];
			});
		})
    });
	$scope.tabs = [
	{"title":"主页","url":"line.html"},
	{"title":"TN曲线","url":"tn.html"},
	{"title":"五轴图","url":"zhou.html"},
	{"title":"效率云图","url":"strom.html"}];
	
	$scope.currentTab = 'line.html';
	$scope.go = function(result){
		$state.go(result)
	};
	$scope.isActiveTab = function(tabUrl){
		return tabUrl == $scope.currentTab;
	};
	$scope.onClickTab = function(tab){
		$scope.currentTab = tab.url;
		// $http({
		// 	method:'get',
		// 	url:'http://172.31.40.29/data/'+name
		// }).success(function(req){
		// 	$scope.item= req.speed;
		// 	$scope.data = [req.torque,req.v,req.p,req.a,req.efficiency];
		// 	$scope.item1 = req.torque;
		// 	$scope.data1 = [req.speed,req.v,req.p,req.a,req.efficiency];
		// })
	};
	//TN曲线图
	$scope.legend = ["扭矩", "电压",'输入功率','电流','效率'];  
	$scope.legend1 = ["转速", "电压",'输入功率','电流','效率'];  
   
    $scope.item = ['200rpm','400rpm','600rpm','800rpm','1000rpm','1200rpm','1400rpm','1600rpm','1800rpm','2000rpm','2200rpm','2400rpm']; 
    $scope.item1 = [0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5];
    $scope.num = [0,1,2,3,4];
    $scope.data = [  
        [1,1,1,1,1,1,1,1,1,1,1,1], //扭矩  
        [196,196,196,196,196,196,196,196,196,196,196,196,], //电压 
        [500,500,500,500,500,500,500,500,500,500,500,500,],    //输入功率  
        [4,4,4,4,4,4,4,4,4,4,4,4],   //电流
        [68,68,68,68,68,68,68,68,68,68,68,68,]//效率
    ]; 
     $scope.data1 = [  
        [800,800,800,800,800,800,800,800,800,800,800,800], //转速  
        [196,196,196,196,196,196,196,196,196,196,196,196,], //电压 
        [500,500,500,500,500,500,500,500,500,500,500,500,],    //输入功率  
        [4,4,4,4,4,4,4,4,4,4,4,4],   //电流
        [68,68,68,68,68,68,68,68,68,68,68,68,]//效率
    ];
}])
  
starter.directive('main', function() {  
    return {  
        scope: {  
            id: "@",  
            legend: "=",  
            item: "=",
            num:"=",
            data: "="  
        },  
        restrict: 'E',  
        template: '<div style="height:700px;"></div>',  
        replace: true,  
        link: function($scope, element, attrs, controller) { 
        	var colors = ['#a62ca6', '#8e0515', '#45baf3','#ff2420','#1a9a1a'];
            var option = { 
            	color:colors,
                // 提示框，鼠标悬浮交互时的信息提示  
                tooltip: {  
                    show: true,  
                    trigger: 'axis'  
                },  
                grid:{

                	left:'23%'
                },
             
                   	//图例  
                legend: {  
                    data: $scope.legend ,
                    textStyle:{
                    	fontSize:22
                    }
                }, 
//              toolbox: {
//			        feature: {
//			            dataView: {show: true, readOnly: false},
//			            restore: {show: true},
//			            saveAsImage: {show: true}
//			        }
//			    },
                // 横轴坐标轴  
                xAxis: [
			        {
			            type: 'category',
//			            boundaryGap:false,
			            axisLabel:{
		            		textStyle:{
		            		fontSize:20
		            	},
		            		formatter: '{value} rpm'
		            },
			            data: $scope.item
			        }
			    ], 
			    
                // 纵轴坐标轴  
                yAxis: [
		        {
		            type: 'value',
//		            name: '扭\n矩\n(N*m)',
		            min: 0,
		            max: 4,
		            position: 'left',
		            offset:205,
		            axisLine: {
		                lineStyle: {
		                    color: colors[0],
		                    width:2
		                }
		            },
		            axisLabel:{
		            	margin:20,
		            	textStyle:{
		            		fontSize:18
		            	}
		            }
		        },
		        {
		            type: 'value',
//		            name: '电\n压\n(V)',
		            min: 193,
		            max: 202,
		            position: 'left',
		            offset: 150,
		            axisLine: {
		                lineStyle: {
		                    color: colors[1],
		                    width:2
		                }
		            },
		             axisLabel:{
		             	margin:15,
		            	textStyle:{
		            		fontSize:18
		            	}
		            },
		        },
		        {
		            type: 'value',
//		            name: '输\n入\n功\n率\n(W)',
		            min: 0,
		            max: 1300,
		            position: 'left',
		            offset:90,
		            axisLine: {
		                lineStyle: {
		                    color: colors[2],
		                    width:2
		                }
		            },
		             axisLabel:{
		            	textStyle:{
		            		fontSize:18
		            	}
		            },
		        },
		        {
		            type: 'value',
//		            name: '电\n流\n(A)',
		            min: 1,
		            max: 6,
		            position: 'left',
		            offset:45,
		            axisLine: {
		                lineStyle: {
		                    color: colors[3],
		                    width:2
		                }
		            },
		       		  axisLabel:{
		       		  	margin:20,
		            	textStyle:{
		            		fontSize:18
		            	}
		            },
		        },
		        {
		            type: 'value',
//		            name: '效\n率\n(%)',
		            min: 10,
		            max: 80,
		            position: 'left',
		            offset:0,
		            axisLine: {
		                lineStyle: {
		                    color: colors[4],
		                    width:2
		                }
		            },
		              axisLabel:{
		              	margin:15,
		            	textStyle:{
		            		fontSize:18
		            	}
		            },
		        }
		    ], 
                // 数据内容数组  
                series: function(){  
                    var serie=[];  
                    for(var i=0;i<$scope.legend.length;i++){  
                        var item = {  
                            name : $scope.legend[i],  
                            type: 'line',
                            yAxisIndex:$scope.num[i],
                            data: $scope.data[i]  
                        };  
                        serie.push(item);  
                    }  
                    return serie;  
                }()  
            };  
            var myChart = echarts.init(document.getElementById($scope.id),'macarons');  
            myChart.setOption(option);  
        }  
    };  
})
starter.directive('line', function() {  
    return {  
        scope: {  
            id: "@",  
            legend1: "=",  
            item1: "=",
            num:"=",
            data1: "="  
        },  
        restrict: 'E',  
        template: '<div style="height:700px;"></div>',  
        replace: true,  
        link: function($scope, element, attrs, controller) { 
        	var colors = ['#a62ca6', '#8e0515', '#45baf3','#ff2420','#1a9a1a'];
            var option = { 
            	color:colors,
                // 提示框，鼠标悬浮交互时的信息提示  
                tooltip: {  
                    show: true,  
                    trigger: 'axis'  
                },  
                grid:{

                	left:'26%'
                },
                   	//图例  
                legend: {  
                    data: $scope.legend1 ,
                    textStyle:{
                    	fontSize:22
                    }
                }, 
//              toolbox: {
//			        feature: {
//			            dataView: {show: true, readOnly: false},
//			            restore: {show: true},
//			            saveAsImage: {show: true}
//			        }
//			    },
                // 横轴坐标轴  
                xAxis: [
			        {
			            type: 'category',
//			            boundaryGap:false,
			            axisLabel:{
		            		textStyle:{
		            		fontSize:20
		            	},
		            	formatter: '{value} N*m'
		            },
			            data: $scope.item1
			        }
			    ], 
			    
                // 纵轴坐标轴  
                yAxis: [
		        {
		            type: 'value',
//		            name: '转速',
		            min: 600,
		            max: 2000,
		            position: 'left',
		            offset:205,
		            axisLine: {
		                lineStyle: {
		                    color: colors[0],
		                    width:2
		                }
		            },
		            axisLabel:{
		            	margin:20,
		            	textStyle:{
		            		fontSize:18
		            	}
		            }
		        },
		        {
		            type: 'value',
//		            name: '电\n压\n(V)',
		            min: 193,
		            max: 202,
		            position: 'left',
		            offset: 150,
		            axisLine: {
		                lineStyle: {
		                    color: colors[1],
		                    width:2
		                }
		            },
		             axisLabel:{
		             	margin:15,
		            	textStyle:{
		            		fontSize:18
		            	}
		            },
		        },
		        {
		            type: 'value',
//		            name: '输\n入\n功\n率\n(W)',
		            min: 0,
		            max: 1300,
		            position: 'left',
		            offset:90,
		            axisLine: {
		                lineStyle: {
		                    color: colors[2],
		                    width:2
		                }
		            },
		             axisLabel:{
		            	textStyle:{
		            		fontSize:18
		            	}
		            },
		        },
		        {
		            type: 'value',
//		            name: '电\n流\n(A)',
		            min: 1,
		            max: 6,
		            position: 'left',
		            offset:45,
		            axisLine: {
		                lineStyle: {
		                    color: colors[3],
		                    width:2
		                }
		            },
		       		  axisLabel:{
		       		  	margin:20,
		            	textStyle:{
		            		fontSize:18
		            	}
		            },
		        },
		        {
		            type: 'value',
//		            name: '效\n率\n(%)',
		            min: 10,
		            max: 80,
		            position: 'left',
		            offset:0,
		            axisLine: {
		                lineStyle: {
		                    color: colors[4],
		                    width:2
		                }
		            },
		              axisLabel:{
		              	margin:15,
		            	textStyle:{
		            		fontSize:18
		            	}
		            },
		        }
		    ], 
                // 数据内容数组  
                series: function(){  
                    var serie=[];  
                    for(var i=0;i<$scope.legend1.length;i++){  
                        var item1 = {  
                            name : $scope.legend1[i],  
                            type: 'line',
                            yAxisIndex:$scope.num[i],
                            data: $scope.data1[i]  
                        };  
                        serie.push(item1);  
                    }  
                    return serie;  
                }()  
				
            };  
            var myChart = echarts.init(document.getElementById($scope.id),'macarons');  
            myChart.setOption(option);  
        }  
    };  
})

