<?php
require_once '../includes/config.php';

// Check if user is logged in and is admin
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true || $_SESSION['role'] !== 'admin') {
    header('Location: ../auth/login.php');
    exit();
}

$title = "Deactivate Student";

// Get student ID
$student_id = intval($_GET['id'] ?? 0);

if ($student_id <= 0) {
    $_SESSION['message'] = 'Invalid student ID!';
    $_SESSION['message_type'] = 'danger';
    header('Location: manage_students.php');
    exit();
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $status = $_POST['status'] == 'active' ? 1 : 0;
    $reason = !empty($_POST['reason']) ? mysqli_real_escape_string($conn, $_POST['reason']) : 'No reason provided';
    
    // Update student status
    $sql = "UPDATE students SET 
            status = ?,
            updated_at = NOW()
            WHERE id = ?";
    
    $stmt = mysqli_prepare($conn, $sql);
    mysqli_stmt_bind_param($stmt, 'ii', $status, $student_id);
    
    if (mysqli_stmt_execute($stmt)) {
        // Log the status change (optional - if you have logs table)
        $log_sql = "INSERT INTO student_status_log (student_id, status, reason, changed_by, changed_at) 
                   VALUES (?, ?, ?, ?, NOW())";
        $log_stmt = mysqli_prepare($conn, $log_sql);
        mysqli_stmt_bind_param($log_stmt, 'iisi', $student_id, $status, $reason, $_SESSION['user_id']);
        mysqli_stmt_execute($log_stmt);
        mysqli_stmt_close($log_stmt);
        
        $action = $status == 1 ? 'activated' : 'deactivated';
        $_SESSION['message'] = "Student $action successfully!";
        $_SESSION['message_type'] = 'success';
        header('Location: manage_students.php?msg=updated');
        exit();
    } else {
        $_SESSION['message'] = 'Error updating student status: ' . mysqli_error($conn);
        $_SESSION['message_type'] = 'danger';
        header('Location: manage_students.php');
        exit();
    }
    
    mysqli_stmt_close($stmt);
}

// Get student data
$sql = "SELECT * FROM students WHERE id = ?";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, 'i', $student_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

if (!$result || mysqli_num_rows($result) == 0) {
    $_SESSION['message'] = 'Student not found!';
    $_SESSION['message_type'] = 'danger';
    header('Location: manage_students.php');
    exit();
}

$student = mysqli_fetch_assoc($result);
mysqli_stmt_close($stmt);

include '../includes/header.php';
?>

<!-- Page Header -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="h3 mb-2"><?php echo $student['status'] == 1 ? 'Deactivate' : 'Activate'; ?> Student</h1>
        <p class="text-muted">Change student account status.</p>
    </div>
    <div>
        <a href="manage_students.php" class="btn btn-secondary">
            <i class="bi bi-arrow-left me-2"></i> Back to Students
        </a>
    </div>
</div>

<div class="row">
    <div class="col-lg-6 mx-auto">
        <div class="card shadow">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="bi bi-person me-2"></i> 
                    Student: <?php echo htmlspecialchars($student['full_name']); ?>
                </h5>
            </div>
            <div class="card-body">
                <div class="alert alert-info">
                    <i class="bi bi-info-circle me-2"></i>
                    <strong>Note:</strong> <?php echo $student['status'] == 1 ? 'Deactivating' : 'Activating'; ?> a student will <?php echo $student['status'] == 1 ? 'prevent' : 'allow'; ?> them from accessing the system.
                </div>
                
                <form method="POST" action="">
                    <div class="mb-3">
                        <label class="form-label">Current Status</label>
                        <div class="form-control bg-light">
                            <?php if ($student['status'] == 1): ?>
                                <span class="badge bg-success">
                                    <i class="bi bi-check-circle me-1"></i> Active
                                </span>
                                <small class="text-muted ms-2">- Student can login and access system</small>
                            <?php else: ?>
                                <span class="badge bg-danger">
                                    <i class="bi bi-x-circle me-1"></i> Inactive
                                </span>
                                <small class="text-muted ms-2">- Student cannot login</small>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Change Status To</label>
                        <div class="form-check mb-2">
                            <input class="form-check-input" type="radio" name="status" 
                                   value="active" id="statusActive"
                                   <?php echo $student['status'] == 0 ? 'checked' : ''; ?>>
                            <label class="form-check-label" for="statusActive">
                                <span class="badge bg-success">Active</span>
                                <small class="text-muted d-block mt-1">Student can login and use all features</small>
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="status" 
                                   value="inactive" id="statusInactive"
                                   <?php echo $student['status'] == 1 ? 'checked' : ''; ?>>
                            <label class="form-check-label" for="statusInactive">
                                <span class="badge bg-danger">Inactive</span>
                                <small class="text-muted d-block mt-1">Student cannot login to the system</small>
                            </label>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Reason for Change <span class="text-muted">(Optional)</span></label>
                        <textarea class="form-control" name="reason" rows="3" 
                                  placeholder="Enter reason for status change..."></textarea>
                        <small class="text-muted">This will be logged for audit purposes.</small>
                    </div>
                    
                    <div class="mt-4">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-save me-1"></i> Update Status
                        </button>
                        <a href="manage_students.php" class="btn btn-secondary">Cancel</a>
                        <a href="delete_student.php?id=<?php echo $student['id']; ?>" 
                           class="btn btn-outline-danger float-end">
                            <i class="bi bi-trash me-1"></i> Delete Instead
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include '../includes/footer.php'; ?>