<?php
class PackagesController{
    private $model;

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

    /**
     * returns json array with virtual products available by categories
     * @return array with products info
     */
    public function getVirtualProductsByCategories(){
        echo json_encode($this->model->getVirtualProductsByCategories());
    }

    public function getProductsByCategory() {
        $country = isset($_REQUEST['idCountry']) ? $_REQUEST['idCountry'] : 0;
        echo json_encode($this->model->getProductsByCategory($country));
    }

    /**
     * returns a failure or success message for adding the template package in the DB
     * @return a failure or success message
     */
    public function createTemplatePackagesData(){
        $packageInfo = isset($_REQUEST['packageInfo']) ? $_REQUEST['packageInfo'] : [];
        $products = isset($_REQUEST['products']) ? $_REQUEST['products'] : [];
        echo json_encode($this->model->createTemplatePackagesData($packageInfo, $products));
    }

    /**
     * returns a failure or success message for adding the package in the DB
     * @return a failure or success message
     */
    public function createPackagesData(){
        $packageData = isset($_REQUEST['packageData']) ? $_REQUEST['packageData'] : [];
        $packageInfo = isset($_REQUEST['packageInfo']) ? $_REQUEST['packageInfo'] : [];
        echo json_encode($this->model->createPackagesData($packageData, $packageInfo));
    }

    /**
     * returns json array with table headers for packages
     * @return list all columns headers
     */
    public function getPackagesHeaders(){
        echo json_encode($this->model->getPackagesHeaders('array'));
    }

    /**
     * returns json response for packages
     * @return list packages json
     */
    public function getPackages(){
        $idCountry = isset($_REQUEST['idCountry']) ? $_REQUEST['idCountry'] : 0;
        $data = ['data' => $this->model->getPackages($idCountry)];
        echo json_encode($data);
    }

    /**
     * returns json array with table headers for my packages
     * @return list all columns headers
     */
    public function getMyPackagesHeaders(){
        echo json_encode($this->model->getMyPackagesHeaders('array'));
    }

    /**
     * returns json response for my packages
     * @return list my packages json
     */
    public function getMyPackages(){
        $country = isset($_REQUEST['idCountry']) ? $_REQUEST['idCountry'] : 0;
        $data = ['data' => $this->model->getMyPackages($country)];
        echo json_encode($data);
    }

    public function getCustomerPrices() {
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        $idCustomer = isset($_REQUEST['idCustomer']) ? $_REQUEST['idCustomer'] : 0;
        echo json_encode($this->model->getCustomerPrices($idPackage, $idCustomer));
    }

    public function uploadNewImage() {
      $file = isset($_FILES['file']) ? $_FILES['file'] : [];
      $options = isset($_REQUEST['options']) ? $_REQUEST['options'] : [];
      echo json_encode($this->model->uploadNewImage($file, $options));
    }

    public function getImagesFromCdn() {
        $idCountry = isset($_REQUEST['idCountry']) ? $_REQUEST['idCountry'] : 0;
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        echo json_encode($this->model->getImagesFromCdn($idCountry, $idPackage));
    }

    public function saveCoverPhotoForPackage() {
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        $publicId = isset($_REQUEST['publicId']) ? $_REQUEST['publicId'] : '';
        echo json_encode($this->model->saveCoverPhotoForPackage($idPackage, $publicId));
    }

    /**
     * include packages template
     */
    public function packagesTemplate(){
        global $user;
        require_once('templates/PackagesTemplate.php');
    }

    /**
     * include create packages tempalte
     */
    public function createTemplatePackagesTemplate(){
        require_once('templates/CreateTemplatePackagesTemplate.php');
    }

    /**
     * include create packages tempalte
     */
    public function createPackagesTemplate(){
        require_once('templates/CreatePackagesTemplate.php');
    }

    /**
     * include create packages from template template
     */
    public function createPackagesFromTemplateTemplate(){
        require_once('templates/CreatePackagesFromTemplateTemplate.php');
    }

