<?php
session_start();
ob_start(); 
error_reporting(E_ALL);
ini_set('display_errors', 1);
include "../include/config.php";
$userlogin = $_SESSION['email'];
$email = $_SESSION['email'];
$role = $_SESSION['role'];
$intake = $_SESSION['department'];

if (isset($_SESSION['email'])){ ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Management </title>

<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;500;600;700&family=Source+Sans+Pro:wght@300;400;600&display=swap" rel="stylesheet">

<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

<!-- Bootstrap -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">

<!-- DataTables -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">

<!-- jQuery & Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>

<style>
:root {
    --primary-color: #17cc27;
    --secondary-color: #202342;
    --accent-color: #ff8a65;
    --light-color: #f8f9fa;
    --dark-color: #343a40;
}

body {
    font-family: 'Montserrat', sans-serif;
    background-color: #f5f7fa;
    color: #333;
    padding: 0;
    margin: 0;
}

.navbar-custom {
    background-color: var(--secondary);
}

.header-content {
    background: var(--primary) ;
    color: white;
    padding: 20px 0;
    border-radius: 0 0 15px 15px;
    margin-bottom: 20px;
}

.card {
    border-radius: 12px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.08);
    border: none;
    margin-bottom: 20px;
}

.card-header {
    background: var(--secondary);
    color: white;
    border-radius: 12px 12px 0 0 !important;
}

.btn-primary {
    background-color: var(--primary);
    border-color: var(--primary);
}

.btn-primary:hover {
    background-color: #14b123;
    border-color: #14b123;
}

.table th {
    font-weight: 600;
    font-size: 0.85rem;
    text-transform: uppercase;
    background-color: #f8f9fa;
}

.student-photo {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid #eaeaea;
}

.status-badge {
    padding: 5px 10px;
    border-radius: 20px;
    font-weight: 500;
    font-size: 0.85rem;
}

.action-buttons .btn {
    padding: 5px 10px;
    margin: 0 2px;
}

/* DataTable customization */
.dataTables_filter input {
    border-radius: 20px;
    padding: 5px 15px;
    border: 1px solid #ddd;
}

.dataTables_length select {
    border-radius: 20px;
    padding: 5px 15px;
    border: 1px solid #ddd;
}

.pagination .page-item .page-link {
    border-radius: 20px;
    margin: 0 2px;
    color: var(--primary-color);
}

.pagination .page-item.active .page-link {
    background-color: var(--primary-color);
    border-color: var(--primary-color);
}

/* Modal customization */
.modal-content {
    border-radius: 12px;
    border: none;
    box-shadow: 0 5px 25px rgba(0,0,0,0.15);
}

.modal-header {
    background:  var(--primary);
    color: white;
    border-radius: 12px 12px 0 0;
}

.form-control, .form-select {
    border-radius: 8px;
    padding: 10px 15px;
    border: 1px solid #ddd;
    transition: all 0.3s;
}

.form-control:focus, .form-select:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(23, 204, 39, 0.2);
}

/* Student detail view */
.student-detail-card {
    border-radius: 12px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.08);
    border: none;
    margin-bottom: 20px;
    overflow: hidden;
}

.student-detail-header {
    background: var(--primary);
    color: white;
    padding: 15px 20px;
}
.btn{

}


.student-detail-photo {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    border: 4px solid white;
    object-fit: cover;
    box-shadow: 0 3px 10px rgba(0,0,0,0.2);
}

.info-table {
    width: 100%;
    border-collapse: collapse;
}

.info-table tr {
    border-bottom: 1px solid #eaeaea;
}

.info-table td {
    padding: 12px 15px;
    vertical-align: top;
}

.info-table tr:last-child {
    border-bottom: none;
}

.info-label {
    font-weight: 600;
    color: var(--secondary-color);
    width: 35%;
}

