<?php
$formatMoney = static fn($value): string => number_format((float)$value, 2, ',', ' ') . ' DH';
$sourceLabel = $documentManager->typeLabel($sourceType);
$targetLabel = $documentManager->typeLabel($targetType);
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">Créer <?= htmlspecialchars(strtolower($targetLabel)) ?> depuis <?= htmlspecialchars(strtolower($sourceLabel)) ?></h1>
        <p class="text-muted mb-0">Sélectionnez le document source à transformer. Le client, les lignes, les prix et les observations seront repris automatiquement.</p>
    </div>
    <a href="index.php?page=documents" class="btn btn-outline-secondary">Retour</a>
</div>

<div class="darlink-card p-4">
    <div class="table-responsive">
        <table class="table table-hover darlink-table align-middle">
            <thead>
            <tr>
                <th>Numéro</th>
                <th>Client</th>
                <th>Date</th>
                <th>Statut</th>
                <th class="text-end">Total TTC</th>
                <th>État</th>
                <th class="text-end">Action</th>
            </tr>
            </thead>
            <tbody>
            <?php if (empty($documents)): ?>
                <tr><td colspan="7" class="text-center text-muted py-4">Aucun document source disponible.</td></tr>
            <?php endif; ?>
            <?php foreach ($documents as $document): ?>
                <?php $alreadyTransformed = !empty($document['existing_child_id']); ?>
                <tr>
                    <td><strong><?= htmlspecialchars($document['numero']) ?></strong></td>
                    <td><?= htmlspecialchars($document['client_nom']) ?></td>
                    <td><?= htmlspecialchars(date('d/m/Y', strtotime((string)$document['date_document']))) ?></td>
                    <td><?= htmlspecialchars((string)((($document['type_document'] ?? '') === 'FACTURE' && ($document['statut'] ?? '') === 'émise') ? 'En cours' : ($document['statut'] ?? '-'))) ?></td>
                    <td class="text-end"><strong><?= $formatMoney($document['total_ttc']) ?></strong></td>
                    <td>
                        <?php if ($alreadyTransformed): ?>
                            <span class="badge bg-secondary">Déjà transformé : <?= htmlspecialchars((string)$document['existing_child_numero']) ?></span>
                        <?php else: ?>
                            <span class="badge bg-success">Disponible</span>
                        <?php endif; ?>
                    </td>
                    <td class="text-end">
                        <?php if ($alreadyTransformed): ?>
                            <a href="index.php?page=documents_show&id=<?= (int)$document['existing_child_id'] ?>" class="btn btn-sm btn-outline-primary">Voir document créé</a>
                        <?php else: ?>
                            <form method="post" action="index.php?page=documents_transform" class="d-inline">
                                <input type="hidden" name="id" value="<?= (int)$document['id'] ?>">
                                <button type="submit" name="target_type" value="<?= htmlspecialchars($targetType) ?>" class="btn btn-sm darlink-btn-primary">Créer <?= htmlspecialchars($targetLabel) ?></button>
                            </form>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
