<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student Data Download</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f4f4f4;
        }
        .container {
            background-color: #fff;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
        }
        select, input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-sizing: border-box;
            font-size: 16px;
        }
        .download-btn {
            display: block;
            width: 100%;
            margin: 30px auto;
            padding: 15px;
            background-color: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            font-size: 18px;
            text-align: center;
            cursor: pointer;
            border: none;
            transition: background-color 0.3s;
        }
        .download-btn:hover {
            background-color: #0056b3;
        }
        .info {
            margin-top: 30px;
            padding: 20px;
            background-color: #e9ecef;
            border-radius: 5px;
        }
        .info h3 {
            margin-top: 0;
            color: #333;
        }
        .info ul {
            padding-left: 20px;
        }
        .info li {
            margin-bottom: 8px;
        }
        .filter-row {
            display: flex;
            gap: 15px;
            margin-bottom: 15px;
        }
        .filter-col {
            flex: 1;
        }
        @media (max-width: 768px) {
            .filter-row {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Student Data Download</h1>
        <p style="text-align: center; color: #666;">This page allows you to download student data in CSV format with filters.</p>
        
        <form method="GET" action="download_csv.php">
            <div class="filter-row">
                <div class="filter-col">
                    <div class="form-group">
                        <label for="session">Session:</label>
                        <select name="session" id="session">
                            <option value="">All Sessions</option>
                            <?php
                            require_once 'config/database.php';
                            
                            // Get unique sessions from the database
                            try {
                                $stmt = $pdo->query("SELECT DISTINCT session FROM students WHERE session IS NOT NULL AND session != '' ORDER BY session DESC");
                                $sessions = $stmt->fetchAll(PDO::FETCH_COLUMN);
                                
                                foreach ($sessions as $session) {
                                    echo '<option value="' . htmlspecialchars($session) . '">' . htmlspecialchars($session) . '</option>';
                                }
                            } catch (Exception $e) {
                                // If there's an error, show some default options
                                echo '<option value="2025">2025</option>';
                                echo '<option value="2024">2024</option>';
                                echo '<option value="2023">2023</option>';
                            }
                            ?>
                        </select>
                    </div>
                </div>
                
                <div class="filter-col">
                    <div class="form-group">
                        <label for="class_name">Class:</label>
                        <select name="class_name" id="class_name">
                            <option value="">All Classes</option>
                            <?php
                            // Get unique classes from the database
                            try {
                                $stmt = $pdo->query("
                                    SELECT DISTINCT c.name 
                                    FROM classes c 
                                    JOIN students s ON c.id = s.class_id 
                                    WHERE c.name IS NOT NULL 
                                    ORDER BY c.name
                                ");
                                $classes = $stmt->fetchAll(PDO::FETCH_COLUMN);
                                
                                foreach ($classes as $class) {
                                    echo '<option value="' . htmlspecialchars($class) . '">' . htmlspecialchars($class) . '</option>';
                                }
                            } catch (Exception $e) {
                                // If there's an error, show some default options
                                echo '<option value="One">One</option>';
                                echo '<option value="Two">Two</option>';
                                echo '<option value="Three">Three</option>';
                            }
                            ?>
                        </select>
                    </div>
                </div>
                
                <div class="filter-col">
                    <div class="form-group">
                        <label for="group_name">Group (Optional):</label>
                        <select name="group_name" id="group_name">
                            <option value="">All Groups</option>
                            <?php
                            // Get unique groups from the database
                            try {
                                $stmt = $pdo->query("SELECT DISTINCT group_name FROM students WHERE group_name IS NOT NULL AND group_name != '' ORDER BY group_name");
                                $groups = $stmt->fetchAll(PDO::FETCH_COLUMN);
                                
                                foreach ($groups as $group) {
                                    echo '<option value="' . htmlspecialchars($group) . '">' . htmlspecialchars($group) . '</option>';
                                }
                            } catch (Exception $e) {
                                // If there's an error, show some default options
                                echo '<option value="A">Group A</option>';
                                echo '<option value="B">Group B</option>';
                                echo '<option value="C">Group C</option>';
                            }
                            ?>
                        </select>
                    </div>
                </div>
            </div>
            
            <button type="submit" class="download-btn">
                <i class="fas fa-download"></i> Download CSV File
            </button>
        </form>
        
        <div class="info">
            <h3>CSV File Contains:</h3>
            <ul>
                <li>first_name</li>
                <li>last_name</li>
                <li>father_name</li>
                <li>mother_name</li>
                <li>email</li>
                <li>phone</li>
                <li>address</li>
                <li>date_of_birth</li>
                <li>gender</li>
                <li>class_name</li>
                <li>session</li>
                <li>group_name</li>
                <li>roll_number</li>
                <li>admission_date</li>
                <li>guardian_name</li>
                <li>guardian_phone</li>
                <li>guardian_email</li>
                <li>guardian_address</li>
                <li>subject_codes</li>
            </ul>
        </div>
    </div>
</body>
</html>