/* Responsive adjustments */
@media (max-width: 768px) {
    .action-buttons {
        display: flex;
        flex-direction: column;
    }
    
    .action-buttons .btn {
        margin: 2px 0;
        width: 100%;
    }
    
    .table-responsive {
        border: none;
    }
    
    .info-table td {
        padding: 8px 10px;
        display: block;
        width: 100%;
    }
    
    .info-label {
        width: 100%;
        font-weight: 700;
        margin-top: 10px;
    }
    
    .student-detail-photo {
        width: 100px;
        height: 100px;
    }
}
</style>
</head>
<body>
<?php include "../include/nav.php";?>
<div class="content" style="padding:15px">
    <br>

<div class="container">
    <?php if (isset($_GET['success'])): ?>
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <i class="fas fa-check-circle me-2"></i> <?php echo $_GET['success']; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
    <?php endif; ?>
    
    <?php if (isset($_GET['error'])): ?>
    <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <i class="fas fa-exclamation-circle me-2"></i> <?php echo $_GET['error']; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
    <?php endif; ?>
    
    <?php
    // Check if we're viewing a single student or the list
    if (isset($_GET['id'])) {
        // View single student details
        $id = $_GET['id'];
        $query = mysqli_query($conn, "SELECT * FROM students WHERE id='$id'");
        if ($row = mysqli_fetch_array($query)) {
            $status_class = $row['status'] == 'active' ? 'bg-success' : 'bg-danger';
    ?>
    
    <div class="student-detail-card">
        <div class="student-detail-header d-flex justify-content-between align-items-center">
            <h4 class="mb-0">Student Information</h4>
            <span class="status-badge bg-light text-dark">
                <i class="fas fa-circle me-1 text-<?php echo $row['status'] == 'active' ? 'success' : 'danger'; ?>"></i>
                <?php echo ucfirst($row['status']); ?>
            </span>
        </div>
        
        <div class="card-body">
            <div class="row">
                <div class="col-md-3 text-center mb-4 mb-md-0">
                    <img src="../../students/img/<?php echo $row['image']; ?>" class="student-detail-photo" alt="Student Photo">
                    <h5 class="mt-3"><?php echo $row['studentname']; ?></h5>
                    <p class="text-muted"><?php echo $row['studentid']; ?></p>
                    
                    <div class="d-grid gap-2 mt-3">
                        <button data-bs-toggle="modal" data-bs-target="#editModal" class="btn btn-warning">
                            <i class="fas fa-edit me-2"></i>Edit Record
                        </button>
                        <a onclick="return confirmDelete()" href="pupildelete.php?id=<?php echo $row['id']; ?>" class="btn btn-danger">
                            <i class="fas fa-trash me-2"></i>Delete Student
                        </a>
                        <a href="students.php" class="btn btn-secondary">
                            <i class="fas fa-arrow-left me-2"></i>Back to List
                        </a>
                    </div>
                </div>
                
                <div class="col-md-9">
                    <div class="table-responsive">
                        <table class="info-table">
                            <tr>
                                <td class="info-label">Student ID</td>
                                <td><?php echo $row['studentid']; ?></td>
                            </tr>
                            <tr>
                                <td class="info-label">Full Name</td>
                                <td><?php echo $row['studentname']; ?></td>
                            </tr>
                            <tr>
                                <td class="info-label">Program</td>
                                <td><?php echo $row['program']; ?></td>
                            </tr>
                            <tr>
                                <td class="info-label">Intake</td>
                                <td><?php echo $row['intake']; ?></td>
                            </tr>
                            <tr>
                                <td class="info-label">Gender</td>
                                <td><?php echo $row['gender']; ?></td>
                            </tr>
                            <tr>
                                <td class="info-label">Study Period</td>
                                <td><?php echo $row['start_year']; ?> - <?php echo $row['end_year']; ?></td>
                            </tr>
                            <tr>
                                <td class="info-label">Contact</td>
                                <td>
                                    <i class="fas fa-phone me-2 text-primary"></i> <?php echo $row['mobile']; ?><br>
                                    <i class="fas fa-envelope me-2 text-primary"></i> <?php echo $row['email']; ?>
                                </td>
                            </tr>
                            <tr>
                                <td class="info-label">Parent Information</td>
                                <td>
                                    <strong><?php echo $row['parent_name']; ?></strong> (<?php echo $row['parent_relation']; ?>)<br>
                                    <i class="fas fa-phone me-2 text-primary"></i> <?php echo $row['parent_mobile']; ?><br>
                                    <i class="fas fa-envelope me-2 text-primary"></i> <?php echo $row['parent_email']; ?>
                                </td>
                            </tr>
                            <tr>
                                <td class="info-label">Boarding Status</td>
                                <td>
                                    <?php echo $row['boarding_status']; ?>
                                    <?php if (!empty($row['hostel_name'])): ?>
                                    - <?php echo $row['hostel_name']; ?>
                                    <?php if (!empty($row['room_number'])): ?>
                                    (Room <?php echo $row['room_number']; ?>)
                                    <?php endif; ?>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <?php } else { ?>
    <div class="alert alert-danger text-center">
        <i class="fas fa-exclamation-circle me-2"></i>Student record not found.
    </div>
    <?php } ?>
    
    <!-- Edit Modal for Single Student View -->
    <div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="editModalLabel"><i class="fas fa-edit me-2"></i>Update Student Record</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <?php
                    $id = $_GET['id'];
                    $query = mysqli_query($conn, "SELECT * FROM students WHERE id='$id'");
                    if ($student = mysqli_fetch_assoc($query)) {
                    ?>
                    <form action="update.php" method="post">
                        <input type="hidden" name="id" value="<?php echo $student['id']; ?>">
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Student ID</label>
                                <input type="text" class="form-control" name="studentid" value="<?php echo $student['studentid']; ?>" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Full Name</label>
                                <input type="text" class="form-control" name="studentname" value="<?php echo $student['studentname']; ?>" required>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Intake</label>
                                <select name="intake" class="form-select" required>
                                    <option value="">Select Intake</option>
                                    <?php
                                    $intakeQuery = mysqli_query($conn, "SELECT * FROM intake");
                                    while ($rowIntake = mysqli_fetch_assoc($intakeQuery)) {
                                        $selected = ($student['intake'] == $rowIntake['intake']) ? 'selected' : '';
                                        echo "<option value='{$rowIntake['intake']}' $selected>{$rowIntake['intake']}</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Program</label>
                                <select name="program" class="form-select" required>
                                    <option value="">Select Program</option>
                                    <?php
                                    $progQuery = mysqli_query($conn, "SELECT * FROM programs");
                                    while ($rowProg = mysqli_fetch_assoc($progQuery)) {
                                        $selected = ($student['program'] == $rowProg['program']) ? 'selected' : '';
                                        echo "<option value='{$rowProg['program']}' $selected>{$rowProg['program']}</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Start Year</label>
                                <input type="number" class="form-control" name="start_year" value="<?php echo $student['start_year']; ?>" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">End Year</label>
                                <input type="number" class="form-control" name="end_year" value="<?php echo $student['end_year']; ?>" required>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Phone Number</label>
                                <input type="text" class="form-control" name="mobile" value="<?php echo $student['mobile']; ?>" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Email Address</label>
                                <input type="email" class="form-control" name="email" value="<?php echo $student['email']; ?>" required>
                            </div>
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">Status</label>
                            <select name="status" class="form-select" required>
                                <option>--Select Status--</option>
                                <option value="clean" <?php echo $student['status'] == 'clean' ? 'selected' : ''; ?>>clean</option>
                                <option value="Transfered" <?php echo $student['status'] == 'Transfered' ? 'selected' : ''; ?>>Transfered</option>
                                <option value="Completed" <?php echo $student['status'] == 'Completed' ? 'selected' : ''; ?>>Completed</option>
                                <option value="Drop Out" <?php echo $student['status'] == 'Drop Out' ? 'selected' : ''; ?>>Drop Out</option>
                                <option value="Deceased" <?php echo $student['status'] == 'Deceased' ? 'selected' : ''; ?>>Deceased</option>
                            </select>
                        </div>
                        <div class="mb-3">
                <label class="form-label">Change Password</label>
                <input type="password" class="form-control" name="pass" placeholder="Enter new password (leave blank to keep current)">
            </div>

                        <div class="d-grid">
                            <button type="submit" name="updatestudent" class="btn  btn-lg" style="background:var(--primary);width:50%;
    color:white;">
                                <i class="fas fa-save me-2"></i>Update Student Record
                            </button>
                        </div>
                    </form>
                    <?php } ?>
                </div>
            </div>
        </div>
    </div>
    
    <?php } else { 
    // View all students in a table
    ?>
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0"><i class="fas fa-list me-2"></i>All Students</h5>
            <a href="student_register.php" class="btn btn-light">
                <i class="fas fa-plus me-2"></i>Add New Student
            </a>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table id="studentsTable" class="table table-striped table-bordered table-hover" style="width:100%">
                    <thead>
                        <tr>
                            <th>Image</th>
                            <th>Student ID</th>
                            <th>Name</th>
                            <th>Parent Name</th>
                            <th>Parent Mobile</th>
                            <th>Intake</th>
                            <th>Program</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $query = mysqli_query($conn, "SELECT * FROM students ORDER BY id DESC");
                        while($row = mysqli_fetch_array($query)){
                            $status_class = $row['status'] == 'active' ? 'bg-success' : 'bg-danger';
                        ?>
                        <tr>
                            <td class="text-center">
                                <img src="../../students/img/<?php echo $row['image']; ?>" class="student-photo" alt="Student Image">
                            </td>
                            <td><?php echo $row['studentid']; ?></td>
                            <td><?php echo $row['studentname']; ?></td>
                            <td><?php echo $row['parent_name']; ?></td>
                            <td><?php echo $row['parent_mobile']; ?></td>
                            <td><?php echo $row['intake']; ?></td>
                            <td><?php echo $row['program']; ?></td>
                            <td><span class="badge <?php echo $status_class; ?> status-badge"><?php echo ucfirst($row['status']); ?></span></td>
                            <td class="action-buttons">
                                <a href="?id=<?php echo $row['id']; ?>" class="btn btn-sm btn-info" data-bs-toggle="tooltip" title="View Details">
                                    <i class="fas fa-eye"></i>
                                </a>
                                <a href="edit.php?id=<?php echo $row['id']; ?>" class="btn btn-sm btn-warning" data-bs-toggle="tooltip" title="Edit Student">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <a onclick="return confirmDelete()" href="pupildelete.php?id=<?php echo $row['id']; ?>" class="btn btn-sm btn-danger" data-bs-toggle="tooltip" title="Delete Student">
                                    <i class="fas fa-trash"></i>
                                </a>
                            </td>
                        </tr>
                        <?php } ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <?php } ?>
</div>

<script>
function confirmDelete() {
    return confirm("Are you sure you want to delete this student? This action cannot be undone.");
}

$(document).ready(function() {
    // Initialize DataTable if we're on the list view
    <?php if (!isset($_GET['id'])) { ?>
    $('#studentsTable').DataTable({
        responsive: true,
        pageLength: 10,
        lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
        order: [[1, 'asc']]
    });
    <?php } ?>
    
    // Initialize tooltips
    var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
    var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl)
    });
    
    // Auto-hide alerts after 5 seconds
    setTimeout(function() {
        $('.alert').fadeOut('slow');
    }, 5000);
});
</script>

</body>
</html>
<?php
} else {
    header("location:index.php");
}
?>