<?php
ob_start();
include("db/cn.php");

// Start session and initialize variables
if (isset($_SESSION['user_name']) && isset($_SESSION['user_role']) && isset($_SESSION['outlet_name']) && isset($_SESSION['outlet_address'])) {
    $userName = $_SESSION['user_name'];
    $userRole = $_SESSION['user_role'];
    $outlet_address = $_SESSION['outlet_address'];
    $outlet_name = $_SESSION['outlet_name'];
}

// Get the school_name from the $_GET method
$customer_name = isset($_GET['class_name']) ? trim($_GET['class_name']) : '';
$customer_name2 = isset($_GET['school_name']) ? trim($_GET['school_name']) : '';

// Check if 'id' parameter is set
if (isset($_GET['id'])) {
    $id = intval($_GET['id']); // Ensure ID is an integer to prevent SQL injection

    // Prepare a delete query
    $stmt = $connection->prepare("DELETE FROM courses WHERE id = ?");
    if ($stmt) {
        $stmt->bind_param("i", $id);

        if ($stmt->execute()) {
           ob_end_clean();
    echo "<script type='text/javascript'>window.location.href = 'course.php';</script>";
         
            exit();
        } else {
            // Redirect back with an error message
            header("Location: view_course.php?class_name=$customer_name&school_name=$customer_name2&message=Failed+to+delete+course");
            exit();
        }
    } else {
        // Handle prepare statement failure
        header("Location: courses_detail.php?class_name=$customer_name&school_name=$customer_name2&message=Database+error");
        exit();
    }
}



ob_end_flush();
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Courses - Detail</title>
    <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<link rel="stylesheet" href="css/custom_css.css" />


</head>

