<?php

function humanize(string $string, string $separator = '_'): string
{
    $replacement = trim($string);

    return ucwords(preg_replace('/[' . $separator . ']+/', ' ', $replacement));
}

function form_hidden($name, $value = '', bool $recursing = false): string
{
    static $form;

    if ($recursing === false) {
        $form = '';
    }

    if (is_array($name)) {
        foreach ($name as $key => $val) {
            form_hidden($key, $val, true);
        }

        return $form;
    }

    if (! is_array($value)) {
        $defaults = [
            'type'  => 'hidden',
            'name'  => is_array($name) ? '' : $name,
            'id'    => $name,
            'value' => $value,
        ];
        $hidden = '<input ' . parse_form_attributes($name, $defaults) . ">\n";
        $form .= $hidden;
    } else {
        foreach ($value as $k => $v) {
            $k = is_int($k) ? '' : $k;
            form_hidden($name . '[' . $k . ']', $v, true);
        }
    }

    return $form;
}

function form_input($name = '', string $value = '', $extra = '', string $type = 'text'): string
{
    $defaults = [
        'type'        => $type,
        'name'        => $name,
        'id'          => $name,
        'value'       => $value,
        'class'       => 'form-control',
        'placeholder' => humanize($name),
    ];

    return '<div class="form-group" id="fg_' . $name . '">
              <label class="control-label" for="' . $name . '">' . humanize($name) . ' : <span class="error-message" id="em_' . $name . '"></span></label>
              <input ' . parse_form_attributes($name, $defaults) . stringify_attributes($extra) . _solidus() . ">
            </div>\n";
}

function form_password($name = '', string $value = '', $extra = ''): string
{
    return form_input($name, $value, $extra, 'password');
}

function form_upload($name, $label, $help = '', $accept = ''): string
{
    $defaults = [
        'type'   => 'file',
        'name'   => $name,
        'id'     => $name,
        'accept' => $accept,
    ];

    return '<div class="form-group" id="fg_' . $name . '">
      <label for="' . $name . '">' . humanize($label) . ': <span class="error-message" id="em_' . $name . '"></span></label>
      <div class="input_file_container">
        <input ' . parse_form_attributes($name, $defaults) . ">\n" .
     '</div>
      <p class="help-block">' . $help . '</p>
    </div>';
}

function form_textarea($name = '', $value = '', $rows = 10, $extra = ''): string
{
    $defaults = [
        'name'        => $name,
        'class'       => 'form-control',
        'rows'        => $rows,
        'placeholder' => humanize($name),
    ];

    return '<div class="form-group" id="fg_' . $name . '">
              <label class="control-label" for="' . $name . '">' . humanize($name) . ' : <span class="error-message" id="em_' . $name . '"></span></label>
              <textarea ' . rtrim(parse_form_attributes($name, $defaults)) . stringify_attributes($extra) . '>'
            . htmlspecialchars($value)
            . '</textarea>'
            . "</div>\n";
}

function form_submit($id = '', $label = '', $type = '', $value = ''): string
{
    $defaults = [
        'type'  => 'submit',
        'class' => 'btn btn-primary ' . $type,
        'id'    => $id,
        'value' => $value,
    ];

    return '<button ' . parse_form_attributes($id, $defaults) . '>'
            . $label
            . "</button>\n";
}

function form_button($id = '', $label = '', $action = '', $type = 'btn-default'): string
{
    $defaults = [
        'type'    => 'button',
        'class'   => 'btn ' . $type,
        'id'      => $id,
        'onclick' => $action,
    ];

    return '<button ' . parse_form_attributes($id, $defaults) . '>'
            . $label
            . "</button>\n";
}

function form_link($href = '', $label = '', $type = 'btn-default'): string
{
    $defaults = [
        'class' => 'btn ' . $type,
        'href'  => $href,
    ];

    return '<a ' . parse_form_attributes($label, $defaults) . '>'
            . $label
            . "</a>\n";
}

function form_datepicker($name = '', string $value = '', $extra = ''): string
{
    $defaults = [
        'type'         => 'text',
        'name'         => $name,
        'id'           => $name,
        'value'        => $value,
        'class'        => 'form-control pull-right datepicker',
        'placeholder'  => humanize($name),
        'autocomplete' => false,
    ];

    return '<div class="form-group" id="fg_' . $name . '">
      <label class="control-label" for="' . $name . '">' . humanize($name) . ' : <span class="error-message" id="em_' . $name . '"></span></label>
      <div class="input-group">
        <div class="input-group-append">
            <span class="input-group-text fs-xl">
                <i class="fal fa-calendar-alt"></i>
            </span>
        </div>
        <input ' . parse_form_attributes($name, $defaults) . stringify_attributes($extra) . _solidus() . ">
      </div>
    </div>\n";
}

