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

class RewardsForm extends Form {

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

    public function init() {    
        $append = "<span class = 'btn input-group-addon'>Dollar</span>";
        $this->addElement(array(
            'type' => 'text',
            //'label'=>'Monto minimo para asignar puntos',
            'label'=>'Por cada',
            'name' => 'min_amount_to_assign_points',
            'append'=>$append,
            'required'=> true
        ));
        
        $append = "<span class = 'btn input-group-addon'>Puntos</span>";
        $this->addElement(array(
            'type' => 'text',
            //'label'=>'Puntos asignados por monto minimo',
            'label'=>'Asignar',
            'name' => 'points_assigned_for_min_amount',
            'append'=>$append,
            'required'=> true
        ));        
        
        $append = "<span class = 'btn input-group-addon'>Dollar</span>";
        $this->addElement(array(
            'type' => 'text',
            'name' => 'value_for_each_point',
            'label'=>'Valor por cada punto',
             'append'=>$append,
            'required'=> true
        ));        
              
        $this->addElement(array(
            'type' => 'select',
            'name' => 'calculate_points_with_price_without_taxes',
            'label'=>'Calcular puntos con precio',
            'multiOptions'=>array('1'=>'Excluyendo impuestos','2'=>'Incluyendo impuestos'),
            'required'=> true
        ));
        
        $this->addElement(array(
            'type' => 'text',
            'name' => 'min_point_to_reedem',
            'label'=>'Puntos necesarios para usar canje',
            'required'=> true
        ));         
        
        $append = "<span class = 'btn input-group-addon'>Dias</span>";
        $this->addElement(array(
            'type' => 'text',
            'name' => 'max_life_days_of_points',
            'label'=>'Vida máxima de puntos.',
            'append'=>$append,
            'required'=> true
        ));         
               
        $this->addElement(array(
            'type' => 'select',
            'name' => 'status',
            'label'=>'Status',
            '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 RewardsRepository();
        $list = $repository->getListStatus();
         
        $array = array();
        foreach($list as $key => $value){
            $array[$key] = $value;
        }
        return $array;        
    }
    
    public function getListCustomers(){
        $repository = new ClienteRepository();
        $result = $repository->getListaSelectClientes();

         $array = array('' => 'Seleccionar una opcion...');
        if ($result) {           
            foreach ($result as $key => $value) {
                $array[$key] = $value;
            }
            return $array;
        }
    }
    
     public function getListTypeEvents(){
        $repository = new RewardsRepository();
        $result = $repository->getTypeEvents();

         $array = array('' => 'Seleccionar una opcion...');
        if ($result) {           
            foreach ($result 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;
    }
}