@extends('agency.layouts.app')

@section('title', 'Çalışma Programım')
@section('page_title', 'Çalışma Programım')

@php
    $breadcrumbs = [
        ['title' => 'Dashboard', 'url' => route('agency.dashboard')],
        ['title' => 'Çalışma Programım', 'url' => '']
    ];

    $user      = Auth::user();
    $agencyId  = Auth::user()->currentAgencyId();

    $resourceIds = \App\Models\Resource::where('user_id', $user->id)
        ->where('agency_id', $agencyId)
        ->pluck('id')
        ->toArray();

    $schedules = \App\Models\ResourceSchedule::with('resource')
        ->whereIn('resource_id', $resourceIds)
        ->where('agency_id', $agencyId)
        ->orderBy('start_date', 'desc')
        ->paginate(25);

    $typeColors = [
        'tour_assignment' => ['bg'=>'#eff6ff','text'=>'#2563eb','dot'=>'#3b82f6','label'=>'Tur Görevi'],
        'block'           => ['bg'=>'#fff1f2','text'=>'#e11d48','dot'=>'#ef4444','label'=>'Genel Bloke'],
        'maintenance'     => ['bg'=>'#fffbeb','text'=>'#d97706','dot'=>'#f59e0b','label'=>'Araç Bakım'],
        'leave'           => ['bg'=>'#f5f3ff','text'=>'#7c3aed','dot'=>'#8b5cf6','label'=>'İzin / Tatil'],
    ];
@endphp

