<?php
header('Access-Control-Allow-Origin: *');
header(
    'Access-Control-Allow-Methods: HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS'
);
header(
    'Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization'
);
header('Content-Type: application/json');
// Connect to the WordPress database
include '../../../../config/db.php';

// Get all users with a specific status
$query = "SELECT o.status as orderStatus, o.dateCreated as orderDate, o.*, s.*, u.* FROM Orders o INNER JOIN Services s ON o.serviceId = s.serviceId 
            LEFT JOIN Users u ON u.userId = o.userId ORDER BY o.orderId DESC";
$result = mysqli_query( $con, $query );
if ( $result ) {
  $users = array();
  while ( $row = mysqli_fetch_assoc( $result ) ) {
    $fname = "";
    $lname = "";
    $empId = $row['employeeId'];
    $serviceId = $row['serviceId'];
    $subcategory = $row['subcategory'];
    $orderId = $row['orderId'];
    if($empId != 0){
      $empQuery = "SELECT * FROM `Employees` WHERE employeeId = $empId";
      $empResult = mysqli_query( $con, $empQuery );
      if ( $empResult ) {
        while ( $empRow = mysqli_fetch_assoc( $empResult ) ) {
          $fname = $empRow['fname'];
          $lname = $empRow['lname'];
        }
      }
    }
    if($serviceId == 1 || $serviceId == 2){
      $orderDataQuery = "SELECT * FROM NameOrders n INNER JOIN Orders o ON o.orderId = n.orderId WHERE n.orderId = $orderId";
    // }else if($serviceId == 2){
    //   $orderDataQuery = "SELECT * FROM TwinNameOrders t INNER JOIN Orders o ON o.orderId = t.orderId WHERE t.orderId = $orderId";
    }else if($serviceId == 4 ||$serviceId == 5){
      $orderDataQuery = "SELECT * FROM BrandingOrders a INNER JOIN Orders o ON o.orderId = a.orderId WHERE a.orderId = $orderId";
    }else if(($subcategory == 5 && $serviceId  == 6) || ($subcategory == 5 && $serviceId == 7)){
      $orderDataQuery = "SELECT * FROM HoroscopeOrders h INNER JOIN Orders o ON o.orderId = h.orderId WHERE h.orderId = $orderId";
    }else if($subcategory == 5 && $serviceId  == 8){
      $orderDataQuery = "SELECT * FROM AuspiciousOrders a INNER JOIN Orders o ON o.orderId = a.orderId WHERE a.orderId = $orderId";
    }else if($subcategory == 5 && $serviceId  == 8){
      $orderDataQuery = "SELECT * FROM AuspiciousOrders a INNER JOIN Orders o ON o.orderId = a.orderId WHERE a.orderId = $orderId";
    }
    $orderDataResult = mysqli_query( $con, $orderDataQuery );
    if ( $orderDataResult ) {
      $orderData = [];
      while ( $orderDataRow = mysqli_fetch_assoc( $orderDataResult ) ) {
         $orderData = $orderDataRow;
      }
    }
    // echo $orderData;
    $row['orderData'] = isset($orderData) ? $orderData : '';
    $row['empFname'] = (isset($fname) && $fname != null) ? $fname : '';
    $row['empLname'] = (isset($lname) && $lname != null) ? $lname : '';
    $users[] = $row;
  }
  $response = array(
    'status' => 'success',
    'orders' => $users
  );
} else {
  $response = array(
    'status' => 'error',
    'message' => mysqli_error( $con )
  );
}

// Return response as JSON
header( 'Content-Type: application/json' );
echo json_encode( $response );

?>