<?php
include 'header.php';


if (isset($_GET['num'])) {
  $tnumbs = $_GET['num'];
  $select = mysqli_query($link, "SELECT * FROM track_update WHERE track_num = '$tnumbs';");
  if (mysqli_num_rows($select) > 0) {
    if (isset($_GET['make_current'])) {
      $id = mysqli_real_escape_string($link, $_GET['id']);
      $current_location = mysqli_real_escape_string($link, $_GET['current_location']);
      $sql = "UPDATE track_update SET is_current_location='no' WHERE track_num='$tnumbs';";
      mysqli_query($link, $sql);
      $sql = "UPDATE track_update SET is_current_location='yes' WHERE track_num='$tnumbs' AND id='$id';";
      mysqli_query($link, $sql);
      $sql = "UPDATE tracking SET current_location='$current_location' WHERE tracking_number='$tnumbs';";
      mysqli_query($link, $sql);
      echo "<script>window.location.href = 'view-updates.php?num=" . $tnumbs . "&location_updated'; </script>";
    }

    if (isset($_GET['delete_update'])) {
      $id = mysqli_real_escape_string($link, $_GET['id']);
      $sql = "DELETE FROM track_update WHERE track_num='$tnumbs' AND id='$id';";
      mysqli_query($link, $sql);
      echo "<script>window.location.href = 'view-updates.php?num=" . $tnumbs . "&update_deleted'; </script>";
    }

    // check if there's any current location
    $select1 = mysqli_query($link, "SELECT * FROM track_update WHERE track_num = '$tnumbs' AND is_current_location='yes';");
    $numrows1 = mysqli_num_rows($select1);
  } else {
    echo "<script>alert('It has not been updated yet');window.location.href = 'dashboard.php'; </script>";
  }
} else {
  echo "<script>window.location.href = 'dashboard.php'; </script>";
}

?>


<div class="row">
  <div class="col-lg-12 grid-margin stretch-card">
    <div class="card">
      <div class="card-body">
        <h4 class="card-title">TRACK UPDATES</h4>
        <?php
        if (isset($_GET['location_updated'])) {
          echo "<div class='alert alert-success'>Current location set successfully!</div>";
        }

        if (isset($_GET['update_deleted'])) {
          echo "<div class='alert alert-success'>Update deleted successfully!</div>";
        }

        if ($numrows1 == 0) {
          echo "<div class='alert alert-danger'>You currently have no current location! kindly select a current location.</div>";
        }
        ?>
        <div class="table-responsive pt-3">
          <table class="table table-dark">
            <thead>
              <tr>
                <th>#</th>
                <th>Location</th>
                <th>Status</th>
                <th>Date</th>
                <th>Time</th>
                <th>Delivery Charge</th>
                <th>Total Charge</th>
                <th>Note</th>
                <th>Updated At</th>
                <th>Location Status</th>
                <th>Action</th>
                <th>Action</th>
              </tr>
            </thead>
            <tbody>

              <?php
              $i = 0;

              while ($row = mysqli_fetch_assoc($select)) {
                $i++
              ?>

                <input type="hidden" name="tnumb" value="<?php echo $row['tracking_number'] ?>">
                <tr>
                  <td><?php echo $i; ?></td>

                  <td><b><?php echo $row['current_location'] ?></b></td>
                  <td><b><?php echo $row['status'] ?></b></td>
                  <td><b><?php echo $row['date'] ?></b></td>
                  <td><b><?php echo $row['time'] ?></b></td>
                  <td><b>$<?php echo $row['delivery_charge'] ?></b></td>
                  <td><b>$<?php echo $row['total_charge'] ?></b></td>
                  <td><b><?php echo $row['note'] ?></b></td>
                  <td><b><?php echo $row['updated_at'] ?></b></td>
                  <td><b><?php if ($row['is_current_location'] == "yes") {
                            echo "<a class='btn btn-info'>Current</a>";
                          } else {
                            echo "---";
                          } ?></b></td>
                  <td>
                    <b>
                      <?php if ($row['is_current_location'] == "no") {
                        echo "<a href='view-updates?num=" . $tnumbs . "&id=" . $row['id'] . "&current_location=" . $row['current_location'] . "&make_current' class='btn btn-warning'>Make Current?</a>";
                      } ?>
                    </b>
                  </td>
                  <td><b><a num="<?php echo $tnumbs ?>" id="<?php echo $row['id']; ?>" onclick="deleteUpdate(this)" class="btn btn-success">Delete</a></b></td>



                <?php } ?>

            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>


<script>
  function deleteUpdate(update) {
    var id = update.getAttribute("id");
    var num = update.getAttribute("num");

    if (confirm("Are you sure you want to delete this update?")) {

      window.location.href = `view-updates?num=${num}&id=${id}&delete_update`;
    }
  }
</script>



<?php
include 'footer.php';
?>