<?php

#################################################################
#  Copyright notice
#
#  (c) 2013 Jérôme Schneider <mail@jeromeschneider.fr>
#  All rights reserved
#
#  http://formal.codr.fr
#
#  This script is part of the Formal project. The Formal
#  project is free software; you can redistribute it
#  and/or modify it under the terms of the GNU General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  The GNU General Public License can be found at
#  http://www.gnu.org/copyleft/gpl.html.
#
#  This script is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  This copyright notice MUST APPEAR in all copies of the script!
#################################################################

namespace Formal\Element;

class Text extends \Formal\Element {
    protected function inputtype() {
        return "text";
    }

    function render() {
        $disabled = "";
        $inputclass = "";
        $groupclass = "";
        $placeholder = "";

        $value = $this->value();
        $label = $this->option("label");
        $prop = $this->option("prop");
        $placeholder = "";
        $helpblock = "";

        if ($this->option("readonly") === true) {
            $inputclass .= " disabled";
            $disabled = " disabled";
        }

        if ($this->option("error") === true) {
            $groupclass .= " is-invalid";
        }

        if (trim($this->option("class")) !== "") {
            $groupclass .= " " . $this->option("class");
        }

        if (trim($this->option("inputclass")) !== "") {
            $inputclass = $this->option("inputclass");
        }

        if (($sPlaceHolder = trim($this->option("placeholder"))) !== "") {
            $placeholder = " placeholder=\"" . htmlspecialchars($sPlaceHolder) . "\" ";
        }

        $clientvalue = htmlspecialchars($value ?? '');

        $sInputType = $this->inputtype();

        if (($sHelp = trim($this->option("help"))) !== "") {
            $helpblock .= "<div class=\"form-text\">" . $sHelp . "</div>";
        }

        if (($aPopover = $this->option("popover")) !== "") {
            $helpblock .= "<div class=\"form-text\">" . $aPopover["content"] . "</div>";
        }

        $sHtml = <<<HTML
    <div class="row mb-3{$groupclass}">
	<label class="col-sm-4 col-form-label" for="{$prop}">{$label}</label>
	<div class="col-sm-8">
		<input type="{$sInputType}" class="{$inputclass}" id="{$prop}" name="data[{$prop}]" value="{$clientvalue}"{$disabled}{$placeholder}/>
		{$helpblock}
	</div>
    </div>
HTML;

        return $sHtml . $this->renderWitness();
    }
}
