<?php

namespace App\Controllers\Auth;

use App\Controllers\BaseController;

class Logout extends BaseController
{
    public function index()
    {
        $db = \Config\Database::connect();
        $id_user = session()->get('id_user');

        // Update waktu logout di login_logs (Tabel #5)
        $db->table('login_logs')
           ->where('id_user', $id_user)
           ->orderBy('login_at', 'DESC')
           ->limit(1)
           ->update(['logout_at' => date('Y-m-d H:i:s')]);

        session()->destroy();
        return redirect()->to('/login')->with('success', 'Anda telah logout.');
    }
}