@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>

        <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="0">Semua Bulan</option>
                            @foreach($labels as $index => $bulan)
                                <option value="{{ $index + 1 }}">{{ $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" onchange="location.href='?tahun=' + this.value">
                            @foreach($tahunList as $tahun)
                                <option value="{{ $tahun }}" {{ $tahun == $tahunTerpilih ? 'selected' : '' }}>{{ $tahun }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>

            <!-- Grafik Keuangan -->
            <div class="col-md-6">
                <div class="card border shadow-sm">
                    <div class="card-body p-4">
                        <h5 class="text-center fw-bold">Keuangan</h5>
                        <canvas id="keuanganBarChart" style="max-height: 300px;"></canvas>
                    </div>
                </div>
            </div>

            <!-- Grafik Kamar -->
            <div class="col-md-6">
                <div class="card border shadow-sm">
                    <div class="card-body p-4">
                        <h5 class="text-center fw-bold">Data Kamar</h5>
                        <canvas id="dataKamarPieChart" style="max-height: 300px;"></canvas>
                    </div>
                </div>
            </div>


@endsection

        @push('scripts')
            <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
            <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2"></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) !!};
                    const dataKamar = {!! json_encode($data_kamar ?? []) !!}; // ✅ benar

                    // BAR CHART KEUANGAN
                    new Chart(document.getElementById('keuanganBarChart').getContext('2d'), {
                        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: value => 'Rp ' + value.toLocaleString('id-ID')
                                    }
                                }
                            },
                            plugins: {
                                tooltip: {
                                    callbacks: {
                                        label: context => `${context.dataset.label}: Rp ${context.parsed.y.toLocaleString('id-ID')}`
                                    }
                                }
                            }
                        }
                    });

                   // PIE CHART DATA KAMAR (Tampilkan jumlah kamar sebagai label)
const ctxKamar = document.getElementById('dataKamarPieChart').getContext('2d');
const kamarLabels = Object.keys(dataKamar);
const kamarData = Object.values(dataKamar);

new Chart(ctxKamar, {
    type: 'pie',
    data: {
        labels: kamarLabels,
        datasets: [{
            data: kamarData,
            backgroundColor: [
                'rgba(75, 192, 192, 0.7)', // Terisi
                'rgba(255, 206, 86, 0.7)', // Kosong
                'rgba(255, 99, 132, 0.7)'  // Rusak
            ],
            borderColor: [
                'rgba(75, 192, 192, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(255, 99, 132, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        responsive: true,
        plugins: {
            tooltip: {
                callbacks: {
                    label: (context) => {
                        const value = context.parsed;
                        return `${context.label}: ${value} kamar`;
                    }
                }
            },
            legend: {
                position: 'bottom'
            },
            datalabels: {
                color: '#000',
                formatter: (value) => `${value} kamar`,
                font: {
                    weight: 'bold',
                    size: 14
                }
            }
        }
    },
    plugins: [ChartDataLabels] // ← tambahkan kembali plugin ini
});
                });
            </script>
        @endpush