<!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>SRMS Admin Edit Student</title>
    <!-- Include necessary stylesheets -->
    <link rel="stylesheet" href="css/bootstrap.min.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/select2/select2.min.css">
    <link rel="stylesheet" href="css/main.css" media="screen">
    <link rel="stylesheet" href="css/admitcard.css" media="screen">
    <script src="js/sweetalret.js"></script>
    <style>
        /* Additional styling for the "Print All Admit Cards" button */
        .print-btn {
            position: fixed;
            bottom: 20px;
            right: 20px;
        }
        .academic-year-record {
            border: 1px solid #ddd;
            padding: 15px;
            margin-bottom: 15px;
            border-radius: 5px;
            background-color: #f9f9f9;
        }
        .year-label {
            font-weight: bold;
            color: #333;
            margin-bottom: 10px;
        }
    </style>
</head>

<body class="top-navbar-fixed">
   
    <div class="main-wrapper">
        <!-- ========== TOP NAVBAR ========== -->
        <?php include('includes/topbar.php'); ?>
        <!-- ========== WRAPPER FOR BOTH SIDEBARS & MAIN CONTENT ========== -->
        <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">Edit Student</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> Student</li>
                                    <li class="">Edit Student</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>Edit Student</h5>
                                            </div>
                                        </div>
                                        <div class="panel-body">
                                            <?php 
                                            include('includes/db.php'); 

                                            // Check if student_id is provided in the query parameters
                                            if (isset($_GET['student_id'])) {
                                                $studentId = $_GET['student_id'];

                                                // Fetch basic student details from the database
                                                $query = "SELECT * FROM students WHERE student_id = $studentId";
                                                $result = $conn->query($query);

                                                if (!$result) {
                                                    die("Query failed: " . $conn->error);
                                                }

                                                if ($result->num_rows > 0) {
                                                    $studentDetails = $result->fetch_assoc();

                                                    // Check if a picture already exists for the student
                                                    $picturePath = "uploads/" . $studentDetails['photo'];
                                                    $existingPicture = file_exists($picturePath);

                                                    // Fetch student's academic history from student_class_section_data
                                                    // Using LEFT JOIN to get all records even if class/section is missing
                                                    $academicQuery = "SELECT scsd.*, c.class, c.class_numeric, s.section_name 
                                                                     FROM student_class_section_data scsd
                                                                     LEFT JOIN classes c ON scsd.class_id = c.class_id
                                                                     LEFT JOIN sections s ON scsd.section_id = s.section_id
                                                                     WHERE scsd.student_id = $studentId
                                                                     ORDER BY scsd.academic_year DESC";
                                                    $academicResult = $conn->query($academicQuery);
                                                    if (!$academicResult) {
                                                        die("Academic query failed: " . $conn->error);
                                                    }
                                                    $academicRecords = array();
                                                    while ($row = $academicResult->fetch_assoc()) {
                                                        $academicRecords[$row['academic_year']] = $row;
                                                    }

                                                    // Get the current/latest academic year record
                                                    $currentAcademicYear = '';
                                                    $currentClassId = '';
                                                    $currentSectionId = '';
                                                    $currentStatus = 'active';
                                                    $currentRemarks = '';
                                                    if (!empty($academicRecords)) {
                                                        $latestYear = max(array_keys($academicRecords));
                                                        $currentRecord = $academicRecords[$latestYear];
                                                        $currentAcademicYear = $currentRecord['academic_year'];
                                                        $currentClassId = $currentRecord['class_id'];
                                                        $currentSectionId = $currentRecord['section_id'];
                                                        $currentStatus = $currentRecord['status'];
                                                        $currentRemarks = $currentRecord['remarks'];
                                                    }
                                            ?>

                                            <form action="process_edit_student.php" method="post" enctype="multipart/form-data">
                                                <input type="hidden" name="student_id" value="<?php echo $studentDetails['student_id']; ?>">

                                                <!-- Basic Student Information -->
                                                <div class="form-row">
                                                    <div class="col-md-12">
                                                        <h5 class="mb-3" style="border-bottom: 2px solid #007bff; padding-bottom: 5px;">Basic Information</h5>
                                                    </div>
                                                    
                                                    <div class="form-group col-md-3">
                                                        <label for="regd_id">Registration ID:[Iemis]</label>
                                                        <input type="text" id="regd_id" name="regd_id" value="<?php echo $studentDetails['regd_id']; ?>" class="form-control" required>
                                                    </div>
                                                    <div class="form-group col-md-3">
                                                        <label for="symbol_no">Symbol No:</label>
                                                        <input type="text" id="symbol_no" name="symbol_no" value="<?php echo $studentDetails['symbol_no']; ?>" class="form-control" required>
                                                    </div>
                                                    <div class="form-group col-md-3">
                                                        <label for="name">Name:</label>
                                                        <input type="text" id="name" name="name" value="<?php echo $studentDetails['name']; ?>" class="form-control" required>
                                                    </div>

                                                    <div class="form-group col-md-3">
                                                        <label for="dob">Date of Birth:</label>
                                                        <input type="date" id="dob" name="dob" value="<?php echo $studentDetails['dob']; ?>" class="form-control" required>
                                                    </div>

                                                    <div class="form-group col-md-3">
                                                        <label for="gender">Gender:</label>
                                                        <select id="gender" name="gender" class="form-control" required>
                                                            <option value="Male" <?php if ($studentDetails['gender'] == 'Male') echo 'selected'; ?>>MALE</option>
                                                            <option value="Female" <?php if ($studentDetails['gender'] == 'Female') echo 'selected'; ?>>FEMALE</option>
                                                            <option value="Others" <?php if ($studentDetails['gender'] == 'Others') echo 'selected'; ?>>OTHERS</option>
                                                        </select>
                                                    </div>

                                                    <div class="form-group col-md-3">
                                                        <label for="group">Ethnic Group:</label>
                                                        <select id="group" name="group" class="form-control" >
                                                            <?php
                                                            // Fetch group options from the 'ent_group' table
                                                            $query_group = "SELECT * FROM ent_group";
                                                            $result_group = $conn->query($query_group);
                                                            
                                                            if (!$result_group) {
                                                                die("Group query failed: " . $conn->error);
                                                            }

                                                            // Populate the dropdown with group options
                                                            while ($row_group = $result_group->fetch_assoc()) {
                                                                echo "<option value='" . $row_group['id'] . "'";
                                                                if ($studentDetails['std_group'] == $row_group['id']) echo 'selected';
                                                                echo ">" . $row_group['group_name'] . "</option>";
                                                            }
                                                            ?>
                                                        </select>
                                                    </div>

                                                    <div class="form-group col-md-3">
                                                        <label for="address">Address:</label>
                                                        <input type="text" id="address" name="address" value="<?php echo $studentDetails['address']; ?>" class="form-control" >
                                                    </div>

                                                    <div class="form-group col-md-3">
                                                        <label for="phone">Phone:</label>
                                                        <input type="text" id="phone" name="phone" value="<?php echo $studentDetails['phone']; ?>" class="form-control" >
                                                    </div>

                                                    <div class="form-group col-md-3">
                                                        <label for="email">Email:</label>
                                                        <input type="email" id="email" name="email" value="<?php echo $studentDetails['email']; ?>" class="form-control" >
                                                    </div>

                                                    <div class="form-group col-md-3">
                                                        <label for="parent_name">Parent Name:</label>
                                                        <input type="text" id="parent_name" name="parent_name" value="<?php echo $studentDetails['parent_name']; ?>" class="form-control" required>
                                                    </div>

                                                    <div class="form-group col-md-3">
                                                        <label for="parent_phone">Parent Phone:</label>
                                                        <input type="text" id="parent_phone" name="parent_phone" value="<?php echo $studentDetails['parent_phone']; ?>" class="form-control" >
                                                    </div>
                                                </div>

                                                <!-- Current Academic Information -->
                                                <div class="form-row mt-4">
                                                    <div class="col-md-12">
                                                        <h5 class="mb-3" style="border-bottom: 2px solid #28a745; padding-bottom: 5px;">Current Academic Information</h5>
                                                    </div>
                                                    
                                                    <div class="form-group col-md-3">
                                                        <label for="academic_year">Academic Year:</label>
                                                        <input type="text" id="academic_year" name="academic_year" value="<?php echo $currentAcademicYear; ?>" class="form-control" pattern="\d{4}-\d{4}" placeholder="YYYY-YYYY" required>
                                                        <small class="form-text text-muted">Format: YYYY-YYYY (e.g., 2082-2083)</small>
                                                    </div>

                                                    <div class="form-group col-md-3">
                                                        <label for="studentClass">Class:</label>
                                                        <select id="studentClass" name="studentClass" class="form-control" required>
                                                            <option value="">Select Class</option>
                                                            <?php
                                                            // Fetch class names from the 'classes' table - REMOVED 'section' column
                                                            $query_class = "SELECT class_id, class, class_numeric FROM classes ORDER BY class_numeric";
                                                            $result_class = $conn->query($query_class);
                                                            
                                                            if (!$result_class) {
                                                                die("Class query failed: " . $conn->error);
                                                            }

                                                            while ($row_class = $result_class->fetch_assoc()) {
                                                                echo "<option value='" . $row_class['class_id'] . "'";
                                                                if ($currentClassId == $row_class['class_id']) echo 'selected';
                                                                echo ">" . $row_class['class'] . "</option>";
                                                            }
                                                            ?>
                                                        </select>
                                                    </div>

                                                    <div class="form-group col-md-3">
                                                        <label for="section">Section:</label>
                                                        <select id="section" name="section" class="form-control" required>
                                                            <option value="">Select Section</option>
                                                            <?php
                                                            // Fetch sections from the 'sections' table
                                                            $query_section = "SELECT section_id, section_name FROM sections WHERE status = 1 ORDER BY section_name";
                                                            $result_section = $conn->query($query_section);
                                                            
                                                            if (!$result_section) {
                                                                die("Section query failed: " . $conn->error);
                                                            }

                                                            while ($row_section = $result_section->fetch_assoc()) {
                                                                echo "<option value='" . $row_section['section_id'] . "'";
                                                                if ($currentSectionId == $row_section['section_id']) echo 'selected';
                                                                echo ">" . $row_section['section_name'] . "</option>";
                                                            }
                                                            ?>
                                                        </select>
                                                    </div>

                                                    <div class="form-group col-md-3">
                                                        <label for="status">Status:</label>
                                                        <select id="status" name="status" class="form-control" required>
                                                            <option value="active" <?php if ($currentStatus == 'active') echo 'selected'; ?>>Active</option>
                                                            <option value="promoted" <?php if ($currentStatus == 'promoted') echo 'selected'; ?>>Promoted</option>
                                                            <option value="transferred" <?php if ($currentStatus == 'transferred') echo 'selected'; ?>>Transferred</option>
                                                            <option value="dropout" <?php if ($currentStatus == 'dropout') echo 'selected'; ?>>Dropout</option>
                                                            <option value="repeated" <?php if ($currentStatus == 'repeated') echo 'selected'; ?>>Repeated</option>
                                                            <option value="others" <?php if ($currentStatus == 'others') echo 'selected'; ?>>Others</option>
                                                        </select>
                                                    </div>

                                                    <div class="form-group col-md-12">
                                                        <label for="remarks">Remarks:</label>
                                                        <textarea id="remarks" name="remarks" class="form-control" rows="2"><?php echo htmlspecialchars($currentRemarks); ?></textarea>
                                                    </div>
                                                </div>

                                                <!-- Academic History (Read-only) -->
                                                <?php if (!empty($academicRecords)): ?>
                                                <div class="form-row mt-4">
                                                    <div class="col-md-12">
                                                        <h5 class="mb-3" style="border-bottom: 2px solid #6c757d; padding-bottom: 5px;">Academic History</h5>
                                                        <?php foreach ($academicRecords as $year => $record): ?>
                                                        <div class="academic-year-record">
                                                            <div class="year-label">Academic Year: <?php echo $year; ?></div>
                                                            <div class="row">
                                                                <div class="col-md-3">
                                                                    <strong>Class:</strong> <?php echo !empty($record['class']) ? $record['class'] : 'N/A'; ?>
                                                                </div>
                                                                <div class="col-md-3">
                                                                    <strong>Section:</strong> <?php echo !empty($record['section_name']) ? $record['section_name'] : 'N/A'; ?>
                                                                </div>
                                                                <div class="col-md-3">
                                                                    <strong>Status:</strong> <?php echo ucfirst($record['status']); ?>
                                                                </div>
                                                                <div class="col-md-3">
                                                                    <strong>Updated:</strong> <?php echo date('Y-m-d', strtotime($record['updated_at'])); ?>
                                                                </div>
                                                                <?php if (!empty($record['remarks'])): ?>
                                                                <div class="col-md-12 mt-2">
                                                                    <strong>Remarks:</strong> <?php echo htmlspecialchars($record['remarks']); ?>
                                                                </div>
                                                                <?php endif; ?>
                                                            </div>
                                                        </div>
                                                        <?php endforeach; ?>
                                                    </div>
                                                </div>
                                                <?php endif; ?>

                                                <!-- Photo Upload -->
                                                <div class="form-row mt-4">
                                                    <div class="col-md-12">
                                                        <h5 class="mb-3" style="border-bottom: 2px solid #ffc107; padding-bottom: 5px;">Photo</h5>
                                                    </div>
                                                    
                                                    <div class="form-group col-md-3">
                                                        <label for="photo">Student Photo:</label>
                                                        <?php if ($existingPicture) : ?>
                                                            <input type="hidden" name="existing_photo" value="<?php echo $studentDetails['photo']; ?>">
                                                            <input type="file" id="photo" name="photo" class="form-control">
                                                            <div class="mt-2">
                                                                <small>Existing Photo:</small><br>
                                                                <img src="<?php echo $picturePath; ?>" alt="Student Photo" style="width:80px; height:80px" class="existing-photo mt-1">
                                                            </div>
                                                        <?php else: ?>
                                                            <input type="file" id="photo" name="photo" class="form-control">
                                                            <small class="text-muted">No existing photo found</small>
                                                        <?php endif; ?>
                                                    </div>
                                                </div>

                                                <!-- Submit Buttons -->
                                                <div class="form-group mt-4">
                                                    <button class="btn btn-primary" type="submit">Update Student</button>
                                                    <a href="manage-students.php" class="btn btn-secondary">Cancel</a>
                                                </div>
                                            </form>

                                            <?php
                                                } else {
                                                    echo "<div class='alert alert-danger'>Student not found.</div>";
                                                }
                                            } else {
                                                echo "<div class='alert alert-danger'>Student ID not provided.</div>";
                                            }
                                            ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                </div>
            </div>
        </div>
    </div>

    <!-- Include necessary scripts -->
    <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/waypoint/waypoints.min.js"></script>
    <script src="js/counterUp/jquery.counterup.min.js"></script>
    <script src="js/toastr/toastr.min.js"></script>
    <script src="js/icheck/icheck.min.js"></script>
    <script src="js/main.js"></script>
    <script src="js/production-chart.js"></script>
    <script src="js/traffic-chart.js"></script>
    <script src="js/task-list.js"></script>

</body>

</html>