@push('styles')
<style>
.my-sched-layout { display:block; }
.ms-topbar { display:flex; flex-wrap:wrap; gap:12px 20px; align-items:center; background:#fff; border:1px solid #e2e8f0; border-radius:14px; padding:12px 16px; margin-bottom:16px; box-shadow:0 1px 4px rgba(0,0,0,.04); }
.ms-grp { display:flex; flex-wrap:wrap; gap:12px; align-items:center; }
.ms-cap { font-size:.68rem; font-weight:800; text-transform:uppercase; letter-spacing:.03em; color:#94a3b8; }
.ms-topbar .li { display:inline-flex; align-items:center; gap:6px; font-size:.78rem; font-weight:600; color:#475569; }
.ms-topbar .li .dot { width:9px; height:9px; border-radius:50%; }
.ms-res { display:inline-flex; align-items:center; gap:7px; background:#f8fafc; border:1px solid #e2e8f0; border-radius:9px; padding:4px 10px 4px 5px; font-size:.78rem; font-weight:700; color:#334155; }

.panel-card { background:#fff;border-radius:16px;border:1px solid #f1f5f9;box-shadow:0 1px 4px rgba(0,0,0,.04);overflow:hidden; }
.panel-head { padding:.9rem 1.25rem;border-bottom:1px solid #f8fafc;font-size:.75rem;font-weight:700;color:#475569;letter-spacing:.4px;text-transform:uppercase;display:flex;align-items:center;gap:.5rem; }
.panel-body { padding:1.25rem; }

.legend-item { display:flex;align-items:center;gap:.6rem;margin-bottom:.6rem; }
.legend-dot  { width:10px;height:10px;border-radius:50%;flex-shrink:0; }
.legend-txt  { font-size:.8rem;color:#475569;font-weight:500; }

/* Table */
.sched-table-wrap { background:#fff;border-radius:16px;border:1px solid #f1f5f9;box-shadow:0 1px 4px rgba(0,0,0,.04);overflow:hidden; }
.sched-table { width:100%;border-collapse:collapse; }
.sched-table thead th { padding:11px 16px;font-size:.7rem;font-weight:700;letter-spacing:.6px;text-transform:uppercase;color:#64748b;background:#f8fafc;border-bottom:1px solid #f1f5f9;white-space:nowrap; }
.sched-table tbody tr { border-bottom:1px solid #f8fafc;transition:background .12s; }
.sched-table tbody tr:last-child { border-bottom:none; }
.sched-table tbody tr:hover { background:#fafbff; }
.sched-table td { padding:13px 16px;vertical-align:middle; }

.r-dot { width:36px;height:36px;border-radius:10px;display:flex;align-items:center;justify-content:center;font-weight:700;font-size:.8rem;color:#fff;flex-shrink:0; }
.rd-guide   { background:linear-gradient(135deg,#3b82f6,#2563eb); }
.rd-vehicle { background:linear-gradient(135deg,#10b981,#059669); }
.rd-captain { background:linear-gradient(135deg,#8b5cf6,#7c3aed); }

.type-badge { display:inline-flex;align-items:center;gap:4px;padding:3px 9px;border-radius:20px;font-size:.7rem;font-weight:700; }

.date-range { font-size:.8rem;color:#0f172a;font-weight:600; }
.date-sub   { font-size:.72rem;color:#94a3b8;margin-top:2px; }

.empty-state { text-align:center;padding:4rem 2rem; }
.empty-icon  { font-size:3rem;color:#cbd5e1;margin-bottom:1rem; }

.pag-wrap { padding:1rem 1.25rem;display:flex;justify-content:space-between;align-items:center;border-top:1px solid #f1f5f9; }
</style>
@endpush

@section('content')
<div class="container-fluid">

    <div class="d-flex align-items-center justify-content-between mb-4">
        <div>
            <h2 class="h5 fw-bold mb-1">Çalışma Programım</h2>
            <p class="text-muted small mb-0">Hesabınıza atanmış kaynakların görev ve planlama kayıtları.</p>
        </div>
    </div>

    <div class="my-sched-layout">

        {{-- Yatay üst bar: plan türleri + kaynaklarım --}}
        <div class="ms-topbar">
            <div class="ms-grp">
                <span class="ms-cap">Plan Türleri</span>
                @foreach($typeColors as $tc)
                    <span class="li"><span class="dot" style="background:{{ $tc['dot'] }}"></span>{{ $tc['label'] }}</span>
                @endforeach
            </div>
            @if(count($resourceIds) > 0)
                @php $myResources = \App\Models\Resource::whereIn('id',$resourceIds)->get(); @endphp
                <div class="ms-grp" style="margin-left:auto">
                    <span class="ms-cap">Kaynaklarım</span>
                    @foreach($myResources as $mr)
                        @php $mi = collect(explode(' ',$mr->name))->map(fn($w)=>mb_strtoupper(mb_substr($w,0,1)))->take(2)->implode(''); @endphp
                        <span class="ms-res"><span class="r-dot rd-{{ $mr->type }}" style="width:22px;height:22px;font-size:.62rem">{{ $mi }}</span>{{ $mr->name }}</span>
                    @endforeach
                </div>
            @endif
        </div>

        {{-- Table (tam genişlik) --}}
        <div class="sched-table-wrap">
            @if($schedules->isEmpty())
                <div class="empty-state">
                    <div class="empty-icon"><i class="fas fa-calendar-times"></i></div>
                    <div style="font-size:1.05rem;font-weight:700;color:#475569">Görev Bulunamadı</div>
                    <div style="font-size:.875rem;color:#94a3b8;margin-top:.4rem">
                        Hesabınıza bağlı kaynaklar için atanmış aktif bir program kaydı bulunmamaktadır.
                    </div>
                </div>
            @else
                <table class="sched-table">
                    <thead>
                        <tr>
                            <th>KAYNAK</th>
                            <th>GÖREV / BAŞLIK</th>
                            <th>TARİH ARALIĞI</th>
                            <th>SÜRE</th>
                            <th>PLAN TÜRÜ</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($schedules as $sched)
                            @php
                                $tc = $typeColors[$sched->schedule_type] ?? ['bg'=>'#f1f5f9','text'=>'#475569','dot'=>'#94a3b8','label'=>$sched->schedule_type];
                                $res = $sched->resource;
                                $mi  = collect(explode(' ',$res->name))->map(fn($w)=>mb_strtoupper(mb_substr($w,0,1)))->take(2)->implode('');
                                $days = $sched->start_date->diffInDays($sched->end_date) + 1;
                            @endphp
                            <tr>
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <div class="r-dot rd-{{ $res->type }}">{{ $mi }}</div>
                                        <div>
                                            <div style="font-size:.83rem;font-weight:700;color:#0f172a">{{ $res->name }}</div>
                                            <div style="font-size:.72rem;color:#94a3b8">
                                                {{ $res->type==='guide'?'Rehber':($res->type==='vehicle'?'Araç':'Kaptan') }}
                                            </div>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div style="font-size:.83rem;font-weight:700;color:#0f172a">{{ $sched->title }}</div>
                                    @if($sched->description)
                                        <div style="font-size:.72rem;color:#94a3b8;margin-top:2px;max-width:240px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis">
                                            {{ $sched->description }}
                                        </div>
                                    @endif
                                </td>
                                <td>
                                    <div class="date-range">
                                        <i class="far fa-calendar-alt me-1 text-muted"></i>
                                        {{ $sched->start_date->format('d.m.Y') }}
                                        @if($sched->start_date->ne($sched->end_date))
                                             → {{ $sched->end_date->format('d.m.Y') }}
                                        @endif
                                    </div>
                                    @if($sched->start_time)
                                        <div class="date-sub">
                                            <i class="far fa-clock me-1"></i>
                                            {{ substr($sched->start_time,0,5) }} – {{ substr($sched->end_time,0,5) }}
                                        </div>
                                    @endif
                                </td>
                                <td>
                                    <span style="font-size:.8rem;font-weight:700;color:#0f172a">{{ $days }}</span>
                                    <span style="font-size:.72rem;color:#94a3b8"> gün</span>
                                </td>
                                <td>
                                    <span class="type-badge" style="background:{{ $tc['bg'] }};color:{{ $tc['text'] }}">
                                        {{ $tc['label'] }}
                                    </span>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>

                @if($schedules->hasPages())
                    <div class="pag-wrap">
                        <div style="font-size:.8rem;color:#64748b">
                            {{ $schedules->firstItem() }}–{{ $schedules->lastItem() }} / {{ $schedules->total() }} kayıt
                        </div>
                        {{ $schedules->links('vendor.pagination.bootstrap-5') }}
                    </div>
                @endif
            @endif
        </div>

    </div>
</div>
@endsection
