<?php

namespace App\Controllers\Superadmin;

use App\Controllers\BaseController;
use App\Models\ActivityLogModel;

class ActivityLog extends BaseController
{
    protected $activityModel;

    protected $db;

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

    /*
    |--------------------------------------------------------------------------
    | Daftar Aktivitas
    |--------------------------------------------------------------------------
    */

    public function index()
    {
        $logs = $this->db->table('activity_logs a')
            ->select('
                a.id_log,
                a.timestamp,
                a.aktivitas,
                u.username,
                u.nama_lengkap,
                r.nama_role
            ')
            ->join('users u', 'u.id_user = a.id_user', 'left')
            ->join('roles r', 'r.id_role = u.id_role', 'left')
            ->orderBy('a.timestamp', 'DESC')
            ->limit(100)
            ->get()
            ->getResultArray();

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

    /*
    |--------------------------------------------------------------------------
    | Detail Aktivitas
    |--------------------------------------------------------------------------
    */

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

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

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

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

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

        $builder = $this->db->table('activity_logs a');

        $builder->select('
            a.id_log,
            a.timestamp,
            a.aktivitas,
            u.username,
            u.nama_lengkap,
            r.nama_role
        ');

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

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

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

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

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

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