<?php
namespace Nibiru\Module\Users\Traits;
/**
 * Trait     UserForm
 * @project  src
 * @desc     This is a PHP trait file, please specify the use
 * @author   stephan - Maschinen Stockert Großhandels GmbH
 * @date     19.04.23
 * @time     15:10
 * @package  ${PACKAGE}
 */
use Nibiru\Factory\Form;
use Nibiru\Module\Users\Plugins\Acl;
trait UserForm
{
    public static function userForm(string $action = '', array $user = [])
    {
        $acl = new Acl();
        Form::create();
        self::openHalfWidthElement('Benutzer Informationen');
        self::openInnerHalfWithElement();
        foreach(self::FORM_CREATE_USER as $entry)
        {
            if(sizeof($user) > 0)
            {
                self::addTextElement($entry['visibleName'], $entry['valueName'], $entry['icon'], $user[$entry['valueName']]);
            }
            else
            {
                self::addTextElement($entry['visibleName'], $entry['valueName'], $entry['icon']);
            }
        }
        foreach(self::FORM_CREATE_PASSWORD as $entry)
        {
            if(sizeof($user) > 0)
            {
                self::addPasswordElement($entry['visibleName'], $entry['valueName'], $entry['icon'], $user['user_pass']);
            }
            else
            {
                self::addPasswordElement($entry['visibleName'], $entry['valueName'], $entry['icon']);
            }
        }
        self::closeHalfWidthElement();
        self::closeHalfWidthElement();
        self::openHalfWidthElement('Konto Informationen');
        self::openInnerHalfWithElement();
        foreach(self::FORM_CREATE_ACCOUNT as $entry)
        {
            if(sizeof($user) > 0)
            {
                self::addTextElement($entry['visibleName'], $entry['valueName'], $entry['icon'], $user[$entry['valueName']]);
            }
            else
            {
                self::addTextElement($entry['visibleName'], $entry['valueName'], $entry['icon']);
            }
        }

        if(sizeof($user) > 0)
        {
            self::addSelectDropdown($acl->loadAclRoles(), 'lock', $user['user_role_id']);
            self::addActiveCheckbox('Konto aktivieren', $user['user_account_active']);
        }
        else
        {
            self::addSelectDropdown($acl->loadAclRoles(), 'lock');
            self::addActiveCheckbox('Konto aktivieren', true);
        }
        self::closeHalfWidthElement();
        if(sizeof($user) > 0)
        {
            self::addHiddenElementEditUser($user);
            self::addSubmitFormButton('Speichern', 'save-alt');
        }
        else
        {
            self::addHiddenElementNewUser();
            self::addSubmitFormButton('Erstellen', 'save-alt');
        }

        self::closeHalfWidthElement();
        return Form::addForm([
            'name'  => 'userForm',
            'action' => $action,
            'class' => 'row',
            'method' => 'post',
            'target' => '_self'
        ]);
    }
    private static function addHiddenElementNewUser()
    {
        Form::addTypeHidden([
            'name'  => 'user_new',
            'value' => 1
        ]);
        Form::addTypeHidden([
            'name'  => 'user_active',
            'value' => 1
        ]);
    }
    private static function addHiddenElementEditUser(array $user = [])
    {
        Form::addTypeHidden([
            'name'  => 'user_edit',
            'value' => 1
        ]);
        Form::addTypeHidden([
            'name'  => 'user_active',
            'value' => $user['user_account_active']
        ]);
        Form::addTypeHidden([
            'name'  => 'user_id',
            'value' => $user['uid']
        ]);
    }
    private static function addSubmitFormButton(string $buttonText = '', string $icon = '')
    {
        Form::addOpenDiv([
            'class' => 'box-footer text-end',
            'value' => ''
        ]);
        Form::addTypeButton([
            'class' => 'btn btn-primary',
            'type'  => 'submit',
            'value' => '<i class="ti-'.$icon.'"></i>&nbsp;'.$buttonText
        ]);
        Form::addCloseDiv();
    }
    /**
     * @desc will add a text element to the form
     * @param string $visibleName
     * @param string $valueName
     * @param string $icon
     * @return void
     */
    private static function addTextElement(string $visibleName = '', string $valueName = '', string $icon = '', string $value = '')
    {
        Form::addTypeLabel([
            'class' => 'form-label',
            'for'   => $valueName,
            'value' => $visibleName
        ]);
        Form::addOpenDiv([
            'class' => 'input-group mb-3',
            'value' => ''
        ]);
        Form::addOpenAny([
            'any' => 'span',
            'class' => 'input-group-text',
            'value' => '<i class="ti-'.$icon.'"></i>'
        ]);
        Form::addCloseAny([
            'any' => 'span'
        ]);
        if($value!="")
        {
            Form::addInputTypeText([
                'class' => 'form-control',
                'name'  => $valueName,
                'placeholder' => $visibleName,
                'value' => $value
            ]);
        }
        else
        {
            Form::addInputTypeText([
                'class' => 'form-control',
                'name'  => $valueName,
                'placeholder' => $visibleName
            ]);
        }

        Form::addCloseDiv();
    }

