<?php
namespace App\Helpers\BootForm;

class BootForm{
    private $placeholder = false;

    //utilisation des sessions pour la gestion des erreurs
    public function __construct($form, $session){
        //injection de dependance pour eviter k si on change
        /* le nom de la facade sa afecte notre classe*/
        $this->form = $form;
        $this->session = $session;
    }

    public function open( array $options = array()){
        //recuperation du nom du model
        //  $controller_name = str_plural(get_class($model))."Controller";

        if (isset($options['placeholder']) && $options['placeholder'] === true ){
            $this->placeholder = true;
        }
        return $this->form->open($options);
    }

    public function close(){
        return $this->form->close();
    }

    //c'est ici quon personnalise tout pour les inputs
    public function input($type ,$name, $label = null , $value = null , $options = [] , $list = []){
        /*recuperation des erreurs liées au entrées*/
        $errors = $this->session->get('errors');
        $return = "";
        //on permet d'envoyer en 2nd parametre les options au lieu du label
        if(is_array($label)){
            $label = null;
            $options = $label;
        }

        //on permet d'envoyer en 2nd parametre les options au lieu de la valeur
        if(is_array($value)){
            $options = $value;
            $value = null;
        }

        if (!$label){
            $label = trans("form.$name");
        }

        //si vrai
        if ($this->placeholder){
            $options['placeholder'] = $label;
            $label = false;
        }
        //si on a defini une option class pour l'input
        if(isset($options['class'])){

            if ($type == 'button' || $type == 'submit'){  $options['class'] = '';}
            else{
                // il ajoute
                $options['class'] .= ' form-control form-control-sm';
            }

        }else{
            $options['class'] = 'form-control form-control-sm ';
        }

        $wrapClass = ($errors && $errors->has($name))? 'has-error':'';
        $return .= '<div class="form-group '.$wrapClass.'">';
//        $return .= $this->form->label($name,$label,['class'=>'txt1']);

        /*placeholder a la place des label*/
        if ($label !== false){
            $required = $options['required'] ?? false;
            $return .= $this->form->label($name,$label);

            if ($required){
                $return .= "<span class='text-danger'> * </span>";
            }
        }

        /*textarea*/
        if($type == 'textarea'){
            $return .= $this->form->textarea($name,$value,$options);
        }elseif($type == 'select'){
            $return .= $this->form->select($name,$list ,$value, $options);
        }elseif ($type == 'password'){
            $return .= '<span class="btn-show-pass"><i class="fa fa-eye"></i></span>'.$this->form->input($type,$name,$value,$options);
        }
        else{

            //si il ya erreurs liées au formulaire
            if ($errors && $errors->has($name)){
                $options['class'] .= ' is-invalid';
            }else{
                //  $options['class'] .= ' is-valid';
            }
            $return .= $this->form->input($type,$name,$value,$options);
        }

        // $turn = ($errors && $errors->has($name))? $errors->first($name):"";
        //$return .= '<span class="'.$focusClass.'"></span>';
        if ($errors && $errors->has($name)){
            $return .= "<small class='help-block'>".$errors->first($name)."</small>";
        }
        $return .= "</div>";



        return $return;
    }

    public function text($name, $label = null , $value = null , $options = array()){
        return  $this->input('text',$name, $label,$value,$options);
    }

    public function date($name, $label = null , $value = null , $options = array()){
        return  $this->input('date',$name, $label,$value,$options);
    }


    public function password($name, $label = null , $value = null , $options = array()){
        return  $this->input('password',$name, $label,$value,$options);
    }

    public function url($name, $label = null , $value = null , $options = array()){
        return  $this->input('url',$name, $label,$value,$options);
    }

    public function tel($name, $label = null , $value = null , $options = array()){
        return  $this->input('tel',$name, $label,$value,$options);
    }

    public function time($name, $label = null , $value = null , $options = array()){
        return  $this->input('time',$name, $label,$value,$options);
    }

    public function number($name, $label = null , $value = null , $options = array()){
        return  $this->input('number',$name, $label,$value,$options);
    }


    public function file($name, $label = null , $value = null , $options = array()){
        return  $this->input('file',$name, $label,$value,$options);
    }

    public function email($name, $label = null , $value = null , $options = array()){
        return  $this->input('email',$name, $label,$value,$options);
    }

    public function submit($name, $label = null , $value = null , $options = array()){
        return  $this->input('submit',$name, $label,$value,$options);
    }

    public function button($name, $label = null , $value = null , $options = array()){
        return  $this->input('button',$name, $label,$value,$options);
    }

    public function textarea($name, $label = null , $value = null , $options = array()){
        return  $this->input('textarea',$name, $label,$value,$options);
    }

    public function select($name, $label = null , $value = null , $list = [] ,$options = array()){
        //on permet d'envoyer en 2nd parametre les options au lieu du label
        if(is_array($label)){
            $list = $label;
            $label = null;

        }
        return  $this->input('select',$name, $label,$value,$options,$list);
    }

}
