<?php
class AddSupplyForm extends Form {    
    public function __construct() {
        $this->setName('addSupply');
        $this->setDefaultFormLabelsColSize(4);
        $this->setDefaultFormElementsColSize(8);
        parent::__construct();
        $this->init();
    }

    public function init() {               
         /*Se utiliza para consulta si existe en la tabla de comprasdetalles_X, si existe se actualiza registro.*/
         $this->addElement(array(
            'type' => 'hidden',
            'name' => 'idDetailTempSupply',
            'required'=>false
        ));      
         
        $this->addElement(array(
            'type' => 'hidden',
            'name' => 'supply_id',
        ));
        
        /*Auxiliar para saber que insumo es; y saber si lleva numero de serie*/
        $this->addElement(array(
            'type' => 'hidden',
            'name' => 'supply_name',
        ));
        
        $this->addElement(array(
            'type' => 'select',
            'name' => 'location_id',
            'label'=>'Locacion',
            'multiOptions'=>$this->getListLocations(),
            'required'=>true,
        )); 
         
         $append = "<span class = 'btn input-group-addon' data-toggle='modal' data-target='#modalAgregarProducto'><i class='fa fa-plus'></i></span>";
         $this->addElement(array(
            'type' => 'text',
            'name' => 'supply',
            'label'=>'Insumo',
            'optionals'=>array('placeholder'=>'Teclea insumo.'),
            'append'=>$append
         ));
         
         /*Este lo uso para mostratlo en mfg*/
         $this->addElement(array(
            'type' => 'select',
            'name' => 'select_supply_id',
            'label'=>'Insumo',
            'multiOptions'=>$this->getListSupplies()
         ));
        
        $this->addElement(array(
            'type' => 'text',
            'name' => 'quantity',
            'label'=>'Cantidad',
            'validators'=>array('double'),
            'class'=>'_maskDouble',
            'required'=>false,
        ));
        
        $this->addElement(array(
            'type' => 'text',
            'name' => 'cost',
            'label'=>'Costo',
            'validators'=>array('double'),
            'class'=>'_maskDouble',
            'required'=>false,
        ));
        
        $this->addElement(array(
            'type' => 'text',
            'name' => 'initial_serial_number',
            'label'=>'# serie inicial',
            'required'=>false,
        ));
        
         $this->addElement(array(
            'type' => 'text',
            'name' => 'final_serial_number',
            'label'=>'# serie final',
            'required'=>false,
        ));
        
        $this->addElement(array(
            'type'=>'button',
            'name'=>'buscar',
            'value'=>$this->_getTranslation('Agregar'),
            'class'=>'btn btn-primary _saveSupply'
        ));
        
        $this->addElement(array(
            'type'=>'button',
            'name'=>'cerrar_modal',
            'value'=>$this->_getTranslation('Cerrar'),
            'class'=>"btn btn-default _closeModalAddSupply"
        ));        
    }  
    
    public function getListLocations(){
        $repository = new LocationRepository();
        $productos = $repository->getListSelectLocations(null,'supplies');
        
        $array = array('' => 'Seleccionar una opcion...'); 
        if($productos){
            foreach ($productos as $key => $value) {
                $array[$key] = $value;
            }   
        }
        
        return $array;
    }
    
    public function getListSupplies(){
        $suppyRepo = new GoodAndServiceCatalogRepository();
        $list = $suppyRepo->getListSelectProducts('good and services');
        
        $array = array(''=>'Seleccionar una opcion...');
        
        if($list){
            foreach($list as $key => $value){
                $array[$key] = $value;
            }
        }
        
        return $array;
    }
}