    /**
     * @desc will add a select dropdown to the form
     * @param array $options
     * @param string $icon
     * @return void
     */
    private static function addSelectDropdown( array $options = [], string $icon = '', int $selected = 0 )
    {
        Form::addTypeLabel([
            'for'   => 'source-api',
            'class' => 'form-label',
            'value' => 'Rolle auswählen:'
        ]);
        Form::addOpenDiv([
            'class' => 'input-group mb-3',
            'value' => ''
        ]);
        Form::addOpenAny([
            'any' => 'span',
            'class' => 'input-group-text',
            'value' => '<i class="ti-'.$icon.'"></i>'
        ]);
        Form::addCloseAny([
            'any' => 'span'
        ]);
        foreach($options as $option)
        {
            if($selected == $option['acl_id'])
            {
                Form::addSelectOption([
                    'context' => $option['acl_role'],
                    'value' => $option['acl_id'],
                    'selected' => 'selected'
                ]);
            }
            else
            {
                Form::addSelectOption([
                    'context' => $option['acl_role'],
                    'value' => $option['acl_id']
                ]);
            }
        }
        Form::addSelect([
            'name' => 'user_acl_id',
            'class' => 'form-control select2'
        ]);
        Form::addCloseDiv();
    }

    /**
     * @desc will add a password element to the form
     * @param string $visibleName
     * @param string $valueName
     * @param string $icon
     * @return void
     */
    private static function addPasswordElement(string $visibleName = '', string $valueName = '', string $icon = '', string $value = '')
    {
        Form::addTypeLabel([
            'class' => 'form-label',
            'for'   => $valueName,
            'value' => $visibleName
        ]);
        Form::addOpenDiv([
            'class' => 'input-group mb-3',
            'value' => ''
        ]);
        Form::addOpenAny([
            'any' => 'span',
            'class' => 'input-group-text',
            'value' => '<i class="ti-'.$icon.'"></i>'
        ]);
        Form::addCloseAny([
            'any' => 'span'
        ]);
        if($value!="")
        {
            Form::addInputTypePassword([
                'class' => 'form-control',
                'name'  => $valueName,
                'placeholder' => $visibleName,
                'value' => $value
            ]);
        }
        else
        {
            Form::addInputTypePassword([
                'class' => 'form-control',
                'name'  => $valueName,
                'placeholder' => $visibleName
            ]);
        }
        Form::addCloseDiv();
    }

    /**
     * @desc will open a half width element for form fields
     * @param string $title
     * @return void
     */
    private static function openHalfWidthElement(string $title = '')
    {
        Form::addOpenDiv([
            'class' => 'col-lg-6 col-13',
            'value' => ''
        ]);
        Form::addOpenDiv([
            'class' => 'box',
            'value' => ''
        ]);
        Form::addOpenDiv([
            'class' => 'box-header with-border',
            'value' => '<h4 class="box-title">'.$title.'</h4>'
        ]);
        Form::addCloseDiv();
    }

    /**
     * @desc will add the inner half width distance frame element for form fields
     * @return void
     */
    private static function openInnerHalfWithElement()
    {
        Form::addOpenDiv([
            'class' => 'form-group',
            'value' => ''
        ]);
        Form::addOpenDiv([
            'class' => 'box-body',
            'value' => ''
        ]);
    }

    /**
     * @desc will add a checkbox for the active user account state
     * @param string $visibleTitle
     * @param bool $active
     * @return void
     */
    private static function addActiveCheckbox(string $visibleTitle = '', int $active = 0)
    {
        Form::addOpenDiv([
            'class' => 'form-group', 'value' => ''
        ]);
        Form::addOpenDiv([
            'class' => 'checkbox checkbox-success',
            'value' => ''
        ]);
        if($active)
        {
            Form::addInputTypeCheckbox([
                'id' => 'checkbox',
                'name' => 'user_account_active',
                'checked' => true
            ]);
        }
        else
        {
            Form::addInputTypeCheckbox([
                'id' => 'checkbox',
                'name' => 'account_active'
            ]);
        }
        Form::addTypeLabel([
            'for' => 'checkbox',
            'value' => $visibleTitle
        ]);
        Form::addCloseDiv();
        Form::addCloseDiv();
    }

    /**
     * @desc will close the inner half width distance frame element, and the half width element for form fields
     * @return void
     */
    private static function closeHalfWidthElement()
    {
        Form::addCloseDiv();
        Form::addCloseDiv();
    }

}