<?php
/**
 * Arquivo de declaração da classe Form.
 */
class Form
{
    /**
     * FORM: cria um form.
     *
     * @param  string $action
     * @param  array  $attr
     * @return string
     */
    public static function open($action = '', $attr = array())
    {
        $attr_str = '';

        foreach ($attr as $k => $v) {
            $attr_str .= "$k=\"$v\" ";
        }

        $html = "<form action=\"$action\" method=\"post\" enctype=\"multipart/form-data\" $attr_str>";

        return $html;
    }

    /**
     * FORM: fecha o form.
     *
     * @return string
     */
    public static function close()
    {
        return '</form>';
    }

    /**
     * File: exibe um input file.
     *
     * @param  string $label
     * @param  string $name
     * @param  string $dirImagem
     * @param  array  $attr
     * @return string
     */
    public static function file($label = '', $name = '', $dirImagem = '', $attr = array())
    {
        $html = '';

        if (null != $dirImagem) {
            if (strstr($label, 'Imagem')) {
                $html .= '<img src="' . $dirImagem . '" id="img_' . $name . '" />';
            } else {
                $html .= '<a href="' . $dirImagem . '" target="_blank" style="font-size:15px; color:#4faf49" >Arquivo cadastrado</a>';
            }

            $html .= '<br>';
        }

        $attr_str = '';

        if (strstr($label, 'Imagem')) {
            $label = "<img src='./public/img/image.gif' style='position:relative; bottom:5px;'  /> &nbsp;&nbsp;" . $label;
        }

        foreach ($attr as $k => $v) {
            $attr_str .= "$k=\"$v\" ";
        }

        $html .= '<label>' . $label . '</label>';
        $html .= '<input type="file" name="' . $name . '" id="' . $name . '" ' . $attr_str . '/>';

        return $html;
    }

    /**
     * Exibe um input text formatado.
     *
     * @param  string  $label
     * @param  string  $name
     * @param  bool $value
     * @param  bool $length
     * @param  array   $attr
     * @param  string  $type
     * @return string
     */
    public static function datetime($label = '', $name = '', $value = false, $length = false, $attr = array(), $type = 'datetime')
    {
        $attr_str = '';

        foreach ($attr as $k => $v) {
            $attr_str .= "$k=\"$v\"";
        }

        if (false !== $length) {
            $length = "maxlength=\"$length\"";
        }

        if (false !== $value) {
            $value = "value=\"$value\"";
        }

        $html = '<input type="text" name="' . $name . '" id="' . $name . '" ' . $value . ' ' . $length . ' ' . $attr_str . ' data-field="' . $type . '"><div id="datepicker"></div>';

        return $html;
    }

    /**
     * Exibe um input text formatado.
     *
     * @param  string  $label
     * @param  string  $name
     * @param  bool $value
     * @param  bool $length
     * @param  array   $attr
     * @param  bool $special_box
     * @return string
     */
    public static function text($label = '', $name = '', $value = false, $length = false, $attr = array(), $special_box = false)
    {
        $attr_str = '';
        $html = '';

        foreach ($attr as $k => $v) {
            $attr_str .= "$k=\"$v\"";
        }

        if (false !== $value) {
            $value = "value=\"$value\"";
        }

        if (false !== $length) {
            $length = "maxlength=\"$length\"";
        }
        $html .= '<input type="text" name="' . $name . '" id="' . $name . '" ' . $value . ' ' . $length . ' ' . $attr_str . '/>';

        return $html;
    }

    /**
     * Exibe um input text formatado.
     *
     * @param  string  $label
     * @param  string  $name
     * @param  bool $value
     * @param  bool $length
     * @param  array   $attr
     * @param  bool $special_box
     * @return string
     */
    public static function number($label = '', $name = '', $value = false, $max = 100, $length = false, $attr = array(), $special_box = false)
    {
        $attr_str = '';
        $html = '';

        foreach ($attr as $k => $v) {
            $attr_str .= "$k=\"$v\"";
        }

        if (false !== $value) {
            $value = "value=\"$value\"";
        }

        if (false !== $length) {
            $length = "maxlength=\"$length\"";
        }
        $html .= '<input type="number" name="' . $name . '" id="' . $name . '" ' . $value . ' ' . $length . ' ' . $attr_str . ' max="'.$max.'" />';

        return $html;
    }