function form_lookup($name, $lookup, $value, $value_lookup, $function)
{
    return '<div class="form-group" id="fg_' . $name . '">
      <label class="control-label" for="' . $name . '">' . humanize($lookup) . ' : <span class="error-message" id="em_' . $name . '"></span></label>
      <div class="input-group">
        <div class="input-group-addon">
            <i class="fa fa-th"></i>
        </div>
        <input type="text" onfocus="' . $function . '" class="form-control" name="' . $lookup . '" id="' . $lookup . '" placeholder="' . humanize($lookup) . '" value="' . $value_lookup . '" autocomplete="false">' .
        form_hidden($name, $value) .
      "</div>
    </div>\n";
}

function form_select2($name, $options, $selected, $label)
{
    $select2 = '<div class="form-group" id="fg_' . $name . '">
      <label class="control-label" for="' . $name . '">' . $label . ' : <span class="error-message" id="em_' . $name . '"></span></label>' . "\n";
    $select2 .= form_dropdown($name, $options, $selected, 'class="form-control '.$name.' select2" id="sel_'.$name.'"');
    $select2 .= "</div>\n";

    return $select2;
}

##FORMAT FORM VERTICAL TANNPA VALUE-------------------------------------------------------
#Contoh penggunaan
//echo x_input_inline("Judul", "judul"); // required by default
//echo x_input_inline("Deskripsi", "deskripsi", "additional-class", false); //required false
//echo x_input_inline("Judul", "judul", '', false); //tanpa class tambahan dan required false
function x_input_inline($label, $name, $required = true, $additionalClass = '') {
    $inputId = htmlspecialchars($name);
    $labelText = htmlspecialchars($label);
    $inputName = htmlspecialchars($name);
    $class = 'form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent' . (!empty($additionalClass) ? ' ' . htmlspecialchars($additionalClass) : '');
    $requiredAttribute = $required ? 'required' : '';

    return "
        <div class=\"form-group\">
            <label class=\"form-label\">$labelText</label>
            <input type=\"text\" id=\"$inputId\" name=\"$inputName\" class=\"$class\" $requiredAttribute>
            <div class=\"invalid-feedback\" id=\"em_$inputId\"></div>
        </div>
    ";
}

#Contoh penggunaan
//echo x_tArea_inline("Pesan", "pesan"); // 7 rows, required, tanpa kelas tambahan
//echo x_tArea_inline("Deskripsi", "deskripsi", 5, false, "additional-class");
function x_tArea_inline($label, $name, $rows = 5, $required = true, $additionalClass = '') {
    $inputId = htmlspecialchars($name);
    $labelText = htmlspecialchars($label);
    $inputName = htmlspecialchars($name);
    $class = 'form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent' . (!empty($additionalClass) ? ' ' . htmlspecialchars($additionalClass) : '');
    $requiredAttribute = $required ? 'required' : '';

    return "
        <div class=\"form-group\">
            <label class=\"form-label\" for=\"$inputId\">$labelText</label>
            <textarea class=\"$class\" id=\"$inputId\" name=\"$inputName\" rows=\"$rows\" $requiredAttribute></textarea>
            <div class=\"invalid-feedback\" id=\"em_$inputId\"></div>
        </div>
    ";
}
#END VERTIKAL TANPA VALUE------------------------------------------------------

##FORMAT FORM VERTICAL WITH VALUE-------------------------------------------------------
#Contoh penggunaan
//echo xx_input_inline("Judul", "judul", "nilai_dari_server"); // required by default
//echo xx_input_inline("Deskripsi", "deskripsi", "nilai_dari_server", "additional-class", false); //required false
//echo xx_input_inline("Judul", "judul", "nilai_dari_server", '', false); //tanpa class tambahan dan required false
function xx_input_inline($label, $name, $value = '', $additionalClass = '', $required = true) {
    $inputId = htmlspecialchars($name);
    $labelText = htmlspecialchars($label);
    $inputName = htmlspecialchars($name);
    $inputValue = htmlspecialchars($value); // Sanitize value
    $class = 'form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent' . (!empty($additionalClass) ? ' ' . htmlspecialchars($additionalClass) : '');
    $requiredAttribute = $required ? 'required' : '';

    return "
        <div class=\"form-group\">
            <label class=\"form-label\">$labelText</label>
            <input type=\"text\" id=\"$inputId\" name=\"$inputName\" value=\"$inputValue\" class=\"$class\" $requiredAttribute>
            <div class=\"invalid-feedback\" id=\"em_$inputId\"></div>
        </div>
    ";
}

