'use strict';

var angular = require('angular');
require('../modules');
require('../services/LoginService');
require('../../../commons/services/AppConfig');

var module = angular.module('login');

module.controller('CALoginCtrl', function($scope, LoginService, $state, $timeout,
	AppConfig) {
	$scope.$on('$ionicView.loaded', function() {
		$scope.calogin();
	});
	init();

	function init() {
		if(!AppConfig.getCustomResult('passwordLoginEnable')) {
			$scope.passwordEnable = false;
		} else {
		 	// 定制是否显示密码登录页面
			$scope.passwordEnable = true;
		}
	}


	$scope.calogin =  function() {
		$scope.loginInfo = '登录中,请稍等...';
		$scope.loginSuccess = true;
		LoginService.calogin()
		.then(function(data) {
			if (!data.success) {
				caloginError(data.message);
			}
		}, function() {
			caloginError();
		});
	};

	// 登录失败的情况
	function caloginError(message) {
		$scope.loginSuccess = false;
		$scope.loginInfo = message ? message : '登录失败,请重新登录！';
	}

	/**
	* 跳转到密码登录页面
	*/
	$scope.toPasswordLogin = function() {
		$scope.loginSuccess = true;
		$state.go('login');
	};
});