<?php

namespace App\Controllers;

use App\Libraries\GeneralLib;
use CodeIgniter\Shield\Entities\User;

class Home extends BaseController
{
    public function index()
    {
        if (!auth()->loggedIn()) {
            return redirect()->to('login');
        }
        $general_lib = new GeneralLib();
        $userdata = service('user_data');
        
        $data['active_menu'] = 'dashboard';
        $data['user'] = auth()->user();
        $data['user_data'] = $userdata;
        $data['user_foto'] = $general_lib->getProfilePic($data['user']->foto);
        
        // $users = model('UserModel');
        // $tuser = model('UserModela');
        // $user = new User([
        //     'username' => '888473',
        //     'email'    => '888473@gmail.com',
        //     'password' => '12345678',
        // ]);

        // $users->save($user);
        // $user = $users->findById($users->getInsertID());
        // $tuser->setLevel($users->getInsertID(), 'user_customer');
        // $user->syncGroups('user_customer');
        // $users->activate($user);

        $level = $data['user']->level;
        
        if ($level == 'admin')
        {
            return $this->index_admin($data);
        }elseif($level == 'user_office'){
            return $this->index_admin($data);
        }elseif($level == 'user_customer')
        {
            return $this->index_agen($data);
        }
    }

    private function index_admin($data)
    {
        $NotifikasiModel = model('NotifadminpengajuanpangkalanModel', true, $db);
        $data['notifikasi_pengajuan_pangkalan'] = $NotifikasiModel->get_latest_notifikasi("5","");

        $Notifikasi2Model = model('NotifadminpengajuanperubahanModel', true, $db);
        $data['notifikasi_pengajuan_perubahan'] = $Notifikasi2Model->get_latest_notifikasi("5","");

        $modelPangkalan = model('PangkalanModel', true, $db);
        $data['jumlah_pengajuan'] = $modelPangkalan->get_count_pangkalan_pengajuan();
        $data['jumlah_waiting'] = $modelPangkalan->get_count_pangkalan_waiting();
        $data['jumlah_revision'] = $modelPangkalan->get_count_pangkalan_revision();
        $data['jumlah_reject'] = $modelPangkalan->get_count_pangkalan_reject();

        $modelPangkalan = model('PangkalanperubahanModel', true, $db);
        $data['jumlah_pengajuan_perubahan'] = $modelPangkalan->get_count_pangkalan_pengajuan();
        $data['jumlah_waiting_perubahan'] = $modelPangkalan->get_count_pangkalan_waiting();
        $data['jumlah_revision_perubahan'] = $modelPangkalan->get_count_pangkalan_revision();
        $data['jumlah_reject_perubahan'] = $modelPangkalan->get_count_pangkalan_reject();
        
        $data['jumlah_pangkalan'] = $modelPangkalan->get_count_pangkalan_approve();
        $data['pangkalan'] = $modelPangkalan->where("status='approve' or status='aktif'")->findAll();

        return view('template/admin/header',$data).view('pages/home/admin/home',$data).view('template/admin/footer',$data);
    }

    private function index_agen($data)
    {
        $NotifikasiModel = model('NotifikasipengajuanpangkalanModel', true, $db);
        $data['notifikasi_pengajuan_pangkalan'] = $NotifikasiModel->get_latest_notifikasi("5","(notifikasi_pengajuan_pangkalan.id_agen='".$data['user_data']->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']."')");

        $modelPangkalan = model('PangkalanModel', true, $db);
        $data['jumlah_pengajuan'] = $modelPangkalan->get_count_pangkalan_pengajuan($data['user_data']->data['id_agen']);
        $data['jumlah_waiting'] = $modelPangkalan->get_count_pangkalan_waiting($data['user_data']->data['id_agen']);
        $data['jumlah_revision'] = $modelPangkalan->get_count_pangkalan_revision($data['user_data']->data['id_agen']);
        $data['jumlah_reject'] = $modelPangkalan->get_count_pangkalan_reject($data['user_data']->data['id_agen']);

        $data['pangkalan'] = $modelPangkalan->where('id_agen',$data['user_data']->data['id_agen'])->where("(status='approve' or status='aktif')")->findAll();

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