<style>
 #table-container-wrapper {
    max-height: 400px; /* Adjust the maximum height as needed */
    overflow-y: auto; /* Enable vertical scroll when content overflows */
}
/* Custom styles for sidebar */    /* Custom styles for sidebar */
    .sidebar {
        width: 100px;
        background: linear-gradient(360deg, #060606, #3C2F23);
        position: fixed;
        bottom: 0;
        top: 60px;
        transition: transform 0.3s ease-in-out;
        transform: translateX(0);
        z-index: 1;
        height: 100%;
        border-radius: 0px;
		   transition: transform 0.3s ease-in-out, width 0.3s ease-in-out;
    }.sidebar.hide {
      transform: translateX(-100px); /* Move sidebar out of the viewport when it is hidden */
    }
      .sidebar .nav-item {
    padding: 10px 0 ;
    text-align: center;
    position: relative;
	font-size:12px;
}
    .sidebar .nav-link {
      color: #fff;
      position: relative; /* Ensure icon and text are positioned relative to the nav link */
      z-index: 2; /* Ensure the text and icon are above the hover background */
    }
    .sidebar .nav-link i {
      margin-bottom: 5px; /* Adjust the margin of the icon */
    }
    /* Custom styles for hover effect */
    .sidebar .nav-item:hover:after {
      background-color: #fff;
      content: '';
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      height: 100%;
      z-index: 1; /* Ensure the hover background is below the text and icon */
    }
    .sidebar .nav-item:hover .nav-link {
      color: #503F2E;
    }
    /* Custom styles for divider */
    .sidebar .nav-item:after {
      content: "";
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      height: 1px;
      background-color: #fff;
      transition: height 0.3s ease-in-out; /* Transition effect for smooth animation */
    }
    /* Custom styles for header */ 
	   .header {
        background: linear-gradient(270deg, #060606, #3C2F23);
        color: #fff;
        padding: 10px;
        position: fixed;
        top: 0;
        width: 100%;
        z-index: 2;
        height: 69px;
        box-shadow: #FFF 2px 0px 0px 0px;
        /* Ensure header appears above sidebar */
    }
    .header .navbar-toggler {
      color: #fff;
    }
    .header .navbar-toggler-icon {
      color: #fff; /* Set the color of the toggle button to white */
    }
    .header .navbar-toggler.white {
      color: #fff !important; /* Ensure the toggle button is white */
    }
    .header .navbar-brand {
      color: #fff; /* Set navbar brand text color to white */
    }
    .header .nav-item .nav-link {
      color: #fff; /* Set nav link text color to white */
    }
    .main-content {
      margin-top: 50px; /* Height of fixed header + padding */
      margin-left: 100px; /* Width of sidebar */
      padding-top: 30px; /* Height of fixed header */
    }
	/* Custom styles for search bar */
    .search-container {
      display: flex;
      align-items: center;
      width: 300px;
      margin: 2px auto;
    }
    .search-input {
      width: 100%;
      padding: 10px 30px 10px 10px; /* Adjust padding to accommodate the icon */
      border: 1px solid #ccc;
      border-radius: 20px;
      font-size: 16px;
    }
    .search-icon {
      position: relative;
      left: -25px; /* Adjust the position of the icon */
      color: #aaa;
    }
    .search-icon i {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
    }
.btn-dark
         {
			 background:#503F2E;
			 color:#fff;
			 border: 1px solid #503F2E;
                 	}
		.btn-dark:hover{
			background:#fff;
			 color:#503F2E;
			 border:1px solid #503F2E;
			}	
			.bg-dark2{
			background:#503F2E;
			color:#fff;	
				}	
	
	 .modal-content {
            animation: modal-animation 0.5s;
        }

        @keyframes modal-animation {
            from {
                opacity: 0;
                transform: scale(0.8);
            }
            to {
                opacity: 1;
                transform: scale(1);
            }
        }
		
		/* Basic styling */
    .tab {
        display: none;
        padding: 20px;
    }
    .active {
        display: block;
    }
    .tab-buttons {
        display: flex;
    }
    .tab-button {
        cursor: pointer;
        padding: 10px 10px;
        background-color: #ffff;
        border: 1px solid #0A5064;
        border-radius: 5px 5px 0 0;
        margin-right: 5px;
		color:#212529;
    }
    .tab-button.active {
        background-color: #212529;
		color:#fff;
    }

    /* Background colors for tab content */
    #tab1 {
        background-color: #fff;
		color:#212529;
		padding:20px;
    }
    #tab2 {
        color:#212529;
		background-color: ;
    }
    #tab3 {
		color:#212529;
        background-color: ;
    }
    /* Add some padding to the button */
        .print-button {
            margin-bottom: 10px;
        }
</style>
<script>
        function printTable() {
            var printContents = document.getElementById('tableToPrint').outerHTML;
            var originalContents = document.body.innerHTML;

            // Open a new window for printing
            var printWindow = window.open('', '', 'height=500,width=800');
            printWindow.document.write('<html><head><title>Print Table</title>');
            printWindow.document.write('<style>table {width: 100%;border-collapse: collapse;} th, td {border: 1px solid black;padding: 10px;text-align: center;} </style>');
            printWindow.document.write('</head><body>');
            printWindow.document.write(printContents);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();  // Trigger the print dialog
        }
    </script>
<body>

<?php include("common/hd.php");?>
<!-- Sidebar -->
<?php include("common/sd.php"); ?>
<div class="main-content">
<br />
<div class="container">
    <div class="row">
        <div class="col-sm-4">
            <h3 class="mb-2 mt-2">Courses Detail</h3>
            
        </div>
        <div class="col-sm-5"></div>
        <div class="col-sm-3">
            <h3 style="background:#060606; padding:20px; border-radius:10px;font-size:18px; color:#fff; font-weight:600;text-transform:uppercase;text-align:center;"><?php echo $customer_name; ?></h3>
            
        </div>
        
    </div>