    /**
     * include view package process template
     */
     public function viewPackageProcessesTemplate() {
         require_once('templates/ViewPackageProcessesTemplate.php');
     }

     /**
      * include edit packages template
      */
      public function editPackagesTemplate() {
          require_once('templates/EditPackagesTemplate.php');
      }

       /**
        * include generic package products by categoruies drag and drop lists
        */
        public function productsByCategoriesDragDropTemplate() {
            require_once('templates/ProductsByCategoriesDragDropTemplate.php');
        }

        /**
         * include generic package template products by categories drag and drop lists
         */
         public function productsTemplateByCategoriesDragDropTemplate() {
             require_once('templates/ProductsTemplateByCategoriesDragDropTemplate.php');
         }


       /**
        * include custom html for editing package templates
        */
        public function editPackageTemplatesHtml() {
            require_once('templates/EditPackageTemplatesHtml.php');
        }

      /**
       * include add packages process template
       */
      public function addPackageProcessTemplate() {
          require_once('templates/AddPackageProcessTemplate.php');
      }

      /**
      * include select package template
      * @return html template for select package
      */
     public function selectPackage() {
         require_once('templates/SelectPackage.html');
     }

     /**
      * include my package details template
      * @return html template for my packages details
      */
     public function myPackagesDetails() {
         require_once('templates/MyPackagesDetails.html');
     }

     /**
      * include set price for package temmplate
      * @return html template for set package price
      */
     public function setPackagePrice() {
         require_once('templates/SetPackagePrice.html');
     }

     /**
      * include temmplate for adding virtual products
      * @return html template for adding virtual products
      */
     public function addVirtualProductsTemplate() {
         require_once('templates/AddVirtualProductsTemplate.php');
     }

     /**
      * include temmplate for package options
      * @return html template for package options
      */
     public function packageOptionsTemplate() {
         require_once('templates/PackageOptionsTemplate.html');
     }

     /**
      * include temmplate for uploading images with CDN
      * @return html template for image upload
      */
     public function uploadImageCdnTemplate() {
         require_once('templates/UploadImageCdnTemplate.php');
     }

     /**
      * include template for viewing images from CDN
      * @return html template for image viewing
      */
     public function displayCdnImagesTemplate() {
         require_once('templates/DisplayCdnImagesTemplate.php');
     }

     /**
      * include temmplate for package details
      * @return html template for package details
      */
     public function packagesDetailsTemplate(){
         global $user;
         require_once('templates/PackagesDetailsTemplate.php');
     }

     /**
      * get all commecrial lead customers linked to package
      * @return json list with commercial lead customers
      */
     public function getComercialLeadCustomers(){
         $idPacakge = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
         echo json_encode($this->model->getComercialLeadCustomers($idPacakge));
     }

     /**
      * get details for a pacakge
      * @return json package details
      */
     public function getPackageInfo(){
         $idPacakge = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
         echo json_encode($this->model->getPackageInfo($idPacakge));
     }

     /**
      * get prices for a package (for broker and if set for commercial lead)
      * @return json price list
      */
     public function getBrokerPriceList(){
         $idPacakge = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
         $idCustomer = isset($_REQUEST['idCustomer']) ? $_REQUEST['idCustomer'] : 0;
         echo json_encode($this->model->getBrokerPriceList($idPacakge, $idCustomer));
     }

     /**
      * update dfeault prices for a commercial lead
      * @return json update message
      */
     public function updateDefaultPrices(){
         $idPacakge = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
         $defaultPrices = isset($_REQUEST['defaultPrices']) ? $_REQUEST['defaultPrices'] : [];
         echo json_encode($this->model->updateDefaultPrices($idPacakge, $defaultPrices));
     }

