<?php 
include 'header.php';
	if (isset($_GET['num'])) {
		$tnumbs = $_GET['num'];
		$select = mysqli_query($link, "SELECT * FROM tracking WHERE tracking_number = '$tnumbs' ");
		if (mysqli_num_rows($select) > 0) {
	     $row = mysqli_fetch_assoc($select);
           $senders_name = $row['sender_name'];  
           $senders_contact = $row['sender_contact'];  
           $senders_mail = $row['sender_email'];  
           $senders_address = $row['sender_address'];
           $receivers_name = $row['receiver_name'];  
           $receivers_contact = $row['receiver_contact'];  
           $receivers_mail = $row['receiver_email'];  
           $receivers_address = $row['receiver_address'];
           $statuss = $row['status'];  
           $dispatch_l = $row['dispatch_location'];  
           $dispatchh = $row['dispatch_date'];  
           $deliveryy = $row['delivery_date'];
           $current_loc = $row['current_location'];
           $desc = $row['pdesc'];
            $shiping_cost = $row['shiping_cost'];
            $clearance_cost = $row['clearance_cost'];
		}else{
			echo "<script>window.location.href = 'dashboard.php'; </script>";
		}
	}else{
		echo "<script>window.location.href = 'dashboard.php'; </script>";
	}



	if (isset($_POST['update'])) {
       $current_lo = text_input($_POST['current_loc']);
       $date = text_input($_POST['date']);
       $time = text_input($_POST['time']);
       $status = text_input($_POST['status']);
       $note = text_input($_POST['note']);
       $shiping_cost = text_input($_POST['shiping_cost']);
       $clearance_cost = text_input($_POST['clearance_cost']);

	     if (empty($current_lo) && empty($date) && empty($time) && empty($status) && empty($note) && empty($shiping_cost) && empty($clearance_cost)) {
	       echo "<script>alert('Check !!! Some fields are empty')</script>";
	     }else{
	     	 $update = mysqli_query($link, "INSERT INTO track_update (`track_num`, `status`, `date`, `time`, `note`, `current_location`,  `shiping_cost`,  `clearance_cost`) VALUES ('$tnumbs', '$status', '$date', '$time', '$note', '$current_lo', '$shiping_cost', '$clearance_cost') ");
         if ($update) {
            mysqli_query($link, "UPDATE tracking SET current_location = '$current_lo', status = '$status' WHERE tracking_number = '$tnumbs' ");
            if ($mail_track_update == "Yes") {
              $subject = "$sitename - Shipment Update";
              $body = "<html>
              <head>
                  <title>Shipment Update</title>
                  <style>
                      body {
                          font-family: 'Roboto', sans-serif;
                          margin: 0;
                          padding: 0;
                          background-color: #f5f5f5;
                      }
                      .container {
                          max-width: 600px;
                          margin: 0 auto;
                          background-color: #ffffff;
                          padding: 20px;
                          box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                      }
                      .header {
                          text-align: center;
                          padding: 20px 0;
                      }
                      .header img {
                          max-width: 150px;
                      }
                      .banner {
                          background-color: #febe42;
                          text-align: center;
                          padding: 40px 20px;
                          color: #ffffff;
                          position: relative;
                      }
                      .banner img {
                          max-width: 100px;
                          border-radius: 50%;
                      }
                      .banner h1 {
                          font-size: 24px;
                          margin: 20px 0 0;
                      }
                      .content {
                          padding: 20px;
                          text-align: center;
                      }
                      .content p {
                          font-size: 16px;
                          color: #333333;
                          line-height: 1.5;
                      }
                      .content h2 {
                          font-size: 20px;
                          color: #333333;
                          margin: 20px 0 10px;
                      }
                      .button-container {
                          text-align: center;
                          margin: 20px 0;
                      }
                      .button-container a {
                          background-color: #febe42;
                          color: #ffffff;
                          padding: 10px 20px;
                          text-decoration: none;
                          border-radius: 5px;
                          font-size: 16px;
                      }
                      @media (max-width: 600px) {
                          .banner img {
                              max-width: 80px;
                          }
                          .banner h1 {
                              font-size: 20px;
                          }
                          .content p, .content h2 {
                              font-size: 14px;
                          }
                          .button-container a {
                              font-size: 14px;
                              padding: 8px 16px;
                          }
                      }
                  </style>
              </head>
              <body>
              <div class='container'>
                  <div class='header'>
                      <img alt='Logo' src='$site_url/admin/images/logo-small-2.png'>
                  </div>
                  <div class='banner'>
                      <h1>Shipment Update</h1>
                  </div>
                  <div class='content'>
                      <p>Dear $receivers_name,</p>
                      <p>We are pleased to inform you that your shipment status has been updated to <strong>$status</strong>.</p>
                      <h2>Tracking Information</h2>
                      <p><strong>Tracking Number:</strong> $tnumbs</p>
                      <p><strong>Status:</strong> $status</p>
                      <p><strong>Current Location:</strong> $current_lo</p>
                      <p><strong>Note:</strong> $note</p>
                      <p>We hope this meets with your approval. Please do not hesitate to get in touch with us if you need further assistance.</p>
                      <div class='button-container'>
                          <a href='$site_url/tracking.php'>Track Your Shipment</a>
                      </div>
                  </div>
              </div>
              </body>
              </html>";
                sendMail($receivers_mail,$subject,$body);
            }
         }
	     	 echo "<script>
            alert('Updated Successfully');
	           window.location.href = 'edit-tracking.php?num=$tnumbs';
	          </script>";
	          exit();
	     }
	}

	function text_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
	}
