<?php
/**
 * CoMarket controlls the actions for the shop
 */
class CoMarketController{
    private $model;

    function __construct(){
        $this->model = new CoMarketModel();
    }

    /**
     * get all packages taht can be sold
     * @return json all packages that can be sold in web shop
     */
    public function getShopPackages(){
        $idCommercialLead = isset($_REQUEST['idCommercialLead']) ? $_REQUEST['idCommercialLead'] : 0;
        $serach = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';
        echo json_encode($this->model->getShopPackages($idCommercialLead, 0, $serach), JSON_NUMERIC_CHECK);
    }

    /**
     * get all comercial leads lined to the user
     * @return json list of commercial leads
     */
    public function getAllCommercialLeads(){
        echo json_encode($this->model->getAllCommercialLeads(), JSON_NUMERIC_CHECK);
    }

    /**
     * get web shop details for a package
     * @return json package details
     */
    public function getShopPackageDetails(){
        $idCommercialLead = isset($_REQUEST['idCommercialLead']) ? $_REQUEST['idCommercialLead'] : 0;
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        echo json_encode($this->model->getShopPackageDetails($idCommercialLead, $idPackage), JSON_NUMERIC_CHECK);
    }

    /**
     * add items to cart
     */
    public function addToCart(){
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        $idPrice = isset($_REQUEST['idPrice']) ? $_REQUEST['idPrice'] : 0;
        $options = isset($_REQUEST['options']) ? $_REQUEST['options'] : '[]';
        echo json_encode($this->model->addToCart($idPackage, $idPrice, $options));
    }
}
