<?php

namespace bin\php\Classes;
class formBuilder
{
    public string $title;
    public string $icon;
    public mixed $submitButton;
    public mixed $closeButton;
    public string $closeButtonLocation;

    public string $submitButtonColor;

    public mixed $submitButtonText;

    public string $submitButtonIcon;

    private array $extraButtonsArray = array();

    public function __construct($title, $icon, $closeButtonLocation, $submitButton = true, $closeButton = true)
    {
        $this->title = __($title);
        $this->icon = $icon;
        $this->submitButton = $submitButton;
        $this->closeButton = $closeButton;
        $this->closeButtonLocation = $closeButtonLocation;
        $this->submitButtonColor = 'primary';
        $this->submitButtonText = __($title);
        $this->submitButtonIcon = $icon;
    }

    public function startForm(): void
    { ?>
        <div class="row"><div class="col-md-8 ms-auto me-auto"><div class="card">
        <div class="card-header">
            <h3><?php echo $this->icon ?>&nbsp; &nbsp;&nbsp;<?php echo $this->title ?></h3>
        </div>
    <?php }

    public function endForm()
    { ?>
        </div></div></div>
    <?php }

    # Add extra buttons to the form footer between the submit and close button
    # Array example:
    # array(
    #   0 => array(
    #       'buttonText' => 'testButton2123',
    #       'buttonIcon' => 'ico123123ncontnet',
    #       'buttonHref' => 'test'
    #       'buttonColor' => 'success'
    #   ) > next array

    public function addExtraButtons($extraButtonsArray): bool
    {
        foreach ($extraButtonsArray as $extraButtonArray) {
            if (is_array($extraButtonArray)) {
                if (array_key_exists('buttonIcon', $extraButtonArray) && array_key_exists('buttonText', $extraButtonArray) && array_key_exists('buttonHref', $extraButtonArray) && array_key_exists('buttonColor', $extraButtonArray)) {
                    $this->extraButtonsArray[] = $extraButtonArray;
                    return true;
                }
            }
        }
        return false;
    }

    public function formFooter(): void
    { ?>
        <div class="card-footer pt-3">
            <div class="row">
                <div class="col d-flex justify-content-end">
                    <?php if ($this->submitButton) { ?>
                        <button type="submit" class="btn btn-<?php echo $this->submitButtonColor ?>"><?php echo $this->submitButtonIcon ?>&nbsp;<?php echo $this->submitButtonText ?></button>&nbsp; &nbsp;
                    <?php }

                    foreach ($this->extraButtonsArray as $extraButtonArray) { ?>
                        <a href="<?php echo $extraButtonArray['buttonHref'] ?>" class="btn btn-<?php echo $extraButtonArray['buttonColor'] ?>"><?php echo $extraButtonArray['buttonIcon'] ?>&nbsp;<?php echo $extraButtonArray['buttonText'] ?></a>&nbsp; &nbsp;
                    <?php }

                    if ($this->closeButton) { ?>
                        <a href="<?php echo $this->closeButtonLocation ?>" class="btn btn-danger">
                            <i class="fas fa-times"></i> <?php echo __('close') ?>
                        </a>
                    <?php } ?>
                </div>
            </div>
        </div>
    <?php }
}