<?php

namespace App\Filters;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;

class AksesKeuangan implements FilterInterface
{
  public function before(RequestInterface $request, $arguments = null)
  {
    $db      = \Config\Database::connect();

    $cekuser = $db->table('karyawan')->where('id', session('id'))->get()->getRowArray();

    // $role_id = session('role_id');
    $role_kode = $cekuser['role_kode'];

    $urli = explode("/", uri_string());
    $uridetail = $urli[2];
    // dd($uricurrent);
    $uripecah = explode("&", $uridetail);
    $idslip = $uripecah[0];
    $idkaryawan = $uripecah[1];


    $builder = $db->table('slip');
    $queryaccess = $builder->where('id_karyawan', session('id'))->where('id', $idslip)->countAllResults();

    // Do something here
    if (session('username') == null) {
      return redirect()->to(base_url('/'));
    } elseif ($role_kode != "KEUANGAN" and $role_kode != "ADMIN" and $queryaccess < 1) {
      return redirect()->to(base_url('/block'));
    }
  }

  //--------------------------------------------------------------------

  public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
  {
    // Do something here
  }
}
