<?php
include "koneksi.php";

// PROSES PENGEMBALIAN
if(isset($_GET['kembali'])){

$id_detail=$_GET['kembali'];

$detail=
mysqli_fetch_assoc(
mysqli_query(
$koneksi,
"
SELECT
d.*,
p.tanggal_kembali
FROM detail_peminjaman d
JOIN peminjaman p
ON d.id_peminjaman=p.id_peminjaman
WHERE d.id_detail='$id_detail'
"
)
);

$id_buku=
$detail['id_buku'];

if(
empty($detail['tanggal_kembali'])
){

$hari_telat=0;

}else{

$batas=
strtotime(
$detail['tanggal_kembali']
);

$hari_ini=
strtotime(
date('Y-m-d')
);

$hari_telat=
max(
0,
floor(
($hari_ini-$batas)
/
86400
)
);

}

$hari_ini=
strtotime(
date('Y-m-d')
);

$hari_telat=0;

if($hari_ini>$batas){

$hari_telat=
floor(
($hari_ini-$batas)
/
86400
);

}

$setting=
mysqli_fetch_assoc(
mysqli_query(
$koneksi,
"
SELECT *
FROM setting_denda
LIMIT 1
"
)
);

$tarif=
$setting['denda_per_hari'];

$total_denda=
$hari_telat*
$tarif;

mysqli_query(
$koneksi,
"
UPDATE detail_peminjaman
SET

tanggal_kembali_aktual='".date('Y-m-d H:i:s')."',

status_buku='Dikembalikan',

hari_telat='$hari_telat',

denda='$total_denda'

WHERE id_detail='$id_detail'
"
);

mysqli_query(
$koneksi,
"
UPDATE peminjaman
SET
status_transaksi='Dikembalikan'
WHERE id_peminjaman='".$detail['id_peminjaman']."'
"
);

mysqli_query(
$koneksi,
"
UPDATE buku
SET

stok_tersedia=
stok_tersedia+1,

status='Tersedia'

WHERE id_buku='$id_buku'
"
);

echo "

<script>

alert(
'Pengembalian berhasil

Hari Telat :
$hari_telat Hari

Total Denda :
Rp ".number_format($total_denda)."
');

window.location='pengembalian.php';

</script>

";

}

// BATALKAN PENGEMBALIAN

if(isset($_GET['batal'])){

$id_detail=$_GET['batal'];

$detail=
mysqli_fetch_assoc(
mysqli_query(
$koneksi,
"
SELECT *
FROM detail_peminjaman
WHERE id_detail='$id_detail'
"
));

$id_buku=
$detail['id_buku'];

mysqli_query(
$koneksi,
"
UPDATE detail_peminjaman
SET

tanggal_kembali_aktual=NULL,

status_buku='Dipinjam',

hari_telat=0,

denda=0

WHERE id_detail='$id_detail'
"
);

mysqli_query(
$koneksi,
"
UPDATE peminjaman
SET

status_transaksi='Dipinjam'

WHERE id_peminjaman=
'".$detail['id_peminjaman']."'
"
);

mysqli_query(
$koneksi,
"
UPDATE buku
SET

stok_tersedia=
IF(stok_tersedia>0,
stok_tersedia-1,
0)

WHERE id_buku='$id_buku'
"
);

echo "

<script>

alert(
'Pengembalian dibatalkan'
);

window.location=
'pengembalian.php';

</script>

";

}
?>

<?php include "template/header.php"; ?>

<div class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">

<?php include "template/h_menu.php"; ?>
<?php include "sidebar.php"; ?>

<!-- CONTENT -->
<div class="content-wrapper">

<section class="content-header">

    <div class="container-fluid">

        <div class="row mb-2">

            <div class="col-sm-6">
                <h1>Data Pengembalian</h1>
            </div>

            <div class="col-sm-6">

                <ol class="breadcrumb float-sm-right">

                    <li class="breadcrumb-item">
                        <a href="index.php">Home</a>
                    </li>

                    <li class="breadcrumb-item active">
                        Pengembalian
                    </li>

                </ol>

            </div>

        </div>

    </div>

</section>

<!-- MAIN CONTENT -->
<section class="content">

<div class="container-fluid">

<div class="card">

<div class="card-header bg-success">

    <h3 class="card-title">
        Data Pengembalian Buku
    </h3>

</div>

<div class="card-body">

<table id="example1"
       class="table table-bordered table-striped">

<thead>

<tr>

    <th>No</th>
    <th>Nama Anggota</th>
    <th>Judul Buku</th>
    <th>Tanggal Pinjam</th>
    <th>Batas Kembali</th>
    <th>Kembali Aktual</th>
<th>Hari Telat</th>
<th>Denda</th>
<th>Status</th>
    <th>Aksi</th>

</tr>

</thead>

<tbody>

<?php
$no = 1;

$query = mysqli_query($koneksi,"
SELECT
p.*,
a.nama,
b.judul,
d.id_detail,
d.status_buku,
d.tanggal_kembali_aktual,
d.hari_telat,
d.denda
FROM peminjaman p
JOIN anggota a
ON p.id_anggota = a.id_anggota
JOIN detail_peminjaman d
ON p.id_peminjaman = d.id_peminjaman
JOIN buku b
ON d.id_buku = b.id_buku
ORDER BY p.id_peminjaman DESC
");

while($data = mysqli_fetch_assoc($query)){
?>

<tr>

    <td><?= $no++ ?></td>

    <td><?= htmlspecialchars($data['nama']) ?></td>

    <td><?= htmlspecialchars($data['judul']) ?></td>

    <td>
        <?= date('d-m-Y', strtotime($data['tanggal_pinjam'])) ?>
    </td>

    <td>
        <?= !empty($data['tanggal_kembali'])
? date(
'd-m-Y',
strtotime($data['tanggal_kembali'])
)
: '-'
?>
    </td>

    <td>

        <?php if($data['tanggal_kembali_aktual'] != NULL){ ?>

            <?= date('d-m-Y H:i',
            strtotime($data['tanggal_kembali_aktual'])) ?>

        <?php } else { ?>

            <span class="text-danger">
                Belum Dikembalikan
            </span>

        <?php } ?>

    </td>
<td>

<?=
$data['hari_telat'] ?? 0
?>

Hari

</td>

<td>

<?php

if(
($data['denda'] ?? 0)>0
){

?>

<span class="badge badge-danger">

Rp
<?= number_format($data['denda']) ?>

</span>

<?php }else{ ?>

<span class="badge badge-success">

Tidak Ada

</span>

<?php } ?>

</td>
    <td>

        <?php if($data['status_transaksi'] == 'Dipinjam'){ ?>

            <span class="badge badge-warning">
                Dipinjam
            </span>

        <?php } else { ?>

            <span class="badge badge-success">
                Dikembalikan
            </span>

        <?php } ?>

    </td>

    <td>

        <?php if($data['status_transaksi'] == 'Dipinjam'){ ?>

        <a href="pengembalian.php?kembali=<?= $data['id_detail'] ?>"
           class="btn btn-success btn-sm"
           onclick="return confirm('Yakin buku akan dikembalikan?')">

            <i class="fas fa-undo"></i>
            Kembalikan

        </a>

        <?php } else { ?>

<a
href="pengembalian.php?batal=<?= $data['id_detail'] ?>"
class="btn btn-danger btn-sm"

onclick="
return confirm(
'Batalkan pengembalian?'
)
">

<i class="fas fa-times"></i>

Batalkan

</a>

<?php } ?>

    </td>

</tr>

<?php } ?>

</tbody>

</table>

</div>
</div>
</div>
</section>
</div>

<?php include "template/footer.php"; ?>