<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1"><?= htmlspecialchars($client['raison_sociale']) ?></h1>
        <p class="text-muted mb-0"><?= htmlspecialchars($client['code_client']) ?> · <?= htmlspecialchars($client['ville'] ?: 'Ville non renseignée') ?></p>
    </div>
    <div class="d-flex gap-2">
        <a href="index.php?page=clients" class="btn btn-outline-secondary">← Liste</a>
        <a href="index.php?page=clients_edit&id=<?= (int)$client['id'] ?>" class="btn btn-darlink-primary">Modifier</a>
    </div>
</div>

<div class="row g-3 mb-4">
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted">CA annuel</div><div class="h4 mb-0">0 MAD</div></div></div>
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted">Marge HT</div><div class="h4 mb-0">0 MAD</div></div></div>
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted">Marge %</div><div class="h4 mb-0">0 %</div></div></div>
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted">Solde</div><div class="h4 mb-0">0 MAD</div></div></div>
</div>

<div class="row g-3">
    <div class="col-md-8">
        <div class="darlink-card p-4 mb-3">
            <h5>Informations client</h5>
            <div class="row mt-3">
                <div class="col-md-6"><strong>Contact :</strong> <?= htmlspecialchars($client['contact'] ?: '-') ?></div>
                <div class="col-md-6"><strong>Email :</strong> <?= htmlspecialchars($client['email'] ?: '-') ?></div>
                <div class="col-md-6"><strong>Téléphone :</strong> <?= htmlspecialchars($client['telephone'] ?: '-') ?></div>
                <div class="col-md-6"><strong>Mobile :</strong> <?= htmlspecialchars($client['mobile'] ?: '-') ?></div>
                <div class="col-md-12 mt-2"><strong>Adresse :</strong> <?= htmlspecialchars(trim(($client['adresse'] ?? '') . ' ' . ($client['code_postal'] ?? '') . ' ' . ($client['ville'] ?? '')) ?: '-') ?></div>
            </div>
        </div>

        <div class="darlink-card p-4">
            <h5>Informations commerciales</h5>
            <div class="row mt-3">
                <div class="col-md-4"><strong>Remise :</strong> <?= htmlspecialchars((string)$client['remise']) ?> %</div>
                <div class="col-md-4"><strong>Règlement :</strong> <?= htmlspecialchars($client['mode_reglement'] ?: '-') ?></div>
                <div class="col-md-4"><strong>Délai :</strong> <?= (int)$client['delai_paiement'] ?> jours</div>
                <div class="col-md-4 mt-2"><strong>Plafond :</strong> <?= number_format((float)$client['plafond_credit'], 2, ',', ' ') ?> MAD</div>
                <div class="col-md-4 mt-2"><strong>ICE :</strong> <?= htmlspecialchars($client['ice'] ?: '-') ?></div>
                <div class="col-md-4 mt-2"><strong>IF :</strong> <?= htmlspecialchars($client['identifiant_fiscal'] ?: '-') ?></div>
            </div>
        </div>

        <div class="darlink-card p-4 mt-3">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="mb-0">Tarifs spécifiques client</h5>
                <span class="badge text-bg-light border"><?= count($tarifsClients ?? []) ?> tarif(s)</span>
            </div>

            <form method="post" action="index.php?page=clients_tarifs_store" class="row g-2 align-items-end mb-4">
                <input type="hidden" name="client_id" value="<?= (int) $client['id'] ?>">
                <div class="col-md-7">
                    <label class="form-label">Article</label>
                    <select name="article_id" class="form-select" required>
                        <option value="">Sélectionner un article...</option>
                        <?php foreach (($articlesTarifs ?? []) as $article): ?>
                            <option value="<?= (int) $article['id'] ?>">
                                <?= htmlspecialchars($article['reference'] . ' - ' . $article['designation']) ?>
                                — standard <?= number_format((float) $article['prix_vente_ht'], 2, ',', ' ') ?> MAD
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Prix HT client</label>
                    <input type="number" step="0.01" min="0" name="prix_ht" class="form-control" required>
                </div>
                <div class="col-md-2">
                    <button class="btn darlink-btn-primary w-100" type="submit">Ajouter</button>
                </div>
            </form>

            <div class="table-responsive">
                <table class="table table-hover align-middle darlink-table">
                    <thead>
                    <tr>
                        <th>Réf.</th>
                        <th>Article</th>
                        <th class="text-end">Prix standard</th>
                        <th class="text-end">Prix client</th>
                        <th class="text-end">Action</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php if (empty($tarifsClients)): ?>
                        <tr><td colspan="5" class="text-center text-muted py-4">Aucun tarif spécifique pour ce client.</td></tr>
                    <?php endif; ?>
                    <?php foreach (($tarifsClients ?? []) as $tarif): ?>
                        <tr>
                            <td><strong><?= htmlspecialchars($tarif['reference']) ?></strong></td>
                            <td><?= htmlspecialchars($tarif['designation']) ?></td>
                            <td class="text-end"><?= number_format((float) $tarif['prix_standard_ht'], 2, ',', ' ') ?> MAD</td>
                            <td class="text-end"><strong><?= number_format((float) $tarif['prix_ht'], 2, ',', ' ') ?> MAD</strong></td>
                            <td class="text-end">
                                <form method="post" action="index.php?page=clients_tarifs_delete" class="d-inline" onsubmit="return confirm('Supprimer ce tarif client ?');">
                                    <input type="hidden" name="id" value="<?= (int) $tarif['id'] ?>">
                                    <input type="hidden" name="client_id" value="<?= (int) $client['id'] ?>">
                                    <button class="btn btn-sm btn-outline-danger" type="submit">Supprimer</button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="col-md-4">
        <div class="darlink-card p-4">
            <h5>Historique</h5>
            <?php if (empty($history)): ?>
                <p class="text-muted mb-0">Aucun historique.</p>
            <?php endif; ?>
            <?php foreach ($history as $item): ?>
                <div class="border-bottom py-2">
                    <div><strong><?= htmlspecialchars($item['action']) ?></strong></div>
                    <div class="small text-muted"><?= htmlspecialchars($item['date_action']) ?></div>
                    <div class="small darlink-history-comment"><?= nl2br(htmlspecialchars($item['commentaire'] ?? '')) ?></div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
