<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>Laporan Laba Rugi - Sistem Informasi Keuangan</title>
	<style>
		* {
			margin: 0;
			padding: 0;
			box-sizing: border-box;
		}
		
		@media print {
			@page {
				size: A4 portrait;
				margin: 1.5cm;
			}
			body {
				-webkit-print-color-adjust: exact;
				print-color-adjust: exact;
			}
		}
		
		body {
			font-family: 'Georgia', 'Times New Roman', Times, serif;
			font-size: 11pt;
			line-height: 1.5;
			color: #2c3e50;
			background: #fff;
			padding: 20px;
		}
		
		.header {
			text-align: center;
			margin-bottom: 25px;
			border-bottom: 2px solid #95a5a6;
			padding-bottom: 15px;
		}
		
		.header h2 {
			font-size: 20pt;
			font-weight: bold;
			margin-bottom: 5px;
			letter-spacing: 2px;
			color: #34495e;
		}
		
		.header h3 {
			font-size: 14pt;
			font-weight: normal;
			margin-top: 5px;
			color: #7f8c8d;
		}
		
		.info-section {
			margin-bottom: 20px;
		}
		
		.info-table {
			width: 100%;
			border-collapse: collapse;
			margin-bottom: 20px;
		}
		
		.info-table td {
			padding: 8px 15px;
			border: 1px solid #d5d5d5;
		}
		
		.info-table td:first-child {
			background-color: #f0f0f0;
			text-align: left;
			font-weight: bold;
			width: 30%;
			color: #2c3e50;
		}
		
		.main-table {
			width: 100%;
			border-collapse: collapse;
			margin-bottom: 25px;
			border: 1px solid #bdc3c7;
		}
		
		.main-table th {
			background-color: #ecf0f1 !important;
			border: 1px solid #bdc3c7;
			padding: 12px 10px;
			text-align: left;
			font-weight: bold;
			font-size: 11pt;
			color: #2c3e50;
		}
		
		.main-table td {
			border: 1px solid #d5d5d5;
			padding: 10px;
			font-size: 10pt;
			color: #2c3e50;
		}
		
		.main-table td.text-left {
			text-align: left;
		}
		
		.main-table td.text-right {
			text-align: right;
		}
		
		.section-header {
			background-color: #e8f5e9 !important;
			font-weight: bold;
			font-size: 12pt;
			padding: 12px 15px;
		}
		
		.section-header-beban {
			background-color: #ffebee !important;
			font-weight: bold;
			font-size: 12pt;
			padding: 12px 15px;
		}
		
		.item-row {
			padding-left: 30px !important;
		}
		
		.total-row {
			background-color: #c8e6c9 !important;
			font-weight: bold;
			border-top: 2px solid #95a5a6;
		}
		
		.total-row-beban {
			background-color: #ffcdd2 !important;
			font-weight: bold;
			border-top: 2px solid #95a5a6;
		}
		
		.laba-rugi-row {
			background-color: #e1f5fe !important;
			font-weight: bold;
			font-size: 13pt;
			border-top: 3px solid #0277bd;
			padding: 15px !important;
		}
		
		.laba-rugi-row.rugi {
			background-color: #fff3e0 !important;
			border-top-color: #e65100;
		}
		
		.text-right {
			text-align: right;
		}
		
		.text-center {
			text-align: center;
		}
		
		.text-left {
			text-align: left;
		}
		
		.text-bold {
			font-weight: bold;
		}
	</style>
