<?php
/**
 * Description of NivelForm
 *
 * @author carlos
 */

class RewardsRulesForm extends Form {

    public function __construct() {
        $this->setActionForm('Rewards.php');
        $this->setName('formrules');
        $this->setClass('rules');
        $this->setMethod('post');
        $this->setDefaultFormElementsColSize(7);
        $this->setDefaultFormLabelsColSize(5);
        parent::__construct();
        $this->init();
    }

    public function init() {       
        $this->addElement(array(
            'type' => 'text',
            'label'=>'Nombre',
            'name' => 'name',
            'required'=> true
        ));
        
        $this->addElement(array(
            'type' => 'select',
            'label'=>'Tipo de puntos',
            'name' => 'type_of_points',
            'multiOptions'=>array('0'=>'x0','1'=>'x1','2'=>'x2','3'=>'x3'),
            'required'=> true
        ));        
              
        $this->addElement(array(
            'type' => 'select',
            'name' => 'product_id',
            'label'=>'Producto',
            'multiOptions'=>$this->getListProduct(),
            'required'=> true
        ));
        
        $this->addElement(array(
            'type' => 'text',
            'name' => 'start_date',
            'label'=>'Fecha inicio',
            'required'=> true
        ));     
        
        $this->addElement(array(
            'type' => 'text',
            'name' => 'end_date',
            'label'=>'Fecha fin',
            'required'=> true
        ));     
               
        $this->addElement(array(
            'type' => 'select',
            'name' => 'status',
            'label'=>'Status',
            'class'=>'select_status_rules',
            'multiOptions' => $this->getListStatus(),
            'required'=> true
        ));
        
        $this->addElement(array(
            'type' => 'button',
            'name' => 'send',
            'value'=> $this->_getTranslation('Terminar'),
            'optionals'=>array(
                'onclick'=>"submit()"),
            'class'=>'btn btn-primary m-t-1 m-b-1'
        ));
        
        $this->addElement(array(
            'type' => 'button',
            'name' => 'cancelar',
            'value'=> $this->_getTranslation('Cancelar'),
            'optionals'=>array(
            'onclick'=>"document.location = '".ROOT_HOST."/Rewards/Controller/Rewards.php'"),
            'class'=>'btn btn-danger m-t-1 m-b-1'
        ));
    }
    
    public function getListStatus(){
        $repository = new RewardsRulesRepository();
        $list = $repository->getListStatus();
         
        $array = array();
        foreach($list as $key => $value){
            $array[$key] = $value;
        }
        return $array;        
    }
    
    public function getListProduct(){
        $repository = new PosProductoRepository();
        $list = $repository->getListSelectProductos();
         
        $array = array();
        foreach($list as $key => $value){
            $array[$key] = $value;
        }
        return $array;        
    }
    
    public function populate($data){
        $tools = new Tools();            
        
        if(isset($data['start_date']) && substr_count($data['start_date'], '-') > 0){           
           $data['start_date'] = $tools->setFormatDateTimeToForm($data['start_date']);
        }
        
        if(isset($data['end_date']) && substr_count($data['end_date'], '-') > 0){           
           $data['end_date'] = $tools->setFormatDateTimeToForm($data['end_date']);
        }
        
        parent::populate($data);
    }
    
    public function isValid() {
        $valid = parent::isValid();        
        $flashmessenger = new FlashMessenger();                
         
        return $valid;
    }
}