@extends('layouts.app')

@section('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">
            @if(can_access_module('masters.departments'))
                <a href="{{ route('departments.master.index') }}" class="btn btn-sm btn-outline-primary">Departments master</a>
            @endif
            @if(can_access_module('masters.roles'))
                <a href="{{ route('roles.index') }}" class="btn btn-sm btn-outline-primary">Roles master</a>
            @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="{{ route('users.departments.quick') }}" class="row g-2">
                        @csrf
                        <div class="col-12">
                            <input name="department_name" class="form-control form-control-sm" placeholder="Department name" required maxlength="120" value="{{ 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="{{ route('users.store') }}" class="row g-2">
                        @csrf
                        <div class="col-12">
                            <label class="form-label">Name</label>
                            <input class="form-control" name="name" required value="{{ old('name') }}">
                        </div>
                        <div class="col-12">
                            <label class="form-label">Email</label>
                            <input type="email" class="form-control" name="email" required value="{{ 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>
                                @foreach($roles as $r)
                                    <option value="{{ $r->id }}" data-admin="{{ $r->is_platform_admin ? '1' : '0' }}" {{ (string) old('role_id') === (string) $r->id ? 'selected' : '' }}>
                                        {{ $r->name }}@if($r->is_platform_admin) (Platform admin) @endif
                                    </option>
                                @endforeach
                            </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>
                                @foreach($departments as $dept)
                                    <option value="{{ $dept->id }}" {{ (string) old('department_id') === (string) $dept->id ? 'selected' : '' }}>{{ $dept->name }}</option>
                                @endforeach
                            </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>
                        @forelse($users as $user)
                            <tr>
                                <td>{{ $user->name }}</td>
                                <td>{{ $user->email }}</td>
                                <td>{{ optional($user->roleRecord)->name ?: '—' }}</td>
                                <td>{{ optional($user->department)->name ?: '—' }}</td>
                                <td class="text-nowrap">
                                    <a href="{{ route('users.edit', $user) }}" class="btn btn-sm btn-outline-secondary">Edit</a>
                                    @if($user->id !== auth()->id())
                                        <form method="POST" action="{{ route('users.destroy', $user) }}" class="d-inline" onsubmit="return confirm('Delete this user?');">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-sm btn-outline-danger">Delete</button>
                                        </form>
                                    @endif
                                    @if(!$user->isPlatformAdmin() && auth()->user()->isPlatformAdmin())
                                        <a href="{{ route('visibility.index', ['user_id' => $user->id]) }}" class="btn btn-sm btn-outline-secondary">Visibility</a>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="5" class="text-center text-muted py-3">No users found.</td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('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>
@endpush
