@extends('layouts.default')

@section('content')
<div class="container-fluid">
    <div class="page-titles d-flex align-items-center justify-content-between flex-wrap gap-2">
        <div>
            <h4>Ajans Görev Panosu</h4>
            <p class="mb-0">Bugün yapılacak işleri, geciken görevleri, atamaları ve müşteri operasyon aksiyonlarını tek ekrandan yönetin.</p>
        </div>
        <div class="d-flex gap-2">
            <form method="POST" action="{{ route('admin.task-board.generate') }}">
                @csrf
                <button class="btn btn-primary btn-sm">Operasyon Görevlerini Üret</button>
            </form>
            <a href="{{ route('admin.operations.index') }}" class="btn btn-outline-secondary btn-sm">Operasyon Merkezi</a>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger">{{ session('error') }}</div>
    @endif
    @if($errors->any())
        <div class="alert alert-danger">
            <strong>Kontrol edin:</strong>
            <ul class="mb-0">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    @php($summary = $data['summary'])
    <div class="row">
        <div class="col-xl-2 col-sm-6"><div class="card"><div class="card-body"><span class="text-muted">Açık Görev</span><h3 class="mb-0">{{ $summary['open_total'] }}</h3></div></div></div>
        <div class="col-xl-2 col-sm-6"><div class="card"><div class="card-body"><span class="text-muted">Bugün</span><h3 class="mb-0 text-primary">{{ $summary['today'] }}</h3></div></div></div>
        <div class="col-xl-2 col-sm-6"><div class="card"><div class="card-body"><span class="text-muted">Geciken</span><h3 class="mb-0 text-danger">{{ $summary['overdue'] }}</h3></div></div></div>
        <div class="col-xl-2 col-sm-6"><div class="card"><div class="card-body"><span class="text-muted">Kritik</span><h3 class="mb-0 text-danger">{{ $summary['critical_open'] }}</h3></div></div></div>
        <div class="col-xl-2 col-sm-6"><div class="card"><div class="card-body"><span class="text-muted">Atanmamış</span><h3 class="mb-0 text-warning">{{ $summary['unassigned'] }}</h3></div></div></div>
        <div class="col-xl-2 col-sm-6"><div class="card"><div class="card-body"><span class="text-muted">Bugün Biten</span><h3 class="mb-0 text-success">{{ $summary['completed_today'] }}</h3></div></div></div>
    </div>

    <div class="card">
        <div class="card-body">
            <form method="GET" class="row g-3 align-items-end">
                <div class="col-lg-2">
                    <label class="form-label">Arama</label>
                    <input type="text" name="q" value="{{ $filters['q'] ?? '' }}" class="form-control" placeholder="Görev / müşteri">
                </div>
                <div class="col-lg-2">
                    <label class="form-label">Müşteri</label>
                    <select name="client_id" class="form-control">
                        <option value="">Tümü</option>
                        @foreach($data['clients'] as $client)
                            <option value="{{ $client->id }}" @selected((string)($filters['client_id'] ?? '') === (string)$client->id)>{{ $client->company_name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-lg-2">
                    <label class="form-label">Öncelik</label>
                    <select name="priority" class="form-control">
                        <option value="">Tümü</option>
                        @foreach(['critical' => 'Kritik', 'high' => 'Yüksek', 'medium' => 'Orta', 'low' => 'Düşük'] as $value => $label)
                            <option value="{{ $value }}" @selected(($filters['priority'] ?? '') === $value)>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-lg-2">
                    <label class="form-label">Durum</label>
                    <select name="status" class="form-control">
                        <option value="">Tümü</option>
                        @foreach(['open' => 'Açık', 'in_progress' => 'İşlemde', 'waiting' => 'Bekliyor', 'completed' => 'Tamamlandı', 'cancelled' => 'İptal'] as $value => $label)
                            <option value="{{ $value }}" @selected(($filters['status'] ?? '') === $value)>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-lg-2">
                    <label class="form-label">Tarih</label>
                    <select name="due" class="form-control">
                        <option value="">Tümü</option>
                        <option value="today" @selected(($filters['due'] ?? '') === 'today')>Bugün</option>
                        <option value="overdue" @selected(($filters['due'] ?? '') === 'overdue')>Geciken</option>
                        <option value="week" @selected(($filters['due'] ?? '') === 'week')>7 Gün</option>
                        <option value="none" @selected(($filters['due'] ?? '') === 'none')>Tarihsiz</option>
                    </select>
                </div>
                <div class="col-lg-2 d-flex gap-2">
                    <button class="btn btn-primary">Filtrele</button>
                    <a href="{{ route('admin.task-board.index') }}" class="btn btn-light">Temizle</a>
                </div>
            </form>
        </div>
    </div>

    <div class="row">
        <div class="col-xl-4">
            <div class="card border-danger">
                <div class="card-header"><h5 class="mb-0 text-danger">Gecikenler</h5></div>
                <div class="card-body">
                    @forelse($data['overdue'] as $task)
                        @include('admin.task-board.partials.task-mini', ['task' => $task])
                    @empty
                        <p class="text-muted mb-0">Geciken görev yok.</p>
                    @endforelse
                </div>
            </div>
        </div>
        <div class="col-xl-4">
            <div class="card border-primary">
                <div class="card-header"><h5 class="mb-0 text-primary">Bugün Yapılacaklar</h5></div>
                <div class="card-body">
                    @forelse($data['today'] as $task)
                        @include('admin.task-board.partials.task-mini', ['task' => $task])
                    @empty
                        <p class="text-muted mb-0">Bugüne atanmış görev yok.</p>
                    @endforelse
                </div>
            </div>
        </div>
        <div class="col-xl-4">
            <div class="card border-info">
                <div class="card-header"><h5 class="mb-0 text-info">Yaklaşan 7 Gün</h5></div>
                <div class="card-body">
                    @forelse($data['upcoming'] as $task)
                        @include('admin.task-board.partials.task-mini', ['task' => $task])
                    @empty
                        <p class="text-muted mb-0">Yaklaşan görev yok.</p>
                    @endforelse
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        @foreach(['open' => 'Açık', 'in_progress' => 'İşlemde', 'waiting' => 'Bekliyor', 'completed' => 'Tamamlandı'] as $status => $label)
            <div class="col-xl-3 col-lg-6">
                <div class="card h-100">
                    <div class="card-header"><h5 class="mb-0">{{ $label }}</h5></div>
                    <div class="card-body">
                        @forelse($data['lanes']->get($status, collect()) as $task)
                            <div class="border rounded p-3 mb-3 {{ $task->isOverdue() ? 'border-danger' : '' }}">
                                <div class="d-flex justify-content-between gap-2">
                                    <strong>{{ $task->title }}</strong>
                                    <span class="badge {{ $task->priorityBadgeClass() }}">{{ $task->priorityLabel() }}</span>
                                </div>
                                <div class="text-muted small mt-1">
                                    {{ optional($task->client)->company_name ?: 'Genel görev' }}
                                </div>
                                @if($task->description)
                                    <p class="small mb-2 mt-2">{{ \Illuminate\Support\Str::limit($task->description, 130) }}</p>
                                @endif
                                <div class="small {{ $task->isOverdue() ? 'text-danger' : 'text-muted' }}">{{ $task->dueLabel() }}</div>
                                <div class="small text-muted">Atanan: {{ optional($task->assignee)->name ?: 'Atanmamış' }}</div>
                                <form method="POST" action="{{ route('admin.task-board.tasks.update', $task) }}" class="mt-2">
                                    @csrf
                                    @method('PATCH')
                                    <div class="d-flex gap-1">
                                        <select name="status" class="form-control form-control-sm">
                                            @foreach(['open' => 'Açık', 'in_progress' => 'İşlemde', 'waiting' => 'Bekliyor', 'completed' => 'Tamamlandı', 'cancelled' => 'İptal'] as $value => $text)
                                                <option value="{{ $value }}" @selected($task->status === $value)>{{ $text }}</option>
                                            @endforeach
                                        </select>
                                        <button class="btn btn-sm btn-primary">Kaydet</button>
                                    </div>
                                </form>
                                <div class="d-flex gap-1 mt-2">
                                    @if($task->status !== 'completed')
                                        <form method="POST" action="{{ route('admin.task-board.tasks.complete', $task) }}">
                                            @csrf
                                            <button class="btn btn-sm btn-success">Bitti</button>
                                        </form>
                                    @endif
                                    <form method="POST" action="{{ route('admin.task-board.tasks.destroy', $task) }}" onsubmit="return confirm('Görev silinsin mi?')">
                                        @csrf
                                        @method('DELETE')
                                        <button class="btn btn-sm btn-outline-danger">Sil</button>
                                    </form>
                                </div>
                            </div>
                        @empty
                            <p class="text-muted mb-0">Bu kolonda görev yok.</p>
                        @endforelse
                    </div>
                </div>
            </div>
        @endforeach
    </div>

    <div class="card mt-4">
        <div class="card-header"><h5 class="mb-0">Yeni Görev Oluştur</h5></div>
        <div class="card-body">
            <form method="POST" action="{{ route('admin.task-board.tasks.store') }}" class="row g-3">
                @csrf
                <div class="col-lg-3">
                    <label class="form-label">Başlık</label>
                    <input type="text" name="title" class="form-control" required>
                </div>
                <div class="col-lg-2">
                    <label class="form-label">Müşteri</label>
                    <select name="client_id" class="form-control">
                        <option value="">Genel</option>
                        @foreach($data['clients'] as $client)
                            <option value="{{ $client->id }}">{{ $client->company_name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-lg-2">
                    <label class="form-label">Kategori</label>
                    <input type="text" name="category" class="form-control" value="general">
                </div>
                <div class="col-lg-1">
                    <label class="form-label">Öncelik</label>
                    <select name="priority" class="form-control">
                        <option value="medium">Orta</option>
                        <option value="critical">Kritik</option>
                        <option value="high">Yüksek</option>
                        <option value="low">Düşük</option>
                    </select>
                </div>
                <div class="col-lg-2">
                    <label class="form-label">Atanan</label>
                    <select name="assigned_to" class="form-control">
                        <option value="">Atanmamış</option>
                        @foreach($data['users'] as $user)
                            <option value="{{ $user->id }}">{{ $user->name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-lg-2">
                    <label class="form-label">Termin</label>
                    <input type="datetime-local" name="due_at" class="form-control">
                </div>
                <div class="col-12">
                    <label class="form-label">Açıklama</label>
                    <textarea name="description" class="form-control" rows="2"></textarea>
                </div>
                <div class="col-12">
                    <button class="btn btn-primary">Görev Ekle</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection
