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

class UserForm extends Form {

    public function __construct() {
        $this->setActionForm('Usuario.php');
        $this->setName('user');
        $this->setClass('usuario');
        $this->setMethod('post');
        $this->setDefaultFormElementsColSize('8');
        $this->setDefaultFormLabelsColSize('4');
        parent::__construct();
        $this->init();
    }

    public function init() {
        $this->addElement(array(
            'type' => 'text',
            'name' => 'usuario',
            'label'=>'Usuario',
            'required'=> true
        ));

        $this->addElement(array(
            'type' => 'password',
            'name' => 'contrasena',
            'label'=>'Contrasena',
            'required'=> true
        ));
        
        $this->addElement(array(
            'type' => 'password',
            'name' => 'confirmar_contrasena',
            'label'=>'Confirmar contrasena',
            'required'=> true
        ));
        
        $this->addElement(array(
            'type' => 'password',
            'name' => 'nip',
            'label'=>'NIP',
            'required'=> false,
        ));
        
         $this->addElement(array(
            'type' => 'password',
            'name' => 'confirm_nip',
            'label'=>'Confirmar NIP',
            'required'=> false,
        ));
        
        $this->addElement(array(
            'type' => 'text',
            'name' => 'nombre',
            'label'=>'Nombre',
            'required'=> true
        ));
        
        $this->addElement(array(
            'type' => 'text',
            'name' => 'apellido',
            'label'=>'Apellido',
            'required'=> true
        ));
        
        $this->addElement(array(
            'type' => 'text',
            'name' => 'telefono',
            'label'=>'Telefono',
            'required'=> false
        ));
        
        $this->addElement(array(
            'type' => 'text',
            'name' => 'email',
            'label'=>'Email',
            'required'=> false
        ));
        
        $this->addElement(array(
            'type' => 'select',
            'name' => 'role',
            'label'=>'Role',
            'multiOptions' => $this->getListRoles(),
            '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'
        ));
        
        $this->addElement(array(
            'type' => 'button',
            'name' => 'cancelar',
            'value'=> $this->_getTranslation('Cancelar'),
            'optionals'=>array(
                'onclick'=>"document.location = '".ROOT_HOST."/Controller/Usuario.php'"),
            'class'=>'btn btn-danger m-t-1'
        ));
    }
    
    public function getListRoles(){
        $repository = new UserRepository();
        $list = $repository->getListRoles();
         
        $array = array('' => 'Seleccionar una opcion...');
        foreach($list as $key => $value){
            $array[$key] = $value;
        }
        return $array;
    }
    
    public function getListStatus(){
        $repository = new UserRepository();
        $list = $repository->getListStatus();
         
        $array = array(''=>'Seleccionar una opcion...');
        foreach($list as $key => $value){
            $array[$key] = $value;
        }
        return $array;            
    }
    
    public function getListLocations(){
        $repository = new LocationRepository();
        $result = $repository->getListSelectLocations();
        $array = array('0'=>'Selecciona una opcion...');
        
        if ($result) {
            foreach ($result as $key => $value) {
                $array[$key] = $value;
            }           
        }
       
        return $array;
    }
    
    public function populate($data){
        
        if($data['role'] === '2' || $data['role'] === '3'){
            $this->setAsRequired(array('id_customer'));
        }else{
            $this->noRequired(array('id_customer'));
        }
        
        if($data['role'] === '5'){
            $this->setAsRequired(array('default_location_for_outsourcing'));
        }else{
            $this->noRequired(array('default_location_for_outsourcing'));
        }
        
        parent::populate($data);
    }
    
   public function setEditForm(){
        $this->noRequired(array('contrasena','confirmar_contrasena','nip','confirmar_nip'));
    }
    
    public function isValid() {
        $rs = parent::isValid();
        
        if($rs){
            $flasmessenger = new FlashMessenger();
            $userRepo = new UserRepository();
            $existUser = $userRepo->existUserName($this->getValueElement('user'),$this->getId());            
            
            if($existUser){
                $flasmessenger->addMessage(array('danger'=>"Nombre de usuario ingresado ya existe. Intente nuevamente."));
                return null;
            }
            
            $contrasena = $this->getValueElement('contrasena');
            $confirmar_contrasena = $this->getValueElement('confirmar_contrasena');
            
            if(strtolower($contrasena) !== strtolower($confirmar_contrasena)){
                $flasmessenger = new FlashMessenger();
                $flasmessenger->addMessage(array('danger'=>'Contraseñas ingreadas no coinciden.'));
                return null;
            }
            
            $nip = $this->getValueElement('nip');
            $confirmar_nip = $this->getValueElement('confirm_nip');
            
            if(strtolower($nip) !== strtolower($confirmar_nip)){
                $flasmessenger = new FlashMessenger();
                $flasmessenger->addMessage(array('danger'=>'NIP ingreados no coinciden.'));
                return null;
            }          
            
            $existNIP = $userRepo->existNIP($this->getValueElement('nip'),$this->getId());
            if($existNIP){
                $flasmessenger->addMessage(array('danger'=>"NIP ingresado ya existe. Intente nuevamente."));
                return null;
            }
        }
        
        return $rs;
    }
}