@extends('layouts.layout')

@section('title', 'Kullanıcı Yönetimi')
@section('header_title', 'Kullanıcı Yönetimi')
@section('header_subtitle', 'Sistemdeki müşteri hesaplarını görüntüleyin, yetkilendirin veya silin.')

@section('content')
@if(session('error'))
    <div class="alert alert-danger" style="background-color: rgba(255, 23, 68, 0.08); border-color: var(--danger); color: var(--danger); margin-bottom: 1.5rem; padding: 1rem; border-radius: var(--radius-sm);">
        {{ session('error') }}
    </div>
@endif

<div class="card">
    <div class="card-title">Kayıtlı Kullanıcılar</div>
    
    <div class="table-responsive">
        <table class="table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Ad Soyad</th>
                    <th>E-posta</th>
                    <th>Rol / Yetki</th>
                    <th>Firma / Grup</th>
                    <th>Kayıt Tarihi</th>
                    <th style="width: 250px;">İşlemler</th>
                </tr>
            </thead>
            <tbody>
                @foreach($users as $u)
                    <tr>
                        <td>#{{ $u->id }}</td>
                        <td>
                            <strong>{{ $u->name }}</strong>
                            @if($u->id === auth()->id())
                                <span style="font-size: 0.75rem; opacity: 0.6; font-style: italic;">(Siz)</span>
                            @endif
                        </td>
                        <td>{{ $u->email }}</td>
                        <td>
                            @if($u->role === 'admin')
                                <span class="badge badge-completed">Yönetici</span>
                            @else
                                <span class="badge badge-pending">Müşteri</span>
                            @endif
                        </td>
                        <td>
                            @if($u->id !== auth()->id())
                                <form action="{{ route('admin.users.assign-organization', $u->id) }}" method="POST" style="display: inline-block; margin: 0; vertical-align: middle; width: 100%;">
                                    @csrf
                                    <select name="organization_id" class="form-select native-select" onchange="this.form.submit()" style="padding: 0.45rem 1.8rem 0.45rem 0.75rem; font-size: 0.85rem; width: 100%; min-width: 160px; border-radius: 6px; background-color: var(--bg-card); border: 1.5px solid var(--border-color); color: var(--text-primary); cursor: pointer; -webkit-appearance: none; -moz-appearance: none; appearance: none; background-image: url('data:image/svg+xml;utf8,<svg xmlns=%22http://www.w3.org/2000/svg%22 width=%2212%22 height=%2212%22 viewBox=%220 0 24 24%22 fill=%22none%22 stroke=%22%235da46b%22 stroke-width=%222%22 stroke-linecap=%22round%22 stroke-linejoin=%22round%22><polyline points=%226 9 12 15 18 9%22></polyline></svg>'); background-repeat: no-repeat; background-position: right 10px center; background-size: 12px; line-height: normal; display: block;">
                                        <option value="">Firma Yok (Kişisel)</option>
                                        @foreach($organizations as $org)
                                            <option value="{{ $org->id }}" {{ $u->organization_id == $org->id ? 'selected' : '' }}>
                                                {{ $org->name }}
                                            </option>
                                        @endforeach
                                    </select>
                                </form>
                            @else
                                <strong>{{ $u->organization ? $u->organization->name : 'Firma Yok' }}</strong>
                            @endif
                        </td>
                        <td>{{ $u->created_at->format('d.m.Y H:i') }}</td>
                        <td>
                            <div style="display: flex; gap: 8px;">
                                @if($u->id !== auth()->id())
                                    <!-- Toggle Role -->
                                    <form action="{{ route('admin.users.toggle', $u->id) }}" method="POST" style="display: inline;">
                                        @csrf
                                        <button type="submit" class="btn btn-secondary btn-sm" style="display: inline-flex; align-items: center; gap: 4px;">
                                            <ion-icon name="swap-horizontal-outline"></ion-icon>
                                            Yetkiyi Değiştir
                                        </button>
                                    </form>
 
                                    <!-- Delete User -->
                                    <form action="{{ route('admin.users.delete', $u->id) }}" method="POST" style="display: inline;" onsubmit="return confirm('Kullanıcıyı ve ilişkili tüm görevlerini silmek istediğinize emin misiniz?');">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-danger btn-sm" style="display: inline-flex; align-items: center; gap: 4px;">
                                            <ion-icon name="trash-outline"></ion-icon>
                                            Sil
                                        </button>
                                    </form>
                                @else
                                    <span style="font-size: 0.85rem; color: var(--text-secondary); font-style: italic;">
                                        Kendi hesabınızı düzenleyemezsiniz.
                                    </span>
                                @endif
                            </div>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>
@endsection
