<?php

namespace App\Controllers\Superadmin;

use App\Controllers\BaseController;
use App\Models\LoginLogModel;

class LoginLog extends BaseController
{
    protected $loginLogModel;
    protected $db;

    public function __construct()
    {
        $this->loginLogModel = new LoginLogModel();
        $this->db = \Config\Database::connect();
    }

    /*
    |--------------------------------------------------------------------------
    | Daftar Login
    |--------------------------------------------------------------------------
    */

    public function index()
    {
        $logs = $this->db->table('login_logs l')
            ->select("
                l.*,
                u.username,
                u.nama_lengkap,
                r.nama_role
            ")
            ->join('users u', 'u.id_user=l.id_user', 'left')
            ->join('roles r', 'r.id_role=u.id_role', 'left')
            ->orderBy('l.login_at','DESC')
            ->limit(100)
            ->get()
            ->getResultArray();

        return view('superadmin/login_log/index',[
            'title'=>'Login Log',
            'logs'=>$logs
        ]);
    }

    /*
    |--------------------------------------------------------------------------
    | Detail Login
    |--------------------------------------------------------------------------
    */

    public function detail($id)
    {
        $log = $this->db->table('login_logs l')
            ->select("
                l.*,
                u.username,
                u.nama_lengkap,
                r.nama_role
            ")
            ->join('users u','u.id_user=l.id_user','left')
            ->join('roles r','r.id_role=u.id_role','left')
            ->where('l.id_login',$id)
            ->get()
            ->getRowArray();

        if(!$log){
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        return view('superadmin/login_log/detail',[
            'title'=>'Detail Login',
            'log'=>$log
        ]);
    }

    /*
    |--------------------------------------------------------------------------
    | Filter
    |--------------------------------------------------------------------------
    */

    public function filter()
    {
        $tanggal = $this->request->getGet('tanggal');
        $role    = $this->request->getGet('role');
        $user    = $this->request->getGet('user');

        $builder = $this->db->table('login_logs l');

        $builder->select("
            l.*,
            u.username,
            u.nama_lengkap,
            r.nama_role
        ");

        $builder->join('users u','u.id_user=l.id_user','left');
        $builder->join('roles r','r.id_role=u.id_role','left');

        if(!empty($tanggal)){
            $builder->where('DATE(l.login_at)',$tanggal);
        }

        if(!empty($role)){
            $builder->where('r.id_role',$role);
        }

        if(!empty($user)){
            $builder->where('u.id_user',$user);
        }

        $logs = $builder
            ->orderBy('l.login_at','DESC')
            ->get()
            ->getResultArray();

        return view('superadmin/login_log/index',[
            'title'=>'Filter Login',
            'logs'=>$logs
        ]);
    }
}