#Contoh penggunaan
//echo xx_tArea_inline("Pesan", "pesan", "nilai_dari_server"); // 7 rows, required, tanpa kelas tambahan
//echo xx_tArea_inline("Deskripsi", "deskripsi", "nilai_dari_server", 5, false, "additional-class");
function xx_tArea_inline($label, $name, $value = '', $rows = 5, $required = true, $additionalClass = '') {
    $inputId = htmlspecialchars($name);
    $labelText = htmlspecialchars($label);
    $inputName = htmlspecialchars($name);
    $textareaValue = htmlspecialchars($value); // Sanitize value
    $class = 'form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent' . (!empty($additionalClass) ? ' ' . htmlspecialchars($additionalClass) : '');
    $requiredAttribute = $required ? 'required' : '';

    return "
        <div class=\"form-group\">
            <label class=\"form-label\" for=\"$inputId\">$labelText</label>
            <textarea class=\"$class\" id=\"$inputId\" name=\"$inputName\" rows=\"$rows\" $requiredAttribute>$textareaValue</textarea>
            <div class=\"invalid-feedback\" id=\"em_$inputId\"></div>
        </div>
    ";
}
#END FORMAT FORM VERTIKAL WITH VALUE-------------------------------------------------------

##FORMAT FORM HORIZONTAL -------------------------------------------------------
function xform_select2($name, $options, $selected, $label, $clabel, $cinput)
{
    $select2 = '<div class="row mb-3">
        <label class="col-sm-'.$clabel.' col-form-label text-right">'.$label.':</label>
        <div class="col-sm-'.$cinput.'">' . "\n";
    $select2 .= form_dropdown($name, $options, $selected, 'class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent'.$name.' select2" id="sel_'.$name.'"');
    $select2 .= '<div class="invalid-feedback" id="em_'.$name.'"></div></div>';
    $select2 .= "</div>\n";

    return $select2;
}

#Input tanpa maxlength
//echo xform_input('nama_menu', 'Nama Menu', $nama_menu, 3, 9, true) //most used w required
//echo xform_input('nama_menu', 'Nama Menu', '', 3, 9, false); // tanpa value w/ required
//echo xform_input('email', 'Email', '', 3, 9, false, '', 'text-danger'); // tanpa value dengan kelas tambahan

#Input dengan maxlength
//echo xform_input('nickname', 'Nickname', '', 3, 9, false, 10); // Dengan maxlength 10

function xform_input($name, $label, $value = '', $clabel = 3, $cinput = 9, $required = false, $maxlength = '', $extraClass = ''): string
{
    // Menggunakan 'text' sebagai default type
    $type = 'text';

    // Mempersiapkan atribut tambahan jika ada
    $requiredAttr = $required ? ' required' : '';
    $maxlengthAttr = !empty($maxlength) ? ' maxlength="' . htmlspecialchars($maxlength) . '"' : '';

    // Jika placeholder tidak diberikan, gunakan label sebagai placeholder
    $placeholder = htmlspecialchars($label);

    // Menggabungkan class default dengan class tambahan jika ada
    $classAttr = 'form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent' . (!empty($extraClass) ? ' ' . htmlspecialchars($extraClass) : '');

    // Buat HTML input sederhana
    return '
        <div class="row mb-3">
            <label class="col-sm-' . $clabel . ' col-form-label text-right">' . htmlspecialchars($label) . ':</label>
            <div class="col-sm-' . $cinput . '">
                <input 
                    type="' . htmlspecialchars($type) . '" 
                    name="' . htmlspecialchars($name) . '" 
                    id="' . htmlspecialchars($name) . '" 
                    class="' . $classAttr . '" 
                    value="' . htmlspecialchars($value) . '" 
                    placeholder="' . $placeholder . '"' . 
                    $requiredAttr . 
                    $maxlengthAttr . 
                '>
                <div class="invalid-feedback" id="em_' . htmlspecialchars($name) . '"></div>
            </div>
        </div>';
}