<?php
require_once DIR_CORE.'couriers-'.DB_TYPE.'.php';
require_once DIR_CORE.'couriers.php';

require_once DIR_CORE.'orders-'.DB_TYPE.'.php';
require_once DIR_CORE.'orders.php';

require_once DIR_CORE.'products-'.DB_TYPE.'.php';
require_once DIR_CORE.'products.php';

if( isset( $_COOKIE['sOrderQC_'.LANGUAGE] ) && !empty( $_COOKIE['sOrderQC_'.LANGUAGE] ) ){
  $aOrder = throwOrderMd5( $_COOKIE['sOrderQC_'.LANGUAGE] );
  if( isset( $aOrder ) && is_array( $aOrder ) )
    $_SESSION['iOrderSession_'.LANGUAGE] = $aOrder[0];
  setCookie( 'sOrderQC_'.LANGUAGE, null, time( ) - 1 );
}

if( $a == 'Basket' ){

  if( isset( $sOption ) ){
    if( isset( $_SESSION['iOrderSession_'.LANGUAGE] ) )
      $iOrder = $_SESSION['iOrderSession_'.LANGUAGE];
    else{
      $_SESSION['iOrderSession_'.LANGUAGE] = $iOrder = throwOrderIdTemp( );
    }

    if( $sOption == 'add' ){
      if( isset( $iProduct ) && is_numeric( $iProduct ) && isset( $iQuantity ) && is_numeric( $iQuantity ) && $iQuantity >= 1 ){
        addOrderProduct( $iProduct, $iQuantity, $iOrder );
      }
      header( 'Location: '.$_SERVER['PHP_SELF'].'?p=ordersBasket' );
      exit;
    }
    elseif( $sOption == 'del' ){
      if( isset( $iElement ) && is_numeric( $iElement ) )
        delOrderElement( $iOrder, $iElement );
    }
    elseif( isset( $_POST['saveBasket'] ) ){
      if( isset( $_POST['aElements'] ) && is_array( $_POST['aElements'] ) ){
        saveOrderProducts( $_POST );
      }
      
      if( isset( $_COOKIE['sOrderQC_'.LANGUAGE] ) )
        setCookie( 'sOrderQC_'.LANGUAGE, null, time( ) - 1 );

      setCookie( 'sOrderQC_'.LANGUAGE, md5( $iOrder ), time( ) + 86400 );
      if( !isset( $_COOKIE['sLanguage'] ) ){
        setCookie( 'sLanguage', LANGUAGE, time( ) + 86400 );
      }
    }
    elseif( $sOption == 'save' ){
      if( isset( $_POST['aElements'] ) && is_array( $_POST['aElements'] ) ){
        saveOrderProducts( $_POST );
      }

      if( isset( $_POST['sSave'] ) ){
        header( 'Location: '.$_SERVER['PHP_SELF'].'?p=ordersDelivery' );
        exit;
      }
    }
  }
  else{
    if( isset( $_SESSION['iOrderSession_'.LANGUAGE] ) )
      $iOrder = $_SESSION['iOrderSession_'.LANGUAGE];
  }

  if( isset( $iOrder ) && is_numeric( $iOrder ) )
    $sBasketList = listBasket( $iOrder, 'orders_basket.tpl' );

  if( !empty( $sBasketList ) )
    $content .= $sBasketList;
  else
    $content .= $tpl->tbHtml( 'orders_basket.tpl', 'NOT_FOUND' );

}
elseif( $a == 'Delivery' ){
  if( isset( $_SESSION['iOrderSession_'.LANGUAGE] ) )
    $iOrder = $_SESSION['iOrderSession_'.LANGUAGE];

  if( isset( $iOrder ) && is_numeric( $iOrder ) ){
    if( isset( $_POST['sOption'] ) && $_POST['sOption'] == 'send' ){
      if( checkOrderFields( $_POST ) === true && checkOrderProducts( $iOrder ) === true ){
        saveOrder( $iOrder, $_POST );

        $aData        = throwOrder( $iOrder );
        $sBasketList  = listBasket( $iOrder, 'orders_summary.tpl' );
        if( !empty( $sBasketList ) ){
          $aData['fSummary'] = tPrice( $aData['fCourierPrice'] + $aList['fSummary'] );
          $aData['sSummary'] = changePriceFormat( $aData['fSummary'] );
          $sBasketList .= $tpl->tbHtml( 'orders_summary.tpl', 'COURIER' );
        }
        $content .= $tpl->tbHtml( 'orders_summary.tpl', 'SHOW' );
        unset( $_SESSION['iOrderSession_'.LANGUAGE] );
      }
      else
        $content .= $tpl->tbHtml( 'messages.tpl', 'FORM_ERROR' );
    }
    else{
      $sBasketList = listBasket( $iOrder, 'orders_delivery.tpl' );
      if( !empty( $sBasketList ) ){
        $sCouriersSelect = listCouriers( 'couriers_select.tpl' );
        $sBasketList .= $tpl->tbHtml( 'orders_delivery.tpl', 'COURIER' );
        $content .= $tpl->tbHtml( 'orders_delivery.tpl', 'FORM' );
      }
      else
        $content .= $tpl->tbHtml( 'orders_delivery.tpl', 'NOT_FOUND' ); 
    }
  }
  else
    $content .= $tpl->tbHtml( 'orders_delivery.tpl', 'NOT_FOUND' );
}
?>