/**
 * InvoiceEmailModalCtrl Controller
 * Extracted from invoices module
 */

Invoices.controller("InvoiceEmailModalCtrl", [
	"$scope",
	"$uibModalInstance",
	"data",
	"client",
	"$http",
	"apiUrl",
	"ErrorsHandler",
	"Messenger",
	function (
		$scope,
		$uibModalInstance,
		data,
		client,
		$http,
		apiUrl,
		ErrorsHandler,
		Messenger
	) {
		// ------------------------------------------------------------------------
		//
		//	SCOPE VARIABLES BELOW -->
		//
		// ------------------------------------------------------------------------

		var paid_text = data.paid == "1" ? "" : "Payment is now due.";
		var type;

		switch (data.type) {
			case "invoice":
				type = "invoice";
				break;
			case "quote":
				type = "quote";
				break;

			default:
				type = "sale receipt";
		}

		$scope.data = {
			invoice_id: data.id,
			invoice_number: data.invoice_number || ("INV-" + data.id),
			send_to: client.data.email || client.data.Email || '',
			subject:
				"Your latest " + type + " from Wiseserve Ltd (Computer Assistance)",
			message:
				"Dear " +
				((data.client && data.client.name) || (client.data && client.data.name) || "Customer") +
				" : \n\n" +
				"Please find attached your latest " +
				type +
				" for work we recently completed.  " +
				paid_text +
				" \n\n" +
				"We appreciate your business and if you have any further issues, questions or queries, we are always here to help. \n\n" +
				"If you appreciated our customer service and support, we'd be really grateful if you left a kind review at https://tinyurl.com/y22tn73n \n\n" +
				"Computer Assistance \n" +
				"01865 451177"
		};

		// Loading state
		$scope.sending = false;

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

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


	$scope.previewInvoice = function () {
		// Open the invoice PDF in a new tab
		var invoiceId = data.invoice_number || data.id;
		var pdfUrl = "/generate-invoice/" + invoiceId;
		window.open(pdfUrl, "_blank");
	};

		$scope.send = function () {
			// Set loading state
			$scope.sending = true;
			
			// Show sending notification using simple Messenger syntax
			if (typeof Messenger !== 'undefined') {
				Messenger("Sending email...");
			}

			$http
				.post(apiUrl("invoices/email-invoice"), $scope.data)
				.success(function (response) {
					$scope.sending = false;
					
					// Show success notification using simple Messenger syntax
					if (typeof Messenger !== 'undefined') {
						Messenger("Email sent successfully!");
					}
					
					// Close modal after showing notification
					$uibModalInstance.close('success');
				})
				.error(function (error, status) {
					$scope.sending = false;
					
					// Show error notification using simple Messenger syntax
					var errorMessage = error && error.error ? error.error : 'Failed to send email';
					if (typeof Messenger !== 'undefined') {
						Messenger("Email failed: " + errorMessage);
					}
					
					// Also call the original error handler for any additional processing
					ErrorsHandler(error, status);
				});
		};

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