var Accounts = angular.module('accounts', []);
Accounts.controller('AccountsCtrl', ['$scope', '$filter', function ($scope, $filter) {

	$scope.piechart = {
		labels: [],
		data: []
	};

	$scope.$watch('pieChartData', function (data) {
		$scope.piechart.labels = ['Repairs', 'Support', 'Onsite'];
		$scope.piechart.data = [data.repairs, data.support, data.onsite];
	}, true);


	$scope.linechart = {
		labels: [],
		series: ['Labour', 'Parts'],
		data: []
	};

	$scope.$watch('lineChartData', function (data) {

		$scope.linechart = {
			labels: [],
			series: ['Labour', 'Parts'],
			data: [[], []]
		};

		_.each(data, function (el) {
			var parts = parseFloat($filter('currency')(el.parts || 0, '').replace(',', '')).toFixed(2);
			$scope.linechart.labels.push(el.timestamp);
			$scope.linechart.data[0].push(el.labour);
			$scope.linechart.data[1].push(parts);
		});
	}, true);

}]);
Accounts.directive('accountsProjects', function () {
	return {
		restrict: 'A',
		templateUrl: 'accounts/projects.html',
		scope: {
			clientId: '@',
			clientType: '@'
		},
		controller: ['$scope', 'Project', 'Invoice', 'ErrorsHandler', '$paginator', 'Messenger', '$http', 'apiUrl', '$window', 'InvoiceBookingService', function ($scope, Project, Invoice, ErrorsHandler, $paginator, Messenger, $http, apiUrl, $window, InvoiceBookingService) {

			$paginator = angular.copy($paginator);
			$scope.paginator = $paginator;
			$scope.filters = {};
			$scope.InvoiceService = InvoiceBookingService;

			$paginator.setOnPageChange(function (page) {
				$scope.load(page);
			});

			$scope.$watch('filters', function () {
				$scope.load($paginator.getCurrentPage());
			}, true);

			$scope.load = function (page) {
				if (!page) page = 1;

				var filters = angular.extend({ page: page, client: true, user: true, results: 'accounts', summary: true }, $scope.filters);
				if ($scope.clientId && $scope.clientType) {
					filters.client_id = $scope.clientId;
					filters.client_type = $scope.clientType;
				}

				$scope.loading = true;

				Project.get(filters, function (response) {
					$paginator.setFromResponse(response);
					$scope.projects = response.data;
					$scope.loading = false;
				}, ErrorsHandler);
			};

			$scope.orderBy = function (name) {
				if ($scope.filters.orderBy && $scope.filters.orderBy == name) {
					$scope.filters.orderType = $scope.filters.orderType == 'asc' ? 'desc' : 'asc';
				}
				else {
					$scope.filters.orderBy = name;
					$scope.filters.orderType = 'asc';
				}
			};

			$scope.save = function (project, _form) {
				Project.update({ id: project.id, invoice_number: project.invoice_number, paid_at: project.paid_at }, function (response) { Messenger("Project #" + project.id + ' has been updated.'); _form.$setPristine(); angular.extend(project, response || {}) }, ErrorsHandler);
			};

			$scope.markPaid = function (project) {
				var inv_no = project.invoice_number ? project.invoice_number : 1;

				Project.update({ id: project.id, paid: 1, invoice_number: inv_no }, function (response) {
					angular.extend(project, response || {});
					Messenger("Project #" + project.id + ' has been marked as paid.');
				}, ErrorsHandler);
			};

			$scope.load();
		}]
	};
});
Accounts.directive('accountsQueue', function () {
	return {
		restrict: 'A',
		templateUrl: 'accounts/queue.html',
		scope: {
			clientId: '@',
			clientType: '@'
		},
		controller: ['$scope', 'Tasks', 'Invoice', 'ErrorsHandler', '$paginator', 'Messenger', '$http', 'apiUrl', '$window', function ($scope, Tasks, Invoice, ErrorsHandler, $paginator, Messenger, $http, apiUrl, $window) {

			$paginator = angular.copy($paginator);
			$scope.paginator = $paginator;
			$scope.filters = {};

			$paginator.setOnPageChange(function (page) {
				$scope.load(page);
			});

			$scope.$watch('filters', function () {
				$scope.load($paginator.getCurrentPage());
			}, true);

			$scope.load = function (page) {
				if (!page) page = 1;

				var filters = angular.extend({ page: page, client: true, user: true, show: 'accounts', summary: true, parts_total: true }, $scope.filters);
				if ($scope.clientId && $scope.clientType) {
					filters.client_id = $scope.clientId;
					filters.client_type = $scope.clientType;
				}

				$scope.loading = true;

				Tasks.get(filters, function (response) {
					$paginator.setFromResponse(response);
					$scope.tasks = response.data;
					$scope.loading = false;
				}, ErrorsHandler);
			};

			$scope.orderBy = function (name) {
				if ($scope.filters.orderBy && $scope.filters.orderBy == name) {
					$scope.filters.orderType = $scope.filters.orderType == 'asc' ? 'desc' : 'asc';
				}
				else {
					$scope.filters.orderBy = name;
					$scope.filters.orderType = 'asc';
				}
			};

			$scope.save = function (task, _form) {
				Tasks.update({ id: task.id, invoice_number: task.invoice_number, paid_at: task.paid_at }, function (response) { Messenger("Task #" + task.id + ' has been updated.'); _form.$setPristine(); angular.extend(task, response || {}) }, ErrorsHandler);
			};

			$scope.markPaid = function (task) {
				Tasks.update({ id: task.id, paid: 1 }, function (response) {
					angular.extend(task, response || {});
					Messenger("Task #" + task.id + ' has been marked as paid.');
				}, ErrorsHandler);
			};

			$scope.generateInvoice = function (task) {
				$window.open('invoices#/invoices/new/' + task.id, '_blank');
			};

			$scope.showInvoice = function (task) {
				$window.open('invoices#/invoices/view/' + task.invoice_number, '_blank');
			};

			$scope.printInvoice = function (task) {
				// Direct PDF generation - no need for two API calls
				var pdfUrl = '/generate-invoice/' + task.invoice_number;
				$window.open(pdfUrl, '_blank');
			}

			$scope.load();
		}]
	};
});
/**
 * Admin Module
 *
 */
