<?php
include '../config/koneksi.php';
include '../helpers/functions.php';

if (!isLogin() || !isClient()) redirect('../auth/login.php');

$id_user = $_SESSION['id'];

$bulan_awal = $_GET['bulan_awal'] ?? date('m');
$tahun_awal = $_GET['tahun_awal'] ?? date('Y');
$bulan_akhir = $_GET['bulan_akhir'] ?? date('m');
$tahun_akhir = $_GET['tahun_akhir'] ?? date('Y');

$tgl_awal = $tahun_awal.'-'.$bulan_awal.'-01';
$tgl_akhir = date("Y-m-t", strtotime($tahun_akhir.'-'.$bulan_akhir.'-01'));

$sql = "
SELECT tanggal,jenis,nominal,keterangan FROM (
    SELECT created_at AS tanggal,
           'Setoran' AS jenis,
           nominal,
           COALESCE(catatan_admin,'Setoran Saldo') AS keterangan
    FROM transactions
    WHERE user_id='$id_user' AND status='confirmed'

    UNION ALL

    SELECT created_at AS tanggal,
           'Penarikan' AS jenis,
           nominal,
           CONCAT(bank_tujuan,' - ',nama_rekening) AS keterangan
    FROM withdrawals
    WHERE user_id='$id_user' AND status='confirmed'
) m
WHERE tanggal BETWEEN '$tgl_awal 00:00:00' AND '$tgl_akhir 23:59:59'
ORDER BY tanggal ASC
";

$data=mysqli_query($conn,$sql);

$total_masuk=0;
$total_keluar=0;
$saldo=0;

$title="Rekap Mutasi Saldo";
include '../layouts/header.php';
?>
<div class="container-fluid">
<div class="row">
<div class="col-lg-2 p-0"><?php include '../layouts/sidebar.php';?></div>
<div class="col-lg-10 p-0">
<?php include '../layouts/navbar.php';?>
<div class="content-wrap">
<div class="card p-4">

<h4 class="mb-4">Rekap Mutasi Saldo</h4>

<form method="get" class="row g-2 mb-4">
<div class="col-md-2">
<select name="bulan_awal" class="form-select">
<?php for($i=1;$i<=12;$i++): ?>
<option value="<?=sprintf('%02d',$i)?>" <?=$bulan_awal==sprintf('%02d',$i)?'selected':''?>><?=date('F',mktime(0,0,0,$i,1));?></option>
<?php endfor;?>
</select>
</div>

<div class="col-md-2">
<input type="number" name="tahun_awal" class="form-control" value="<?=$tahun_awal?>">
</div>

<div class="col-md-2">
<select name="bulan_akhir" class="form-select">
<?php for($i=1;$i<=12;$i++): ?>
<option value="<?=sprintf('%02d',$i)?>" <?=$bulan_akhir==sprintf('%02d',$i)?'selected':''?>><?=date('F',mktime(0,0,0,$i,1));?></option>
<?php endfor;?>
</select>
</div>

<div class="col-md-2">
<input type="number" name="tahun_akhir" class="form-control" value="<?=$tahun_akhir?>">
</div>

<div class="col-md-4">
<button class="btn btn-primary">Tampilkan</button>
<a href="rekap_mutasi.php" class="btn btn-secondary">Reset</a>
</div>

</form>

<div class="table-responsive">
<table class="table table-bordered table-hover align-middle">
<thead>
<tr>
<th>No</th>
<th>Tanggal</th>
<th>Jenis</th>
<th>Keterangan</th>
<th>Masuk</th>
<th>Keluar</th>
<th>Saldo</th>
</tr>
</thead>
<tbody>

<?php
$no=1;
if(mysqli_num_rows($data)>0):
while($r=mysqli_fetch_assoc($data)):
$masuk=0;
$keluar=0;

if($r['jenis']=='Setoran'){
    $masuk=$r['nominal'];
    $saldo+=$masuk;
    $total_masuk+=$masuk;
}else{
    $keluar=$r['nominal'];
    $saldo-=$keluar;
    $total_keluar+=$keluar;
}
?>
<tr>
<td><?=$no++?></td>
<td><?=date('d-m-Y H:i',strtotime($r['tanggal']))?></td>
<td>
<?php if($r['jenis']=='Setoran'):?>
<span class="badge bg-success">Setoran</span>
<?php else:?>
<span class="badge bg-danger">Penarikan</span>
<?php endif;?>
</td>
<td><?=$r['keterangan']?></td>
<td><?=$masuk?rupiah($masuk):'-'?></td>
<td><?=$keluar?rupiah($keluar):'-'?></td>
<td><strong><?=rupiah($saldo)?></strong></td>
</tr>
<?php endwhile; else: ?>
<tr><td colspan="7" class="text-center">Tidak ada data.</td></tr>
<?php endif;?>
</tbody>
<tfoot>
<tr class="table-light">
<th colspan="4">TOTAL</th>
<th><?=rupiah($total_masuk)?></th>
<th><?=rupiah($total_keluar)?></th>
<th><?=rupiah($total_masuk-$total_keluar)?></th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</div>
</div>

<?php include '../layouts/footer.php'; ?>
