

<?php $__env->startSection('content'); ?>
    <div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
        <h4 class="mb-0">Users</h4>
        <div class="d-flex gap-2 flex-wrap">
            <?php if(can_access_module('masters.departments')): ?>
                <a href="<?php echo e(route('departments.master.index')); ?>" class="btn btn-sm btn-outline-primary">Departments master</a>
            <?php endif; ?>
            <?php if(can_access_module('masters.roles')): ?>
                <a href="<?php echo e(route('roles.index')); ?>" class="btn btn-sm btn-outline-primary">Roles master</a>
            <?php endif; ?>
        </div>
    </div>

    <div class="row g-4">
        <div class="col-lg-4">
            <div class="card shadow-sm mb-3">
                <div class="card-header fw-bold">Quick add department</div>
                <div class="card-body">
                    <form method="POST" action="<?php echo e(route('users.departments.quick')); ?>" class="row g-2">
                        <?php echo csrf_field(); ?>
                        <div class="col-12">
                            <input name="department_name" class="form-control form-control-sm" placeholder="Department name" required maxlength="120" value="<?php echo e(old('department_name')); ?>">
                        </div>
                        <div class="col-12">
                            <button class="btn btn-sm btn-outline-success w-100">Add department</button>
                        </div>
                    </form>
                </div>
            </div>

            <div class="card shadow-sm">
                <div class="card-header fw-bold">Create user</div>
                <div class="card-body">
                    <form method="POST" action="<?php echo e(route('users.store')); ?>" class="row g-2">
                        <?php echo csrf_field(); ?>
                        <div class="col-12">
                            <label class="form-label">Name</label>
                            <input class="form-control" name="name" required value="<?php echo e(old('name')); ?>">
                        </div>
                        <div class="col-12">
                            <label class="form-label">Email</label>
                            <input type="email" class="form-control" name="email" required value="<?php echo e(old('email')); ?>">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Password</label>
                            <input type="password" class="form-control" name="password" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Confirm</label>
                            <input type="password" class="form-control" name="password_confirmation" required>
                        </div>
                        <div class="col-12">
                            <label class="form-label">Role <span class="text-danger">*</span></label>
                            <select class="form-select" name="role_id" id="user_role_id" required>
                                <option value="">— Select role —</option>
                                <?php $__currentLoopData = $roles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $r): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($r->id); ?>" data-admin="<?php echo e($r->is_platform_admin ? '1' : '0'); ?>" <?php echo e((string) old('role_id') === (string) $r->id ? 'selected' : ''); ?>>
                                        <?php echo e($r->name); ?><?php if($r->is_platform_admin): ?> (Platform admin) <?php endif; ?>
                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <div class="form-text">Choose Administrator to create a full admin user.</div>
                        </div>
                        <div class="col-12" id="department_field">
                            <label class="form-label">Department</label>
                            <select class="form-select" name="department_id">
                                <option value="">— None —</option>
                                <?php $__currentLoopData = $departments; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $dept): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($dept->id); ?>" <?php echo e((string) old('department_id') === (string) $dept->id ? 'selected' : ''); ?>><?php echo e($dept->name); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <div class="form-text">Assign a department for staff roles (module visibility uses department defaults).</div>
                        </div>
                        <div class="col-12 d-grid">
                            <button class="btn btn-success">Create user</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="col-lg-8">
            <div class="card shadow-sm">
                <div class="card-header fw-bold">All users</div>
                <div class="card-body p-0">
                    <table class="table table-sm mb-0 align-middle">
                        <thead>
                        <tr>
                            <th>Name</th>
                            <th>Email</th>
                            <th>Role</th>
                            <th>Department</th>
                            <th></th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php $__empty_1 = true; $__currentLoopData = $users; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                            <tr>
                                <td><?php echo e($user->name); ?></td>
                                <td><?php echo e($user->email); ?></td>
                                <td><?php echo e(optional($user->roleRecord)->name ?: '—'); ?></td>
                                <td><?php echo e(optional($user->department)->name ?: '—'); ?></td>
                                <td class="text-nowrap">
                                    <a href="<?php echo e(route('users.edit', $user)); ?>" class="btn btn-sm btn-outline-secondary">Edit</a>
                                    <?php if($user->id !== auth()->id()): ?>
                                        <form method="POST" action="<?php echo e(route('users.destroy', $user)); ?>" class="d-inline" onsubmit="return confirm('Delete this user?');">
                                            <?php echo csrf_field(); ?>
                                            <?php echo method_field('DELETE'); ?>
                                            <button type="submit" class="btn btn-sm btn-outline-danger">Delete</button>
                                        </form>
                                    <?php endif; ?>
                                    <?php if(!$user->isPlatformAdmin() && auth()->user()->isPlatformAdmin()): ?>
                                        <a href="<?php echo e(route('visibility.index', ['user_id' => $user->id])); ?>" class="btn btn-sm btn-outline-secondary">Visibility</a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                            <tr><td colspan="5" class="text-center text-muted py-3">No users found.</td></tr>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
(function () {
    var sel = document.getElementById('user_role_id');
    var deptField = document.getElementById('department_field');
    if (!sel || !deptField) return;
    function toggle() {
        var opt = sel.options[sel.selectedIndex];
        var isAdmin = opt && opt.getAttribute('data-admin') === '1';
        deptField.style.display = isAdmin ? 'none' : '';
        if (isAdmin) {
            var ds = deptField.querySelector('select');
            if (ds) ds.value = '';
        }
    }
    sel.addEventListener('change', toggle);
    toggle();
})();
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH D:\laragon\www\lt\resources\views/users/index.blade.php ENDPATH**/ ?>