<?php

namespace App\Helpers;

class UserHelper extends CommonHelper
{
    public function __construct()
    {
        parent::__construct();
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'passwd':
            case 'confirmpassword':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' form-control' : 'form-control';
                $extras['style'] = 'width:100%;';
                $form = form_password($field, "", $extras);
                break;
            case 'role':
                $currentRoles = is_array($value)
                    ? array_map('strtolower', array_map('trim', $value))
                    : [];
                $form = '';
                //Form페이지에서는 맨앞에것 제외하기 위함
                array_shift($viewDatas['formOptions'][$field]['options']);
                foreach ($viewDatas['formOptions'][$field]['options'] as $key => $label) {
                    $checked = in_array(strtolower(trim($key)), $currentRoles);
                    $form .= '<label class="me-3">';
                    $form .= form_checkbox('role[]', $key, $checked, ['id' => "role_{$key}", ...$extras]);
                    $form .= " {$label}";
                    $form .= '</label>';
                }
                break;
            default:
                $form = parent::getFieldForm($field, $value, $viewDatas, $extras);
                break;
        }
        return $form;
    } //
}
