<div class="wide form">

<?php
    $form = $this->beginWidget('GxActiveForm', array('action' => Yii::app()->createUrl($this->route), 'method' => 'get'));
    $this->widget('application.extensions.moneymask.MMask',
                  array('element' => '#Contratos_COT_VLRMENSAL, #Contratos_COT_VLRMENSAL_FINAL'));
?>

    <div class="row">
        <?= $form->label($model, 'COT_IDCONTRATO'); ?>
        <?= $form->textField($model, 'COT_IDCONTRATO'); ?>
    </div>
    <div class="row">
        <?= $form->label($model, 'COT_DTCADASTRO'); ?>
        <?php $form->widget('ext.Utils.widgets.CrmDatePicker', array('model' => $model, 'attribute' => 'COT_DTCADASTRO')); ?> à
        <?php $form->widget('ext.Utils.widgets.CrmDatePicker', array('model' => $model, 'attribute' => 'COT_DTCADASTRO_FINAL')); ?>
    </div>
    <div class="row">
        <?= $form->label($model, 'COT_DTATIVACAO'); ?>
        <?php $form->widget('ext.Utils.widgets.CrmDatePicker', array('model' => $model, 'attribute' => 'COT_DTATIVACAO')); ?> à
        <?php $form->widget('ext.Utils.widgets.CrmDatePicker', array('model' => $model, 'attribute' => 'COT_DTATIVACAO_FINAL')); ?>
    </div>

    <div class="row">
        <?= $form->label($model, 'COT_CLI_NOME'); ?>
        <?php
            $this->widget('ext.Utils.widgets.CrmAutoComplete', array(
                'model' => $model,
                'attribute' => 'COT_CLI_NOME',
                'url' => 'autocomplete/documentos',
                'params' => array(
                    'revenda: $("#Contratos_COT_IDREVENDA").val()',
                    'vendedor: $("#Contratos_COT_IDVENDEDOR").val()',
                    'documento: $("#Contratos_COT_SITUACAO").val()',
                    'status: ""+$("#Contratos_COT_STATUS").val()',
                    'field: "COT_CLI_NOME"'
                )
            ));
        ?>
    </div>
    <div class="row">
        <?php echo $form->label($model, 'COT_STATUS_REVENDAS'); ?>
        <?php
            $model->COT_STATUS_REVENDAS = 'A';
            $opcoes = array('T' => 'Todas', 'A' => 'Ativas', 'I' => 'Inativas');
            echo $form->dropDownList($model, 'COT_STATUS_REVENDAS', $opcoes);
        ?>
    </div>

    <?php if (!User::isRevenda()) { ?>
        <div class="row">
            <?= $form->label($model, 'COT_IDREVENDA'); ?>
            <?php
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'COT_IDREVENDA',
                'options' => array('prompt' => 'Todos')
            ));
            ?>
            <?= $form->error($model, 'COT_IDREVENDA'); ?>
        </div><!-- row -->
    <?php } ?>

    <?php if (User::canListUsers()) { ?>
        <div class="row">
            <?= $form->label($model, 'COT_IDVENDEDOR'); ?>
            <?= $form->dropDownList($model, 'COT_IDVENDEDOR', User::getFuncionariosComercial(), array('prompt' => 'Todos')); ?>
            <?= $form->error($model, 'COT_IDVENDEDOR'); ?>
        </div><!-- row -->
    <?php } else { ?>
        <?= $form->hiddenField($model, 'COT_IDVENDEDOR', array('type' => "hidden", 'value' => User::getId())); ?>
    <?php } ?>

    <div class="row">
        <?= $form->label($model, 'COT_SITUACAO'); ?>
        <?php
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'COT_SITUACAO',
                'data' => array(Contratos::CONTRATO => 'Contrato',
                                Contratos::ADENDO => 'Adendo',
                                Contratos::PEDIDO_IMPLANTACAO => 'Pedido implantação',
                                Contratos::PEDIDO_REATIVACAO => 'Pedido reativação',
                                Contratos::ORCAMENTO => 'Orçamento'),
                'options' => array('prompt' => 'Todos')
            ));
        ?>
    </div>

    <div class="row COT_IDSERVICO" style="display:none">
        <?= $form->label($model, 'COT_IDSERVICO',
        array('label' => 'Serviço dos adendos', 'class' => 'line_small')); ?>
        <?php
            $this->widget('application.extensions.multiselect.JMultiSelect',
                array(
                'model' => $model,
                'attribute' => 'COT_IDSERVICO',
                'data' => CacheUtil::getServicosAdendosList(),
                'options' => array('prompt' => 'Todos')
            ));
        ?>
    </div>

    <div class="row">
        <?= $form->label($model, 'COT_CATDOCUMENTO'); ?>
        <?php
            $data = array('P' => 'Documentos pendentes',
                'A' => 'Documentos ativos',
                'R' => 'Documentos reprovados',
                'C' => 'Documentos cancelados');
            natcasesort($data);
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'COT_CATDOCUMENTO',
                'data' => $data,
            ));
        ?>
    </div>

    <div class="row">
        <?= $form->label($model, 'COT_IDPRODUTO'); ?>
        <?php
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'COT_IDPRODUTO',
                'data' => CacheUtil::getProdutosList(),
                'options' => array('prompt' => 'Todos')
            ));
        ?>
    </div>

    <div class="row">
        <?= $form->label($model, 'COT_MODULOS', array('class' => 'line_small')); ?>
        <?php
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'COT_MODULOS',
                'data' => CacheUtil::getModulosComplementaresList(),
                'options' => array('prompt' => 'Todos')
            ));
        ?>
    </div>

    <div class="row">
        <?= $form->label($model, 'COT_VLRMENSAL', array('label' => 'Valor negociado')); ?>
        <?= $form->textField($model, 'COT_VLRMENSAL', array('maxlength' => 14, 'style' => 'width:100px')); ?> à
        <?= $form->textField($model, 'COT_VLRMENSAL_FINAL', array('maxlength' => 14, 'style' => 'width:100px')); ?>
    </div>

    <div class="row">
        <?= $form->label($model, 'COT_CLI_CNPJCPF'); ?>
        <?php
            $this->widget('ext.Utils.widgets.CrmAutoComplete', array(
                'model' => $model,
                'attribute' => 'COT_CLI_CNPJCPF',
                'url' => 'autocomplete/documentos',
                'params' => array(
                    'revenda: $("#Contratos_COT_IDREVENDA").val()',
                    'vendedor: $("#Contratos_COT_IDVENDEDOR").val()',
                    'documento: $("#Contratos_COT_SITUACAO").val()',
                    'status: $("#Contratos_COT_STATUS").val()',
                    'field: "COT_CLI_CNPJCPF"'
                    ),
                'htmlOptions' => array(
                    'maxlength' => '19',
                )
            ));
        ?>
    </div>
    <div class="row">
        <?= $form->label($model, 'COT_STATUS'); ?>
        <?php
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'COT_STATUS',
                'data' => CacheUtil::getStatusDocumentosList(),
            ));
        ?>
    </div>

    <div class="row">
        <?= $form->label($model, 'CLI_IDMOTIVO'); ?>
        <?php
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'CLI_IDMOTIVO',
                'data' => CacheUtil::getComoNosConheceuList(),
            ));
        ?>
    </div>

    <div class="row">
        <?= $form->label($model, 'COT_IDMOTIVO_FP', array('class' => 'line_small')); ?>
        <?php
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'COT_IDMOTIVO_FP',
                'data' => CacheUtil::getComoNosConheceuList(),
            ));
        ?>
    </div>

    <?php if(User::isAdmin() || User::getPrivilegio() == 7) { ?>
    <div class="row">
        <?= $form->label($model, 'COT_GERADO_AUTOMATICAMENTE'); ?>
        <?= $form->dropDownList($model, 'COT_GERADO_AUTOMATICAMENTE', [1 => 'Sim'], array('prompt' => 'Não')); ?>
    </div>
    <?php } ?>

    <div class="row buttons">
        <input type="hidden" name="busca" value="1" />
        <?= FaHtml::searchButton(); ?>
    </div>

    <?php $this->endWidget(); ?>

