<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Mvisitor;

class DashboardController extends Controller
{
    public function index()
    {
        // Mengambil data terbaru dari kolom 'masuk', 'keluar', dan 'total'
        $masuk = Mvisitor::orderBy('created_at', 'desc')->first(['masuk']);
        $keluar = Mvisitor::orderBy('created_at', 'desc')->first(['keluar']);
        $total = Mvisitor::orderBy('created_at', 'desc')->first(['total']);

        return view('dashboard', [
            'nilaivisitor' => $masuk ? $masuk->masuk : 0,
            'nilaikeluar' => $keluar ? $keluar->keluar : 0,
            'nilaitotal' => $total ? $total->total : 0,
        ]);
    }

    public function bacamasuk()
    {
        // Mengambil data terbaru dari kolom 'masuk'
        $masuk = Mvisitor::orderBy('created_at', 'desc')->first(['masuk']);
        return response()->json(['nilaivisitor' => $masuk ? $masuk->masuk : 0]);
    }

    public function bacakeluar()
    {
        // Mengambil data terbaru dari kolom 'keluar'
        $keluar = Mvisitor::orderBy('created_at', 'desc')->first(['keluar']);
        return response()->json(['nilaikeluar' => $keluar ? $keluar->keluar : 0]);
    }

    public function bacatotal()
    {
        // Mengambil data terbaru dari kolom 'total'
        $total = Mvisitor::orderBy('created_at', 'desc')->first(['total']);
        return response()->json(['nilaitotal' => $total ? $total->total : 0]);
    }
}
