<?php
$isEdit = ($mode ?? 'create') === 'edit';
$action = $isEdit ? 'clients_update' : 'clients_store';

function client_value(array $client, string $key, string $default = ''): string {
    return htmlspecialchars((string)($client[$key] ?? $default));
}

function client_error(array $errors, string $key): string {
    return isset($errors[$key]) ? '<div class="invalid-feedback d-block">' . htmlspecialchars($errors[$key]) . '</div>' : '';
}

function client_is_invalid(array $errors, string $key): string {
    return isset($errors[$key]) ? ' is-invalid' : '';
}
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">
            <i class="bi bi-person-vcard me-2"></i><?= $isEdit ? 'Modifier le client' : 'Nouveau client' ?>
        </h1>
        <p class="text-muted mb-0">Fiche commerciale client.</p>
    </div>

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

<form method="post" action="index.php?page=<?= $action ?>" class="darlink-form-card" novalidate>
    <?php if ($isEdit): ?>
        <input type="hidden" name="id" value="<?= (int)($client['id'] ?? 0) ?>">
    <?php endif; ?>

    <div class="darlink-form-header d-flex justify-content-between align-items-center">
        <div>
            <div class="text-muted small">Code client</div>
            <div class="h4 mb-0"><?= client_value($client, 'code_client', 'Automatique') ?></div>
        </div>
        <span class="darlink-badge darlink-badge-primary">DarlinkForm v1.0</span>
    </div>

    <div class="darlink-form-body">

        <div class="darlink-section-title">Informations générales</div>

        <div class="row g-3">
            <div class="col-md-3 darlink-form-group">
                <label class="darlink-label">Type de client *</label>
                <select name="type_client_id" class="darlink-select<?= client_is_invalid($errors, 'type_client_id') ?>" required>
                    <option value="">Sélectionner...</option>
                    <?php foreach ($typesClients as $type): ?>
                        <option value="<?= (int)$type['id'] ?>" <?= (string)($client['type_client_id'] ?? '') === (string)$type['id'] ? 'selected' : '' ?>>
                            <?= htmlspecialchars($type['libelle']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
                <?= client_error($errors, 'type_client_id') ?>
            </div>

            <div class="col-md-3 darlink-form-group">
                <label class="darlink-label">Code client *</label>
                <input name="code_client" class="darlink-input<?= client_is_invalid($errors, 'code_client') ?>" value="<?= client_value($client, 'code_client') ?>" readonly>
                <?= client_error($errors, 'code_client') ?>
            </div>

            <div class="col-md-6 darlink-form-group">
                <label class="darlink-label">Raison sociale *</label>
                <input name="raison_sociale" class="darlink-input<?= client_is_invalid($errors, 'raison_sociale') ?>" value="<?= client_value($client, 'raison_sociale') ?>" autofocus required placeholder="Ex. Hôtel Atlas">
                <?= client_error($errors, 'raison_sociale') ?>
            </div>

            <div class="col-md-4 darlink-form-group">
                <label class="darlink-label">Contact</label>
                <input name="contact" class="darlink-input" value="<?= client_value($client, 'contact') ?>" placeholder="Responsable achats">
            </div>

            <div class="col-md-4 darlink-form-group">
                <label class="darlink-label">Téléphone *</label>
                <input name="telephone" class="darlink-input<?= client_is_invalid($errors, 'telephone') ?>" value="<?= client_value($client, 'telephone') ?>" placeholder="05...">
                <?= client_error($errors, 'telephone') ?>
            </div>

            <div class="col-md-4 darlink-form-group">
                <label class="darlink-label">Mobile / WhatsApp *</label>
                <input name="mobile" class="darlink-input<?= client_is_invalid($errors, 'mobile') ?>" value="<?= client_value($client, 'mobile') ?>" placeholder="06...">
                <?= client_error($errors, 'mobile') ?>
            </div>

            <div class="col-md-6 darlink-form-group">
                <label class="darlink-label">E-mail</label>
                <input name="email" type="email" class="darlink-input<?= client_is_invalid($errors, 'email') ?>" value="<?= client_value($client, 'email') ?>" placeholder="contact@client.ma">
                <?= client_error($errors, 'email') ?>
            </div>

            <div class="col-md-6 darlink-form-group">
                <label class="darlink-label">Site web</label>
                <input name="site_web" class="darlink-input" value="<?= client_value($client, 'site_web') ?>">
            </div>
        </div>

        <div class="darlink-section-title mt-4">Adresse</div>

        <div class="row g-3">
            <div class="col-md-12 darlink-form-group">
                <label class="darlink-label">Adresse</label>
                <input name="adresse" class="darlink-input" value="<?= client_value($client, 'adresse') ?>">
            </div>

            <div class="col-md-3 darlink-form-group">
                <label class="darlink-label">Code postal</label>
                <input name="code_postal" class="darlink-input" value="<?= client_value($client, 'code_postal') ?>">
            </div>

            <div class="col-md-5 darlink-form-group">
                <label class="darlink-label">Ville</label>
                <input name="ville" class="darlink-input" value="<?= client_value($client, 'ville') ?>" placeholder="Casablanca, Marrakech, Agadir...">
            </div>

            <div class="col-md-4 darlink-form-group">
                <label class="darlink-label">Pays</label>
                <input name="pays" class="darlink-input" value="<?= client_value($client, 'pays', 'Maroc') ?>">
            </div>
        </div>

        <div class="darlink-section-title mt-4">Informations fiscales</div>

        <div class="row g-3">
            <div class="col-md-4 darlink-form-group">
                <label class="darlink-label">ICE</label>
                <input name="ice" class="darlink-input" value="<?= client_value($client, 'ice') ?>">
            </div>

            <div class="col-md-4 darlink-form-group">
                <label class="darlink-label">Identifiant fiscal</label>
                <input name="identifiant_fiscal" class="darlink-input" value="<?= client_value($client, 'identifiant_fiscal') ?>">
            </div>

            <div class="col-md-4 darlink-form-group">
                <label class="darlink-label">Registre de commerce</label>
                <input name="registre_commerce" class="darlink-input" value="<?= client_value($client, 'registre_commerce') ?>">
            </div>
        </div>

        <div class="darlink-section-title mt-4">Informations commerciales</div>

        <div class="row g-3">
            <div class="col-md-3 darlink-form-group">
                <label class="darlink-label">Commercial</label>
                <input name="commercial" class="darlink-input" value="<?= client_value($client, 'commercial') ?>" placeholder="Admin">
            </div>

            <div class="col-md-2 darlink-form-group">
                <label class="darlink-label">Remise %</label>
                <input name="remise" type="number" min="0" max="100" step="0.01" class="darlink-input" value="<?= client_value($client, 'remise', '0.00') ?>">
            </div>

            <div class="col-md-3 darlink-form-group">
                <label class="darlink-label">Mode règlement</label>
                <input name="mode_reglement" class="darlink-input" placeholder="Comptant, 30 jours..." value="<?= client_value($client, 'mode_reglement') ?>">
            </div>

            <div class="col-md-2 darlink-form-group">
                <label class="darlink-label">Délai jours</label>
                <input name="delai_paiement" type="number" min="0" class="darlink-input" value="<?= client_value($client, 'delai_paiement', '0') ?>">
            </div>

            <div class="col-md-2 darlink-form-group">
                <label class="darlink-label">Plafond crédit</label>
                <input name="plafond_credit" type="number" min="0" step="0.01" class="darlink-input" value="<?= client_value($client, 'plafond_credit', '0.00') ?>">
            </div>
        </div>

        <div class="darlink-section-title mt-4">Observations</div>

        <div class="darlink-form-group mb-0">
            <textarea name="observations" class="darlink-textarea" placeholder="Notes internes, horaires de livraison, habitudes de commande..."><?= client_value($client, 'observations') ?></textarea>
        </div>

    </div>

    <div class="darlink-form-actions">
        <a href="index.php?page=clients" class="btn btn-outline-secondary">
            Annuler
        </a>
        <button type="submit" class="btn darlink-btn-primary">
            <i class="bi bi-check-lg me-1"></i> Enregistrer
        </button>
    </div>
</form>