<?php
require_once("dt_sql_config.php"); // database connection config.
$OrderID = $_GET['OrderID'];

$sql = "SELECT order_number, 
                CONCAT_WS(' ', first_name, last_name) AS name, 
                CONCAT_WS(' ', address1, city, state, zip) AS address, 
                CONCAT_WS(' or ', phone1, phone2) as phones,
                email, 
                request_date, 
                status,
                description,
                stop_number,
                gps_arrive,
                gps_depart,
                service_unit,
                started_at,
                finished_at,
                timeframe,
                route_label,
                amount, 
                pieces,
                notes_count,
                items_count,
                images_count
        FROM dispatch.orders
        WHERE `order_number` LIKE '%".$OrderID."%'";
$result = mysqli_query($db1, $sql);
while($row = mysqli_fetch_assoc($result)) {
  $name = $row["name"];
  $address = $row["address"];
  $phones = $row["phones"];
  $email = $row["email"];
  $request_date = $row["request_date"];
  $status = $row["status"];
  $description = $row["description"];
  $stop_number = $row["stop_number"];
  $service_unit = $row["service_unit"];
  $started_at = $row["started_at"];
  $finished_at = $row["finished_at"];
  $timeframe = $row["timeframe"];
  $route_label = $row["route_label"];
  $amount = $row["amount"];
  $pieces = $row["pieces"];
  $notes_count = $row["notes_count"];
  $items_count = $row["items_count"];
  $images_count = $row["images_count"];
}
$sql = "SELECT * FROM dispatch.drivers WHERE `order_number` LIKE '%".$OrderID."%'";
$result = mysqli_query($db1, $sql);
while($row = mysqli_fetch_assoc($result)) {$driver_name = $row["driver_name"];}

$sql = "SELECT * FROM dispatch.notes WHERE `order_number` LIKE '%".$OrderID."%'";
$result = mysqli_query($db1, $sql);
while($row = mysqli_fetch_assoc($result)) {
  $note = $row["note"];
  $note_author = $row["note_author"];
  $note_created_at = $row["note_created_at"];
}


$sql = "SELECT * FROM dispatch.images WHERE `order_number` LIKE '%".$OrderID."%'";
$result = mysqli_query($db1, $sql);
while($row = mysqli_fetch_assoc($result)) {}


?>
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
  </head>
  <body>
    <div class="container">
      <header class="d-flex flex-wrap justify-content-center py-3 mb-4 border bg-info">
        <a href="http://fam2/DeliveryHistory/" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-dark text-decoration-none">
          <span class="fs-4 text-white">DispatchTrack Historical data</span>
        </a>
        <button type="button" class="btn btn-light" onclick="printFunction()">Print</button>
      </header>
    </div>
    <div class="container">
      <div class="row">
        <div class="col">
        <h5 class="card-title">Contact Info</h5>
          Name: <?php echo $name; ?> </br>
          Address: <?php echo $address; ?> </br>
          Phone(s): <?php echo $phones; ?> </br>
          Email: <?php echo $email; ?> </br>
        </div>
        <div class="col">
          <h5 class="card-title">Delivery Info</h5>
          Truck: <?php echo $service_unit; ?> </br>
          Driver: <?php echo $driver_name; ?></br>
          Stop Number: <?php echo $stop_number; ?> </br>
          Status: <?php echo $status; ?> </br>
          Started at: <?php echo $started_at; ?></br>
          Finished at: <?php echo $finished_at; ?></br>
          Pieces: <?php echo $pieces; ?></br>
        </div>
        <div class="col">
          <h5 class="card-title">Misc. Info</h5>
          Description: <?php echo $description; ?> </br>
          Timeframe: <?php echo $timeframe; ?> </br>
          Route Label: <?php echo $route_label; ?> </br>
          Sale Amount: <?php echo $amount; ?> </br>
          Order #: <?php echo $OrderID; ?> </br>
        </div>
      </div>
      <div class="row">
        <h5 class="card-title">Product lines</h5>
        <table class="table">
          <thead>
            <tr>
              <th scope="col">#</th>
              <th scope="col">Product</th>
              <th scope="col">Description</th>
              <th scope="col">Quantity</th>
              <th scope="col">Delivered</th>
              <th scope="col">Amount</th>
              <th scope="col">Return Code</th>
            </tr>
          </thead>
          <tbody>
              <?php
              $x = 1;
              if($items_count > 0){
                $sql = "SELECT * FROM dispatch.items WHERE `order_number` LIKE '%".$OrderID."%'";
                $result = mysqli_query($db1, $sql);
                while($row = mysqli_fetch_assoc($result)) {
                  echo "<tr>
                  <th scope='row'>$x</th>
                  <td>".$row['serial_number']."</td>
                  <td>".$row['description']."</td>
                  <td>".$row['quantity']."</td>
                  <td>".$row['delivered']."</td>
                  <td>".$row['amount']."</td>";
                  if($row["return_code"]){
                    echo "<td>".$row['return_code']."</td>";
                  }else{
                    echo"<td></td>";
                  }
                  $x++;
                }
              }
              echo "</tr>";
              ?>
          </tbody>
        </table>
      </div>

      <div class="row">
        <h5 class="card-title">Notes</h5>
        <table class="table">
          <thead>
            <tr>
              <th scope="col">#</th>
              <th scope="col">Note</th>
              <th scope="col">Note Author</th>
              <th scope="col">Created</th>
            </tr>
          </thead>
          <tbody>
              <?php
              $x = 1;
              if($notes_count > 0){
                $sql = "SELECT * FROM dispatch.notes WHERE `order_number` LIKE '%".$OrderID."%'";
                $result = mysqli_query($db1, $sql);
                while($row = mysqli_fetch_assoc($result)) {
                  echo "<tr>
                  <th scope='row'>$x</th>
                  <td>".$row['note']."</td>
                  <td>".$row['note_author']."</td>
                  <td>".$row['note_created_at']."</td>
                  ";
                  $x++;
                }
              }
              echo "</tr>";
              ?>
          </tbody>
        </table>
      </div>

      <div class="row">
        <h5 class="card-title">Images</h5>
        <table class="table">
          <thead>
            <tr>
              <th scope="col">#</th>
              <th scope="col">Image link</th>
              <th scope="col">Created</th>
            </tr>
          </thead>
          <tbody>
              <?php
              $x = 1;
              if($images_count > 0){
                $sql = "SELECT * FROM dispatch.images WHERE `order_number` LIKE '%".$OrderID."%'";
                $result = mysqli_query($db1, $sql);
                while($row = mysqli_fetch_assoc($result)) {
                  echo "<tr>
                  <th scope='row'>$x</th>
                  <td><a href='".$row['image_src']."' target='_blank'>".$row["image_name"]."</a></td>
                  <td>".$row['image_created_at']."</td>
                  ";
                  $x++;
                }
              }
              echo "</tr>";
              ?>
          </tbody>
        </table>
      </div>

    </div>
    <script>
      function printFunction() { 
        window.print(); 
      }
    </script>
  </body>
</html>
