<?php 
include("connection/conn.php");


// Handle deletion request
if (isset($_POST['delete'])) {
    $subject_id = $_POST['subject_id'];
    
    // Delete the subject from the database
    $delete_query = "DELETE FROM subjects WHERE id = $subject_id";
    if (mysqli_query($link, $delete_query)) {
        echo "<script>alert('Subject deleted successfully');</script>";
    } else {
        echo "<script>alert('Error deleting subject');</script>";
    }
}

// Fetch all subjects
$query = "SELECT subjects.id, subjects.subject_name, course_category.course 
          FROM subjects 
          JOIN course_category ON subjects.course_id = course_category.id
          ORDER BY subjects.id ASC";
$result = mysqli_query($link, $query);
?>

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Administrator | Manage Category</title>
		<link href="css/bootstrap.css" rel="stylesheet" />
		<link href="css/style.css" rel="stylesheet" />
		<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
		<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css' />
	</head>
	
	<body>
		<div id="wrapper">
			<?php
				include("top-header.php");
				include("right-sec.php");
			?>
			<div id="page-wrapper" >
				<div id="page-inner">
					<div class="row">
						<div class="col-md-12">
							<h3 class="page-header">
								Manage Category<small></small>
							</h3>
							
							<div class="table-sec">
								<table border="1">
        <tr>
            <th>ID</th>
            <th>Category</th>
            <th>Course Name</th>
            <th>Actions</th>
        </tr>
        <?php while ($row = mysqli_fetch_assoc($result)) { ?>
        <tr>
            <td><?php echo $row['id']; ?></td>
            <td><?php echo $row['course']; ?></td>
            <td><?php echo $row['subject_name']; ?></td>
            <td>
                <a href="edit_subject.php?id=<?php echo $row['id']; ?>"><i class="fa fa-edit"></i> </a>
                
                <!-- Delete Form -->
                <form method="POST" style="display:inline;" onsubmit="return confirm('Are you sure you want to delete this subject?');">
                    <input type="hidden" name="subject_id" value="<?php echo $row['id']; ?>" />
                    <button type="submit" name="delete" style="border: none; background: none; cursor: pointer;">
                        <i class="fa fa-trash"></i>
                    </button>
                </form>
            </td>
        </tr>
        <?php } ?>
    </table>
							</div>
						</div>
					</div> 
				</div>
			</div>
			<footer class="footer-sec">
				<div class="container">
					<div class="row">
						<div class="col-md-12">
							<p class="text-center">All rights reserved. Template Design by: <a href="">drmakeoversalon</a></p>
						</div>
					</div>
				</div>
			</footer>
		</div>
		
		<!-- jQuery Js -->
		<script src="js/jquery-1.10.2.js"></script>
		<script src="js/bootstrap.min.js"></script>
		<script src="js/jquery.metisMenu.js"></script>
		<script src="js/custom-scripts.js"></script>
	</body>
</html>
