@extends('layouts.app1')

@section('content')
<div class="container-fluid p-4" style="background-color: #F5F5F5; border-radius: 10px;">
    <h3 class="fw-bold" style="color: #063986;">Selamat Datang Di Kos Calista</h3>

    <div class="row mt-4">
        <div class="col-md-4">
            <div class="card border shadow-sm">
                <div class="card-body text-center p-4">
                    <div class="d-flex justify-content-center">
                        <div class="rounded-circle d-flex align-items-center justify-content-center" 
                            style="width: 60px; height: 60px; background-color: #0080B5;">
                            <i class="bi bi-person-badge-fill text-white" style="font-size: 30px;"></i>
                        </div>
                    </div>
                    <h3 class="fw-bold mt-3 mb-0">{{ $totalUsers }}</h3>
                    <p class="mb-0">Total Customer</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border shadow-sm">
                <div class="card-body text-center p-4">
                    <div class="d-flex justify-content-center">
                        <div class="rounded-circle d-flex align-items-center justify-content-center" 
                            style="width: 60px; height: 60px; background-color: #0080B5;">
                            <i class="bi bi-door-closed-fill text-white" style="font-size: 30px;"></i>
                        </div>
                    </div>
                    <h3 class="fw-bold mt-3 mb-0">{{ $totalRooms }}</h3>
                    <p class="mb-0">Total Kamar</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border shadow-sm">
                <div class="card-body text-center p-4">
                    <div class="d-flex justify-content-center">
                        <div class="rounded-circle d-flex align-items-center justify-content-center" 
                            style="width: 60px; height: 60px; background-color: #0080B5;">
                            <i class="bi bi-journal-check text-white" style="font-size: 30px;"></i>
                        </div>
                    </div>
                    <h3 class="fw-bold mt-3 mb-0">{{ $totalBookings }}</h3>
                    <p class="mb-0">Total Booking</p>
                </div>
            </div>
        </div>
    </div>

<!-- Grafik Keuangan -->
<div class="row mt-4">
    <div class="col-12 text-center mb-3">
        <div class="d-flex justify-content-center gap-3 flex-wrap">
            <div>
                <label for="bulanSelect" class="form-label fw-semibold mb-1">Pilih Bulan</label>
                <select id="bulanSelect" class="form-select">
                    <option value="all">Semua Bulan</option>
                    @foreach($labels as $index => $bulan)
                        <option value="{{ $index }}">{{ $bulan }}</option>
                    @endforeach
                </select>
            </div>
            <div>
                <label for="tahunSelect" class="form-label fw-semibold mb-1">Pilih Tahun</label>
                <select id="tahunSelect" class="form-select">
                    @foreach($tahunList as $tahun)
                        <option value="{{ $tahun }}" {{ $tahun == $tahunTerpilih ? 'selected' : '' }}>{{ $tahun }}</option>
                    @endforeach
                </select>
            </div>
        </div>
    </div>

    <div class="col-md-6">
        <div class="card border shadow-sm">
            <div class="card-body p-4">
                <h5 class="text-center fw-bold">Bar Chart Keuangan</h5>
                <canvas id="keuanganBarChart"></canvas>
            </div>
        </div>
    </div>
    
    <div class="col-md-6">
        <div class="card border shadow-sm">
            <div class="card-body p-4">
                <h5 class="text-center fw-bold">Pie Chart Keuangan</h5>
                <canvas id="keuanganPieChart"></canvas>
            </div>
        </div>
    </div>
</div>

@endsection

@push('scripts')
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
    const labels = {!! json_encode($labels) !!};
    const pemasukan = {!! json_encode($pemasukan) !!};
    const pengeluaran = {!! json_encode($pengeluaran) !!};
    const keuntungan = {!! json_encode($keuntungan) !!};

    // === BAR CHART ===
    const ctxBar = document.getElementById('keuanganBarChart').getContext('2d');
    new Chart(ctxBar, {
        type: 'bar',
        data: {
            labels: labels,
            datasets: [
                {
                    label: 'Pemasukan',
                    data: pemasukan,
                    backgroundColor: 'rgba(54, 162, 235, 0.6)',
                    borderColor: 'rgba(54, 162, 235, 1)',
                    borderWidth: 1
                },
                {
                    label: 'Pengeluaran',
                    data: pengeluaran,
                    backgroundColor: 'rgba(255, 99, 132, 0.6)',
                    borderColor: 'rgba(255, 99, 132, 1)',
                    borderWidth: 1
                },
                {
                    label: 'Keuntungan',
                    data: keuntungan,
                    type: 'line',
                    borderColor: 'rgba(75, 192, 192, 1)',
                    backgroundColor: 'rgba(75, 192, 192, 0.2)',
                    borderWidth: 2,
                    tension: 0.3,
                    fill: false
                }
            ]
        },
        options: {
            responsive: true,
            interaction: {
                mode: 'index',
                intersect: false,
            },
            stacked: false,
            scales: {
                y: {
                    beginAtZero: true,
                    ticks: {
                        callback: function(value) {
                            return 'Rp ' + value.toLocaleString('id-ID');
                        }
                    }
                }
            },
            plugins: {
                tooltip: {
                    callbacks: {
                        label: function(context) {
                            return context.dataset.label + ': Rp ' + context.parsed.y.toLocaleString('id-ID');
                        }
                    }
                }
            }
        }
    });

    // === PIE CHART ===
    const bulanSelect = document.getElementById('bulanSelect');
    const ctxPie = document.getElementById('keuanganPieChart').getContext('2d');

    let pieChart = new Chart(ctxPie, {
        type: 'pie',
        data: {
            labels: ['Pemasukan', 'Pengeluaran', 'Keuntungan'],
            datasets: [{
                data: [pemasukan[0], pengeluaran[0], keuntungan[0]],
                backgroundColor: [
                    'rgba(54, 162, 235, 0.7)',
                    'rgba(255, 99, 132, 0.7)',
                    'rgba(75, 192, 192, 0.7)'
                ],
                borderColor: [
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 99, 132, 1)',
                    'rgba(75, 192, 192, 1)'
                ],
                borderWidth: 1
            }]
        },
        options: {
            responsive: true,
            plugins: {
                tooltip: {
                    callbacks: {
                        label: function(context) {
                            return context.label + ': Rp ' + context.parsed.toLocaleString('id-ID');
                        }
                    }
                }
            }
        }
    });

    // Event listener untuk ubah bulan
    bulanSelect.addEventListener('change', function () {
        const i = parseInt(this.value);
        pieChart.data.datasets[0].data = [pemasukan[i], pengeluaran[i], keuntungan[i]];
        pieChart.update();
    });
});
</script>
@endpush
