<?php
namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Shield\Entities\User;
use App\Libraries\GeneralLib;
use App\Libraries\Pluploadhandler;

class Agenagen extends BaseController
{
    use ResponseTrait;

    public function view_agen()
    {
        if (!auth()->loggedIn()) {
            return redirect()->to('login');
        }

        $user = auth()->user();
        if (!$user->can('self_agen.access')) {
            return redirect()->to('home');
        }

        $session = session();
        $general_lib = new GeneralLib();
        $userdata = service('user_data');

        $NotifikasiModel = model('NotifikasipengajuanpangkalanModel', true, $db);
        $data['notifikasi_pengajuan_pangkalan'] = $NotifikasiModel->get_latest_notifikasi("5","(notifikasi_pengajuan_pangkalan.id_agen='".$userdata->data['id_agen']."')");
        $Notifikasi2Model = model('NotifikasipengajuanperubahanModel', true, $db);
        $data['notifikasi_pengajuan_perubahan'] = $Notifikasi2Model->get_latest_notifikasi("5","(notifikasi_pengajuan_perubahan.id_agen='".$data['user_data']->data['id_agen']."')");
        
        $data['error']=$session->getFlashdata('error');
        $data['active_menu'] = 'data_agen';
        $data['user'] = auth()->user();
        $data['user_data'] = $userdata;
        $data['user_foto'] = $general_lib->getProfilePic($data['user']->foto);

        $db = db_connect();
        $builder = $db->table('vagen');
        $tdata = $builder->where("id_agen",$userdata->data['id_agen'])->get()->getRow();
        if ($tdata == null)
        {
            return redirect()->to('home');
        }

        $data['data'] = $tdata;
        $builder = $db->table('vpangkalan_simple');
        $tdata = $builder->where("id_agen",$userdata->data['id_agen'])->get()->getResult();
        $data['pangkalan'] = $tdata;
        $session = session();
        $data['error']=$session->getFlashdata('error');
        $data['success']=$session->getFlashdata('success');
        $data['form_error']=$session->getFlashdata('form_validation');

        return view('template/agen/header',$data).view('pages/agen/agen/view',$data).view('template/agen/footer',$data);
    }
}
?>