/**
 * InvoiceOnlinePaymentModalCtrl Controller
 * Extracted from invoices module
 */

Invoices.controller('InvoiceOnlinePaymentModalCtrl', ['$scope', 'data', 'InvoiceTransaction', 'ErrorsHandler', '$uibModalInstance', function ($scope, data, InvoiceTransaction, ErrorsHandler, $uibModalInstance) {

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

	var thisYear = parseInt(moment().format("YYYY"), 10);

	$scope.data = {
		amount: (data.amount_due > 0) ? data.amount_due : 0,
		invoice_id: data.id,
		method_of_payment: 'card',
		notes: 'Stripe - Online Card Payment',
		exp_month: '01',
		exp_year: thisYear
	};

	$scope.months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
	$scope.years = [];

	// Generate the years
	for (var i = thisYear; i < thisYear + 10; i++) {
		$scope.years.push(i);
	};

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


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

	$scope.save = function () {
		$scope.data.payment_date = moment().format("YYYY-MM-DD");
		$scope.loading = true;
		InvoiceTransaction.onlinePayment($scope.data, function (response) {
			angular.extend(data, response.invoice);
			data.transactions.push(response);
			$uibModalInstance.close(response);
			$scope.loading = false;
		}, function (a, b) {
			$scope.loading = false;
			ErrorsHandler(a, b);
		});
	};

	$scope.cancel = function () {
		$uibModalInstance.dismiss('cancel');
	};

}]);
