@extends('layouts.app')
@section('title', 'Jadwal Bulanan - SIM Jaga RSUD')
@section('caption', 'Jadwal Bulan Berjalan')
@section('content')
@php
    $leader = $employees->firstWhere('role', 'Pimpinan');
    $pj = $employees->where('role', 'PJ')->values();
    $previous = $month->copy()->subMonth()->format('Y-m');
    $next = $month->copy()->addMonth()->format('Y-m');
@endphp
<div class="page-heading">
    <div><p class="eyebrow">JADWAL PERAWAT RSUD</p><h1>{{ $month->translatedFormat('F Y') }}</h1><p>Jadwal shift bulan berjalan dan daftar ketidakhadiran pegawai.</p></div>
    <button class="primary-btn" data-open="leave-public">+ Tambah Pengajuan</button>
</div>
<section class="hero">
    <div class="hero-copy">
        <p class="hero-label">{{ now()->translatedFormat('l, d F Y') }}</p>
        <h2>Pelayanan yang baik dimulai dari jadwal yang tertata.</h2>
        <p>Setiap shift memiliki penanggung jawab dan rotasi perawat yang jelas.</p>
        <div class="hero-tags"><span><i></i> Jadwal terpublikasi</span><span>Dikelola melalui back office Laravel</span></div>
    </div>
</section>
<section class="stats-grid">
    <article class="stat-card"><div class="stat-icon teal">P</div><div><p>Pegawai Aktif</p><strong>{{ $employees->count() }}</strong><span>Masuk rotasi jadwal</span></div></article>
    <article class="stat-card"><div class="stat-icon blue">PJ</div><div><p>Penanggung Jawab</p><strong>{{ $pj->count() }}</strong><span>Tersedia per shift</span></div></article>
    <article class="stat-card"><div class="stat-icon orange">L</div><div><p>Hari Libur</p><strong>{{ $holidays->count() }}</strong><span>Pada bulan ini</span></div></article>
    <article class="stat-card"><div class="stat-icon purple">C</div><div><p>Cuti / Izin</p><strong>{{ $leaves->count() }}</strong><span>Pengajuan aktif</span></div></article>
</section>
<section class="card monthly">
    <div class="section-head monthly-head">
        <div><p class="eyebrow">KALENDER SHIFT</p><h3>Jadwal {{ $month->translatedFormat('F Y') }}</h3></div>
        <div class="calendar-actions">
            <div class="legend"><span><i class="pagi"></i>Pagi</span><span><i class="siang"></i>Siang</span><span><i class="malam"></i>Malam</span><span><i class="libur"></i>Libur</span></div>
            <div class="month-control"><a href="{{ route('schedule', ['month' => $previous]) }}">&lsaquo;</a><strong>{{ $month->translatedFormat('F Y') }}</strong><a href="{{ route('schedule', ['month' => $next]) }}">&rsaquo;</a></div>
        </div>
    </div>
    <div class="calendar-week"><b>Senin</b><b>Selasa</b><b>Rabu</b><b>Kamis</b><b>Jumat</b><b>Sabtu</b><b>Minggu</b></div>
    <div class="month-grid">
        @foreach($calendarDays as $day)
        <div class="day-cell {{ $day['current_month'] ? '' : 'dim' }} {{ $day['date']->isToday() ? 'today' : '' }}">
            <span class="day-num">{{ $day['date']->day }}</span>
            <div class="day-shifts"><span class="chip-pagi">P {{ 6 + $day['date']->day % 3 }}</span><span class="chip-siang">S {{ 5 + $day['date']->day % 2 }}</span><span class="chip-malam">M {{ 4 + $day['date']->day % 2 }}</span></div>
            @if($day['holiday'])<div class="day-note">{{ $day['holiday']->name }}</div>@elseif($day['leave_count'])<div class="day-note">{{ $day['leave_count'] }} pegawai tidak masuk</div>@endif
        </div>
        @endforeach
    </div>
</section>
<section class="card requests">
    <div class="section-head"><div><p class="eyebrow">ROTASI SHIFT</p><h3>Pembagian Shift Utama</h3></div><span class="status">PJ tersedia pada setiap shift</span></div>
    <div class="shift-list">
        @foreach([['Pagi','07:00 - 14:00','pagi'],['Siang','14:00 - 21:00','siang'],['Malam','21:00 - 07:00','malam']] as $index => $shift)
        @php
            $shiftNurses = $nurses->count() ? collect(range(0, min(3, $nurses->count() - 1)))->map(fn ($offset) => $nurses[($index * 2 + $offset) % $nurses->count()]) : collect();
            $shiftPj = $pj->count() ? $pj[$index % $pj->count()] : $shiftNurses->first();
        @endphp
        <div class="shift-row"><div><div class="shift-name"><i class="{{ $shift[2] }}"></i>{{ $shift[0] }}</div><small class="shift-time">{{ $shift[1] }}</small></div><div class="nurses">@foreach($shiftNurses as $nurse)<span class="face f{{ $loop->iteration }}" title="{{ $nurse->name }}">{{ str($nurse->name)->afterLast(' ')->substr(0, 2)->upper() }}</span>@endforeach</div><span class="pj">PJ: {{ $shiftPj?->name ?? 'Belum diatur' }}</span></div>
        @endforeach
    </div>
</section>
<section class="card requests">
    <div class="section-head"><div><p class="eyebrow">KETIDAKHADIRAN</p><h3>Cuti, Izin, Sakit & Libur Pengganti</h3></div></div>
    <div class="data-table">
        <div class="request-row request-head"><span>Pegawai</span><span>Jenis</span><span>Periode</span><span>Durasi</span><span>Status</span></div>
        @forelse($leaves as $leave)
        <div class="request-row"><strong>{{ $leave->employee->name }}</strong><span class="badge {{ str($leave->type)->slug() }}">{{ $leave->type }}</span><span>{{ $leave->start_date->translatedFormat('d M Y') }} - {{ $leave->end_date->translatedFormat('d M Y') }}</span><span>{{ $leave->start_date->diffInDays($leave->end_date) + 1 }} hari</span><span class="status">{{ $leave->status }}</span></div>
        @empty <p class="empty-state">Belum ada pengajuan pada bulan ini.</p> @endforelse
    </div>
</section>
@endsection
@section('modals')
<div class="modal-backdrop" id="leave-public">
    <form class="modal-card" method="POST" action="{{ route('schedule.leaves.store') }}">
        @csrf
        <button type="button" class="modal-close" data-close>x</button><p class="eyebrow">PENGAJUAN BARU</p><h3>Tambah Cuti / Izin</h3>
        <label>Nama pegawai<select name="employee_id" required>@foreach($employees as $employee)<option value="{{ $employee->id }}">{{ $employee->name }}</option>@endforeach</select></label>
        <label>Jenis<select name="type" required>@foreach(['Cuti','Izin','Sakit','Libur Pengganti'] as $type)<option>{{ $type }}</option>@endforeach</select></label>
        <div class="form-row"><label>Tanggal mulai<input name="start_date" type="date" required></label><label>Sampai<input name="end_date" type="date" required></label></div>
        <label>Catatan<textarea name="notes"></textarea></label>
        <button class="primary-btn form-submit">Kirim Pengajuan</button>
    </form>
</div>
@endsection
