<?php
/**
 * @author		Kano
 * @copyright	Kano Technology Limited
 * @license		Commercial
 */

defined("_JEXEC") or die("Restricted access");

class TradeController extends JControllerLegacy
{
	protected $default_view = 'trades';

    public function display($cachable = false, $urlparams = false)
	{
		JForm::addFormPath(JPATH_COMPONENT_ADMINISTRATOR . '/models/forms');
		JForm::addFieldPath(JPATH_COMPONENT_ADMINISTRATOR . '/models/fields');

        // Get the document object.
		$document	= JFactory::getDocument();

		// Set the default view name and format from the Request.
		$vName   = $this->input->getCmd('view', 'trades');
		$vFormat = $document->getType();
		$lName   = $this->input->getCmd('layout', 'default');
		
        if ($vName=="backup") {
			BackupHelper::store();
    	} elseif ($vName=="restore") {
			BackupHelper::restore();
    	} elseif ($vName=="import") {
			BackupHelper::import();
		} else {
			// Get the model and the view
			$model = $this->getModel($vName);
			$view = $this->getView($vName, $vFormat);
			
			// Push the model into the view (as default).
			$view->setModel($model, true);
			$view->setLayout($lName);
			
			// Push document object into the view.
			$view->document = $document;

			// Display the view
			$view->display();
		}
    }

	public function enquiry()
    {
        $input = JFactory::getApplication()->input;

        $db = JFactory::getDbo();
        $user = JFactory::getUser();
    	$session = JFactory::getSession();
        $cart_id = $session->get('cart_id');
 
        $data = array();
        $data['cart_id'] = $cart_id;
        $data['trade_id'] = $input->getInt('id');
        $data['created_by']=$user->id;
        $data['created'] = JFactory::getDate()->format('%Y-%m-%d %H:%M:%S');
		
        // Check exist
        $query = "SELECT id FROM #__trade_cart WHERE trade_id=".(int)$data['trade_id']." AND cart_id='".$cart_id."'";
		$db->setQuery($query);
		$trade = $db->loadResult();
				
		if (empty($trade)) {
			$model = $this->getModel('Cart');
			$model->save($data);
		}
		
		$this->setRedirect( JRoute::_('index.php?Itemid=124'),false);
    }
	
	public function add_cart_item()
    {
        $input = JFactory::getApplication()->input;

        $db = JFactory::getDbo();
        $user = JFactory::getUser();
    	$session = JFactory::getSession();
        $cart_id = $session->get('cart_id');
 
        $data = array();
        $data['cart_id'] = $cart_id;
        $data['trade_id'] = $input->getInt('id');
        $data['created_by']=$user->id;
        $data['created'] = JFactory::getDate()->format('%Y-%m-%d %H:%M:%S');
		
        // Check exist
        $query = "SELECT id FROM #__trade_cart WHERE trade_id=".(int)$data['trade_id']." AND cart_id='".$cart_id."'";
		$db->setQuery($query);
		$trade = $db->loadResult();
				
		if (empty($trade)) {
			$model = $this->getModel('Cart');
			$model->save($data);
		}
		echo '{"status":"success"}';
		//echo json_encode($data);
		exit();
		
    }
	
    public function update_cart_item()
    {
        $input = JFactory::getApplication()->input;
        $db = JFactory::getDbo();
        $user = JFactory::getUser();
    	$session = JFactory::getSession();
        $cart_id = $session->get('cart_id');
		$attributes = $input->get('attributes','','array');
		var_dump($attributes);
		
        $data = array();
        $data['cart_id'] = $cart_id;
        $data['id'] = $input->getInt('id');
        $data['quantity'] = $input->getInt('quantity');
		if (!empty($attributes)) $data['remark'] = json_encode($attributes);
		
        $user->id?$data['created_by']:$user->id;
		
    	$model = $this->getModel('Cart');
        $model->save($data);
		
		echo json_encode($data);
		exit();
		
		//$this->setRedirect( JRoute::_('index.php?option=com_trade&view=carts&Itemid=187'),false);
		//return;
	}
	
    public function remove_cart_item()
    {
    	$session = JFactory::getSession();
        $cart_id = $session->get('cart_id');
      
        $input = JFactory::getApplication()->input;
        $trade_id = $input->getInt('id');

        $db = JFactory::getDbo();
        $query = "DELETE FROM #__trade_cart WHERE trade_id=".(int)$trade_id." AND cart_id='".$cart_id."'";
        $db->setQuery($query);
        $db->query();
		echo '{"status":"success"}';
		//echo json_encode($data);
		exit();
      // $this->setRedirect( JRoute::_('index.php?option=com_trade&view=carts&Itemid=187'));
		//return;
    }
	
    public function delete_cart_item()
    {
    	$session = JFactory::getSession();
        $cart_id = $session->get('cart_id');
      
        $input = JFactory::getApplication()->input;
        $trade_id = $input->getInt('id');

        $db = JFactory::getDbo();
        $query = "DELETE FROM #__trade_cart WHERE id=".(int)$trade_id." AND cart_id='".$cart_id."'";
        $db->setQuery($query);
        $db->query();
		//echo '{"status":"success"}';
		//echo json_encode($data);
		//exit();
       $this->setRedirect( JRoute::_('index.php?Itemid=175'));
		//return;
    }
	
    public function cart_total()
    {
    	$session = JFactory::getSession();
        $cart_id = $session->get('cart_id');
      
        //$input = JFactory::getApplication()->input;
       // $trade_id = $input->getInt('id');

        $db = JFactory::getDbo();
        $query = "SELECT count(*) FROM #__trade_cart WHERE cart_id='".$cart_id."'";
        $db->setQuery($query);
        $count = $db->loadResult();
		if ($count>0)
			echo "已選商機 (".$count.")";
		else
			echo "已選商機";
		exit();
       // $this->setRedirect( JRoute::_('index.php?option=com_trade&view=carts&Itemid=187'));
		//return;
    }
    public function cart_total2()
    {
    	$session = JFactory::getSession();
        $cart_id = $session->get('cart_id');
        $db = JFactory::getDbo();
        $query = "SELECT count(*) FROM #__trade_cart WHERE cart_id='".$cart_id."'";
        $db->setQuery($query);
        $count = $db->loadResult();
		echo (int)$count;
		exit();

    }
}

?>