     /**
      * update insert or update a price list for the commercial lead
      * @return json update message
      */
     public function updateMyPackage(){
         $idPacakge = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
         $selectedCustomers = isset($_REQUEST['selectedCustomers']) ? $_REQUEST['selectedCustomers'] : [];
         echo json_encode($this->model->updateMyPackage($idPacakge, $selectedCustomers));
     }

     /**
      * get prices by types and supplier products total price
      * @return json lit of prices
      */
     public function getPriceTypes(){
         $idPacakge = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
         $sumBy = isset($_REQUEST['sumBy']) ? $_REQUEST['sumBy'] : 'type';
         echo json_encode($this->model->getPriceTypes($idPacakge, $sumBy), JSON_NUMERIC_CHECK);
     }

     /**
      * update values for package prices and comission split
      * @return josn update message
      */
     public function updateBrokerPricesAndCommission(){
         $idPacakge = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
         $prices = isset($_REQUEST['prices']) ? $_REQUEST['prices'] : [];
         $commissionSplit = isset($_REQUEST['commissionSplit']) ? $_REQUEST['commissionSplit'] : [];
         echo json_encode($this->model->updateBrokerPricesAndCommission($idPacakge, $prices, $commissionSplit));
     }

    /**
     * returns json response for template packages
     * @return list template packages json
     */
    public function getTemplatePackages(){
        echo json_encode($this->model->getTemplatePackages());
     }

     /**
      * returns json response for editable template packages
      * @return list template packages json
      */
     public function getTemplatePackagesForEdit(){
         echo json_encode($this->model->getTemplatePackagesForEdit());
      }

     /**
      * get products for selected package
      * @return json list of products
      */
     public function getProductsInPackage(){
         $idCountry = isset($_REQUEST['idCountry']) ? $_REQUEST['idCountry'] : 0;
         $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
         echo json_encode($this->model->getProductsInPackage($idCountry, $idPackage));
     }

     /**
      * update packages
      * @return status message
      */
     public function updateInformationDataInPackage(){
         $packageData = isset($_REQUEST['packageData']) ? $_REQUEST['packageData'] : '[]';
         $packageInfo = isset($_REQUEST['packageInfo']) ? $_REQUEST['packageInfo'] : '[]';
         echo json_encode($this->model->updateInformationDataInPackage($packageData, $packageInfo));
     }

     /**
      * update package templates
      * @return status message
      */
     public function updateInformationDataInPackageTemplate(){
         $packageInfo = isset($_REQUEST['packageInfo']) ? $_REQUEST['packageInfo'] : '[]';
         $products = isset($_REQUEST['products']) ? $_REQUEST['products'] : '[]';
         echo json_encode($this->model->updateInformationDataInPackageTemplate($packageInfo, $products));
     }

     /**
      * get all product categories
      */
     public function getProductCategories() {
         echo json_encode($this->model->getProductCategories());
     }

     /**
      * insert new virtual product
      */
     public function addVirtualProduct() {
         $productName = isset($_REQUEST['productName']) ? $_REQUEST['productName'] : '';
         $idCategory = isset($_REQUEST['idCategory']) ? $_REQUEST['idCategory'] : 0;
         echo json_encode($this->model->addVirtualProduct($productName, $idCategory));
     }

     /**
      * insert new virtual product
      */
     public function getOptionsAndPackages() {
         $idCountry = isset($_REQUEST['idCountry']) ? $_REQUEST['idCountry'] : 0;
         echo json_encode($this->model->getOptionsAndPackages($idCountry));
     }

     public function getPackageTypes(){
         echo json_encode($this->model->getPackageTypes());
     }

     public function updatePackageOptions(){
         $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
         $groups = isset($_REQUEST['groups']) ? $_REQUEST['groups'] : '[]';
         $additionalPackages = isset($_REQUEST['additionalPackages']) ? $_REQUEST['additionalPackages'] : '[]';
         echo json_encode($this->model->updatePackageOptions($idPackage, $groups, $additionalPackages));
     }

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