<?php

class Endosso_TipossolicitacaoController extends Agro_Controller_Action {
    public $_columnList = array(
        'ds_tipo_endosso'  => array('label' => 'Tipo de Endosso', 'align' => 'left', 'width' => '30%'),
        'ds_orientacao' => array('label' => 'Orientação', 'align' => 'left', 'width' => '30%', 'nowrap' => 'wrap'),
        'ds_chave' => array('label' => 'Chave', 'align' => 'center', 'width' => '30%')
    );

    public function init() {
        $this->view->title = 'Endosso » Tipo de Solicitação do Endosso';
    }

    public function getForm() {
        $Form = new Endosso_Form_TiposSolicitacao();
        $params = $this->getRequest()->getParams();
        $Form->populate($params);
        return $Form;
    }

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

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

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

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

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

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

        return $where;
    }

    public function listAction() {
        $this->getHelper('layout')->disableLayout();
        $Model = $this->getModel();
        $where = $this->getFilter();
        $Model->_order = 'id';
        $recordsPerPage = ($this->recordsPerPage ? $this->recordsPerPage : RECORDS_PER_PAGE);
        $Rs = $Model->fetchAllPaginator($where, $this->getRequest()->getParam('page'), $recordsPerPage, $this->_paginator);
        $this->view->paginator = $Rs;
        //parent::listAction();
    }

    public function selecionartipoAction()
    {
        $this->getHelper('layout')->disableLayout();
        $model = $this->getModel();
        $where = [
            "fl_ativo is true",
            "ds_chave NOT IN('" . Endosso_Model_TiposSolicitacao::DS_CHAVE_COM_MOVIMENTO_PREMIO . "', '" . Endosso_Model_TiposSolicitacao::DS_CHAVE_SEM_MOVIMENTO_PREMIO . "')"
        ];
        $this->view->tipos = $model->fetchAll(implode(' AND ', $where), $model->_order)->toArray();
        $this->view->exibirCancelamentoApolice = true;

        if (
            in_array(
                $this->getRequest()->getParam('id_status'),
                [
                    Seguro_Model_Propostas::ID_STATUS_CONF_PENDENCIAS,
                    Seguro_Model_Propostas::ID_STATUS_AGUARDANDO_ENVIO_SEG,
                    Seguro_Model_Propostas::ID_STATUS_ENVIADA_AGUARDANDO_CONF
                ]
            )
        ) {
            $this->view->exibirCancelamentoApolice = false;
        }
    }
}