</div>
<div class="container">
    <div class="row">
        <div class="col-sm-11">
            <?php
            $rec = mysqli_query($connection,"select * from courses where class_name = '$customer_name' && school_name = '$customer_name2' ");
            ?>
             <button onclick="printTable()" class="print-button btn btn-dark">Print Table</button>

    <table id="tableToPrint" class="table table-bordered table-hover text-center text-uppercase">
        <tr class="bg-dark text-white" style="font-size:14px;">
           
            <th>Sr#</th>
            <th>Class Name</th>
            <th>Subject</th>
            <th>Company</th>
            <th>Supplier</th>
            <th>T.P(Rs)</th>
            <th>Price(Rs)</th>
            <th>Percentage</th>
            <th>(%) Amount</th>
            <th>Rack No</th>
            <th>Delete</th>
        </tr>
        <?php 
        $sr_no = 1;
        while($row = mysqli_fetch_array($rec)){
            $school_name = $row["school_name"];
        ?>
        <tr>
     
            <td><?php echo $sr_no++; ?></td>
            <td><?php echo $row["class_name"]; ?></td>
            <td><?php echo $row["course_name"]; ?></td>
            <td><?php echo $row["company"]; ?></td>
            <td><?php echo $row["publisher"]; ?></td>
            <td><?php echo $row["trade_price"]; ?></td>
            <td><?php echo $row["price"]; ?></td>
            <td><?php echo $row["percentage"]; ?></td>
            <td><?php echo $row["per_amount"]; ?></td>
            <td><?php echo $row["rack_number"]; ?></td>
            <td><a href="view_course.php?id=<?php echo $row["id"]; ?>" ><button type="button" class="btn btn-danger btn-sm">x</button></a></td>
        </tr>
        <?php
        }
        ?>
    </table>
            
            
        </div>
        <div class="col-sm-1">
           <a href="course_detail.php?school_name=<?php echo $school_name; ?>"><button type="button" class="btn btn-dark btn-sm">Back</button></a>
        </div>
    </div>


</div>

</div>    
</body>

<script>
 // Function to switch between tabs
    function openTab(evt, tabName) {
        // Get all elements with class="tab" and hide them
        var tabs = document.getElementsByClassName("tab");
        for (var i = 0; i < tabs.length; i++) {
            tabs[i].style.display = "none";
        }

        // Get all elements with class="tab-button" and remove the class "active"
        var tabButtons = document.getElementsByClassName("tab-button");
        for (var i = 0; i < tabButtons.length; i++) {
            tabButtons[i].classList.remove("active");
        }

        // Show the current tab, and add an "active" class to the button that opened the tab
        document.getElementById(tabName).style.display = "block";
        evt.currentTarget.classList.add("active");
    }
  document.addEventListener("DOMContentLoaded", function () {
    var sidebar = document.getElementById("sidebar");
    var overlay = document.createElement("div");
    overlay.classList.add("overlay");
    document.body.appendChild(overlay);

    function toggleSidebar() {
        sidebar.classList.toggle("hide");
        overlay.classList.toggle("hide");

        var mainContent = document.querySelector(".main-content");
        if (sidebar.classList.contains("hide")) {
            mainContent.style.marginLeft = "0";
        } else {
            mainContent.style.marginLeft = "100px"; // Width of the sidebar
        }
    }

    overlay.addEventListener("click", function () {
        toggleSidebar();
    });

    // Add toggle functionality to navbar toggler
    var navbarToggler = document.querySelector(".navbar-toggler");
    navbarToggler.addEventListener("click", function () {
        toggleSidebar();
    });

    // Automatically hide sidebar on all screen sizes
    // sidebar.classList.add("hide");
    // overlay.classList.add("hide");
    
    // Add class to body to prevent scrolling when sidebar is open
    // document.body.classList.add("noscroll");

    // Check if screen size is larger than a certain threshold (e.g., 768px for desktop screens)
    function checkScreenSize() {
        if (window.innerWidth > 768) {
            sidebar.classList.remove("hide");
            overlay.classList.remove("hide");
        } else {
            sidebar.classList.add("hide");
            overlay.classList.add("hide");
        }
    }

    // Check screen size on initial load and whenever the window is resized
    checkScreenSize();
    window.addEventListener("resize", checkScreenSize);
});
 
	</script>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

</html>