<?php

namespace App\Controllers\Superadmin;

use App\Controllers\BaseController;
use App\Models\UserModel;
use App\Models\ActivityLogModel;

class Profile extends BaseController
{
    protected $userModel;
    protected $activityModel;

    public function __construct()
    {
        $this->userModel = new UserModel();
        $this->activityModel = new ActivityLogModel();
    }

    /*
    |--------------------------------------------------------------------------
    | Profile
    |--------------------------------------------------------------------------
    */

    public function index()
    {
        $user = $this->userModel->find(session()->get('id_user'));

        if (!$user) {
            return redirect()->to('/login');
        }

        return view('superadmin/profile/index', [
            'title' => 'Profile Saya',
            'user'  => $user
        ]);
    }
    public function edit()
{
    $user = $this->userModel->find(session()->get('id_user'));

    if (!$user) {
        return redirect()->to('/login');
    }

    return view(
        'superadmin/profile/edit',
        [
            'title' => 'Edit Profile',
            'user' => $user
        ]
    );
}

public function update()
{
    $id = session()->get('id_user');

    $user = $this->userModel->find($id);

    if (!$user) {
        return redirect()->to('/login');
    }

    $rules = [

        'username' => "required|is_unique[users.username,id_user,{$id}]",

        'nama_lengkap' => 'required',

        'email' => "required|valid_email|is_unique[users.email,id_user,{$id}]"

    ];

    if (!$this->validate($rules)) {

        return redirect()
            ->back()
            ->withInput()
            ->with(
                'errors',
                $this->validator->getErrors()
            );

    }

    $this->userModel->update($id, [

        'username' => $this->request->getPost('username'),

        'nama_lengkap' => $this->request->getPost('nama_lengkap'),

        'email' => $this->request->getPost('email')

    ]);

    $this->activityModel->logActivity(

        $id,

        'Mengubah Profile'

    );

    return redirect()
        ->to('/superadmin/profile')
        ->with(
            'success',
            'Profile berhasil diperbarui.'
        );
}

public function changePassword()
{
    $id = session()->get('id_user');

    $user = $this->userModel->find($id);

    if (!$user) {

        return redirect()->back();

    }

    $old = $this->request->getPost('old_password');

    $new = $this->request->getPost('new_password');

    $confirm = $this->request->getPost('confirm_password');

    if (!password_verify($old, $user['password'])) {

        return redirect()
            ->back()
            ->with(
                'error',
                'Password lama salah.'
            );

    }

    if ($new != $confirm) {

        return redirect()
            ->back()
            ->with(
                'error',
                'Konfirmasi password tidak sama.'
            );

    }

    $this->userModel->update($id, [

        'password' => password_hash(
            $new,
            PASSWORD_DEFAULT
        )

    ]);

    $this->activityModel->logActivity(

        $id,

        'Mengganti Password'

    );

    return redirect()

        ->to('/superadmin/profile')

        ->with(
            'success',
            'Password berhasil diganti.'
        );
}

public function password()
{
    return view(
        'superadmin/profile/password',
        [
            'title'=>'Ganti Password'
        ]
    );
}

}