@extends('layouts.default')
@section('title', $page_title)
@section('content')
<div class="container-fluid">
    @include('admin.partials.page-header', ['actions' => new \Illuminate\Support\HtmlString(
        '<a href="'.route('admin.clients.show', $client).'" class="btn btn-light me-2">Müşteriye Dön</a>'.
        '<a href="'.route('admin.clients.portal-users.create', $client).'" class="btn btn-primary">Portal Kullanıcısı Ekle</a>'
    )])

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger">{{ session('error') }}</div>
    @endif

    <div class="card">
        <div class="card-header border-0 pb-0">
            <div>
                <h4 class="card-title mb-1">{{ $client->company_name }}</h4>
                <p class="text-muted mb-0">Müşteri portalına erişebilecek kullanıcıları buradan yönetebilirsin.</p>
            </div>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-responsive-md">
                    <thead>
                    <tr>
                        <th>Ad Soyad</th>
                        <th>E-posta</th>
                        <th>Rol</th>
                        <th>Durum</th>
                        <th>Son Portal Girişi</th>
                        <th class="text-end">İşlem</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($client->users as $user)
                        <tr>
                            <td>{{ $user->name }}</td>
                            <td>{{ $user->email }}</td>
                            <td>
                                <form action="{{ route('admin.clients.portal-users.update', [$client, $user]) }}" method="POST" class="d-flex gap-2 align-items-center">
                                    @csrf
                                    @method('PATCH')
                                    <select name="role" class="form-control form-control-sm" style="max-width:150px;">
                                        <option value="viewer" @selected($user->pivot->role === 'viewer')>Görüntüleyici</option>
                                        <option value="report_viewer" @selected($user->pivot->role === 'report_viewer')>Rapor Kullanıcısı</option>
                                        <option value="manager" @selected($user->pivot->role === 'manager')>Müşteri Yetkilisi</option>
                                    </select>
                                    <label class="mb-0 small d-flex align-items-center gap-1">
                                        <input type="checkbox" name="is_active" value="1" @checked($user->pivot->is_active ?? true)> Aktif
                                    </label>
                                    <button class="btn btn-sm btn-light" type="submit">Kaydet</button>
                                </form>
                            </td>
                            <td>
                                <span class="badge {{ ($user->pivot->is_active ?? true) ? 'badge-success' : 'badge-secondary' }}">
                                    {{ ($user->pivot->is_active ?? true) ? 'Aktif' : 'Pasif' }}
                                </span>
                            </td>
                            <td>{{ $user->pivot->last_portal_login_at ? \Carbon\Carbon::parse($user->pivot->last_portal_login_at)->format('d.m.Y H:i') : '-' }}</td>
                            <td class="text-end">
                                <form action="{{ route('admin.clients.portal-users.destroy', [$client, $user]) }}" method="POST" onsubmit="return confirm('Bu kullanıcının müşteri portal erişimi kaldırılsın mı?')">
                                    @csrf
                                    @method('DELETE')
                                    <button class="btn btn-sm btn-danger" type="submit">Erişimi Kaldır</button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="6" class="text-center text-muted py-5">Henüz portal kullanıcısı yok.</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