</head>
<body>

	<div class="header">
		<h2>LAPORAN LABA RUGI</h2>
		<h3>SISTEM INFORMASI KEUANGAN</h3>
	</div>

	<?php 
	include '../koneksi.php';
	
	if(isset($_GET['periode'])){
		$periode = $_GET['periode'];
		
		// Tentukan tanggal berdasarkan periode
		if($periode == 'bulan'){
			$tgl_dari = date('Y-m-01');
			$tgl_sampai = date('Y-m-t');
			$periode_text = "Bulan " . date('F Y');
		}elseif($periode == 'tahun'){
			$tgl_dari = date('Y-01-01');
			$tgl_sampai = date('Y-12-31');
			$periode_text = "Tahun " . date('Y');
		}elseif($periode == 'custom'){
			if(isset($_GET['tanggal_dari']) && isset($_GET['tanggal_sampai'])){
				$tgl_dari = $_GET['tanggal_dari'];
				$tgl_sampai = $_GET['tanggal_sampai'];
				$periode_text = date('d F Y', strtotime($tgl_dari)) . " - " . date('d F Y', strtotime($tgl_sampai));
			}else{
				echo '<div style="text-align:center; padding:20px;">Silahkan pilih tanggal untuk periode kustom.</div>';
				exit;
			}
		}else{
			echo '<div style="text-align:center; padding:20px;">Periode tidak valid.</div>';
			exit;
		}
		?>

		<div class="info-section">
			<table class="info-table">
				<tr>
					<td>PERIODE LAPORAN</td>
					<td>:</td>
					<td><?php echo $periode_text; ?></td>
				</tr>
				<tr>
					<td>TANGGAL LAPORAN</td>
					<td>:</td>
					<td><?php echo date('d F Y'); ?></td>
				</tr>
			</table>
		</div>

		<table class="main-table">
			<thead>
				<tr>
					<th width="60%" class="text-left">KETERANGAN</th>
					<th width="40%" class="text-right">JUMLAH (Rp)</th>
				</tr>
			</thead>
			<tbody>
				<?php 
				// ========== BAGIAN PENDAPATAN ==========
				?>
				<tr>
					<td colspan="2" class="section-header">PENDAPATAN</td>
				</tr>
				
				<?php
				$total_pendapatan = 0;
				
				// Ambil semua kategori pemasukan
				$kategori_pemasukan = mysqli_query($koneksi, "
					SELECT k.kategori_id, k.kategori, 
						   COALESCE(SUM(t.transaksi_nominal), 0) as total
					FROM kategori k
					LEFT JOIN transaksi t ON k.kategori_id = t.transaksi_kategori 
						AND (t.transaksi_jenis = 'Pemasukan' OR t.transaksi_jenis = 'pemasukan')
						AND DATE(t.transaksi_tanggal) >= '$tgl_dari'
						AND DATE(t.transaksi_tanggal) <= '$tgl_sampai'
					GROUP BY k.kategori_id, k.kategori
					HAVING total > 0
					ORDER BY k.kategori ASC
				");
				
				$ada_pendapatan = false;
				while($kp = mysqli_fetch_array($kategori_pemasukan)){
					if($kp['total'] > 0){
						$ada_pendapatan = true;
						$total_pendapatan += $kp['total'];
						?>
						<tr>
							<td class="text-left item-row"><?php echo $kp['kategori']; ?></td>
							<td class="text-right"><?php echo number_format($kp['total'], 0, ',', '.'); ?></td>
						</tr>
						<?php
					}
				}
				
				if(!$ada_pendapatan){
					?>
					<tr>
						<td class="text-left item-row" style="color: #999;">Tidak ada pendapatan</td>
						<td class="text-right" style="color: #999;">0</td>
					</tr>
					<?php
				}
				?>
				
				<tr class="total-row">
					<td class="text-left text-bold" style="padding-left: 20px;">TOTAL PENDAPATAN</td>
					<td class="text-right text-bold"><?php echo number_format($total_pendapatan, 0, ',', '.'); ?></td>
				</tr>
				
				<?php
				// ========== BAGIAN BEBAN ==========
				?>
				<tr>
					<td colspan="2" class="section-header-beban">BEBAN</td>
				</tr>
				
				<?php
				$total_beban = 0;
				
				// Ambil semua kategori pengeluaran
				$kategori_pengeluaran = mysqli_query($koneksi, "
					SELECT k.kategori_id, k.kategori, 
						   COALESCE(SUM(t.transaksi_nominal), 0) as total
					FROM kategori k
					LEFT JOIN transaksi t ON k.kategori_id = t.transaksi_kategori 
						AND (t.transaksi_jenis = 'Pengeluaran' OR t.transaksi_jenis = 'pengeluaran')
						AND DATE(t.transaksi_tanggal) >= '$tgl_dari'
						AND DATE(t.transaksi_tanggal) <= '$tgl_sampai'
					GROUP BY k.kategori_id, k.kategori
					HAVING total > 0
					ORDER BY k.kategori ASC
				");
				
				$ada_beban = false;
				while($kb = mysqli_fetch_array($kategori_pengeluaran)){
					if($kb['total'] > 0){
						$ada_beban = true;
						$total_beban += $kb['total'];
						?>
						<tr>
							<td class="text-left item-row"><?php echo $kb['kategori']; ?></td>
							<td class="text-right"><?php echo number_format($kb['total'], 0, ',', '.'); ?></td>
						</tr>
						<?php
					}
				}
				
				if(!$ada_beban){
					?>
					<tr>
						<td class="text-left item-row" style="color: #999;">Tidak ada beban</td>
						<td class="text-right" style="color: #999;">0</td>
					</tr>
					<?php
				}
				?>
				
				<tr class="total-row-beban">
					<td class="text-left text-bold" style="padding-left: 20px;">TOTAL BEBAN</td>
					<td class="text-right text-bold"><?php echo number_format($total_beban, 0, ',', '.'); ?></td>
				</tr>
				
				<?php
				// ========== BAGIAN LABA/RUGI BERSIH ==========
				$laba_rugi = $total_pendapatan - $total_beban;
				$is_rugi = $laba_rugi < 0;
				$label = $laba_rugi >= 0 ? 'LABA BERSIH' : 'RUGI BERSIH';
				?>
				
				<tr class="laba-rugi-row <?php echo $is_rugi ? 'rugi' : ''; ?>">
					<td class="text-left text-bold"><?php echo $label; ?></td>
					<td class="text-right text-bold"><?php echo number_format(abs($laba_rugi), 0, ',', '.'); ?></td>
				</tr>
				
			</tbody>
		</table>

		<?php 
	}else{
		?>
		<div style="text-align:center; padding:20px; font-family: Georgia, serif;">
			Silahkan Filter Laporan Terlebih Dulu.
		</div>
		<?php
	}
	?>

	<script>
		window.print();
	</script>

</body>
</html>
