<?php

namespace App\Support;

use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Session;

class Form
{
    /**
     * Generate a CSRF token form field.
     *
     * @return string
     */
    public static function token()
    {
        return '<input type="hidden" name="_token" value="' . csrf_token() . '">';
    }

    /**
     * Generate a hidden form field.
     *
     * @param string $name
     * @param string $value
     * @param array $options
     * @return string
     */
    public static function hidden($name, $value = '', $options = [])
    {
        $attributes = self::buildAttributes($options);
        return '<input type="hidden" name="' . $name . '" value="' . $value . '"' . $attributes . '>';
    }

    /**
     * Generate a text input form field.
     *
     * @param string $name
     * @param string $value
     * @param array $options
     * @return string
     */
    public static function text($name, $value = '', $options = [])
    {
        $attributes = self::buildAttributes($options);
        return '<input type="text" name="' . $name . '" value="' . $value . '"' . $attributes . '>';
    }

    /**
     * Generate a textarea form field.
     *
     * @param string $name
     * @param string $value
     * @param array $options
     * @return string
     */
    public static function textarea($name, $value = '', $options = [])
    {
        $attributes = self::buildAttributes($options);
        return '<textarea name="' . $name . '"' . $attributes . '>' . $value . '</textarea>';
    }

    /**
     * Generate a select form field.
     *
     * @param string $name
     * @param array $list
     * @param string $selected
     * @param array $options
     * @return string
     */
    public static function select($name, $list = [], $selected = null, $options = [])
    {
        $attributes = self::buildAttributes($options);
        $html = '<select name="' . $name . '"' . $attributes . '>';
        
        foreach ($list as $value => $label) {
            $isSelected = ($selected == $value) ? ' selected' : '';
            $html .= '<option value="' . $value . '"' . $isSelected . '>' . $label . '</option>';
        }
        
        $html .= '</select>';
        return $html;
    }

    /**
     * Generate a checkbox form field.
     *
     * @param string $name
     * @param string $value
     * @param bool $checked
     * @param array $options
     * @return string
     */
    public static function checkbox($name, $value = '1', $checked = false, $options = [])
    {
        $attributes = self::buildAttributes($options);
        $checkedAttr = $checked ? ' checked' : '';
        return '<input type="checkbox" name="' . $name . '" value="' . $value . '"' . $checkedAttr . $attributes . '>';
    }

    /**
     * Generate a label form field.
     *
     * @param string $name
     * @param string $value
     * @param array $options
     * @return string
     */
    public static function label($name, $value = null, $options = [])
    {
        $attributes = self::buildAttributes($options);
        $value = $value ?: ucfirst(str_replace('_', ' ', $name));
        return '<label for="' . $name . '"' . $attributes . '>' . $value . '</label>';
    }

    /**
     * Build HTML attributes from options array.
     *
     * @param array $options
     * @return string
     */
    private static function buildAttributes($options)
    {
        $attributes = '';
        
        foreach ($options as $key => $value) {
            if ($key === 'required' && $value === '') {
                $attributes .= ' required';
            } elseif ($key === 'required' && $value) {
                $attributes .= ' required';
            } elseif ($key === 'disabled' && $value) {
                $attributes .= ' disabled';
            } elseif ($key === 'class' && $value) {
                $attributes .= ' class="' . $value . '"';
            } elseif ($key === 'placeholder' && $value) {
                $attributes .= ' placeholder="' . $value . '"';
            } elseif ($key === 'maxlength' && $value) {
                $attributes .= ' maxlength="' . $value . '"';
            } elseif ($key === 'rows' && $value) {
                $attributes .= ' rows="' . $value . '"';
            } elseif ($key === 'id' && $value) {
                $attributes .= ' id="' . $value . '"';
            } elseif ($key === 'style' && $value) {
                $attributes .= ' style="' . $value . '"';
            } elseif ($key === 'data-format' && $value) {
                $attributes .= ' data-format="' . $value . '"';
            } elseif ($key === 'autocomplete' && $value) {
                $attributes .= ' autocomplete="' . $value . '"';
            } elseif ($key === 'summernote' && $value) {
                $attributes .= ' summernote';
            } elseif ($key === 'summernote-height' && $value) {
                $attributes .= ' summernote-height="' . $value . '"';
            } elseif ($key === 'bs-datepicker' && $value) {
                $attributes .= ' bs-datepicker';
            } elseif ($key === 'arch-change' && $value) {
                $attributes .= ' arch-change="' . $value . '"';
            } elseif ($key === 'ng-model' && $value) {
                $attributes .= ' ng-model="' . $value . '"';
            } elseif ($key === 'ng-required' && $value) {
                $attributes .= ' ng-required="' . $value . '"';
            } elseif ($key === 'ng-disabled' && $value) {
                $attributes .= ' ng-disabled="' . $value . '"';
            } elseif ($key === 'ng-change' && $value) {
                $attributes .= ' ng-change="' . $value . '"';
            } elseif ($key === 'ng-show' && $value) {
                $attributes .= ' ng-show="' . $value . '"';
            } elseif ($key === 'numbers-only' && $value) {
                $attributes .= ' numbers-only';
            } elseif ($key === 'lookup-stock' && $value) {
                $attributes .= ' lookup-stock';
            } elseif ($key === 'stock-selected' && $value) {
                $attributes .= ' stock-selected="' . $value . '"';
            } elseif ($key === 'ui-select2' && $value) {
                $attributes .= ' ui-select2';
            } elseif ($key === 'multiple' && $value) {
                $attributes .= ' multiple';
            } elseif ($key === 'remove-selected' && $value) {
                $attributes .= ' remove-selected="' . $value . '"';
            } elseif ($key === 'name' && $value) {
                $attributes .= ' name="' . $value . '"';
            }
        }
        
        return $attributes;
    }
} 