?>



<div class="row">
	<div class="col-12 grid-margin stretch-card">
		<div class="card">
			<div class="card-body">
				<label style="font-weight: bold;font-size: 25px;">TRACKING NUMBER  - <?php echo $tnumbs ?></label>
				
			</div>
		</div>
    </div>
    <div class="col-md-12 grid-margin stretch-card">
      <div class="card">
        <div class="card-body">
          <form method="post" action="edit-tracking.php?num=<?php echo $tnumbs ?>" class="forms-sample">
            <div class="form-group">
              <label for="exampleInputUsername1">Status</label>
              <select class="form-control" name="status">
                <option value="Pending">Pending</option>
                <option value="Active">Active</option>
                <option value="Inactive">Inactive</option>
                <option value="Picked Up">Picked Up</option>
                <option value="Arrived">Arrived</option>
                <option value="Delivered">Delivered</option>
                <option value="Departed">Departed</option>
                <option value="On hold">On hold</option>
              </select>
            </div>
            <div class="form-group">
              <label for="exampleInputPassword1">Current Location</label>
              <input type="text" class="form-control" value="" name="current_loc" id="exampleInputPassword1" placeholder="Current Location ">
            </div>

            <div class="form-group">
              <label for="exampleInputPassword1">Date</label>
              <input type="date" class="form-control" value="" name="date" id="" placeholder="Date">
            </div>

            <div class="form-group">
              <label for="exampleInputPassword1">Time</label>
              <input type="time" class="form-control" value="" name="time" id="" placeholder="">
            </div>

            <div class="form-group">
              <label for="exampleInputPassword1">Note</label>
              <textarea name="note" class="form-control"></textarea>
            </div>

            <div class="form-group">
              <label for="exampleInputPassword1">Shiping Cost</label>
              <input type="text" class="form-control" value="" name="shiping_cost" id="" placeholder="">
            </div>

            <div class="form-group">
              <label for="exampleInputPassword1">Clearance Cost</label>
              <input type="text" class="form-control" value="" name="clearance_cost" id="" placeholder="">
            </div>

        </div>
      </div>
    </div>
    
</div>
    <div class="col-12 grid-margin stretch-card">
          <div class="card">
            <div class="card-body">
            	<button class="btn btn-lg btn-success btn-block" name="update">Update</button>
            </div>
        </div>
        </form>
    </div>


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