<?php
    $is_edit = isset($user);
    $action  = $is_edit ? site_url('users/edit/' . $user->id) : site_url('users/create');
?>

<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="card shadow-sm">
            <div class="card-header bg-white d-flex justify-content-between align-items-center">
                <h5 class="mb-0">
                    <i class="bi bi-<?= $is_edit ? 'pencil' : 'person-plus'; ?>"></i>
                    <?= $is_edit ? 'Modifier l\'utilisateur' : 'Nouvel utilisateur'; ?>
                </h5>
                <a href="<?= site_url('users'); ?>" class="btn btn-sm btn-outline-secondary">
                    <i class="bi bi-arrow-left"></i> Retour
                </a>
            </div>
            <div class="card-body">
                <?= form_open($action); ?>

                <div class="row">
                    <!-- Nom complet -->
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Nom complet <span class="text-danger">*</span></label>
                        <input type="text" name="full_name" class="form-control" required
                               value="<?= set_value('full_name', $is_edit ? $user->full_name : ''); ?>">
                    </div>

                    <!-- Username -->
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Nom d'utilisateur <span class="text-danger">*</span></label>
                        <input type="text" name="username" class="form-control" required
                               value="<?= set_value('username', $is_edit ? $user->username : ''); ?>">
                    </div>

                    <!-- Email -->
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Email <span class="text-danger">*</span></label>
                        <input type="email" name="email" class="form-control" required
                               value="<?= set_value('email', $is_edit ? $user->email : ''); ?>">
                    </div>

                    <!-- Téléphone -->
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Téléphone</label>
                        <input type="text" name="phone" class="form-control"
                               value="<?= set_value('phone', $is_edit ? $user->phone : ''); ?>">
                    </div>

                    <!-- Rôle -->
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Rôle <span class="text-danger">*</span></label>
                        <select name="role_id" class="form-select" required>
                            <option value="">-- Sélectionner --</option>
                            <?php foreach ($roles as $r): ?>
                            <option value="<?= $r->id; ?>" 
                                <?= set_select('role_id', $r->id, $is_edit && $user->role_id == $r->id); ?>>
                                <?= $r->label; ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <!-- Localisation -->
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Localisation</label>
                        <select name="location_id" class="form-select">
                            <option value="">— Aucune (Toutes) —</option>
                            <?php foreach ($locations as $loc): ?>
                            <option value="<?= $loc->id; ?>"
                                <?= set_select('location_id', $loc->id, $is_edit && $user->location_id == $loc->id); ?>>
                                <?= $loc->name; ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                        <div class="form-text">Obligatoire pour les Agents Locaux.</div>
                    </div>

                    <?php if ($is_edit): ?>
                    <!-- Statut (uniquement en modification) -->
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Statut</label>
                        <select name="status" class="form-select">
                            <option value="active"    <?= set_select('status', 'active',    $user->status == 'active');    ?>>Actif</option>
                            <option value="inactive"  <?= set_select('status', 'inactive',  $user->status == 'inactive');  ?>>Inactif</option>
                            <option value="suspended" <?= set_select('status', 'suspended', $user->status == 'suspended'); ?>>Suspendu</option>
                        </select>
                    </div>
                    <?php endif; ?>

                    <?php if ( ! $is_edit): ?>
                    <!-- Mot de passe (uniquement à la création) -->
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Mot de passe <span class="text-danger">*</span></label>
                        <input type="password" name="password" class="form-control" 
                               minlength="8" required>
                        <div class="form-text">Minimum 8 caractères.</div>
                    </div>

                    <div class="col-md-6 mb-3">
                        <label class="form-label">Confirmer <span class="text-danger">*</span></label>
                        <input type="password" name="confirm_password" class="form-control" required>
                    </div>
                    <?php endif; ?>
                </div>

                <hr>

                <div class="d-flex gap-2">
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-check-lg"></i> <?= $is_edit ? 'Enregistrer' : 'Créer'; ?>
                    </button>
                    <a href="<?= site_url('users'); ?>" class="btn btn-outline-secondary">Annuler</a>
                </div>

                <?= form_close(); ?>
            </div>
        </div>
    </div>
</div>
