<?php
include '../db/db.php';
session_start();
$log= htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
if (empty($log)) {
	 
  $redirectUrl = "../index.php";
  echo "<script type=\"text/javascript\">";
  echo "window.location.href = '$redirectUrl'";
  echo "</script>";
	 
}else{

  $sqlu="SELECT * FROM details_of_users WHERE user='$log' and val='1'";
  $resultu=mysqli_query($conn, $sqlu);
  $countu=mysqli_num_rows($resultu);
  
	if($countu==1){
	
	  $sqlc="SELECT * FROM details_of_users where user='$log'";
	  $resultc=mysqli_query($conn, $sqlc);
	  while ($rowc = mysqli_fetch_array($resultc)){
	  $name=$rowc['id'];
	  }		
	
		$id = htmlspecialchars(trim($_GET['id']), ENT_QUOTES, 'UTF-8');
		$sql = mysqli_fetch_assoc(mysqli_query($conn, "SELECT acc_no FROM supplier WHERE id='$id'"));
		$acc_no = $sql['acc_no'];
		
		/*$result = mysql_query("SELECT * FROM stock_card WHERE sup='$id'");
		$num_rows = mysql_num_rows($result);
		
		if ($num_rows > 0) {  
					
		echo "<script>alert('Details already exists!')</script>";
		$redirectUrl = "../add_supplier.php";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";
		
		}  else { */
		
		mysqli_query($conn, "DELETE FROM supplier WHERE id='$id'");
		mysqli_query($conn, "DELETE FROM account_posting WHERE L_Code='$acc_no'");
		
		echo "<script>alert('Deleted Successfully!')</script>";
		$redirectUrl = "../add_supplier.php";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";
		
		//} 

	
	} else{
	  $redirectUrl = "../index.php";
	  echo "<script type=\"text/javascript\">";
	  echo "window.location.href = '$redirectUrl'";
	  echo "</script>";
	
	}
}

?>