<!--School Detail Edit -->

<?php
include('session_validator.php');
include("includes/db.php");

// Fetch school data from the database
$sql = "SELECT * FROM schools";
$result = $conn->query($sql);
$schools = $result->fetch_all(MYSQLI_ASSOC);
?>



<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>View/Edit Schools</title>
    <link rel="stylesheet" href="css/bootstrap.css" media="screen">
    <link rel="stylesheet" href="css/font-awesome.min.css" media="screen">
    <link rel="stylesheet" href="css/animate-css/animate.min.css" media="screen">
    <link rel="stylesheet" href="css/lobipanel/lobipanel.min.css" media="screen">
    <link rel="stylesheet" href="css/prism/prism.css" media="screen">
    <link rel="stylesheet" href="css/main.css" media="screen">
    <script src="js/modernizr/modernizr.min.js"></script>
    <style>
        /* Add your custom styles here */
    </style>
</head>

<body class="top-navbar-fixed">
    <div class="main-wrapper">
        <?php include('includes/topbar.php'); ?>
        <div class="content-wrapper">
            <div class="content-container">
                <?php include('includes/leftbar.php'); ?>

                <div class="main-page">
                    <div class="container-fluid">
                        <div class="row page-title-div">
                            <div class="col-md-6">
                                <h2 class="title">View/Edit Schools</h2>
                            </div>
                        </div>

                        <div class="row breadcrumb-div">
                            <div class="col-md-6">
                                <ul class="breadcrumb">
                                    <li><a href="dashboard.php"><i class="fa fa-home"></i> Home</a></li>
                                    <li class="active">View/Edit Schools</li>
                                </ul>
                            </div>
                        </div>

                    </div>

                    <section class="section">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="panel">
                                        <div class="panel-heading">
                                            <div class="panel-title">
                                                <h5>School List</h5>
                                            </div>
                                        </div>

                                        <div class="panel-body">
<table class="table table-bordered">
    <thead>
        <tr>
            <th>ID</th>
            <th>School Detail</th>
            <th>Logo</th>
            <!-- Additional table headers as needed -->
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($schools as $school) { ?>
            <tr>
                <td><?= $school['id'] ?></td>
                <td> <strong>Name: </strong> <?= $school['school_name'] ?>
                	<br>
                	<strong> Address:</strong> <?= $school['address'] ?>
                	<br>
                	<strong> Estd: </strong><?= $school['establishment_year'] ?>
                	<br>
                	<strong> Contact </strong> <?= $school['phone'] ?>
                	<br>
                	<strong> Email:</strong> <?= $school['email'] ?>
                	<br>
                	<strong> Website:</strong> <?= $school['website'] ?>
                	<br>
                	<strong> Iemis:</strong> <?= $school['school_code'] ?>
                	<br>
                	<strong> NEB Code:</strong> <?= $school['neb_code'] ?>
                	<br>
                	<strong> CTEVT:</strong> <?= $school['ctevt_code'] ?>
                	<br>
                	<strong> Pan No:</strong> <?= $school['pan'] ?>
	                </td>
               
      
                <td>
                    <?php
                    // Displaying the school's logo or default icon
                    $imageFilename = $school["logo"];
                    $imagePath = $imageFilename;
                    // echo $imagePath;

                    if ($imageFilename !== null && file_exists($imagePath) && is_readable($imagePath)) {
                        // If image is not NULL and exists, display it
                        echo "<img src='$imagePath' alt='School Logo' width='80' height='80'>";
                    } else {
                        // If image doesn't exist, display default "logo-blank" image
                        $defaultImagePath = "uploads/user-blank.png"; // Adjust the filename if needed
                        echo "<img src='$defaultImagePath' alt='Default Logo' width='50' height='50'>";
                    }
                    ?>
                </td>
                                                            
<td>
    <a href="#" class="btn btn-primary editSchoolBtn" data-toggle="modal" data-target="#editSchoolModal"
       data-id="<?= $school['id'] ?>"
       data-name="<?= $school['school_name'] ?>"
       data-address="<?= $school['address'] ?>"
       data-establishment-year="<?= $school['establishment_year'] ?>"
       data-phone="<?= $school['phone'] ?>"
       data-email="<?= $school['email'] ?>"
       data-website="<?= $school['website'] ?>"
       data-logo="<?= $school['logo'] ?>"
       data-school-code="<?= $school['school_code'] ?>"
       data-neb-code="<?= $school['neb_code'] ?>"
       data-ctevt-code="<?= $school['ctevt_code'] ?>"
       data-pan="<?= $school['pan'] ?>">
        Edit
    </a>
</td>


                                                        </tr>
                                                    <?php } ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                </div>
            </div>
        </div>
    </div>


