<?php
Yii::app()->clientScript->registerScript('submit', '
$(document).ready( function() {
    $("#submit_button").click(function() {
        $(this).prop("disabled",true).parents("form").submit();
    });
})
');
$countRevendas = count(User::getRevendas());
if ($model->isNewRecord && empty($_POST['CampanhasMarketing_Cidade'])) {
    $model->MKT_DATA_ENVIO = date('d/m/Y');
    $model->MKT_RESPONSAVEL = User::getId();
    if($countRevendas == 1) {
        $model->Revenda = array_keys(User::getRevendas());
    }
}
$this->widget('application.extensions.EChosen.EChosen', array('target' => '.echosen','useJQuery' => true));
?>
<style type="text/css">
    .select2-selection.select2-selection--multiple .select2-search__field {
        border: none!important;
        box-shadow: none!important;
    }
    .select2.select2-container,
    .select2.select2-container .select2-selection.select2-selection--multiple {
        min-width: 464px!important;
            width: auto!important;
        max-width: auto!important;
    }
    textarea {
        min-height: 215px!important;
    }
    .ui-multiselect.ui-widget span.ui-icon+span {
        font-size: 14px!important;
        font-family: "Helvetica Neue",Helvetica,Arial,sans-serif!important;
        color: #555555!important;
    }
    .chosen-container-multi {
        margin-top: 0!important;
        margin-bottom: 0!important;
    }
    .chosen-container-multi .chosen-choices li.search-field input[type="text"] {
        height: 20px!important;
    }
</style>
<div class="form">

    <?php $form = $this->beginWidget('GxActiveForm', array('id' => 'atendimentos-form', 'enableAjaxValidation' => false, 'htmlOptions' => array('class'=>'col-xs-12 form-cadastro', 'enctype' => 'multipart/form-data'), 'focus' => array($model, 'MKT_TITULO'))); ?>

    <?php echo FaHtml::getFormInfo($form, $model); ?>

    <div class="col-xs-12 col-sm-4">

        <div class="col-xs-12 form-group">
            <?php echo $form->labelEx($model, 'MKT_TITULO'); ?>
            <?php echo $form->textField($model, 'MKT_TITULO'); ?>
            <?php echo $form->error($model, 'MKT_TITULO'); ?>
        </div>

        <?php if (User::canListUsers()) { ?>
            <div class="col-xs-12 form-group">
                <?php echo $form->labelEx($model, 'MKT_RESPONSAVEL', array('label' => 'Responsável pelo envio')); ?>
                <?php echo $form->dropDownList($model, 'MKT_RESPONSAVEL', User::getFuncionariosComercialAdministrativo(), array('prompt' => 'Selecione')); ?>
                <?php echo $form->error($model, 'MKT_RESPONSAVEL'); ?>
            </div>
        <?php } ?>

        <div class="col-xs-6 col-sm-6 col-md-4 col-lg-3 form-group">
            <?php echo $form->labelEx($model, 'MKT_DATA_ENVIO'); ?>
            <?php $form->widget('ext.Utils.widgets.CrmDatePicker', array('model' => $model, 'attribute' => 'MKT_DATA_ENVIO')); ?>
            <?php echo $form->error($model, 'MKT_DATA_ENVIO'); ?>
        </div>

        <div class="col-xs-6 col-sm-12 col-md-8 col-lg-9 form-group row">
            <?php if (!empty($model->MKT_TMPBANNER)) { ?>
                <div class="img-tmp">
                    <?php echo $model->getTmpLogo(false, 90);?>
                    <a href="#img-tmp" onclick="return removerLogoTmp()" class="excluir_arquivo">
                        <i class="fa fa-times"></i>
                    </a>
                        <div class="color-red">Logo não definitivo, caso você queira alterar o mesmo selecione outro arquivo.
                            <?php if (!$model->isNewRecord) {?>
                                <br />Caso você queira excluir este logo e manter o anterior, use a opção excluir da imagem.
                            <?php }?>
                        </div>
                </div>
                <?php if (!$model->isNewRecord) {?>
                    <div class="oculto img-antiga">
                        <?php echo $model->getLogo(false, 90);?>
                    </div>
                <?php }?>
            <?php } else if (!$model->isNewRecord && !empty($model->MKT_BANNER)) { ?>
                <?php echo $model->getLogo(false, 90);?>
            <?php }?>

            <?php echo $form->labelEx($model, 'MKT_BANNER');?>
            <?php echo $form->fileField($model, 'MKT_BANNER', array('accept' => 'jpg,jpeg,png,gif'));?>
            <?php echo $form->hiddenField($model, 'MKT_TMPBANNER');?>
            <?php echo $form->error($model, 'MKT_BANNER');?>

        </div>
    </div>

    <div class="col-xs-12 col-sm-8">
        <div class="col-xs-12 form-group atd_obs">
            <?php echo $form->labelEx($model, 'MKT_TEXTO'); ?>
            <?php echo $form->textArea($model, 'MKT_TEXTO'); ?>
            <?php echo $form->error($model, 'MKT_TEXTO'); ?>
        </div>
    </div>
    <div class="clearfix"></div>
    <hr />
    <h1>Filtros de prospectos/clientes</h1>


    <div class="row">
        <?php if($countRevendas > 1) { ?>
            <div class="col-sm-6 col-md-4 col-lg-4">
                <?php echo $form->labelEx($model, 'Revenda'); ?>
                <?php
                $this->widget('application.extensions.multiselect.JMultiSelect', array(
                    'model' => $model,
                    'attribute' => 'Revenda',
                    'data' => User::getRevendas(),
                    'options' => ['noneSelectedText' => 'Selecione']
                ));
                ?>
                <?php echo $form->error($model, 'Revenda'); ?>
            </div>
        <?php } ?>

        <div class="col-sm-6 col-md-4 col-lg-4 row">
            <?php echo $form->labelEx($model, 'Regiao'); ?>
            <?php
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'Regiao',
                'data' => User::getRegioes(),
                'options' => ['noneSelectedText' => 'Selecione']
            ));
            ?>
            <?php echo $form->error($model, 'Regiao'); ?>
        </div>

        <div class="col-sm-6 col-md-4 col-lg-4 row">
            <?php echo $form->labelEx($model, 'Estado'); ?>
            <?php echo $form->dropDownList($model, 'Estado', User::getEstados(), array('class' => 'echosen', 'multiple' => 'multiple')); ?>
            <?php echo $form->error($model, 'Estado'); ?>
        </div>

        <div class="col-sm-6 col-md-4 col-lg-4 row">
            <?php echo $form->labelEx($model, 'Cidade'); ?>
            <?php echo $form->dropDownList($model, 'Cidade', User::getCidadesEstados(), array('class' => 'echosen', 'multiple' => 'multiple')); ?>
            <?php echo $form->error($model, 'Cidade'); ?>
        </div>

        <div class="col-sm-6 col-md-4 col-lg-4 row">
            <?php echo $form->labelEx($model, 'Produto'); ?>
            <?php
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'Produto',
                'data' => CacheUtil::getProdutosList(),
                'options' => ['noneSelectedText' => 'Selecione']
            ));
            ?>
            <?php echo $form->error($model, 'Produto'); ?>
        </div>

        <div class="col-sm-6 col-md-4 col-lg-4 row">
            <?php echo $form->labelEx($model, 'ModuloComplementar'); ?>
            <?php
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'ModuloComplementar',
                'data' => CacheUtil::getModulosComplementaresList(),
                'options' => ['noneSelectedText' => 'Selecione']
            ));
            ?>
            <?php echo $form->error($model, 'ModuloComplementar'); ?>
        </div>

        <div class="col-sm-6 col-md-4 col-lg-4 row">
            <?php echo $form->labelEx($model, 'DocumentoFiscal'); ?>
            <?php
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'DocumentoFiscal',
                'data' => CacheUtil::getDocumentosFiscaisList(),
                'options' => ['noneSelectedText' => 'Selecione']
            ));
            ?>
            <?php echo $form->error($model, 'DocumentoFiscal'); ?>
        </div>

        <div class="col-sm-6 col-md-4 col-lg-4 row">
            <?php echo $form->labelEx($model, 'RamoAtividade'); ?>
            <?php echo $form->dropDownList($model, 'RamoAtividade', CacheUtil::getRamosAtividadeList(), array('multiple' => 'multiple', 'class' => 'echosen')); ?>
            <?php echo $form->error($model, 'RamoAtividade'); ?>
        </div>

        <div class="col-sm-6 col-md-4 col-lg-4 row">
            <?php echo $form->labelEx($model, 'Status'); ?>
            <?php
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'Status',
                'data' => Util::getStatusClienteList(),
                'options' => ['noneSelectedText' => 'Selecione']
            ));
            ?>
            <?php echo $form->error($model, 'Status'); ?>
        </div>

        <div class="col-sm-6 col-md-4 col-lg-4 row">
            <?php echo $form->labelEx($model, 'TipoPessoa'); ?>
            <?php
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'TipoPessoa',
                'data' => Util::getTiposPessoaList(),
                'options' => ['noneSelectedText' => 'Selecione']
            ));
            ?>
            <?php echo $form->error($model, 'TipoPessoa'); ?>
        </div>

        <div class="col-sm-6 col-md-4 col-lg-4 row">
            <?php echo $form->labelEx($model, 'NivelInteresse'); ?>
            <?php echo $form->dropDownList($model, 'NivelInteresse', Util::getNiveisInteresselist(), array('multiple' => 'multiple', 'class' => 'echosen')); ?>
            <?php echo $form->error($model, 'NivelInteresse'); ?>
        </div>

        <div class="col-sm-6 col-md-4 col-lg-4 row">
            <?php echo $form->labelEx($model, 'ComoConheceu'); ?>
            <?php
            $this->widget('application.extensions.multiselect.JMultiSelect', array(
                'model' => $model,
                'attribute' => 'ComoConheceu',
                'data' => CacheUtil::getComoNosConheceuList(),
                'options' => ['noneSelectedText' => 'Selecione']
            ));
            ?>
            <?php echo $form->error($model, 'ComoConheceu'); ?>
        </div>
    </div>
    <div class="col-sxs-12 row">
    <?php
    echo FaHtml::saveButton();
    $this->endWidget();
    ?>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        jQuery('body').undelegate('#CampanhasMarketing_Revenda','change').delegate('#CampanhasMarketing_Revenda','change',function(){
            var revenda = $("#CampanhasMarketing_Revenda").val();
            var regiao = $("#CampanhasMarketing_Regiao").val();
            jQuery.ajax({
                type: 'GET',
                url: createUrl('autocomplete/regioes'),
                cache: false,
                data: {revenda: revenda, selecione:false},
                success: function(html){
                    jQuery("#CampanhasMarketing_Regiao").html(html);
                    selectField('CampanhasMarketing_Regiao', regiao);
                    $('#CampanhasMarketing_Regiao').trigger('change');
                }
            });

            return false;
        });
        function reloadCidades() {
            var revenda = $("#CampanhasMarketing_Revenda").val();
            var regiao = $("#CampanhasMarketing_Regiao").val();
            var cidade = $("#CampanhasMarketing_Cidade").val();
            var estado = $("#CampanhasMarketing_Estado").val();

            jQuery.ajax({
                type: 'GET',
                url: createUrl('autocomplete/cidades'),
                cache: false,
                data: {revenda: revenda, regiao: regiao, estado: estado, selecione:false},
                success: function(html){
                    jQuery("#CampanhasMarketing_Cidade").html(html);
                    selectField('CampanhasMarketing_Cidade', cidade);
                    $(".echosen").trigger("chosen:updated");
                }
            });
        }
        jQuery('body').undelegate('#CampanhasMarketing_Estado','change').delegate('#CampanhasMarketing_Estado','change',function(){
            reloadCidades();
            return false;
        });
           jQuery('body').undelegate('#CampanhasMarketing_Regiao','change').delegate('#CampanhasMarketing_Regiao','change',function(){
            reloadCidades();
            return false;
        });
    });
</script>