<?php
namespace App\Controllers;

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

class Pengguna extends BaseController
{
    use ResponseTrait;

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

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

        $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);

        $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='".$userdata->data['id_agen']."')");
        
        return view('template/agen/header',$data).view('pages/pengguna/agen/pengguna',$data).view('template/agen/footer',$data);
    }

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

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

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

        $data = array();
        $data['status'] = 'success';

        $request = [
            'password' => $this->request->getPost('password',FILTER_SANITIZE_ADD_SLASHES)
        ];

        $validation = \Config\Services::validation();
        $validation->setRule('password', 'Password', 'required|min_length[8]');

        if (!$validation->run($request)) {
            return $this->fail($validation->getErrors(),400,null,'input');
        }else{
            $db = db_connect();
            $builder = $db->table('agen');
            $builder->where('id_agen',$userdata->data['id_agen']);
            $builder->update($request);

            $user->fill([
                'username' => $userdata->data['sold_id'],
                'email' => $userdata->data['sold_id'],
                'password' => $request['password']
            ]);

            $users = model('UserModel');
            $users->save($user);

            return $this->respond(['message'=>'success'],200);
        }
    }

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

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

        $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);

        $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","");
        
        return view('template/admin/header',$data).view('pages/pengguna/admin/pengguna',$data).view('template/admin/footer',$data);
    }

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

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

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

        $data = array();
        $data['status'] = 'success';

        $request = [
            'nama' => $this->request->getPost('nama',FILTER_SANITIZE_ADD_SLASHES),
            'email' => $this->request->getPost('email',FILTER_SANITIZE_ADD_SLASHES),
            'password' => $this->request->getPost('password',FILTER_SANITIZE_ADD_SLASHES)
        ];

        $validation = \Config\Services::validation();
        $validation->setRule('nama', 'Nama', 'required');
        $validation->setRule('email', 'Email', 'required');
        $validation->setRule('password', 'Password', 'required|min_length[8]');

        if (!$validation->run($request)) {
            return $this->fail($validation->getErrors(),400,null,'input');
        }else{
            $db = db_connect();
            $builder = $db->table('user_office');
            $builder->where('id_user',$userdata->data->id_user);
            $builder->update($request);

            $user->fill([
                'username' => $request['nama'],
                'email' => $request['email'],
                'password' => $request['password']
            ]);

            $users = model('UserModel');
            $users->save($user);

            return $this->respond(['message'=>'success'],200);
        }
    }
}
?>