<?php
/**
 * @version     1.4.7
 * @package     sellacious
 *
 * @copyright   Copyright (C) 2012-2017 Bhartiy Web Technologies. All rights reserved.
 * @license     SPL Sellacious Private License; see http://www.sellacious.com/spl.html
 * @author      Izhar Aazmi <info@bhartiy.com> - http://www.bhartiy.com
 */
// no direct access.
defined('_JEXEC') or die;

use Joomla\Utilities\ArrayHelper;

/**
 * list controller class
 *
 * @since  1.0.0
 */
class SellaciousControllerOrders extends SellaciousControllerBase
{
	/**
	 * @var	 string  The prefix to use with controller messages.
	 */
	protected $text_prefix = 'COM_SELLACIOUS_ORDERS';

	/**
	 * Load form for the status update of an item in orders list
	 *
	 * @return  void
	 */
	public function getItemStatusFormAjax()
	{
		$app = JFactory::getApplication();
		$me  = JFactory::getUser();

		try
		{
			if (!JSession::checkToken())
			{
				throw new Exception(JText::_('JINVALID_TOKEN'));
			}

			if ($me->guest)
			{
				throw new Exception(JText::_('COM_SELLACIOUS_ACCESS_NOT_ALLOWED'));
			}

			$post = $app->input->get('jform', array(), 'array');

			$order_id  = ArrayHelper::getValue($post, 'order_id');
			$item_uid  = ArrayHelper::getValue($post, 'item_uid');
			$status_id = ArrayHelper::getValue($post, 'status');

			$status    = $this->helper->order->getStatus($order_id, $item_uid);
			$status_id = $status_id ? $status_id : $status->s_id;

			// We don't want notes to be auto-populated
			$status->notes          = null;
			$status->customer_notes = null;

			$form = $this->helper->order->getStatusForm($status_id, $status);
			$args = array('form' => $form, 'full' => false);
			$html = JLayoutHelper::render('com_sellacious.order.item.status_form', $args);

			$data = array(
				'message' => '',
				'data'    => preg_replace('|[\r\n\t]+|', '', $html),
				'status'  => 1,
			);
		}
		catch (Exception $e)
		{
			$data = array(
				'message' => $e->getMessage(),
				'data'    => null,
				'status'  => 0,
			);
		}

		echo json_encode($data);

		$app->close();
	}

	/**
	 * Load a log of the status updates of an item in orders list
	 *
	 * @return  void
	 */
	public function getItemStatusLogAjax()
	{
		$app = JFactory::getApplication();
		$me  = JFactory::getUser();

		try
		{
			if (!JSession::checkToken())
			{
				throw new Exception(JText::_('JINVALID_TOKEN'));
			}

			if ($me->guest)
			{
				throw new Exception(JText::_('COM_SELLACIOUS_ACCESS_NOT_ALLOWED'));
			}

			$post = $app->input->get('jform', array(), 'array');

			$order_id = ArrayHelper::getValue($post, 'order_id');
			$item_uid = ArrayHelper::getValue($post, 'item_uid');

			$table = $this->helper->order->getTable('OrderItem');
			$table->load(compact('order_id', 'item_uid'));

			$order = $this->helper->order->getItem($order_id);
			$item  = $table->getProperties();
			$log   = $this->helper->order->getStatusLog($order_id, $item_uid);

			$data = array('order' => $order, 'item' => $item, 'log' => $log);
			$html = JLayoutHelper::render('com_sellacious.order.item.status_log', $data);

			$data = array(
				'message' => '',
				'data'    => preg_replace('|[\r\n\t]+|', '', $html),
				'status'  => 1,
			);
		}
		catch (Exception $e)
		{
			ob_end_clean();
			$data = array(
				'message' => $e->getMessage(),
				'data'    => null,
				'status'  => 0,
			);
		}

		echo json_encode($data);

		$app->close();
	}

	/**
	 * Update status information for an order item
	 *
	 * @throws Exception
	 * @return void
	 */
	public function setItemStatusAjax()
	{
		$app = JFactory::getApplication();
		$me  = JFactory::getUser();

		try
		{
			if (!JSession::checkToken())
			{
				throw new Exception(JText::_('JINVALID_TOKEN'));
			}

			if ($me->guest)
			{
				throw new Exception(JText::_('COM_SELLACIOUS_ACCESS_NOT_ALLOWED'));
			}

			$post = $app->input->post->get('jform', array(), 'array');

			$order_id = ArrayHelper::getValue($post, 'order_id');
			$item_uid = ArrayHelper::getValue($post, 'item_uid');

			$table = $this->helper->order->getTable('OrderItem');
			$table->load(compact('order_id', 'item_uid'));

			if (!$order_id || !$item_uid || !$table->get('id'))
			{
				throw new Exception(JText::_($this->text_prefix . '_ORDER_ITEM_INVALID'));
			}

			if ($this->helper->access->check('order.item.edit.status') ||
				($this->helper->access->check('order.item.edit.status.own') && $table->get('seller_uid') == $me->id))
			{
				$this->helper->order->setStatus($post);
				$status = $this->helper->order->getStatus($order_id, $item_uid);

				try
				{
					$dispatcher = JEventDispatcher::getInstance();
					JPluginHelper::importPlugin('sellacious');
					$dispatcher->trigger('onAfterOrderChange', array('com_sellacious.order', $order_id));
				}
				catch (Exception $e)
				{
					// Email sending failed. Ignore for now
				}
			}
			else
			{
				throw new Exception(JText::_('COM_SELLACIOUS_ACCESS_NOT_ALLOWED'));
			}

			$status->next_status = $this->helper->order->getStatuses(null, $status->status, false, true);

			$data = array(
				'message' => JText::_($this->text_prefix . '_SHIPMENT_ORDER_ITEM_UPDATE_SUCCESS'),
				'data'    => $status,
				'status'  => 1,
			);
		}
		catch (Exception $e)
		{
			$data = array(
				'message' => $e->getMessage(),
				'data'    => null,
				'status'  => 0,
			);
		}

		echo json_encode($data);

		$app->close();
	}
}
