<?php
class PurchaseForm extends Form {

    public function __construct() {
        $this->setActionForm('Compra.php');
        $this->setName('purchase');
        $this->setClass('purchase');
        $this->setMethod('post');
        parent::__construct();
        $this->init();
    }

    public function init() {      
        $login = new Login();     
        $attributes_wrapper_append_select = array('class'=>'select2-bootstrap-append');       
        
        $this->addElement(array(
            'type'=>'hidden',
            'name'=>'add_another_one'
        ));        
        
        $this->addElement(array(
            'type' => 'text',
            'name' => 'purchase_date',
            'label'=>'Fecha de compra',
            'validators'=>array('date'),
            'required'=>true
        ));
        
        $append = "<span class = 'btn input-group-addon' data-toggle='modal' data-target='#modalAgregarProveedor'><i class='fa fa-plus'></i></span>";     
        $this->addElement(array(
            'type' => 'select',
            'name' => 'vendor_id',
            'label'=>'Proveedor',
            'multiOptions'=>$this->listVendor(),
            'required'=>true,
            'class'=>'selectProveedor', // Clase para selecto cuando se agrega un nuevo proveedor y se recarga lista.
            'append'=>$append,
            'wrapper_attributes'=>$attributes_wrapper_append_select
        ));  
        
        $this->addElement(array(
            'type' => 'text',
            'name' => 'purchase_number',
            'label'=>'Compra #',
            'required'=>true
        ));        
        
        $this->addElement(array(
            'type' => 'text',
            'name' => 'total',
            'label'=>'Total',
            'class'=>'_maskDouble',
            'required'=>true
        )); 
         
        $this->addElement(array(
            'type' => 'textarea',
            'name' => 'comments',
            'label'=>'Comentarios',
            'required'=> false
        ));
        
        $this->addElement(array(
            'type' => 'button',
            'name' => 'send',
            'value'=> $this->_getTranslation('Terminar'),
            'optionals'=>array("onClick"=>"$('#add_another_one').val(0);$(this).prop('disabled',true);submit();"),
            'class'=>'btn btn-primary'
        ));
        
        $this->addElement(array(
            'type' => 'button',
            'name' => 'send_and_add_new',
            'value'=> $this->_getTranslation('Terminar y agregar otra'),
            'optionals'=>array("onClick"=>"$('#add_another_one').val(1);$(this).prop('disabled',true);submit();"),
            'class'=>'btn btn-primary'
        ));
        
        $this->addElement(array(
            'type' => 'button',
            'name' => 'cancelar',
            'value'=> $this->_getTranslation('Cancelar'),
            'optionals'=>array(
            'onclick'=>"document.location = '".ROOT_HOST."/Controller/Compra.php'"),
            'class'=>'btn btn-danger'
        ));
    }   
    
    public function listVendor(){
        $repository = new VendorRepository();
        $result = $repository->getListSelectVendors();
        
        $array = array('' => 'Seleccionar una opcion...');
        if ($result) {            
            foreach ($result as $key => $value) {
                $array[$key] = $value;
            }           
        }
         return $array;
    }
    
    public function populate($data) {      
        $purchasetRepo = new PurchaseRepository();
        $data = $this->formatDouble($data,$purchasetRepo->inputs_double);
        
        parent::populate($data);
    }
    
    public function isValid(){
        $isValid = parent::isValid();
        
        
        if($isValid){
            $purchaseRepo = new PurchaseRepository();
            $purchase_number = $this->getValueElement('purchase_number');
            $vendor_id = $this->getValueElement('vendor_id');
            
            if($this->getActionController() == 'insert'){
                $existPurchase = $purchaseRepo->existPurchase($purchase_number,$vendor_id);
            }elseif($this->getActionController() == 'edit'){
                $existPurchase = $purchaseRepo->existPurchase($purchase_number,$vendor_id,$this->getId());
            }
            
            if($existPurchase){
                $purchaseRepo->flashmessenger->addMessage(array('danger'=>"Esta compra ya esta registrada en el sistema."));
                return null;
            }
        }
        
        return $isValid;
    }
}
