/**
 * ClientsMainCtrl Controller
 * Extracted from clients module
 */

ClientsModule.controller('ClientsMainCtrl', ['$scope', 'Client', '$routeParams', '$location', 'Customer', 'ErrorsHandler', 'Messenger', '$http', 'apiUrl', 'CustomerForm', '$uibModal', function ($scope, Client, $routeParams, $location, Customer, ErrorsHandler, Messenger, $http, apiUrl, CustomerForm, $uibModal) {

	// ------------------------------------------------------------------------
	//
	//	SCOPE VARIABLES BELOW -->
	//
	// ------------------------------------------------------------------------

	if ($location.path() == '') {
		$location.path('/clients');
		return;
	}

	$scope.data = [];

	$scope.activeTab = 0;

	$scope.tabs = [
		{
			title: 'Customers',
			template: 'clients/customers-table.html'
		},
		{
			title: 'Business Clients',
			template: 'clients/clients-table.html'
		},
		{
			title: 'Marketing',
			template: 'clients/marketing.html'
		}
	];

	CustomerForm.onCreate = function (data) {
		// Close the modal
		$scope.customerModal.dismiss();
		$location.path('/customer/' + data.id);
	};

	// ------------------------------------------------------------------------
	//
	//	WATCHERS AND EVENT LISTENERS BELOW -->
	//
	// ------------------------------------------------------------------------

	$scope.$on('$routeChangeSuccess', function () {
		$scope.loadStats();
	});

	// ------------------------------------------------------------------------
	//
	//	FUNCTIONS BELOW THIS LINE -->
	//
	// ------------------------------------------------------------------------

	$scope.loadStats = function () {
		$http.get(apiUrl('clients/helpers/overview'))
			.success(function (response) {

				$scope.data = response;

			}).error(ErrorsHandler);
	};

	$scope.showClientsWizard = function () {
		var modalInstance = $uibModal.open({
			templateUrl: 'clients/modals/new-client.html',
			controller: 'AddClientCtrl',
			size: 'lg',
			backdrop: 'static'
		});
	};

	$scope.showCustomerForm = function () {
		$scope.customerModal = $uibModal.open({
			templateUrl: 'clients/modals/new-customer.html',
			controller: 'AddCustomerCtrl',
			size: 'lg'
		});
	};
}]);
