<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-users-cog me-2 text-primary"></i><?= $target ? 'Edit User' : 'New User' ?></h5>
    <a href="<?= url('/Users') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<div class="rs-card p-3">
<form method="post" action="<?= url('/Users/save') ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="user_id" value="<?= (int) ($target['user_id'] ?? 0) ?>">

    <div class="row g-2">
        <div class="col-md-6"><label class="form-label">Username *</label><input type="text" class="form-control form-control-sm" name="username" required value="<?= e($target['username'] ?? '') ?>" <?= $target ? 'readonly' : '' ?>></div>
        <div class="col-md-6"><label class="form-label">Role *</label>
            <select name="role_id" class="form-select form-select-sm" required>
                <option value="">— pick a role —</option>
                <?php foreach ($roles as $r): ?>
                    <option value="<?= (int) $r['role_id'] ?>" <?= (int) ($target['role_id'] ?? 0) === (int) $r['role_id'] ? 'selected' : '' ?>><?= e($r['name']) ?><?php if ($r['description']): ?> — <?= e($r['description']) ?><?php endif; ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-6"><label class="form-label">First Name</label><input type="text" class="form-control form-control-sm" name="f_name" value="<?= e($target['f_name'] ?? '') ?>"></div>
        <div class="col-md-6"><label class="form-label">Last Name</label><input type="text" class="form-control form-control-sm" name="l_name" value="<?= e($target['l_name'] ?? '') ?>"></div>
        <div class="col-md-6"><label class="form-label">Email</label><input type="email" class="form-control form-control-sm" name="email" value="<?= e($target['email'] ?? '') ?>"></div>
        <div class="col-md-3"><label class="form-label">Phone</label><input type="text" class="form-control form-control-sm" name="phone" value="<?= e($target['phone'] ?? '') ?>"></div>
        <div class="col-md-3"><label class="form-label">Status</label>
            <select name="status" class="form-select form-select-sm">
                <?php foreach (['active','inactive','suspended'] as $s): ?><option <?= ($target['status'] ?? 'active') === $s ? 'selected' : '' ?>><?= $s ?></option><?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-6"><label class="form-label">Job Title</label><input type="text" class="form-control form-control-sm" name="job_title" value="<?= e($target['job_title'] ?? '') ?>"></div>
        <div class="col-md-6"><label class="form-label">Password <?= $target ? '<small class="text-muted">(leave blank to keep)</small>' : '*' ?></label><input type="password" class="form-control form-control-sm" name="password" autocomplete="new-password" <?= $target ? '' : 'required' ?>></div>
    </div>

    <div class="mt-3 d-flex gap-2">
        <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-save me-1"></i>Save</button>
        <?php if (!empty($target['user_id'])): ?><a class="btn btn-sm btn-outline-danger" href="<?= url('/Users/delete/' . (int) $target['user_id']) ?>" onclick="return confirm('Delete?')"><i class="fas fa-trash me-1"></i>Delete</a><?php endif; ?>
    </div>
</form>
</div>