</div><!-- search-form -->
<script type="text/javascript">
    $(document).ready(function () {
        $('#Contratos_COT_STATUS_REVENDAS').change(function(){
            var status = $(this).val();
            jQuery.ajax({
                type: 'GET',
                url: createUrl('autocomplete/revendasStatus'),
                cache: false,
                data: {status: status},
                success: function(html){
                    $("#Contratos_COT_IDREVENDA").empty();
                    $("#Contratos_COT_IDREVENDA").append(html);
                    $("#Contratos_COT_IDREVENDA").multiselect('refresh');
                }
            });
            return false;
        }).change();
        jQuery('body').undelegate('#Contratos_COT_IDREVENDA','change').delegate('#Contratos_COT_IDREVENDA','change',function(){
            var revenda = $(this).val();
            jQuery.ajax({
                type: 'GET',
                url: createUrl('autocomplete/vendedor'),
                cache: false,
                data: {revenda: revenda, selecione: true},
                success: function(html){
                    jQuery("#Contratos_COT_IDVENDEDOR").html(html);
                }
            });
            return false;
        });
    });
    jQuery('body').undelegate('#Contratos_COT_SITUACAO','change').delegate('#Contratos_COT_SITUACAO','change',function(){
        if($('#Contratos_COT_SITUACAO').find('option[value=<?= Contratos::ADENDO; ?>]').prop('selected')) {
            $('.row.COT_IDSERVICO').show();
        } else {
            $('.row.COT_IDSERVICO').hide();
        }
        return false;
    });
</script>