<?php

session_start();
include 'db.php';
include "mail.php";

$year = date("Y", time());


if ($_SERVER["REQUEST_METHOD"] == "POST") {
	if (isset($_POST['new'])) {
		$sname = trim($_POST['shippername']);
		$sname = strip_tags($sname);
		$sname = htmlspecialchars($sname);

		$sphone = trim($_POST['shipperphone']);
		$sphone = strip_tags($sphone);
		$sphone = htmlspecialchars($sphone);

		$saddress = trim($_POST['shipperaddress1']);
		$saddress = strip_tags($saddress);
		$saddress = htmlspecialchars($saddress);

		$semail = trim($_POST['shipperemail']);
		$semail = strip_tags($semail);
		$semail = htmlspecialchars($semail);

		$rname = trim($_POST['receivername']);
		$rname = strip_tags($rname);
		$rname = htmlspecialchars($rname);

		$rphone = trim($_POST['receiverphone']);
		$rphone = strip_tags($rphone);
		$rphone = htmlspecialchars($rphone);

		$raddress = trim($_POST['receiveraddress1']);
		$raddress = strip_tags($raddress);
		$raddress = htmlspecialchars($raddress);

		$email = $_POST['receiveremail'];
		$remail = trim($_POST['receiveremail']);
		$remail = strip_tags($remail);
		$remail = htmlspecialchars($remail);

		$cno = trim($_POST['consignmentnumber']);
		$cno = strip_tags($cno);
		$cno = htmlspecialchars($cno);

		$origin = trim($_POST['origin']);
		$origin = strip_tags($origin);
		$origin = htmlspecialchars($origin);

		$destination = trim($_POST['destination']);
		$destination = strip_tags($destination);
		$destination = htmlspecialchars($destination);

		$status = trim($_POST['status']);
		$status = strip_tags($status);
		$status = htmlspecialchars($status);

		$delivery_mode = trim($_POST['delivery_mode']);
		$delivery_mode = strip_tags($delivery_mode);
		$delivery_mode = htmlspecialchars($delivery_mode);

		$service_type = trim($_POST['service_type']);
		$service_type = strip_tags($service_type);
		$service_type = htmlspecialchars($service_type);

		$weight = trim($_POST['weight']);
		$weight = strip_tags($weight);
		$weight = htmlspecialchars($weight);

		

		$item = trim($_POST['item']);
		$item = strip_tags($item);
		$item = htmlspecialchars($item);
		
		//$charge = trim($_POST['charge']);
	//	$charge = strip_tags($charge);
	//	$charge = htmlspecialchars($charge);


		$expdelivdate = trim($_POST['exptdeliverydate']);
		$expdelivdate = strip_tags($expdelivdate);
		$expdelivdate = htmlspecialchars($expdelivdate);

		$payment_mode = trim($_POST['payment_mode']);
		$payment_mode = strip_tags($payment_mode);
		$payment_mode = htmlspecialchars($payment_mode);

		$shipment_type = trim($_POST['shipment_type']);
		$shipment_type = strip_tags($shipment_type);
		$shipment_type = htmlspecialchars($shipment_type);

		$qty = trim($_POST['qty']);
		$qty = strip_tags($qty);
		$qty = htmlspecialchars($qty);

		$bol = trim($_POST['bol']);
		$bol = strip_tags($bol);
		$bol = htmlspecialchars($bol);
		
		$total = trim($_POST['total']);
		$total = strip_tags($total);
		$total = htmlspecialchars($total);

		
	
		$time  = date("h:i");

		$ac_check = "SELECT * from parcel WHERE cno='$cno'";
		$ac_result = mysqli_query($db, $ac_check) or die(mysqli_error($connection));
		
		//$image = $_FILES['image']['name'];
	//	$folder = "../parcels/".$image;
	//	$temp= $_FILES['image']['tmp_name'];

		if (mysqli_num_rows($ac_result) > 0) {
			$_SESSION['msg-e'] = "Tracking number already Exist.";
			echo ("<script>location.href = 'new.php';</script>");
		} else {
		    
		 //   if(move_uploaded_file($temp,$folder)){
			$new = "INSERT INTO `parcel`(`sname`, `sphone`, `saddress`, `semail`, `rname`, `rphone`, `raddress`, `remail`, `cno`, `origin`, `destination`, `status`, `expdelivdate`,  `mode`, `product`, `weight`,`service_type`,`total`, `quantity`,`bol`,`payment`,`shipment_type`) VALUES ('$sname', '$sphone', '$saddress', '$semail', '$rname', '$rphone', '$raddress', '$remail', '$cno', '$origin', '$destination', '$status', '$expdelivdate',  '$delivery_mode', '$item', '$weight', '$service_type','$total', '$qty', '$bol', '$payment_mode', '$shipment_type')";
			if ($db->query($new) === TRUE) {

				$message = "You have a new package order from " . $sname;

				/*$messag = '<br><br>
				<div class="contents" style="max-width: 600px; margin: 0 auto; border: 2px solid #000036;">

			<div class="header" style="background-color: #f0f8ff; padding: 15px; text-align: center;">
				<div class="logo" style="width: 260px;text-align: center; margin: 0 auto;">   
				<img src="https://www.Portsaclogistics.com/admin/logo.png" alt="Parcels Delivery Express" style="width: 100%;">             
				</div>
			</div>
			<div style="text-align: center;">
			<div class="mailtext" style="padding: 30px 15px; margin:40px; background-color: #f0f8ff; font-family: "Open Sans", sans-serif; font-size: 16px; line-height: 26px;">
			<br><br>
			<br><br>
			' . $message . '
			<br><br>
			<br><br>
			Your Package Tracking ID is: ' . $cno . '<br><br>
			Please click the button or follow the link below to view your package status and location <br><br>
			   <a href="https://Portsaclogistics.com/track.php" style="	background-color: #2ecc71;
			   color: #fff;
			   padding: 5px 15px 3px 15px;
			   background: linear-gradient(#90caf9, #64b5f6);
			   font-weight: 600;
			   border-radius: 15px;	
			   padding: 10px 0;
			   margin: 10px;
			   display: inline-block;
			   width: 100px;
			   text-transform: uppercase;
			   text-decoration: none;
			   color: #ffff;
			   font-weight: 600;
			   border-radius: 4px;
			   text-align: center;">
			   Track Package
			   </a>
			   <br><br>
			   If the button is not clickable, copy and paste the link into your browser
			   <br><br>
			   </div>
   
			   https://Portsaclogistics.com/track.php
			   <br><br>
			   <br><br>
			   
			</div>
			</div>

			<br><br>			
			<br><br>
  

</div>

			<div class="footer" style="background-color: #f0f8ff; padding: 15px; text-align: center; border-top: 1px solid rgba(255, 255, 255, 0.2);">			
			<strong style="color: #000036;">' . $year . ' Parcels Delivery Express.&nbsp; All Rights Reserved.</strong>
			</div>

				</div>
			<br><br>';*/

			$messag = '<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Courier Service Email</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    body {
      margin: 0;
      font-family: Arial, sans-serif;
      background-color: #f4f4f4;
      color: #333;
    }
    .email-container {
      max-width: 600px;
      margin: auto;
      background: #ffffff;
      padding: 20px;
      border-radius: 8px;
    }
    .header {
      background-color: #0d6efd;
      color: #ffffff;
      padding: 20px;
      text-align: center;
      border-radius: 8px 8px 0 0;
    }
    .header h1 {
      margin: 0;
      font-size: 24px;
    }
    .section {
      padding: 20px;
    }
    .section h2 {
      font-size: 20px;
      color: #0d6efd;
    }
    .btn {
      display: inline-block;
      background-color: #0d6efd;
      color: white;
      padding: 12px 20px;
      margin-top: 20px;
      text-decoration: none;
      border-radius: 5px;
    }
    .footer {
      font-size: 12px;
      color: #999;
      text-align: center;
      padding: 20px;
    }
    @media (max-width: 600px) {
      .email-container {
        padding: 10px;
      }
    }
  </style>
</head>
<body>

  <div class="email-container">
    <div class="header">
      <h1>Your Trusted Courier Partner</h1>
    </div>

    <div class="section">
      <h2>Fast. Reliable. Secure.</h2>
      <p>We provide nationwide delivery services with real-time tracking, guaranteed delivery windows, and the best customer service.</p>
      <p>Your Package Tracking ID is: ' . $cno . '<br><br></p>
      <a href="https://Portsaclogistics.com/track.php" class="btn">Track Your Package</a>
    </div>

    <div class="section">
      <h2>Why Choose Us?</h2>
      <ul>
        <li>✔ 24/7 Tracking</li>
        <li>✔ Real-time Notifications</li>
        <li>✔ Door-to-Door Delivery</li>
        <li>✔ Affordable Rates</li>
      </ul>
    </div>

    <div class="footer">
      © 2025 Your Courier Company. All rights reserved.<br>
      123 Main Street, Your City, Country<br>
      <a href="https://Portsaclogistics.com/unsubscribe" style="color: #999;">Unsubscribe</a>
    </div>
  </div>

</body>
</html>
';
				$subject = 'New Package Delivery';
				//send_mail($email, $messag, $subject);

				$_SESSION['msg-s'] = "Added Successfully.";
                $_SESSION['id'] = mysqli_insert_id($db);
				echo ("<script>location.href = 'index.php';</script>");
				exit();
			} else {
				$_SESSION['msg-e'] =  "Unable to Add. Please Contact Support.";
			//	echo mysqli_error($db);
				echo ("<script>location.href = 'new.php';</script>");
				exit();
			}
		   /* }else{
		        echo "no";
		    }*/
		}
	}


	
	if (isset($_POST['login'])) {
		$username = mysqli_real_escape_string($db, $_POST['username']);
		$password = mysqli_real_escape_string($db, $_POST['password']);
		$login = "SELECT * FROM admin WHERE username = '$username' AND password = '$password'";
		$dologin = mysqli_query($db, $login);

		if (mysqli_num_rows($dologin)) {
			$_SESSION['email'] = $username;
			echo ("<script>location.href = 'index.php';</script>");
		} else {
			$_SESSION['msg-e'] = "Username or Password error.";
			echo ("<script>location.href = 'login.php';</script>");
		}
	}


	if (isset($_POST['delete'])) {
		$id = $_POST['delid'];


		$query = "SELECT * FROM parcel WHERE id = '$id'";
		$result = mysqli_query($db, $query);
		$row = mysqli_fetch_assoc($result);
		$cno = $row['cno'];
		$del = "DELETE FROM `parcel` WHERE id = '$id'";
		$dell = "DELETE FROM `track` WHERE cno = '$cno'";

		if (mysqli_query($db, $del) and mysqli_query($db, $dell)) {
			$_SESSION['msg-s'] = "Delete Successfully.";
			echo ("<script>location.href = 'index.php';</script>");
		} else {
			$_SESSION['msg-e'] = "Unable to Delete. Please Contact Support.";
			echo ("<script>location.href = 'index.php';</script>");
		}
	}

	if (isset($_POST['update'])) {
		$id = $_POST['id'];
		$_SESSION['id'] = $id;
		echo ("<script>location.href = 'update.php';</script>");
	}

	if (isset($_POST['edit'])) {
		$id = $_POST['id'];
		$_SESSION['id'] = $id;
		echo ("<script>location.href = 'edit.php';</script>");
	}

	if (isset($_POST['updatestatus'])) {
		$udate = $_POST['updateddate'];
		$utime = $_POST['updatedtime'];
		$status = $_POST['ustatus'];
		$location = $_POST['location'];
		$about = $_POST['about'];
		$remarks = $_POST['remarks'];
		$cno = $_POST['cno'];

		$new_status = "INSERT INTO `track`(`cno`, `udate`, `utime`, `location`, `status`, `about`, `remark`) VALUES ('$cno', '$udate', '$utime', '$location', '$status', '$about', '$remarks')";
		$new_sta = "UPDATE `parcel` SET `status` = '$status'  WHERE cno = '$cno'";
		if ($db->query($new_status) === TRUE) {
			if ($db->query($new_sta) === TRUE) {
				$_SESSION['msg-s'] = "Added Successfully.";
				echo ("<script>location.href = 'update.php';</script>");
				exit();
			} else {
				$_SESSION['msg-e'] = "Unable to Add. Please Contact Support.";
				echo ("<script>location.href = 'update.php';</script>");
				exit();
			}
		}
	}

	if (isset($_POST['track'])) {
		$cno = $_POST['cno'];
		$tra = "SELECT * FROM parcel WHERE cno = '$cno'";
		$dotra = mysqli_query($db, $tra);

		if (mysqli_num_rows($dotra)) {
			$_SESSION['cno'] = $cno;
			echo ("<script>location.href = '../track-result.php';</script>");
		} else {
			$_SESSION['invalid-e'] = "Invalid Tracking Number.";
			echo ("<script>location.href = '../track.php';</script>");
		}
	}


if(isset($_POST['receivemessage'])){
	$city = $_POST['city'];
	$weight= $_POST['weight'];
	$name = $_POST['name'];
	$distance = $_POST['distance'];
	$email = $_POST['email'];
	$message = $_POST['message'];
}
}