<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">
            <i class="bi bi-percent me-2"></i>TVA
        </h1>
        <p class="text-muted mb-0">
            Gérer les taux de TVA utilisés dans Darlink ERP.
        </p>
    </div>

    <a href="index.php?page=parametres" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left me-1"></i> Retour aux paramètres
    </a>
</div>

<div class="darlink-card darlink-form-card mb-4">
    <div class="darlink-form-header">
        <h2 class="h5 mb-0">Nouveau taux de TVA</h2>
    </div>

    <form method="post" action="index.php?page=parametres_taux_tva_store">
        <div class="darlink-form-body">
            <div class="row g-3 align-items-end">
                <div class="col-md-5 darlink-form-group mb-0">
                    <label class="darlink-label">Libellé</label>
                    <input
                        name="libelle"
                        class="darlink-input"
                        placeholder="Ex. TVA 20 %"
                        required
                    >
                </div>

                <div class="col-md-3 darlink-form-group mb-0">
                    <label class="darlink-label">Taux</label>
                    <input
                        type="number"
                        name="taux"
                        class="darlink-input text-end"
                        min="0"
                        max="100"
                        step="0.01"
                        placeholder="20,00"
                        required
                    >
                </div>

                <div class="col-md-2 darlink-form-group mb-0">
                    <label class="darlink-label">Par défaut</label>
                    <select name="par_defaut" class="darlink-select">
                        <option value="0">Non</option>
                        <option value="1">Oui</option>
                    </select>
                </div>

                <div class="col-md-2">
                    <button type="submit" class="btn darlink-btn-primary w-100">
                        <i class="bi bi-plus-lg me-1"></i> Ajouter
                    </button>
                </div>
            </div>
        </div>
    </form>
</div>

<div class="darlink-card darlink-table-card">
    <div class="darlink-table-toolbar">
        <div class="darlink-table-toolbar-left">
            <strong>Liste des taux de TVA</strong>
        </div>

        <div class="darlink-table-toolbar-right">
            <span class="text-muted small">
                <?= count($tauxTva) ?> taux
            </span>
        </div>
    </div>

    <div class="table-responsive darlink-table-wrapper">
        <table class="table align-middle darlink-table darlink-table-modern">
            <thead>
            <tr>
                <th>Libellé</th>
                <th style="width:150px;">Taux</th>
                <th style="width:150px;">Défaut</th>
                <th style="width:160px;">État</th>
                <th class="text-end" style="width:180px;">Actions</th>
            </tr>
            </thead>

            <tbody>
            <?php if (empty($tauxTva)): ?>
                <tr>
                    <td colspan="5" class="text-center text-muted py-5">
                        Aucun taux de TVA enregistré.
                    </td>
                </tr>
            <?php endif; ?>

            <?php foreach ($tauxTva as $tva): ?>
                <tr>
                    <td>
                        <form
                            method="post"
                            action="index.php?page=parametres_taux_tva_update"
                            class="d-flex gap-2"
                        >
                            <input
                                type="hidden"
                                name="id"
                                value="<?= (int)$tva['id'] ?>"
                            >

                            <input
                                name="libelle"
                                class="darlink-input"
                                value="<?= htmlspecialchars($tva['libelle']) ?>"
                                required
                            >
                    </td>

                    <td>
                        <input
                            type="number"
                            name="taux"
                            class="darlink-input text-end"
                            min="0"
                            max="100"
                            step="0.01"
                            value="<?= htmlspecialchars((string)$tva['taux']) ?>"
                            required
                        >
                    </td>

                    <td>
                        <?php if ((int)$tva['par_defaut'] === 1): ?>
                            <span class="darlink-badge darlink-badge-success">
                                <i class="bi bi-star-fill me-1"></i> Par défaut
                            </span>
                        <?php else: ?>
                            <span class="darlink-badge darlink-badge-light">
                                Standard
                            </span>
                        <?php endif; ?>

                        <input
                            type="hidden"
                            name="par_defaut"
                            value="<?= (int)$tva['par_defaut'] ?>"
                        >
                    </td>

                    <td>
                        <select name="actif" class="darlink-select">
                            <option value="1" <?= (int)$tva['actif'] === 1 ? 'selected' : '' ?>>
                                Actif
                            </option>
                            <option value="0" <?= (int)$tva['actif'] === 0 ? 'selected' : '' ?>>
                                Inactif
                            </option>
                        </select>
                    </td>

                    <td class="text-end darlink-actions">
                            <button
                                type="submit"
                                class="darlink-icon-btn darlink-icon-btn-edit"
                                title="Enregistrer"
                            >
                                <i class="bi bi-check-lg"></i>
                            </button>
                        </form>

                        <?php if ((int)$tva['par_defaut'] !== 1): ?>
                            <form
                                method="post"
                                action="index.php?page=parametres_taux_tva_default"
                                class="d-inline"
                            >
                                <input
                                    type="hidden"
                                    name="id"
                                    value="<?= (int)$tva['id'] ?>"
                                >

                                <button
                                    type="submit"
                                    class="darlink-icon-btn darlink-icon-btn-order"
                                    title="Définir par défaut"
                                >
                                    <i class="bi bi-star"></i>
                                </button>
                            </form>
                        <?php endif; ?>

                        <form
                            method="post"
                            action="index.php?page=parametres_taux_tva_delete"
                            class="d-inline"
                            onsubmit="return confirm('Désactiver ce taux de TVA ?');"
                        >
                            <input
                                type="hidden"
                                name="id"
                                value="<?= (int)$tva['id'] ?>"
                            >

                            <button
                                type="submit"
                                class="darlink-icon-btn darlink-icon-btn-delete"
                                title="Désactiver"
                            >
                                <i class="bi bi-trash"></i>
                            </button>
                        </form>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>