<?php
$previewClients = $preview['clients'] ?? [];
$previewErrors = $preview['errors'] ?? [];
$sampleClients = array_slice($previewClients, 0, 20);
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">Import clients Excel</h1>
        <p class="text-muted mb-0">Import de votre fichier clients Darlink au format .xlsx</p>
    </div>
    <a href="index.php?page=clients" class="btn btn-outline-secondary">← Retour clients</a>
</div>

<?php if (!empty($errors)): ?>
    <div class="alert alert-danger">
        <?php foreach ($errors as $error): ?>
            <div><?= htmlspecialchars($error) ?></div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

<?php if ($result): ?>
    <div class="darlink-card p-4 mb-4">
        <h5 class="mb-3">Résultat de l'import</h5>
        <div class="row g-3">
            <div class="col-md-3"><div class="darlink-stat"><span>Créés</span><strong><?= (int)$result['created'] ?></strong></div></div>
            <div class="col-md-3"><div class="darlink-stat"><span>Mis à jour</span><strong><?= (int)$result['updated'] ?></strong></div></div>
            <div class="col-md-3"><div class="darlink-stat"><span>Ignorés</span><strong><?= (int)$result['ignored'] ?></strong></div></div>
            <div class="col-md-3"><div class="darlink-stat"><span>Erreurs</span><strong><?= count($result['errors'] ?? []) ?></strong></div></div>
        </div>
        <?php if (!empty($result['errors'])): ?>
            <div class="alert alert-warning mt-3 mb-0">
                <?php foreach ($result['errors'] as $error): ?>
                    <div><?= htmlspecialchars($error) ?></div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
        <div class="mt-4">
            <a href="index.php?page=clients" class="btn darlink-btn-primary">Voir la liste des clients</a>
        </div>
    </div>
<?php endif; ?>

<div class="darlink-card p-4 mb-4">
    <h5 class="mb-3">1. Choisir le fichier Excel</h5>
    <p class="text-muted">Colonnes reconnues : CdClient, Société, Nom, Adresse, Code postal, Ville, Téléphone, Mobile, E-Mail.</p>

    <form method="post" action="index.php?page=clients_import_preview" enctype="multipart/form-data" class="row g-3 align-items-end">
        <div class="col-md-8">
            <label class="form-label">Fichier clients (.xlsx)</label>
            <input type="file" name="client_file" class="form-control" accept=".xlsx" required>
        </div>
        <div class="col-md-4">
            <button type="submit" class="btn darlink-btn-primary w-100">Prévisualiser</button>
        </div>
    </form>
</div>

<?php if ($preview): ?>
    <div class="darlink-card p-4 mb-4">
        <h5 class="mb-3">2. Prévisualisation</h5>
        <div class="row g-3 mb-4">
            <div class="col-md-3"><div class="darlink-stat"><span>Lignes détectées</span><strong><?= (int)$preview['total'] ?></strong></div></div>
            <div class="col-md-3"><div class="darlink-stat"><span>Clients valides</span><strong><?= (int)$preview['valid'] ?></strong></div></div>
            <div class="col-md-3"><div class="darlink-stat"><span>Anomalies</span><strong><?= count($previewErrors) ?></strong></div></div>
            <div class="col-md-3"><div class="darlink-stat"><span>Aperçu</span><strong><?= count($sampleClients) ?></strong></div></div>
        </div>

        <?php if (!empty($previewErrors)): ?>
            <div class="alert alert-warning">
                <strong>Lignes à vérifier :</strong>
                <?php foreach (array_slice($previewErrors, 0, 20) as $error): ?>
                    <div><?= htmlspecialchars($error) ?></div>
                <?php endforeach; ?>
                <?php if (count($previewErrors) > 20): ?>
                    <div>... <?= count($previewErrors) - 20 ?> autre(s) anomalie(s)</div>
                <?php endif; ?>
            </div>
        <?php endif; ?>

        <div class="table-responsive">
            <table class="table table-hover align-middle darlink-table">
                <thead>
                <tr>
                    <th>Code</th>
                    <th>Raison sociale</th>
                    <th>Contact</th>
                    <th>Ville</th>
                    <th>Téléphone</th>
                    <th>Mobile</th>
                    <th>Email</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($sampleClients as $client): ?>
                    <tr>
                        <td><strong><?= htmlspecialchars($client['code_client']) ?></strong></td>
                        <td><?= htmlspecialchars($client['raison_sociale']) ?></td>
                        <td><?= htmlspecialchars($client['contact'] ?: '-') ?></td>
                        <td><?= htmlspecialchars($client['ville'] ?: '-') ?></td>
                        <td><?= htmlspecialchars($client['telephone'] ?: '-') ?></td>
                        <td><?= htmlspecialchars($client['mobile'] ?: '-') ?></td>
                        <td><?= htmlspecialchars($client['email'] ?: '-') ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <?php if (!empty($previewClients)): ?>
        <div class="darlink-card p-4">
            <h5 class="mb-3">3. Confirmer l'import</h5>
            <form method="post" action="index.php?page=clients_import_execute">
                <input type="hidden" name="token" value="<?= htmlspecialchars((string)$token) ?>">

                <div class="mb-3">
                    <label class="form-label">Mode d'import</label>
                    <select name="mode" class="form-select">
                        <option value="skip">Ajouter les nouveaux et ignorer les clients déjà existants</option>
                        <option value="update">Ajouter les nouveaux et mettre à jour les clients existants</option>
                    </select>
                </div>

                <div class="alert alert-info">
                    Les doublons sont détectés avec le <strong>code client</strong>. Exemple : le code Excel 1 devient CL000001.
                </div>

                <div class="d-flex justify-content-end gap-2">
                    <a href="index.php?page=clients" class="btn btn-outline-secondary">Annuler</a>
                    <button type="submit" class="btn darlink-btn-primary">Importer les clients</button>
                </div>
            </form>
        </div>
    <?php endif; ?>
<?php endif; ?>
