<?php

namespace App\Helpers;

use CodeIgniter\HTTP\IncomingRequest;
use App\Models\UserModel;

class UserHelper extends CommonHelper
{
    protected ?IncomingRequest $request = null;
    public function __construct(?IncomingRequest $request = null)
    {
        parent::__construct($request);
        $this->setTitleField(UserModel::TITLE);
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        if (in_array($viewDatas['action'], ['create', 'modify', 'create_form', 'modify_form'])) {
            $extras = (strpos($viewDatas['field_rules'][$field], 'required') !== false) ? ["class" => "form-control", "required" => "", ...$extras] : ["class" => "form-control", ...$extras];
        }
        switch ($field) {
            case 'passwd':
            case 'confirmpassword':
                $form = form_password($field, "", ["autocomplete" => $field, ...$extras]);
                break;
            default:
                $form = parent::getFieldForm($field, $value, $viewDatas, $extras);
                break;
        }
        return $form;
    } //
}
