<?php
function setting_value(array $settings, string $key, string $default = ''): string {
    return htmlspecialchars((string)($settings[$key] ?? $default));
}
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">Paramètres société</h1>
        <p class="text-muted mb-0">Ces informations alimentent automatiquement les PDF : factures, devis, BL, avoirs.</p>
    </div>
    <a href="index.php?page=dashboard" class="btn btn-outline-secondary">Retour</a>
</div>

<?php if (!empty($errors['general'])): ?>
    <div class="alert alert-danger"><?= htmlspecialchars($errors['general']) ?></div>
<?php endif; ?>

<form method="post" action="index.php?page=parametres_societe_save" class="darlink-card p-4">
    <div class="darlink-section-title">Identité</div>
    <div class="row g-3 mb-4">
        <div class="col-md-6">
            <label class="form-label">Raison sociale *</label>
            <input name="raison_sociale" class="form-control form-darlink" required value="<?= setting_value($settings, 'raison_sociale', 'Darlink ERP') ?>">
        </div>
        <div class="col-md-6">
            <label class="form-label">Nom commercial</label>
            <input name="nom_commercial" class="form-control form-darlink" value="<?= setting_value($settings, 'nom_commercial') ?>">
        </div>
        <div class="col-md-12">
            <label class="form-label">Adresse</label>
            <textarea name="adresse" class="form-control form-darlink" rows="2"><?= setting_value($settings, 'adresse') ?></textarea>
        </div>
        <div class="col-md-3"><label class="form-label">Code postal</label><input name="code_postal" class="form-control form-darlink" value="<?= setting_value($settings, 'code_postal') ?>"></div>
        <div class="col-md-5"><label class="form-label">Ville</label><input name="ville" class="form-control form-darlink" value="<?= setting_value($settings, 'ville') ?>"></div>
        <div class="col-md-4"><label class="form-label">Pays</label><input name="pays" class="form-control form-darlink" value="<?= setting_value($settings, 'pays', 'Maroc') ?>"></div>
    </div>

    <div class="darlink-section-title">Contact</div>
    <div class="row g-3 mb-4">
        <div class="col-md-4"><label class="form-label">Téléphone</label><input name="telephone" class="form-control form-darlink" value="<?= setting_value($settings, 'telephone') ?>"></div>
        <div class="col-md-4"><label class="form-label">Mobile</label><input name="mobile" class="form-control form-darlink" value="<?= setting_value($settings, 'mobile') ?>"></div>
        <div class="col-md-4"><label class="form-label">E-mail</label><input name="email" type="email" class="form-control form-darlink" value="<?= setting_value($settings, 'email') ?>"></div>
        <div class="col-md-6"><label class="form-label">Site web</label><input name="site_web" class="form-control form-darlink" value="<?= setting_value($settings, 'site_web') ?>"></div>
        <div class="col-md-6"><label class="form-label">Logo / chemin fichier</label><input name="logo_path" class="form-control form-darlink" value="<?= setting_value($settings, 'logo_path') ?>" placeholder="Ex. public/assets/img/logo.png"></div>
    </div>

    <div class="darlink-section-title">Informations légales Maroc</div>
    <div class="row g-3 mb-4">
        <div class="col-md-3"><label class="form-label">ICE</label><input name="ice" class="form-control form-darlink" value="<?= setting_value($settings, 'ice') ?>"></div>
        <div class="col-md-3"><label class="form-label">IF</label><input name="identifiant_fiscal" class="form-control form-darlink" value="<?= setting_value($settings, 'identifiant_fiscal') ?>"></div>
        <div class="col-md-3"><label class="form-label">RC</label><input name="registre_commerce" class="form-control form-darlink" value="<?= setting_value($settings, 'registre_commerce') ?>"></div>
        <div class="col-md-3"><label class="form-label">Patente</label><input name="patente" class="form-control form-darlink" value="<?= setting_value($settings, 'patente') ?>"></div>
    </div>

    <div class="darlink-section-title">Banque</div>
    <div class="row g-3 mb-4">
        <div class="col-md-4"><label class="form-label">Banque</label><input name="banque" class="form-control form-darlink" value="<?= setting_value($settings, 'banque') ?>"></div>
        <div class="col-md-4"><label class="form-label">RIB</label><input name="rib" class="form-control form-darlink" value="<?= setting_value($settings, 'rib') ?>"></div>
        <div class="col-md-4"><label class="form-label">IBAN</label><input name="iban" class="form-control form-darlink" value="<?= setting_value($settings, 'iban') ?>"></div>
        <div class="col-md-4"><label class="form-label">SWIFT/BIC</label><input name="swift" class="form-control form-darlink" value="<?= setting_value($settings, 'swift') ?>"></div>
        <div class="col-md-2"><label class="form-label">Devise</label><input name="devise" class="form-control form-darlink" value="<?= setting_value($settings, 'devise', 'MAD') ?>"></div>
        
    </div>

    <div class="darlink-section-title">Documents</div>
    <div class="row g-3 mb-4">
        <div class="col-md-6"><label class="form-label">Conditions de règlement</label><textarea name="conditions_reglement" class="form-control form-darlink" rows="3"><?= setting_value($settings, 'conditions_reglement') ?></textarea></div>
        <div class="col-md-6"><label class="form-label">Mentions légales</label><textarea name="mentions_legales" class="form-control form-darlink" rows="3"><?= setting_value($settings, 'mentions_legales') ?></textarea></div>
        <div class="col-md-12"><label class="form-label">Pied de page</label><input name="pied_de_page" class="form-control form-darlink" value="<?= setting_value($settings, 'pied_de_page') ?>"></div>
    </div>

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