@extends('layouts.app')

@section('title', __('messages.users'))

@section('content')
<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="fw-bold mb-0">{{ __('messages.users') }} Management</h5>
        <span class="text-muted fs-xs">System Users, Roles, Branch Access, and Status Controls</span>
    </div>
    <div>
        <a href="{{ route('users.create') }}" class="btn btn-sm btn-primary">
            <i class="bi bi-person-plus me-1"></i> Add New User
        </a>
    </div>
</div>

<div class="card-erp">
    <div class="table-responsive">
        <table class="table table-dense table-hover mb-0">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Name</th>
                    <th>Username / Email</th>
                    <th>Phone</th>
                    <th>Assigned Roles</th>
                    <th>Branch Access</th>
                    <th>Status</th>
                    <th>Last Login</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($users as $user)
                    <tr>
                        <td>{{ $user->id }}</td>
                        <td class="fw-bold text-dark">
                            {{ $user->name }}
                            @if($user->is_super_admin)
                                <span class="badge bg-danger fs-xs ms-1">Super Admin</span>
                            @endif
                        </td>
                        <td>
                            <div><i class="bi bi-person text-muted"></i> {{ $user->username ?? '-' }}</div>
                            <div class="text-muted fs-xs"><i class="bi bi-envelope"></i> {{ $user->email }}</div>
                        </td>
                        <td>{{ $user->phone ?? '-' }}</td>
                        <td>
                            @foreach($user->roles as $role)
                                <span class="badge bg-secondary fs-xs me-1">{{ $role->name }}</span>
                            @endforeach
                            @if($user->roles->isEmpty() && !$user->is_super_admin)
                                <span class="text-muted fs-xs">No Role</span>
                            @endif
                        </td>
                        <td>
                            @if($user->branches->isNotEmpty())
                                @foreach($user->branches as $b)
                                    <span class="badge bg-info text-dark fs-xs me-1">{{ $b->code }}</span>
                                @endforeach
                            @else
                                <span class="badge bg-light text-dark fs-xs border">All Branches</span>
                            @endif
                        </td>
                        <td>
                            @if($user->status === 'active')
                                <span class="badge bg-success">Active</span>
                            @else
                                <span class="badge bg-danger">Inactive</span>
                            @endif
                        </td>
                        <td class="fs-xs text-muted">
                            {{ $user->last_login_at ? $user->last_login_at->format('Y-m-d H:i') : 'Never' }}
                            @if($user->last_login_ip)
                                <div>({{ $user->last_login_ip }})</div>
                            @endif
                        </td>
                        <td class="text-end">
                            <a href="{{ route('users.edit', $user->id) }}" class="btn btn-dense btn-outline-primary me-1">
                                <i class="bi bi-pencil"></i> Edit
                            </a>
                            @if($user->id !== auth()->id())
                                <form action="{{ route('users.destroy', $user->id) }}" method="POST" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this user?')">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn btn-dense btn-outline-danger">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </form>
                            @endif
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="9" class="text-center py-4 text-muted">No users found.</td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
    <div class="p-2 border-top">
        {{ $users->links() }}
    </div>
</div>
@endsection
