<?php

class BakingForm extends Form {



    public function __construct() {

        $this->setActionForm('Baking.php');

        $this->setEnctype('multipart/form-data');

        $this->setName('baking');

        $this->setMethod('post');

        $this->setDefaultFormLabelsColSize(4);

        $this->setDefaultFormElementsColSize(8);

        parent::__construct();

        $this->init();

    }



    public function init() {        

         $this->addElement(array(

            'type' => 'select',

            'name' => 'id_sucursal',

            'label'=>'Seleccione una sucursal:',

            'multiOptions'=>$this->getListStores(),

            'required'=>true,

            'optionals' => array('onchange' => 'activeSucursal(this)'),

            'col-size-label'=>'3',

            'col-size-element'=>'9',

         ));
    
        

        $this->addElement(array(

            'type' => 'select',

            'name' => 'status',

            'label'=>'Status',

            'multiOptions'=>array('1'=>'Activa'),

            'required'=>false

         ));      

       

        $this->addElement(array(

            'type' => 'textarea',

            'name' => 'comentarios',

            'label'=>'Notas',

        ));

        

        $this->addElement(array(

            'id'=> 'attachments',

            'type' => 'file',

            'name' => "attachments[]",

            'label' => 'Documentos adjuntos',

            'class' => 'file upload',

            'required' => false,

            'optionals' => array(

                'title' => 'Documentos adjuntos',

                'multiple'=>'',

                'data-show-preview'=>true,

                'data-show-upload'=>false

            )

        ));       

        

        $this->addElement(array(

            'type'=>'button',

            'name'=>'agregar_producto',

            'value'=>$this->_getTranslation('Agregar producto'),

            'class'=>'btn btn-info _addDetail'

        ));        

        

        $this->addElement(array(

            'type'=>'button',

            'name'=>'terminar',

            'value'=>$this->_getTranslation('Terminar'),

            'class'=>'btn btn-primary',

            'optionals'=>array("onClick"=>"submit('Baking')")

        ));        

    }    

       


     public function getListStores(){

        $repo = new StoreRepository();

        $result = $repo->getListSelectStores();

        

        $login = new Login();

        $storeIdArray = $login->getStoreIdArray();

        

        if($login->getRole()!='1'){

            if ($result) {
                    
                $array = array(''=>'Seleccionar una opcion...');

                foreach ($result as $key => $value) {

                    if(in_array($key, $storeIdArray)){$array[$key] = $value;}                    

                }

            }   

        }else{

            $array = array(''=>'Seleccionar una opcion...');

            if ($result) {               

                foreach ($result as $key => $value) {

                    $array[$key] = $value;

                }

            }   

        } 

        return $array;

    }

    

    function populate($data) {

        $tools = new Tools(); 

        

        if(isset($data['date']) && substr_count($data['date'], '-') > 0){

           $data['date'] = $tools->setFormatDateTimeToForm($data['date']);

        }

        

        parent::populate($data);        

    }

    

    function isValid() {

        $rs = parent::isValid();

        

        if($rs){

            $comments = $this->getValueElement('comments');

            $comments = trim($comments);

            

            if(empty($comments)){

                $flashmessenger = new FlashMessenger();

                $flashmessenger->addMessage(array('danger'=>'Campo notas es obligatorio.'));

                return null;

            }

        }

        return $rs;

    }

}

