<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['test_csv'])) {
    $file = $_FILES['test_csv'];
    
    if ($file['error'] === UPLOAD_ERR_OK) {
        $handle = fopen($file['tmp_name'], "r");
        $headers = fgetcsv($handle);
        
        $sampleData = [];
        for ($i = 0; $i < 5; $i++) {
            $row = fgetcsv($handle);
            if ($row !== FALSE) {
                $sampleData[] = $row;
            }
        }
        fclose($handle);
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Check CSV Format</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <h3>Check Your CSV Format</h3>
        
        <form method="POST" enctype="multipart/form-data" class="mb-4">
            <div class="mb-3">
                <label class="form-label">Upload a sample of your CSV</label>
                <input type="file" name="test_csv" class="form-control" accept=".csv" required>
            </div>
            <button type="submit" class="btn btn-primary">Check Format</button>
        </form>
        
        <?php if (isset($headers) && isset($sampleData)): ?>
        <div class="alert alert-info">
            <h5>CSV Analysis:</h5>
            <p><strong>Headers found:</strong> <?php echo implode(', ', $headers); ?></p>
            
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <?php foreach ($headers as $header): ?>
                            <th><?php echo $header; ?></th>
                        <?php endforeach; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($sampleData as $row): ?>
                    <tr>
                        <?php foreach ($row as $cell): ?>
                            <td>
                                <?php 
                                if (strlen($cell) > 50) {
                                    echo substr($cell, 0, 50) . '... [' . strlen($cell) . ' chars]';
                                } else {
                                    echo $cell;
                                }
                                ?>
                            </td>
                        <?php endforeach; ?>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            
            <?php 
            $appnumIndex = array_search('Appnum', $headers);
            $photoIndex = array_search('StdImage', $headers);
            
            if ($appnumIndex !== false && $photoIndex !== false): 
            ?>
                <div class="alert alert-success">
                    <i class="fas fa-check-circle"></i> CSV format is correct! Found both required columns.
                </div>
            <?php else: ?>
                <div class="alert alert-danger">
                    <i class="fas fa-exclamation-triangle"></i> Missing required columns!
                    <ul>
                        <li>Appnum found: <?php echo $appnumIndex !== false ? 'Yes' : 'No'; ?></li>
                        <li>StdImage found: <?php echo $photoIndex !== false ? 'Yes' : 'No'; ?></li>
                    </ul>
                </div>
            <?php endif; ?>
        </div>
        <?php endif; ?>
    </div>
</body>
</html>