<?php
class OrdersController{
    private $model;

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

    /**
     * returns json response for ongoing orders
     * @return list orders json
     */
    public function getActiveOrders(){
        echo json_encode($this->model->getActiveOrders(), JSON_NUMERIC_CHECK);
    }

    /**
     * returns json response for completed orders
     * @return list orders json
     */
    public function getHistoryOrders(){
        echo json_encode($this->model->getHistoryOrders(), JSON_NUMERIC_CHECK);
    }

    /**
    * gets the info for an order
    * @return [json] returns order info
    */
   public function getOrderInfo() {
       $idOrder = isset($_REQUEST['idOrder']) ? $_REQUEST['idOrder'] : 0;
       echo json_encode($this->model->getOrderInfo($idOrder), JSON_NUMERIC_CHECK);
   }

   /**
    * update comment for an order
    * @return json update message
    */
   public function addOrderComment() {
       $idOrder = isset($_REQUEST['idOrder']) ? $_REQUEST['idOrder'] : 0;
       $comment = isset($_REQUEST['comment']) ? $_REQUEST['comment'] : null;
       echo json_encode($this->model->addOrderComment($idOrder, $comment));
   }

    /**
    * get customer questionnaires documents
    * @return json list of documents
    */
   public function getOrderDocumentsPerType(){
       $idOrder = isset($_REQUEST['idOrder']) ? $_REQUEST['idOrder'] : 0;
       $documentType = isset($_REQUEST['documentType']) ? $_REQUEST['documentType'] : '';
       echo json_encode($this->model->getOrderDocumentsPerType($idOrder, $documentType), JSON_NUMERIC_CHECK);
   }

   /**
    * upload againa questionnaire
    * @return json upload message
    */
   public function reUploadQuestionaire(){
       $file = isset($_FILES['file']) ? $_FILES['file'] : [];
       $idOrder = isset($_REQUEST['idOrder']) ? $_REQUEST['idOrder'] : 0;
       $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
       $idDocument = isset($_REQUEST['idDocument']) ? $_REQUEST['idDocument'] : 0;
       echo json_encode($this->model->reUploadQuestionaire($idOrder, $idPackage, $idDocument, $file));
   }

   /**
    * returns the comments and the user id based on the comment type
    * @return json array with comments
    */
   public function getCommentsByType() {
       $idOrder = isset($_REQUEST['idOrder']) ? $_REQUEST['idOrder'] : 0;
       $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
       $idProcessStep = isset($_REQUEST['idProcessStep']) ? $_REQUEST['idProcessStep'] : 0;
       $commentType = isset($_REQUEST['commentType']) ? $_REQUEST['commentType'] : '';
       echo json_encode($this->model->getCommentsByType($idOrder, $idPackage, $idProcessStep, $commentType), JSON_NUMERIC_CHECK);
   }

   /**
    * get information for customer acceptance
    * @return json object with customer information
    */
   public function getCustomerAcceptance(){
       $idOrder = isset($_REQUEST['idOrder']) ? $_REQUEST['idOrder'] : 0;
       echo json_encode($this->model->getCustomerAcceptance($idOrder), JSON_NUMERIC_CHECK);
   }

   /**
    * upload acceptance document
    * @return json upload message
    */
   public function uploadAcceptanceDocument(){
       $idOrder = isset($_REQUEST['idOrder']) ? $_REQUEST['idOrder'] : 0;
       $file = isset($_FILES['file']) ? $_FILES['file'] : [];
       echo json_encode($this->model->uploadAcceptanceDocument($idOrder, $file));
   }

   /**
    * customer change acceptance status for a package
    * @return Array message confirmation
    */
   public function acceptDeclineInstallation(){
       $idOrder = isset($_REQUEST['idOrder']) ? $_REQUEST['idOrder'] : 0;
       $actionType = isset($_REQUEST['actionType']) ? $_REQUEST['actionType'] : '';
       $declineReason = isset($_REQUEST['declineReason']) ? $_REQUEST['declineReason'] : '';
       echo json_encode($this->model->acceptDeclineInstallation($idOrder, $actionType, $declineReason));
   }

    /**
     * returns the installation dates proposed/accepted/rejected by users
     * @return json array with installation dates
     */
     public function getInstallationDates() {
         $idOrder = isset($_REQUEST['idOrder']) ? $_REQUEST['idOrder'] : 0;
         $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
         echo json_encode($this->model->getInstallationDates($idOrder, $idPackage), JSON_NUMERIC_CHECK);
     }

     public function getAllDataForInstallation() {
         $idOrder = isset($_REQUEST['idOrder']) ? $_REQUEST['idOrder'] : 0;
         $stepIds = isset($_REQUEST['stepsNameForInstallation']) ? $_REQUEST['stepsNameForInstallation'] : '[]';
         $fileType = isset($_REQUEST['fileType']) ? $_REQUEST['fileType'] : '';

         echo json_encode($this->model->getAllDataForInstallation($idOrder, $stepIds, $fileType));
     }

   /**
    * returns a message if the new date for the installation was successfully added
    * @return json array with confirmation messages
    */
    public function updateInstallationDate() {
        $idOrder = isset($_REQUEST['idOrder']) ? $_REQUEST['idOrder'] : 0;
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        $installationDate = isset($_REQUEST['installationDate']) ? $_REQUEST['installationDate'] : '';
        $status = isset($_REQUEST['status']) ? $_REQUEST['status'] : '';
        echo json_encode($this->model->updateInstallationDate($idOrder, $idPackage, $installationDate, $status));
    }

    /**
     * returns a message if the date was removed successfully
     * @return json array with confirmation messages
     */
    public function removeMyDate() {
        $idOrder = isset($_REQUEST['idOrder']) ? $_REQUEST['idOrder'] : 0;
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        $installationDate = isset($_REQUEST['installationDate']) ? $_REQUEST['installationDate'] : '';
        echo json_encode($this->model->removeMyDate($idOrder, $idPackage, $installationDate));
    }

    /**
     * sends a mail to the support team
     * @return json confirmation message
     */
    public function sendSupportMail() {
        $ordersInfo = isset($_REQUEST['orderInfo']) ? $_REQUEST['orderInfo'] : '[]';
        $orderPackages = isset($_REQUEST['orderPackages']) ? $_REQUEST['orderPackages'] : '[]';
        $userText = isset($_REQUEST['supportText']) ? $_REQUEST['supportText'] : '';
        echo json_encode($this->model->sendSupportMail($ordersInfo, $orderPackages, $userText));
    }
}
