<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cetak Laporan - {{ strtoupper($title ?? 'Laporan') }}</title>
    <style>
        body {
            font-family: 'Times New Roman', Times, serif;
            font-size: 12pt;
            color: #000;
            background: #fff;
            margin: 0;
            padding: 20px;
        }
        .kop-surat {
            text-align: center;
            border-bottom: 3px solid #000;
            padding-bottom: 15px;
            margin-bottom: 20px;
            position: relative;
        }
        .kop-surat img {
            position: absolute;
            left: 20px;
            top: 0;
            height: 80px;
        }
        .kop-surat h1 {
            font-size: 18pt;
            margin: 0;
            text-transform: uppercase;
        }
        .kop-surat h2 {
            font-size: 14pt;
            margin: 5px 0;
        }
        .kop-surat p {
            font-size: 10pt;
            margin: 0;
        }
        .report-title {
            text-align: center;
            font-size: 14pt;
            font-weight: bold;
            margin: 20px 0 10px 0;
            text-transform: uppercase;
            text-decoration: underline;
        }
        .filter-info {
            margin-bottom: 15px;
            font-size: 11pt;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        table, th, td {
            border: 1px solid #000;
        }
        th, td {
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
            -webkit-print-color-adjust: exact;
        }
        .text-center {
            text-align: center;
        }
        .text-right {
            text-align: right;
        }
        .signature {
            margin-top: 50px;
            width: 100%;
            display: table;
        }
        .signature-col {
            display: table-cell;
            width: 50%;
            text-align: center;
        }
        .signature-space {
            height: 80px;
        }
        
        @media print {
            body {
                padding: 0;
            }
            .no-print {
                display: none;
            }
            @page {
                size: A4;
                margin: 2cm;
            }
        }
    </style>
</head>
<body onload="window.print()">

    <div class="no-print" style="margin-bottom: 20px; text-align: right;">
        <button onclick="window.print()" style="padding: 8px 15px; background: #0f172a; color: white; border: none; border-radius: 4px; cursor: pointer;">
            🖨️ Cetak Sekarang
        </button>
        <button onclick="window.close()" style="padding: 8px 15px; background: #ef4444; color: white; border: none; border-radius: 4px; cursor: pointer; margin-left: 10px;">
            ❌ Tutup
        </button>
    </div>

    <div class="kop-surat">
        <img src="{{ asset('images/logo tirta sanjiwani.png') }}" alt="Logo PDAM" style="position: absolute; left: 10px; top: 0; width: 80px; height: auto;">
        <h1>PERUSAHAAN DAERAH AIR MINUM</h1>
        <h2>(PDAM)</h2>
        <p>Jl. Kendedes No.8, Gianyar, Kec. Gianyar, Kabupaten Gianyar, Bali 80511</p>
        <p>Telp: +62 823-3152-7309 | Email: tekleperumda@gmail.com</p>
    </div>

    <div class="report-title">
        LAPORAN {{ $title ?? 'DATA' }}
    </div>

    <div class="filter-info">
        @if(!empty($filters['tanggal_dari']) || !empty($filters['tanggal_sampai']))
            <strong>Periode:</strong> 
            {{ !empty($filters['tanggal_dari']) ? \Carbon\Carbon::parse($filters['tanggal_dari'])->format('d M Y') : 'Awal' }} 
            s/d 
            {{ !empty($filters['tanggal_sampai']) ? \Carbon\Carbon::parse($filters['tanggal_sampai'])->format('d M Y') : 'Sekarang' }}<br>
        @else
            <strong>Periode:</strong> Semua Waktu<br>
        @endif
        
        @if(!empty($filters['status']))
            <strong>Status:</strong> {{ strtoupper(str_replace('_', ' ', $filters['status'])) }}<br>
        @endif
        
        @if(!empty($filters['search']))
            <strong>Pencarian:</strong> "{{ $filters['search'] }}"<br>
        @endif
    </div>

    <table>
        <thead>
            <tr>
                <th width="5%" class="text-center">No</th>
                @foreach($columns as $col)
                    <th>{{ $col }}</th>
                @endforeach
            </tr>
        </thead>
        <tbody>
            @forelse($data as $index => $item)
                <tr>
                    <td class="text-center">{{ $index + 1 }}</td>
                    
                    @if($type == 'kasbon')
                        <td>{{ $item->nama_teknisi ?? '-' }}</td>
                        <td>{{ \Carbon\Carbon::parse($item->tanggal_kasbon)->format('d/m/Y') }}</td>
                        <td class="text-right">Rp {{ number_format($item->jumlah_kasbon, 0, ',', '.') }}</td>
                        <td class="text-center">{{ strtoupper($item->status) }}</td>
                        <td>{{ $item->keperluan ?? '-' }}</td>
                    
                    @elseif($type == 'teknisi')
                        <td>{{ $item->nama }}</td>
                        <td class="text-center">{{ strtoupper($item->status) }}</td>
                        <td class="text-center">{{ $item->hadir }}</td>
                        <td class="text-center">{{ $item->izin }}</td>
                        <td class="text-center">{{ $item->sakit ?? 0 }}</td>
                        <td class="text-center">{{ $item->alpha }}</td>
                        <td class="text-center">{{ $item->total_absensi }}</td>
                        <td class="text-center">
                            {{ $item->total_absensi > 0 ? round(($item->hadir / $item->total_absensi) * 100, 1) : 0 }}%
                        </td>
                    
                    @elseif($type == 'absensi')
                        <td>{{ $item->nama_teknisi ?? '-' }}</td>
                        <td>{{ \Carbon\Carbon::parse($item->tanggal)->format('d/m/Y') }}</td>
                        <td class="text-center">{{ strtoupper($item->status) }}</td>
                        <td>{{ $item->keterangan ?? '-' }}</td>
                    
                    @elseif($type == 'pekerjaan')
                        <td>{{ $item->id_penugasan }}</td>
                        <td>{{ $item->jenis_pekerjaan ?? '-' }}</td>
                        <td>{{ $item->nama_teknisi ?? '-' }}</td>
                        <td class="text-center">{{ strtoupper(str_replace('_', ' ', $item->status_pekerjaan)) }}</td>
                        <td>{{ $item->tanggal_mulai ? \Carbon\Carbon::parse($item->tanggal_mulai)->format('d/m/Y') : '-' }}</td>
                        <td>{{ $item->tanggal_diselesaikan ? \Carbon\Carbon::parse($item->tanggal_diselesaikan)->format('d/m/Y') : '-' }}</td>
                        
                    @elseif($type == 'penggajian')
                        <td>{{ $item->nama_teknisi ?? '-' }}</td>
                        <td>
                            @php
                                $bulan = [1=>'Januari',2=>'Februari',3=>'Maret',4=>'April',5=>'Mei',6=>'Juni',7=>'Juli',8=>'Agustus',9=>'September',10=>'Oktober',11=>'November',12=>'Desember'];
                                $namaBulan = $bulan[(int)$item->periode_bulan] ?? '-';
                            @endphp
                            {{ $namaBulan }} {{ $item->periode_tahun }}
                        </td>
                        <td class="text-right">Rp {{ number_format($item->gaji_bersih, 0, ',', '.') }}</td>
                        <td class="text-center">{{ strtoupper(str_replace('_', ' ', $item->status_pembayaran)) }}</td>
                    
                    @elseif($type == 'data_teknisi')
                        <td>{{ $item->nama ?? '-' }}</td>
                        <td>{{ $item->email ?? '-' }}</td>
                        <td>{{ $item->no_telephone ?? '-' }}</td>
                        <td class="text-center">{{ $item->tanggal_masuk ? \Carbon\Carbon::parse($item->tanggal_masuk)->format('d/m/Y') : '-' }}</td>
                        <td class="text-center">{{ strtoupper(str_replace('_', ' ', $item->status ?? '-')) }}</td>
                    @endif
                </tr>
            @empty
                <tr>
                    <td colspan="{{ count($columns) + 1 }}" class="text-center">Tidak ada data yang sesuai dengan filter.</td>
                </tr>
            @endforelse
        </tbody>
    </table>

    <div class="signature">
        <div class="signature-col" style="width: 60%;">
            <!-- Kolom kosong di kiri -->
        </div>
        <div class="signature-col" style="width: 40%; text-align: center;">
            <p>Gianyar, {{ \Carbon\Carbon::now()->format('d F Y') }}</p>
            <p><strong>Mandor Lapangan</strong></p>
            <div class="signature-space"></div>
            <p><strong>(Kusaini)</strong></p>
        </div>
    </div>

</body>
</html>
