<?php
namespace App\Controllers;

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

class Notifikasiadmin extends BaseController
{
    use ResponseTrait;

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

        $user = auth()->user();

        $session = session();
        $general_lib = new GeneralLib();
        $userdata = service('user_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","");

        $data['error']=$session->getFlashdata('error');
        $data['active_menu'] = 'data_pangkalan';
        $data['user'] = auth()->user();
        $data['user_data'] = $userdata;
        $data['user_foto'] = $general_lib->getProfilePic($data['user']->foto);
        
        return view('template/admin/header',$data).view('pages/notifikasi/admin/list_pengajuan',$data).view('template/admin/footer',$data);
    }

    public function get_list_pengajuan(){
        if (!auth()->loggedIn()) {
            return $this->fail(['message'=>'error'],400,null,'error');
        }

        $user = auth()->user();

        $session = session();
        $general_lib = new GeneralLib();
        $userdata = service('user_data');
        //die(var_dump($userdata));
        $data_post = [
            'draw' => $this->request->getPost('draw'),
            'columns' => $this->request->getPost('columns'),
            'order' => $this->request->getPost('order'),
            'start' => $this->request->getPost('start'),
            'length' => $this->request->getPost('length'),
            'search' => $this->request->getPost('search'),
            'searchBuilder' => $this->request->getPost('searchBuilder'),
        ];

        $DataModel = model('NotifadminpengajuanpangkalanModel', true, $db);
        return $DataModel->GetDT($data_post,"");
    }

    public function get_list_perubahan(){
        if (!auth()->loggedIn()) {
            return $this->fail(['message'=>'error'],400,null,'error');
        }

        $user = auth()->user();

        $session = session();
        $general_lib = new GeneralLib();
        $userdata = service('user_data');
        //die(var_dump($userdata));
        $data_post = [
            'draw' => $this->request->getPost('draw'),
            'columns' => $this->request->getPost('columns'),
            'order' => $this->request->getPost('order'),
            'start' => $this->request->getPost('start'),
            'length' => $this->request->getPost('length'),
            'search' => $this->request->getPost('search'),
            'searchBuilder' => $this->request->getPost('searchBuilder'),
        ];

        $DataModel = model('NotifadminpengajuanperubahanModel', true, $db);
        return $DataModel->GetDT($data_post,"");
    }

    public function tandai_pengajuan()
    {
        if (!auth()->loggedIn()) {
            return $this->fail(['message'=>'error'],400,null,'error');
        }

        $user = auth()->user();

        $id = $this->request->getPost('id_notifikasi',FILTER_SANITIZE_ADD_SLASHES);
        
        $data['id'] = $id;
            
        $userdata = service('user_data');

        $NotifModel = model('NotifadminpengajuanpangkalanModel', true, $db);
        $update = [
            'dilihat' => '1'
        ];

        if ($NotifModel->update($id,$update))
        {
            $data['id'] = $id;
            return $this->respond($data,200);
        }else{
            return $this->fail($NotifModel->builder->db->error(),400,null,'db');
        }
    }

    public function tandai_perubahan()
    {
        if (!auth()->loggedIn()) {
            return $this->fail(['message'=>'error'],400,null,'error');
        }

        $user = auth()->user();

        $id = $this->request->getPost('id_notifikasi',FILTER_SANITIZE_ADD_SLASHES);
        
        $data['id'] = $id;
            
        $userdata = service('user_data');

        $NotifModel = model('NotifadminpengajuanperubahanModel', true, $db);
        $update = [
            'dilihat' => '1'
        ];

        if ($NotifModel->update($id,$update))
        {
            $data['id'] = $id;
            return $this->respond($data,200);
        }else{
            return $this->fail($NotifModel->builder->db->error(),400,null,'db');
        }
    }
}
?>