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

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

    /**
     * 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));
    }

    /**
     * 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));
    }

    /**
     * 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));
    }

    /**
     * update quantity for a product
     * @return json update message
     */
    public function updateQuantity(){
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        $idPrice = isset($_REQUEST['idPrice']) ? $_REQUEST['idPrice'] : 0;
        $idCustomerInstance = isset($_REQUEST['idCustomerInstance']) ? $_REQUEST['idCustomerInstance'] : 0;
        $quantity = isset($_REQUEST['quantity']) ? $_REQUEST['quantity'] : 1;
        echo json_encode($this->model->updateQuantity($idPackage, $idCustomerInstance, $idPrice, $quantity));
    }

    /**
     * remove items from carts
     * @return json update message
     */
    public function removeFromCart(){
        $idCart = isset($_REQUEST['idCart']) ? $_REQUEST['idCart'] : 0;
        echo json_encode($this->model->removeFromCart($idCart));
    }

    /**
     * get the name and code of the country
     */
    public function getCountryDetailsById() {
        $idCountry = isset($_REQUEST['idCountry']) ? $_REQUEST['idCountry'] : 0;
        echo json_encode($this->model->getCountryDetailsById($idCountry));
    }

    /**
     * get count of items in te cart
     * @return json number of items in the cart
     */
    public function getShopCartCount(){
        echo json_encode($this->model->getShopCartCount());
    }

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

    /**
     * get web shop cart content
     * @return josn list of packages in the cart
     */
    public function getShopCart(){
        echo json_encode($this->model->getShopCart());
    }

    /**
     * gets the details for the customer logged in, if any
     * @return Array of details
     */
    public function getCustomerDetails() {
        echo json_encode($this->model->getCustomerDetails());
    }

    /**
     * get the countries for delivery address
     * @return array with countries available
     */
    public function getCountries() {
        echo json_encode($this->model->getCountries());
    }

    public function placeOrder() {
        $cartPackages = isset($_REQUEST['cartPackages']) ? $_REQUEST['cartPackages'] : '[]';
        $deliveryInfo = isset($_REQUEST['deliveryInfo']) ? $_REQUEST['deliveryInfo'] : '[]';
        $billingInfo = isset($_REQUEST['billingInfo']) ? $_REQUEST['billingInfo'] : '[]';
        $details = isset($_REQUEST['details']) ? $_REQUEST['details'] : '[]';

        echo json_encode($this->model->placeOrder($cartPackages, $deliveryInfo, $billingInfo, $details));
    }

    /**
     * get document sfor packages in cart
     * @return json Array of documents
     */
    public function getCartDocuments(){
        $packages = isset($_REQUEST['packages']) ? $_REQUEST['packages'] : '[]';
        echo json_encode($this->model->getCartDocuments($packages));
    }

    /**
     * upload a questionaire form the cart page
     * @return json upload message
     */
    public function uploadOrderDocument(){
        $file = isset($_FILES['file']) ? $_FILES['file'] : [];
        $idDocumentType = isset($_REQUEST['idDocumentType']) ? $_REQUEST['idDocumentType'] : '';
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        echo json_encode($this->model->uploadOrderDocument($file, $idDocumentType, $idPackage));
    }

    /**
     * incude template for serach input
     */
    public function shopPackageSearchTemplate(){
        require_once('templates/ShopPackageSearchTemplate.php');
    }

    /**
     * include shop template
     */
    public function shopTemplate(){
        require_once('templates/ShopTemplate.php');
    }

    /**
     * template for showing the packages in the shop
     */
    public function shopPackagesTemplate(){
        require_once('templates/ShopPackagesTemplate.php');
    }

    /**
     * tempalte for showing details for a selected package
     */
    public function shopPackageDetailsTemplate(){
        global $user;
        require_once('templates/ShopPackageDetailsTemplate.php');
    }

    /**
     * get template for shop cart
     */
    public function shopCartTemplate(){
        require_once('templates/ShopCartTemplate.php');
    }

    /**
     * get template for the cart review
     */
    public function cartReview() {
        require_once('templates/CartReview.php');
    }

    /**
     * open shop page
     */
    public function showPage(){
        require_once('ShopPage.php');
    }
}
