<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<div class="row justify-content-center">
    <div class="col-lg-10">
        <div class="stat-card">
            <h1 class="h5 mb-3">Edit User</h1>

            <?php if (session()->getFlashdata('errors')): ?>
                <div class="alert alert-danger py-2 small">
                    <ul class="mb-0 ps-3">
                        <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                            <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            <?php endif; ?>

            <form action="<?= base_url('users/' . $user->id) ?>" method="post" novalidate>
                <?= csrf_field() ?>

                <div class="row g-3">
                    <div class="col-md-6">
                        <label class="form-label">First Name</label>
                        <input type="text" name="first_name" class="form-control" value="<?= esc(old('first_name', $user->first_name)) ?>" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Last Name</label>
                        <input type="text" name="last_name" class="form-control" value="<?= esc(old('last_name', $user->last_name)) ?>" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Username</label>
                        <input type="text" name="username" class="form-control" value="<?= esc(old('username', $user->username)) ?>" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Email</label>
                        <input type="email" name="email" class="form-control" value="<?= esc(old('email', $user->email)) ?>" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Phone</label>
                        <input type="text" name="phone" class="form-control" value="<?= esc(old('phone', $user->phone)) ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Status</label>
                        <input type="text" class="form-control" value="<?= esc(ucfirst($user->status)) ?>" disabled>
                        <div class="form-text">Use Activate/Deactivate from the users list to change status.</div>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Branch</label>
                        <select name="branch_id" class="form-select">
                            <option value="">— None —</option>
                            <?php foreach ($branches as $b): ?>
                                <option value="<?= $b['id'] ?>" <?= (string) old('branch_id', $user->branch_id) === (string) $b['id'] ? 'selected' : '' ?>><?= esc($b['name']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Department</label>
                        <select name="department_id" class="form-select">
                            <option value="">— None —</option>
                            <?php foreach ($departments as $d): ?>
                                <option value="<?= $d['id'] ?>" <?= (string) old('department_id', $user->department_id) === (string) $d['id'] ? 'selected' : '' ?>><?= esc($d['name']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <?= view('partials/user_access_section', [
                        'roles'        => $roles,
                        'groupedPerms' => $groupedPerms,
                        'assignedIds'  => $assignedIds,
                        'accessMode'   => $accessMode,
                        'user'         => $user,
                    ]) ?>

                    <div class="col-12">
                        <div class="form-check">
                            <input type="hidden" name="tpin_required" value="0">
                            <input class="form-check-input" type="checkbox" name="tpin_required" value="1" id="tpin_required"
                                <?= (string) old('tpin_required', ! empty($user->tpin_required) ? '1' : '0') === '1' ? 'checked' : '' ?>>
                            <label class="form-check-label" for="tpin_required">
                                Require TPIN for sensitive actions
                            </label>
                            <div class="form-text">User must set a TPIN in their profile.</div>
                        </div>
                    </div>
                </div>

                <div class="alert alert-warning small mt-3 mb-0">
                    <i class="fa-solid fa-triangle-exclamation me-1"></i>
                    Switching to a different role logs the user out on save. Custom permission updates apply immediately.
                </div>

                <div class="mt-4 d-flex gap-2">
                    <button type="submit" class="btn btn-primary">Save Changes</button>
                    <a href="<?= base_url('users') ?>" class="btn btn-outline-secondary">Cancel</a>
                </div>
            </form>
        </div>
    </div>
</div>

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