<?php
namespace App\Dashboard\Widget;

class OrderControlPanel extends \Engine\Dashboard\Widgets\WidgetAbstract
{
	/**
	 * The default cache lifetime.
	 * @var Int
	 */
	protected $cacheLifetime = 60;

	/**
	 * The default options associated when the widget is added.
	 * @var array
	 */
	protected function getDefaultOptions()
	{
		$return = [];
		return $return;
	}

	/**
	 * Generate the options form
	 */
	protected function populateLocalOptionsForm(\Engine\Form\Container\GeneralAbstract $form)
	{
		return $form;
	}

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Dashboard\Widgets\WidgetAbstract::getWidgetCacheLifeDurations()
	 */
	protected function getWidgetCacheLifeDurations()
	{
		return [0, 30, 60, 300, 600, 900, 1800, 3600];
	}

	/**
	 * Extract data from the form return($R) and sets internal configs($this->options) accordingly
	 */
	protected function extractFormOptions($formData)
	{
		$options = [];
		$this->setOptions($options);
	}

	/**
	 * Returns the content id of the current widget.
	 */
	protected function getContentId()
	{
		return $this->getOption_rec('idOrder')."_".serialize($this->getOptions());
	}


	/**
	 * Sets the collected processed data.
	 * @return Array
	 */
	protected function getData()
	{
		$calledClass = get_called_class();
		$className = stripClassNamespace($calledClass);
		$loaderLang = \App\Loader_Lang::getInstance();
		$loaderLang->loadAppFile("_customLoad/dashboard/widgets/_general.lang.php");
		$loaderLang->loadAppFile("_customLoad/dashboard/widgets/{$className}.lang.php");
		$_LANG = $loaderLang->getAll();

		$output = [];

		// validate order
		if ($this->getOption_rec('idOrder')) {
			if (!is_numeric($this->getOption_rec('idOrder')) || $this->getOption_rec('idOrder') <= 0) {
				$output['errorMessage'] = $_LANG['dashboard']['widgetsList'][$calledClass]['messages']['invalidIdOrder'];
			}

			if (!$output['errorMessage']) {
				$orderEntity = \Entity\Order::select($this->getOption_rec('idOrder'));
				if (!$orderEntity->recordExists()) {
					$output['errorMessage'] = $_LANG['dashboard']['widgetsList'][$calledClass]['messages']['invalidIdOrder'];
				}

				// check user access
				switch(\App\UserAccess\CurrentUserAccessFast::getInstance()->getAccessOptionValue('reservationView')) {
					case 'all':
						break;
					case 'own':
						if ($orderEntity->idUser != CURRENT_ID_USER) {
							$output['errorMessage'] = $_LANG['dashboard']['widgetsList'][$calledClass]['messages']['reservationView']['own'];
						}
						break;
					case 'deptAndOff':
						if ($orderEntity->idOffice != CURRENT_ID_OFFICE || $orderEntity->user->idDepartment != CURRENT_ID_DEPT) {
							$output['errorMessage'] = $_LANG['dashboard']['widgetsList'][$calledClass]['messages']['reservationView']['deptAndOff'];
						}
						break;
					case 'office':
						if ($orderEntity->idOffice != CURRENT_ID_OFFICE) {
							$output['errorMessage'] = $_LANG['dashboard']['widgetsList'][$calledClass]['messages']['reservationView']['office'];
						}
						break;
					default:
						throw new \Engine\Dashboard\Exception('Wrong user access value for `reservationView` in `'.__METHOD__.'` method.');
				}

			}
		}

		if (!$output['errorMessage']) {

			\App\Dashboard\Widget\Helper\Order::getInstance($this->getOption_rec('idOrder'));
			$output['orderId'] = $this->getOption_rec('idOrder');

			$buttonOutput = [];

			$orderEntity = \Entity\Order::update($output['orderId']);


			// ORDER VALIDATIONS

			if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Orders/OrdersList')) {
				$buttonOutput['order']['redirectToList'] = true;
			}

			if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('reservationDocumentsManagement')) {
				$buttonOutput['order']['adminDocuments'] = true;
			}
			if (!\App\Config\IssueDocuments::getInstance()->getOrderDocsList()) {
				$buttonOutput['order']['adminDocuments'] = true;
			}

			if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Orders/Cancel-cancelOrder')) {
				$buttonOutput['order']['cancelOrder'] = true;
			}

			// SERVICE VALIDATIONS
			if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Orders/ServicesList')) {
				$buttonOutput['service']['redirectToList'] = true;
			}

			if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('reservationProcessFromImportedRes')) {
				$buttonOutput['service']['addImportedService'] = true;
			}

			if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('reservationServiceAdd')) {
				$buttonOutput['service']['addManualService'] = true;
			}

			// CLIENT INVOICE VALIDATIONS
			if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Invoices/InvoicesList')) {
				$buttonOutput['clientInvoice']['redirectToList'] = true;
			}

			if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Invoices/InvoiceInsert')) {
				$buttonOutput['clientInvoice']['addInvoice'] = true;
			}

			// SUPPLIER INVOICE VALIDATIONS
			if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Invoices/SupplierInvoices')) {
				$buttonOutput['supplierInvoice']['redirectToList'] = true;
			}

			if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Invoices/SupplierInvoices-insert')) {
				$buttonOutput['supplierInvoice']['addSupplierInvoice'] = true;
			}

			// BALANCE VALIDATIONS
			if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Invoices/BalancesAuto')) {
				$buttonOutput['balance']['redirectToList'] = true;
			}

			if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Invoices/BalancesList-confirm')) {
				$buttonOutput['balance']['orderBalance'] = true;
			}


			$output['buttons'] = $buttonOutput;

			// ASSOCIATE THE COLUMNS LIST
			$output['columnsList'] = [
				'entity' => 		true,
				'viewOption'	=>	true,
			];

		}

		return $output;
	}



}