    /**
     * TEXTAREA: exibe um textarea formatado.
     *
     * @param  string  $label
     * @param  string  $name
     * @param  bool $value
     * @param  array   $attr
     * @param mixed $id
     * @return string
     */
    public static function textarea($label = '', $name = '', $id = '', $value = false, $attr = array())
    {
        $attr_str = '';
        $html = '';

        foreach ($attr as $k => $v) {
            $attr_str .= "$k=\"$v\"";
        }

        $html .= '<textarea name="' . $name . '" id="' . $id . '" rows="8" cols="40"' . $attr_str . '>' . $value . '</textarea>';

        return $html;
    }

    /**
     * RADIO : exibe um radio button formatado.
     *
     * @param  string  $name
     * @param  bool $value
     * @param  array   $attr
     * @return string
     */
    public static function radio($name = '', $value = false, $attr = array())
    {
        $attr_str = '';

        foreach ($attr as $k => $v) {
            $attr_str .= "$k=\"$v\"";
        }

        if (false !== $value) {
            $value = "value=\"$value\"";
        }

        $html = '<input name="' . $name . '" type="radio" ' . $value . ' ' . $attr_str . ' />';

        return $html;
    }

    /**
     * PASSWORD: exibe um input password formatado.
     *
     * @param  string  $label
     * @param  string  $name
     * @param  bool $value
     * @param  bool $length
     * @param  array   $attr
     * @param  bool $special_box
     * @return string
     */
    public static function password($label = '', $name = '', $value = false, $length = false, $attr = array(), $special_box = false)
    {
        $attr_str = '';

        foreach ($attr as $k => $v) {
            $attr_str .= "$k=\"$v\"";
        }

        if (false !== $value) {
            $value = "value=\"$value\"";
        }

        if (false !== $length) {
            $length = "maxlength=\"$length\"";
        }

        $html = (!$special_box) ? '<label>' . $label . '</label>' : '<label class="left">' . $label . '</label>';
        $html .= '<input type="password" name="' . $name . '" id="' . $name . '" ' . $value . ' ' . $length . ' ' . $attr_str . '/>';

        return $html;
    }

    /**
     * Metodo estatico para criacao de SELECT.
     *
     * @param  string $label
     * @param  string $name
     * @param  array  $dados
     * @param  array  $selected
     * @param  array  $attr
     * @return string
     */
    public static function select($label = '', $name = '', $dados = array(), $selected = array(), $attr = array())
    {
        $attr_str = '';
        $html = '';

        $id = $name;
        $id = str_replace('[', '', $id);
        $id = str_replace(']', '', $id);

        $selected = (is_array($selected)) ? $selected : array($selected);

        foreach ($attr as $k => $v) {
            $attr_str .= "$k=\"$v\"";
        }
        $html = '<label>' . $label . '</label>';
        $html .= "<select data-placeholder=' ' name=" . $name . ' id=' . $id . ' ' . $attr_str . '>';

        if (is_array($dados)) {
            foreach ($dados as $sel) {
                foreach ($sel as $id => $value) {
                    if ('-2' != $id) {
                        if (in_array($id, $selected)) {
                            $html .= "<option value='" . $id . "' selected>" . $value . '</option>';
                        } else {
                            $html .= "<option value='" . $id . "'>" . $value . '</option>';
                        }
                    } else {
                        $html .= "<option value='' class='sel-header'>" . $value . '</option>';
                    }
                }
            }
        }

        $html .= '</select>';

        return $html;
    }

    /**
     * HIDDEN: exibe input tipo hidden.
     *
     * @param  string  $name
     * @param  bool $value
     * @return string
     */
    public static function hidden($name = '', $value = false)
    {
        $html = '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . $value . '" />';

        return $html;
    }

    /**
     * CHECKBOX: exibe input tipo checkbox.
     *
     * @param  string  $name
     * @param  bool $value
     * @param  string  $texto
     * @param  bool $checked
     * @return string
     */
    public static function checkbox($name = '', $value = false, $texto = '', $checked = false)
    {
        $checked = $checked ? ' checked' : '';
        $html = '<p><input type="checkbox" id="' . $name . '" name="' . $name . '" value="' . $value . '" ' . $checked . ' /> ' . $texto . '</p>';

        return $html;
    }

    /**
     * BUTTON: exibe button.
     *
     * @param  string $text
     * @param  array  $attr
     * @return string
     */
    public static function button($text = '', $attr = array())
    {
        $attr_str = '';
        foreach ($attr as $k => $v) {
            $attr_str .= "$k=\"$v\"";
        }
        $html = '<button type="button" ' . $attr_str . '>' . $text . '</button>';

        return $html;
    }
}
