<?php

class Atendimento_TipocancelamentoController extends Agro_Controller_Action {
    public $_columnList = array (
        'id' => 'Código',
        'ds_tipo_cancelamento' => 'Tipo',
        'ds_chave' => 'Chave'
    );

    public function init() {
        $this->view->title = 'Atendimento » Configurações » Tipo Cancelamento';
    }

    public function getForm() {
        return new Atendimento_Form_TipoCancelamento();
    }

    public function getModel() {
        return new Atendimento_Model_TipoCancelamento();
    }

    public function formAction() {
		try {
			$this->view->form = $this->getForm();
            $this->view->form->id_motivo_cancelamento_apolice->setRequired(false);
			$this->view->title .= ' » Formulário';
			$this->view->ambienteProducao = $this->ambienteProducao;
			$Model = $this->getModel();

			if (!$this->view->back) {
				$this->view->back = "/{$this->_forwardSettings['module']}/{$this->_forwardSettings['controller']}";
			}

			if ($this->getRequest()->isPost()) {

				if ($this->view->form->isValid($this->getRequest()->getPost())) {

					$this->beforeSave();

					if (method_exists($Model, 'save')) {
						try {
							$Row = $Model->save($this->getRequest()->getParams());



							if ($Row) {
								$this->view->showMessage(MSG_SUCCESS, 'success');
							} else {
								if ($Model->getMessages()) {
									$this->view->showMessage($Model->getMessages(), 'error');
								} else {
									$this->view->showMessage(MSG_ERROR, 'error');
								}

								$this->view->form->populate($this->getRequest()->getParams());
							}
						} catch (Exception $e) {
							$this->view->showMessage($e->getMessage(), 'error');
						}
					} else {
						$Row = $this->save($this->getRequest()->getParams());
					}

					if ($Row) {
						$this->afterSave($Row);
					}

					if ($this->_backAfterSave && $Row) {
						$this->view->back = '';
						$this->_forward($this->_forwardSettings['action'], $this->_forwardSettings['controller'], $this->_forwardSettings['module'], array());

					} else if ($Row && is_object($Row)) {
						$this->view->form->populate($Row->toArray());
					}
				} else {
					$Errors = $this->view->form->getMessages();
					$this->view->showMessage($this->getHtmlFormError($Errors), 'error');
					$this->view->form->populate($this->getRequest()->getParams());
				}
			} else if ($this->getRequest()->getParam('id')) {
				$row = $this->getRow();
				$this->view->form->populate($row);
			}
		} catch (Exception $e) {

			switch ($e->getCode()) {
				case 23505:
					$this->view->showMessage(MSG_UNIQUE, 'error');
					break;
				//forbidden
				case 403:
					$this->view->showMessage($e->getMessage(), 'error');
                    $this->view->form->populate($this->getRequest()->getParams());
					break;
			}

			return false;
		}
    }

    public function getFilter() {
        $where = array();

        if ($this->getRequest()->getParam('ds_tipo_cancelamento')) {
            $where[] = array(
                'field' => 'ds_tipo_cancelamento',
                'operator' => 'ilike',
                'value' => '%'.$this->getRequest()->getParam('ds_tipo_cancelamento').'%'
            );
        }

        if ($this->getRequest()->getParam('fl_ativo')) {
            $where[] = array (
                'field' => 'fl_ativo',
                'operator' => ' = ',
                'value' => $this->getRequest()->getParam('fl_ativo')
            );
        }

        return $where;
    }
}