<div class="modal fade" id="editSchoolModal" tabindex="-1" role="dialog" aria-labelledby="editSchoolModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editSchoolModalLabel">Edit School Details</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <!-- Form for editing school details goes here -->
<form id="editSchoolForm" enctype="multipart/form-data">
    <div class="form-group">
        <label for="editSchoolName">School Name</label>
        <input type="text" class="form-control" id="editSchoolName" name="editSchoolName" required>
    </div>

    <div class="form-group">
        <label for="editSchoolAddress">Address</label>
        <input type="text" class="form-control" id="editSchoolAddress" name="editSchoolAddress" required>
    </div>

    <div class="form-group">
        <label for="editEstablishmentYear">Establishment Year</label>
        <input type="text" class="form-control" id="editEstablishmentYear" name="editEstablishmentYear" required>
    </div>

    <div class="form-group">
        <label for="editSchoolPhone">Phone</label>
        <input type="text" class="form-control" id="editSchoolPhone" name="editSchoolPhone" required>
    </div>

    <div class="form-group">
        <label for="editSchoolEmail">Email</label>
        <input type="email" class="form-control" id="editSchoolEmail" name="editSchoolEmail" required>
    </div>

    <div class="form-group">
        <label for="editSchoolWebsite">Website</label>
        <input type="url" class="form-control" id="editSchoolWebsite" name="editSchoolWebsite" required>
    </div>
<div class="form-group">
        <label for="editSchoolCode">School Code</label>
        <input type="text" class="form-control" id="editSchoolCode" name="editSchoolCode" required>
    </div>

    <div class="form-group">
        <label for="editNEBCode">NEB Code</label>
        <input type="text" class="form-control" id="editNEBCode" name="editNEBCode" required>
    </div>

    <div class="form-group">
        <label for="editCTEVTCode">CTEVT Code</label>
        <input type="text" class="form-control" id="editCTEVTCode" name="editCTEVTCode" required>
    </div>

    <div class="form-group">
        <label for="editPAN">PAN</label>
        <input type="text" class="form-control" id="editPAN" name="editPAN" required>
    </div>
       <div class="form-group">
        <label for="editSchoolLogo">Logo</label>
        <input type="text" class="form-control" id="editSchoolLogo" name="editSchoolLogo" >
        <input type="file" class="form-control" id="editSchoolLogo" name="editSchoolLogo">
    </div>

    
 <input type="hidden" id="editSchoolId" name="editSchoolId">
    <button type="submit" class="btn btn-primary">Save Changes</button>
</form>




            </div>
        </div>
    </div>
</div>
    <script src="js/jquery/jquery-2.2.4.min.js"></script>
<!-- Add this script in your HTML file -->
<script>
    $(document).ready(function () {
        // Handle click event for the "Edit" button
        $('.editSchoolBtn').click(function () {
            // Get school details from the data attributes
            var schoolId = $(this).data('id');
            var schoolName = $(this).data('name');
            var address = $(this).data('address');
            var establishmentYear = $(this).data('establishment-year');
            var phone = $(this).data('phone');
            var email = $(this).data('email');
            var website = $(this).data('website');
            var logo = $(this).data('logo');
            var schoolCode = $(this).data('school-code');
            var nebCode = $(this).data('neb-code');
            var ctevtCode = $(this).data('ctevt-code');
            var pan = $(this).data('pan');

            // Populate the modal form with the retrieved data
            $('#editSchoolId').val(schoolId);
            $('#editSchoolName').val(schoolName);
            $('#editSchoolAddress').val(address);
            $('#editEstablishmentYear').val(establishmentYear);
            $('#editSchoolPhone').val(phone);
            $('#editSchoolEmail').val(email);
            $('#editSchoolWebsite').val(website);
            $('#editSchoolLogo').val(logo);
            $('#editSchoolCode').val(schoolCode);
            $('#editNEBCode').val(nebCode);
            $('#editCTEVTCode').val(ctevtCode);
            $('#editPAN').val(pan);

            // Populate other form fields as needed
        });

                // When the form is submitted
        $("#editSchoolForm").submit(function (e) {
            // Prevent the default form submission
            e.preventDefault();

            // Create FormData object to handle file uploads
            var formData = new FormData(this);

            // Make an AJAX request to update data
            $.ajax({
                type: "POST",
                url: "update_school.php",
                data: formData,
                contentType: false,
                processData: false,
                success: function (response) {
                    // Handle the response
                    console.log(response);

                    // Close the modal after a successful submission
                    $('#editSchoolModal').modal('hide');

                    // Show an alert message
                    alert("School data updated successfully.");

                    // Refresh the page if needed
                    location.reload();
                },
                error: function (error) {
                    console.error("AJAX request failed: ", error);
                }
            });
        });
    });
</script>




<script src="js/jquery/jquery-2.2.4.min.js"></script>
<script src="js/bootstrap/bootstrap.min.js"></script>
<script src="js/pace/pace.min.js"></script>
<script src="js/lobipanel/lobipanel.min.js"></script>
<script src="js/iscroll/iscroll.js"></script>
<script src="js/prism/prism.js"></script>
<script src="js/DataTables/datatables.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>