<?php

class Seguro_Form_AutorizadoCorretor extends Agro_Form {
	
	public function init() {		
		$this->addElement('hidden', 'id_proposta', array('decorators' => $this->buttonDecorator));
				
		$Corretor = new Sistema_Model_Corretores();
		$this->addElement('select', 'id_usuario_corretor', array(
			'decorators' => $this->elementDecorator,
			'MultiOptions' => $Corretor->getComboUsuario(false),
			'label' => 'Corretor'
		));
		
		$this->addElement('select', 'id_usuario_preposto', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Sublogin'
		));
	}
	
	public function populate(array $values) {
		$User = Zend_Auth::getInstance()->getIdentity();

		if ($values["id_usuario_corretor"] > 0){
			$Prepostos = new Sistema_Model_Prepostos();
			$Corretores = new Sistema_Model_Corretores();
			$Corretor = $Corretores->fetchRow("id_usuario={$values["id_usuario_corretor"]}");
			
			$opcoesPrepostos = $Prepostos->getComboUsuario($Corretor->id_usuario);
			
			if ($User->tp_usuario == 'C') {
				$opcoesCorretores = $Corretores->fetchPairs("id_usuario", "ds_nome_corretor", false, false, 1, "id_usuario={$values["id_usuario_corretor"]}", false);

				$opcoesPrepostos = $opcoesPrepostos + $opcoesCorretores;
			} else {
				$opcoesCorretores = $Corretores->fetchPairs("id_usuario", "ds_nome_corretor", false, false, 1, "id_usuario={$values["id_usuario_corretor"]}");
			}

			$this->id_usuario_preposto->setMultiOptions($opcoesPrepostos);

			$this->id_usuario_corretor->setMultiOptions($opcoesCorretores);

			$this->id_usuario_corretor->setAttrib('readonly', 'readonly');
		}
		
		parent::populate($values);
	}
}