<?php

namespace App\Controllers;

class Home extends BaseController {

    // Khusus Untuk Menangani URL http://glenlis
    public function index() {
        $session = session();

        // Jika belum login, arahkan ke /login
        if (! $session->get('isLoggedIn')) {
            return redirect()->to('/login');
        }

        // Jika sudah login, arahkan sesuai level
        switch ($session->get('userlevel')) {
            case 1: return redirect()->to('admin');
            case 2: return redirect()->to('lab');
            case 3: return redirect()->to('analyst');
            case 4: return redirect()->to('cs');
            default: return redirect()->to('login');
        }
    }

    public function dummyPage() {
        return view('dummy_page');
    }

    public function printReport($accessnumber) {
        $userroleid = session()->get('userroleid');
        
        if (in_array($userroleid, [0, 1, 2])) {
            return redirect()->to("/report/{$accessnumber}");
        } elseif ($userroleid == 4) {
            return redirect()->to("/report/print/{$accessnumber}");
        }
        
        return $this->response->setStatusCode(403)->setJSON(['message' => 'Unauthorized']);
    }
}
