<?php
session_start();

if(
!isset($_SESSION['status'])
||
$_SESSION['status']!='login'
){
header("Location: login.php");
exit();
}

include "koneksi.php";


// TOTAL
$total_anggota=
mysqli_fetch_assoc(
mysqli_query(
$koneksi,
"SELECT COUNT(*) jml FROM anggota"
)
)['jml'] ?? 0;

$total_buku=
mysqli_fetch_assoc(
mysqli_query(
$koneksi,
"SELECT COUNT(*) total FROM buku"
)
)['total'] ?? 0;

$total_peminjaman=
mysqli_fetch_assoc(
mysqli_query(
$koneksi,
"SELECT COUNT(*) jml FROM peminjaman"
)
)['jml'] ?? 0;

$total_tersedia=
mysqli_fetch_assoc(
mysqli_query(
$koneksi,
"
SELECT
SUM(stok_tersedia)
tersedia
FROM buku
"
)
)['tersedia'] ?? 0;



// GRAFIK PINJAM
$bulan=[];

$peminjaman=[];

$q=
mysqli_query(
$koneksi,
"
SELECT

MONTH(
tanggal_pinjam
)
bulan,

COUNT(*) total

FROM peminjaman

GROUP BY bulan
"
);

while(
$d=
mysqli_fetch_assoc($q)
){

$bulan[]=
$d['bulan'];

$peminjaman[]=
$d['total'];

}



// GRAFIK KEMBALI

$kembali=[];

$q2=
mysqli_query(
$koneksi,
"
SELECT

MONTH(
tanggal_kembali_aktual
)
bulan,

COUNT(*) total

FROM detail_peminjaman

WHERE
tanggal_kembali_aktual
IS NOT NULL

GROUP BY bulan
"
);

while(
$d=
mysqli_fetch_assoc($q2)
){

$kembali[]=
$d['total'];

}



// GRAFIK BUKU TAMU

$aktivitas=[];

$total=[];

if(
mysqli_num_rows(
mysqli_query(
$koneksi,
"
SHOW TABLES
LIKE 'buku_tamu'
"
)
)
){

$q3=
mysqli_query(
$koneksi,
"
SELECT

aktivitas,

COUNT(*) jumlah

FROM buku_tamu

GROUP BY aktivitas
"
);

while(
$d=
mysqli_fetch_assoc($q3)
){

$aktivitas[]=
$d['aktivitas'];

$total[]=
$d['jumlah'];

}

}

$status =
mysqli_fetch_assoc(
mysqli_query(
$koneksi,
"
SELECT
SUM(stok_tersedia) tersedia,
SUM(stok_total-stok_tersedia) dipinjam
FROM buku
"
)
);

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"; ?>


<div class="content-wrapper">

<section class="content-header">

<div class="container-fluid">

<h1>

Dashboard Perpustakaan

</h1>

</div>

</section>
<section class="content">
<div class="container-fluid">

<div class="row">

<div class="col-md-3">
<div class="card analytics-card">
<div class="card-body">
<div class="analytics-title">
Total Anggota
</div>
<div class="analytics-number">
<?= $total_anggota ?>
</div>
</div>
</div>
</div>

<div class="col-md-3">
<div class="card analytics-card">
<div class="card-body">
<div class="analytics-title">
Total Buku
</div>
<div class="analytics-number">
<?= $total_buku ?>
</div>
</div>
</div>
</div>

<div class="col-md-3">
<div class="card analytics-card">
<div class="card-body">
<div class="analytics-title">
Total Peminjaman
</div>
<div class="analytics-number">
<?= $total_peminjaman ?>
</div>
</div>
</div>
</div>

<div class="col-md-3">
<div class="card analytics-card">
<div class="card-body">
<div class="analytics-title">
Stok Tersedia
</div>
<div class="analytics-number">
<?= $total_tersedia ?>
</div>
</div>
</div>
</div>

</div>

<div class="row mt-3">

<div class="col-md-8">
<div class="card chart-card">
<div class="card-header">
Grafik Peminjaman & Pengembalian
</div>
<div class="card-body">
<canvas id="chartGabungan"></canvas>
</div>
</div>
</div>

<div class="col-md-4">
<div class="card chart-card">
<div class="card-header">
Status Buku
</div>
<div class="card-body">
<canvas id="statusBuku"></canvas>
</div>
</div>
</div>

</div>

<div class="row mt-3">

<div class="col-md-6">
<div class="card chart-card">
<div class="card-header">
Aktivitas Buku Tamu
</div>
<div class="card-body">
<canvas id="tamu"></canvas>
</div>
</div>
</div>

<div class="col-md-6">
<div class="card chart-card">
<div class="card-header">
Peminjaman Bulanan
</div>
<div class="card-body">
<canvas id="pinjam"></canvas>
</div>
</div>
</div>

</div>

</div>
</section>

</div>

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

<script>
new Chart(
document.getElementById('chartGabungan'),
{
type:'line',
data:{
labels:<?= json_encode($bulan) ?>,
datasets:[
{
label:'Peminjaman',
data:<?= json_encode($peminjaman) ?>,
borderColor:'#4f46e5',
backgroundColor:'rgba(79,70,229,.2)',
fill:true
},
{
label:'Pengembalian',
data:<?= json_encode($kembali) ?>,
borderColor:'#f97316',
backgroundColor:'rgba(249,115,22,.2)',
fill:true
}
]
}
}
);

new Chart(
document.getElementById('statusBuku'),
{
type:'doughnut',
data:{
labels:['Tersedia','Dipinjam'],
datasets:[{
data:[
<?= $status['tersedia'] ?>,
<?= $status['dipinjam'] ?>
]
}]
}
}
);
</script>

<script>


new Chart(

document
.getElementById(
'pinjam'
),

{

type:'bar',

data:{

labels:
<?= json_encode($bulan) ?>,

datasets:[{

label:
'Peminjaman',

data:
<?= json_encode($peminjaman) ?>

}]

}

}

);


new Chart(

document
.getElementById(
'tamu'
),

{

type:'pie',

data:{

labels:
<?= json_encode($aktivitas) ?>,

datasets:[{

data:
<?= json_encode($total) ?>

}]

}

}

);

</script>