/**
 * PagesOrdersCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('PagesOrdersCtrl', ['$scope', '$ClientsManager', 'Order', 'ErrorsHandler', '$location', 'Messenger', 'Page', 'PartsService', 'InvoiceBookingService', function ($scope, $ClientsManager, Order, ErrorsHandler, $location, Messenger, Page, PartsService, InvoiceService) {

	/**
	 * The repair data
	 *
	 * @object
	 */
	$scope.data = {};

	/**
	 * All the possible repair statuses.
	 *
	 * @type array
	 */
	$scope.statuses = [];

	/**
	 * A local reference to the clients manager service.
	 *
	 * @object
	 */
	$scope.clientManager = $ClientsManager;

	/**
	 * The total price.
	 *
	 * @type Float
	 */
	$scope.total = 0;

	/**
	 * A reference to the PartsService object that hold
	 * all shared data related to parts.
	 *
	 * @type Object
	 */
	$scope.PartsService = PartsService;

	/**
	 * A reference to the InvoiceService object that hold
	 * all shared data related to invoices.
	 *
	 * @type Object
	 */
	$scope.InvoiceService = InvoiceService;
	// --------------------------
	// Functions below
	// --------------------------

	/**
	 * Watch when the taskId becomes active and load the page.
	 *
	 * @return null
	 */
	$scope.$watch('taskId', function (value) {
		if (value) {
			Page.setTitle($scope.taskId);
			$scope.loadData();
		}
	});

	/**
	 * Loads the repair data, by sending an ajax request.
	 *
	 * @returns void
	 */
	$scope.loadData = function () {
		Order.get({ taskId: $scope.taskId }, function (res) {
			$scope.data = res;
			$ClientsManager.setClient(res.client);
			$ClientsManager.setClientType(res.client_type);

		}, ErrorsHandler);
	};

	/**
	 * Updates the repair data
	 */
	$scope.update = function (ev) {
		Order.update($scope.data, function (res) {
			$scope.data = res;
			$scope.labour_changed = false;
			Messenger("I've succesfully saved the remote support job!");
		}, ErrorsHandler);
	};


	/**
	 * Retrieves the current selected status object.
	 *
	 * @return object
	 */
	$scope.getStatus = function () {
		return _.find($scope.statuses, function (el) { return el.id == $scope.data.status_id; });
	};

	/**
	 * Returns the status name.
	 *
	 * @return string
	 */
	$scope.getStatusName = function () {
		var status = $scope.getStatus();

		return (status) ? status.name : '';
	};

	/**
	 * Updates the current task status.
	 *
	 * @param  integer id - status id
	 * @return void
	 */
	$scope.updateStatus = function (id) {

		//If it is completed, not to cancel
		if (id == 29 && $scope.data.completed)
			return;

		//If it is not completed, not to mark as collected
		if (id == 30 && !$scope.data.completed)
			return;

		if (id == $scope.data.status_id)
			return;

		Order.update({ id: $scope.data.id, status_id: id }, function (res) {
			$scope.data.status_id = id;
		}, ErrorsHandler);
	};


	$scope.generateInvoice = function (data) {

		if (PartsService.hasPartsOnOrder())
			alert("It is not possible to create an invoice for parts on order.");
		else
			InvoiceService.generate(data)
	};



	$scope.getPaymentDisplay = function (value) {
		return (value == '1') ? 'paid' : 'not paid';
	};

	$scope.getStatusIcon = function () {
		//Cancelled
		if ($scope.data.status_id == 29)
			return 'times';

		//On order
		if ($scope.data.status_id == 2)
			return 'clock-o';

		//Delivered
		if ($scope.data.status_id == 16)
			return 'truck';

		//Collected
		if ($scope.data.status_id == 30)
			return 'cube';
	};
}]);
