<?= $this->extend('layout/main') ?>
<?= $this->section('content') ?>

<h5 class="mb-4"><?= $mode === 'edit' ? 'Modifier le Client' : 'Nouveau Client' ?></h5>

<div class="row">
    <div class="col-lg-8">
        <div class="table-card">
            <form method="POST" action="<?= $mode === 'edit' ? '/clients/edit/' . $client['id'] : '/clients/create' ?>">
                <?= csrf_field() ?>
                <div class="row g-3">
                    <div class="col-md-6">
                        <label class="form-label">Code client</label>
                        <input type="text" name="code" class="form-control" value="<?= $client['code'] ?? '' ?>" <?= $mode === 'create' ? 'readonly' : '' ?>>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Nom de la societe *</label>
                        <input type="text" name="company_name" class="form-control" value="<?= $client['company_name'] ?? '' ?>" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Contact</label>
                        <input type="text" name="contact_name" class="form-control" value="<?= $client['contact_name'] ?? '' ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Email</label>
                        <input type="email" name="email" class="form-control" value="<?= $client['email'] ?? '' ?>">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Telephone</label>
                        <input type="text" name="phone" class="form-control" value="<?= $client['phone'] ?? '' ?>">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Fax</label>
                        <input type="text" name="fax" class="form-control" value="<?= $client['fax'] ?? '' ?>">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Pays</label>
                        <select name="country" class="form-select">
                            <option value="Tunisie" <?= ($client['country'] ?? '') === 'Tunisie' ? 'selected' : '' ?>>Tunisie</option>
                            <option value="France" <?= ($client['country'] ?? '') === 'France' ? 'selected' : '' ?>>France</option>
                            <option value="Maroc" <?= ($client['country'] ?? '') === 'Maroc' ? 'selected' : '' ?>>Maroc</option>
                            <option value="Algerie" <?= ($client['country'] ?? '') === 'Algerie' ? 'selected' : '' ?>>Algerie</option>
                        </select>
                    </div>
                    <div class="col-md-8">
                        <label class="form-label">Adresse</label>
                        <textarea name="address" class="form-control" rows="2"><?= $client['address'] ?? '' ?></textarea>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Ville</label>
                        <input type="text" name="city" class="form-control" value="<?= $client['city'] ?? '' ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Matricule fiscal</label>
                        <input type="text" name="tax_id" class="form-control" value="<?= $client['tax_id'] ?? '' ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">ICE</label>
                        <input type="text" name="ice" class="form-control" value="<?= $client['ice'] ?? '' ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Conditions de paiement (jours)</label>
                        <input type="number" name="payment_terms" class="form-control" value="<?= $client['payment_terms'] ?? 30 ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Statut</label>
                        <select name="is_active" class="form-select">
                            <option value="1" <?= ($client['is_active'] ?? 1) == 1 ? 'selected' : '' ?>>Actif</option>
                            <option value="0" <?= ($client['is_active'] ?? 1) == 0 ? 'selected' : '' ?>>Inactif</option>
                        </select>
                    </div>
                    <div class="col-12">
                        <label class="form-label">Notes</label>
                        <textarea name="notes" class="form-control" rows="3"><?= $client['notes'] ?? '' ?></textarea>
                    </div>
                </div>
                <div class="d-flex justify-content-between mt-4">
                    <a href="/clients" class="btn btn-outline-secondary">Annuler</a>
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-check-lg"></i> <?= $mode === 'edit' ? 'Mettre a jour' : 'Creer' ?>
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
