<?php

class OrderTraking{

    /**
     * add tracking number and url for order/package/supplier
     * @param  INT $idOrder       id for the order
     * @param  INT $idSupplier    id for the supplier
     * @param  String $trackingNumber traking id
     * @param  String $trackingUrl traking url
     * @return Array                update message
     */
    public function addTracking($idOrder, $idSupplier, $trackingNumber, $trackingUrl) {
        global $database;
        $idOrder = $database->escapeValue($idOrder);
        $idSupplier = $database->escapeValue($idSupplier);
        $trackingNumber = $database->escapeValue($trackingNumber);
        $trackingUrl = $database->escapeValue($trackingUrl);
        $data = [];

        if (filter_var($trackingUrl, FILTER_VALIDATE_URL) === FALSE) {
            $data['messages'][] = [
                'code' => 'error',
                'message' => 'INVALID_URL'
            ];

            return $data;
        }

        $sql = "INSERT INTO ".TABLES['rel_order_supplier_options']." (
                  idOrder,
                  idSupplier,
                  trackingNumber,
                  trackingUrl
                )
                VALUES
                  ($idOrder, $idSupplier, '$trackingNumber', '$trackingUrl')";
          $query = $database->query($sql);

          if($database->affectedRows() > 0){
              $data['messages'][] = [
                  'code' => 'success',
                  'message' => 'TRAKING_ID_ADDED'
              ];
          }else{
              $data['messages'][] = [
                  'code' => 'warning',
                  'message' => 'NO_CHANGES'
              ];
          }

          return $data;
    }

    /**
     * update dates for products in an order
     * @param  INT $idTracking          id for the tracking info
     * @param  String $trackingNumber   traking id
     * @param  String $trackingUrl      traking url
     * @return Array                update message
     */
    public function updateTracking($idTracking, $trackingNumber, $trackingUrl){
        global $database;
        $idTracking = $database->escapeValue($idTracking);
        $trackingNumber = $database->escapeValue($trackingNumber);
        $trackingUrl = $database->escapeValue($trackingUrl);
        $data = [];

        if (filter_var($trackingUrl, FILTER_VALIDATE_URL) === FALSE) {
            $data['messages'][] = [
                'code' => 'error',
                'message' => 'INVALID_URL'
            ];

            return $data;
        }

        $sql = "UPDATE ".TABLES['rel_order_supplier_options']."
                SET trackingNumber='$trackingNumber',
                    trackingUrl='$trackingUrl'
                WHERE id=$idTracking";
        $query = $database->query($sql);

        if($database->affectedRows() > 0){
            $data['messages'][] = [
                'code' => 'success',
                'message' => 'TRAKING_ID_UPDATED'
            ];
        }else{
            $data['messages'][] = [
                'code' => 'warning',
                'message' => 'NO_CHANGES'
            ];
        }

        return $data;
    }

    public function removeTracking($idTracking) {
        global $database;
        $idTracking = $database->escapeValue($idTracking);
        $data = [];

        $sql = "DELETE FROM ".TABLES['rel_order_supplier_options']." WHERE id=$idTracking";
        $result = $database->query($sql);

        if($database->affectedRows()){
            $data['messages'][] = [
                'code' => 'success',
                'message' => 'TRACKING_REMOVED'
            ];
        } else {
            $data['messages'][] = [
                'code' => 'error',
                'message' => 'TRACKING_REMOVED_ERROR'
            ];
        }

        return $data;
    }
}
