<?php
// Diagnostic file to check the teacher leave system
session_start();

echo "<h1>Teacher Leave System Diagnosis</h1>";

// Check if required files exist
$required_files = [
    'config/database.php',
    'includes/auth.php',
    'teachers/leave_application.php',
    'admin/leave_applications.php'
];

echo "<h2>File Existence Check:</h2>";
foreach ($required_files as $file) {
    $exists = file_exists($file);
    $status = $exists ? '✓ EXISTS' : '✗ MISSING';
    $color = $exists ? 'green' : 'red';
    echo "<p style='color: $color;'>$file: $status</p>";
}

// Check database connection
echo "<h2>Database Connection:</h2>";
try {
    require_once 'config/database.php';
    echo "<p style='color: green;'>✓ Database connection successful</p>";
    
    // Check if teacher_leaves table exists
    echo "<h2>Database Table Check:</h2>";
    try {
        $stmt = $pdo->query("SHOW TABLES LIKE 'teacher_leaves'");
        if ($stmt->rowCount() > 0) {
            echo "<p style='color: green;'>✓ teacher_leaves table exists</p>";
            
            // Show table info
            $stmt = $pdo->query("DESCRIBE teacher_leaves");
            $columns = $stmt->fetchAll();
            echo "<h3>Table Structure:</h3><ul>";
            foreach ($columns as $column) {
                echo "<li>{$column['Field']} ({$column['Type']})</li>";
            }
            echo "</ul>";
        } else {
            echo "<p style='color: red;'>✗ teacher_leaves table does NOT exist</p>";
            echo "<p>Run the create_leave_table.php script through your web browser to create the table.</p>";
        }
    } catch (Exception $e) {
        echo "<p style='color: red;'>✗ Error checking table: " . $e->getMessage() . "</p>";
    }
    
} catch (Exception $e) {
    echo "<p style='color: red;'>✗ Database connection failed: " . $e->getMessage() . "</p>";
}

// Check session
echo "<h2>Session Information:</h2>";
if (isset($_SESSION['user_id'])) {
    echo "<p style='color: green;'>✓ User is logged in</p>";
    echo "<p>User ID: {$_SESSION['user_id']}</p>";
    echo "<p>User Role: {$_SESSION['user_role']}</p>";
    echo "<p>Username: {$_SESSION['username']}</p>";
} else {
    echo "<p style='color: orange;'>User is NOT logged in. You need to log in to access the leave application page.</p>";
}

echo "<h2>Access Instructions:</h2>";
echo "<p>To access the teacher leave application:</p>";
echo "<ol>";
echo "<li>Start your MAMP server</li>";
echo "<li>Access the URL: http://localhost/bidda/</li>";
echo "<li>Login as a teacher account</li>";
echo "<li>Go to: http://localhost/bidda/teachers/leave_application.php</li>";
echo "</ol>";

echo "<h2>Available Links:</h2>";
echo "<ul>";
echo "<li><a href='create_leave_table.php'>Create/Verify Leave Table</a> (Run this first to ensure table exists)</li>";
echo "<li><a href='teachers/leave_application.php'>Teacher Leave Application</a> (Login as teacher required)</li>";
echo "<li><a href='admin/leave_applications.php'>Admin Leave Approval Panel</a> (Login as admin required)</li>";
echo "</ul>";

echo "<h2>Troubleshooting Tips:</h2>";
echo "<ul>";
echo "<li>Make sure MAMP Apache and MySQL servers are running</li>";
echo "<li>Ensure you're logged in with the correct role (teacher for leave application, admin for approval)</li>";
echo "<li>If getting 'page not found', make sure the file exists in the correct directory</li>";
echo "<li>Check that the database table was created (run create_leave_table.php first)</li>";
echo "</ul>";
?>