<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Select a company — {{ config('app.name') }}</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
    <link href="{{ asset('css/axis-enterprise.css') }}?v={{ filemtime(public_path('css/axis-enterprise.css')) }}" rel="stylesheet">
</head>
<body class="axis-auth-body axis-select-company-body">
<div class="axis-select-company-card">
    <div class="text-center mb-4">
        @include('partials.login-brand-logo')
        @if(!empty($groupWelcome))
            <p class="company-group-welcome">
                Welcome to {{ $groupWelcome['group_name'] }} ({{ $groupWelcome['group_code'] }}) group.
            </p>
        @endif
        <h1 class="select-company-title">Select a company</h1>
    </div>

    @if(session('success'))
        <div class="alert alert-success py-2 mb-3" style="font-size:13px">{{ session('success') }}</div>
    @endif

    @if($errors->any())
        <div class="alert alert-danger mb-3">
            <i class="bi bi-exclamation-triangle me-1"></i>{{ $errors->first() }}
        </div>
    @endif

    <div class="company-picker-toolbar">
        <a href="{{ route('select-company', ['sort' => $sort, 'add' => $showAddPanel ? 0 : 1]) }}"
           class="btn btn-sm {{ $showAddPanel ? 'btn-primary' : 'btn-outline-primary' }}">
            <i class="bi bi-plus-lg me-1"></i>Add
        </a>
        <a href="{{ route('select-company', ['sort' => $sort === 'asc' ? 'desc' : 'asc', 'add' => $showAddPanel ? 1 : 0]) }}"
           class="btn btn-sm btn-outline-secondary">
            <i class="bi bi-arrow-down-up me-1"></i>{{ $sort === 'asc' ? 'Asc' : 'Desc' }}
        </a>
    </div>

    <div class="company-picker-list">
        @forelse($grouped as $group)
            <div class="company-picker-columns">
                <span class="company-picker-columns-spacer" aria-hidden="true"></span>
                <span>Company Name</span>
                <span>Last login Date</span>
                <span class="company-picker-columns-spacer" aria-hidden="true"></span>
            </div>

            @foreach($group['companies'] as $company)
                <div class="company-picker-row">
                    <form method="POST" action="{{ route('select-company.select') }}" class="company-picker-item flex-grow-1">
                        @csrf
                        <input type="hidden" name="company_id" value="{{ $company['company_id'] }}">
                        <button type="submit" class="company-picker-btn">
                            <span class="company-picker-icon">
                                @if($company['has_logo'] && $company['logo_url'])
                                    <img src="{{ $company['logo_url'] }}" alt="" class="logo logo--company logo--picker">
                                @else
                                    <i class="bi bi-building"></i>
                                @endif
                            </span>
                            <span class="company-picker-name">
                                {{ $company['name'] }}
                                @if(isset($currentCompanyId) && $currentCompanyId == $company['company_id'])
                                    <span class="badge rounded-pill text-bg-primary ms-1">Current</span>
                                @endif
                            </span>
                            <span class="company-picker-login">
                                {{ $company['last_used_at'] ? $company['last_used_at']->format('d-m-Y') : '—' }}
                            </span>
                            <i class="bi bi-chevron-right company-picker-arrow"></i>
                        </button>
                    </form>
                    <form method="POST" action="{{ route('select-company.hide') }}" class="company-picker-hide-form">
                        @csrf
                        <input type="hidden" name="company_id" value="{{ $company['company_id'] }}">
                        <button type="submit" class="company-picker-hide-btn" title="Hide from list">
                            <i class="bi bi-x-lg"></i>
                        </button>
                    </form>
                </div>
            @endforeach
        @empty
            <div class="text-center text-muted py-4" style="font-size:14px">
                <i class="bi bi-eye-slash d-block mb-2" style="font-size:24px"></i>
                All companies are hidden.
                @if($hidden->isNotEmpty())
                    Click <strong>Add</strong> to show hidden companies.
                @endif
            </div>
        @endforelse
    </div>

    @if($showAddPanel)
        <div class="company-picker-hidden-panel mt-4">
            <div class="company-picker-hidden-title">
                <i class="bi bi-eye-slash me-1"></i>Hidden companies
            </div>
            <div class="company-picker-columns company-picker-columns--compact">
                <span class="company-picker-columns-spacer" aria-hidden="true"></span>
                <span>Company Name</span>
                <span>Last login Date</span>
                <span class="company-picker-columns-spacer" aria-hidden="true"></span>
            </div>
            @forelse($hidden as $company)
                <div class="company-picker-row">
                    <div class="company-picker-btn company-picker-btn-static flex-grow-1">
                        <span class="company-picker-icon">
                            @if($company['has_logo'] && $company['logo_url'])
                                <img src="{{ $company['logo_url'] }}" alt="" class="logo logo--company logo--picker">
                            @else
                                <i class="bi bi-building"></i>
                            @endif
                        </span>
                        <span class="company-picker-name">{{ $company['name'] }}</span>
                        <span class="company-picker-login">
                            {{ $company['last_used_at'] ? $company['last_used_at']->format('d-m-Y') : '—' }}
                        </span>
                        <span class="company-picker-arrow" aria-hidden="true"></span>
                    </div>
                    <form method="POST" action="{{ route('select-company.unhide') }}">
                        @csrf
                        <input type="hidden" name="company_id" value="{{ $company['company_id'] }}">
                        <button type="submit" class="btn btn-sm btn-outline-primary company-picker-add-btn">
                            <i class="bi bi-plus-lg me-1"></i>Show
                        </button>
                    </form>
                </div>
            @empty
                <div class="text-muted py-3" style="font-size:13px">No hidden companies.</div>
            @endforelse
        </div>
    @endif

    <div class="d-flex justify-content-between align-items-center mt-4 pt-3 border-top">
        @if(auth()->check())
            <form action="{{ route('logout') }}" method="POST">
                @csrf
                <button type="submit" class="btn btn-link text-muted text-decoration-none px-0">
                    <i class="bi bi-box-arrow-right me-1"></i>Sign out
                </button>
            </form>
            <a href="{{ route('dashboard') }}" class="btn btn-outline-secondary btn-sm">Skip</a>
        @else
            <a href="{{ route('login') }}" class="btn btn-link text-muted text-decoration-none px-0">
                <i class="bi bi-arrow-left me-1"></i>Back to login
            </a>
            <form action="{{ route('select-company.skip') }}" method="POST">
                @csrf
                <button type="submit" class="btn btn-outline-secondary btn-sm">Skip</button>
            </form>
        @endif
    </div>
</div>
</body>
</html>
