<?php
Yii::app()->clientScript->registerScript('submit', '
$(document).ready( function() {
   $("#submit_button").click(function() {
      $(this).prop("disabled",true)
         .parents("form").submit();
      });
})
');
$this->PageTitle = 'Atividade Múltiplos Clientes &raquo; Nova';
if ($model->isNewRecord && empty($_GET['Atendimentos'])) {
    $model->ATD_DTCADASTRO = date('d/m/Y');
    $model->ATD_HORAINICIO = date('H:i');
    $model->ATD_IDFUNCIONARIO = User::getId();
    $model->ATD_SITUACAO = 'R';
} else {
    $model->ATD_HORAINICIO = substr($model->ATD_HORAINICIO, 0, 5);
    $model->ATD_HORAFINAL = substr($model->ATD_HORAFINAL, 0, 5);
    $model->ATD_TOTALHORAS = substr($model->ATD_TOTALHORAS, 0, 5);
}
?>
<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;
    }
</style>
<div class="form">

    <?php $form = $this->beginWidget('GxActiveForm', array('id' => 'atendimentos-form', 'enableAjaxValidation' => false, 'focus' => array($model, 'ATD_IDCLIENTE'))); ?>

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

    <div class="row">
        <?php echo $form->labelEx($model, 'ATD_IDCLIENTE', array('label' => 'Clientes')); ?>
        <?php
        $this->widget('ext.select2.ESelect2', array(
            'model' => $model,
            'attribute' => 'ATD_IDCLIENTE',
            'relatedModel' => 'Clientes',
            'relatedValue' => 'CLI_NOME',
            'relatedValue2' => 'CLI_FANTASIA',
            'ajaxConf' => array(
                'url' => Yii::app()->createUrl('autocomplete/clientes'),
                'params' => array(
                    'field: "CLI_NOME"',
                    'field2: "CLI_FANTASIA"',
                    'id: "CLI_IDCLIENTE"',
                    'not: $(this).val()',
                    )
                ),
            'htmlOptions' => array(
                'multiple' => 'multiple',
                )
            )
        );
        ?>
        <?php echo $form->error($model, 'ATD_IDCLIENTE'); ?>
    </div><!-- row -->

    <div class="row">
        <?php echo $form->labelEx($model, 'ATD_DTCADASTRO'); ?>
        <?php $form->widget('ext.Utils.widgets.CrmDatePicker', array('model' => $model, 'attribute' => 'ATD_DTCADASTRO')); ?>
        <?php echo $form->error($model, 'ATD_DTCADASTRO'); ?>
    </div><!-- row -->
    <div class="row">
        <?php echo $form->labelEx($model, 'ATD_SITUACAO'); ?>
        <?php echo $form->dropDownList($model, 'ATD_SITUACAO', array('A' => 'Agendada', 'C' => 'Cancelada', 'R' => 'Realizada'), array('prompt' => 'Selecione')); ?>
        <?php echo $form->error($model, 'ATD_SITUACAO'); ?>
    </div><!-- row -->
    <div class="row">
        <?php echo $form->labelEx($model, 'ATD_HORAINICIO'); ?>
        <?php echo $form->textField($model, 'ATD_HORAINICIO', array('maxlength' => 5, 'class' => 'format_hora hora_inicio')); ?>
        <?php echo $form->error($model, 'ATD_HORAINICIO'); ?>
    </div><!-- row -->
    <div class="row">
        <?php echo $form->labelEx($model, 'ATD_HORAFINAL',array('required' => true)); ?>
        <?php echo $form->textField($model, 'ATD_HORAFINAL', array('maxlength' => 5, 'class' => 'format_hora hora_final')); ?>
        <?php echo $form->error($model, 'ATD_HORAFINAL'); ?>
    </div><!-- row -->
    <div class="row">
        <?php echo $form->labelEx($model, 'ATD_TOTALHORAS'); ?>
        <?php echo $form->textField($model, 'ATD_TOTALHORAS', array('readonly' => 'enabled', 'class' => 'total_horas')); ?>
        <?php echo $form->error($model, 'ATD_TOTALHORAS'); ?>
    </div><!-- row -->
    <div class="row">
        <?php echo $form->labelEx($model, 'ATD_TIPO'); ?>
        <?php
        $tipos = Util::$tipo_de_contato_suprimido_2;
        echo $form->dropDownList($model, 'ATD_TIPO', $tipos, array(
            'prompt' => 'Selecione',
            'ajax' => array(
                'type' => 'GET',
                'url' => CController::createUrl('autocomplete/motivos'),
                'update' => '#Atendimentos_ATD_IDMOTIVO')
            )
        );
        ?>
        <?php echo $form->error($model, 'ATD_TIPO'); ?>
    </div><!-- row -->
    <div class="row">
        <?php echo $form->labelEx($model, 'ATD_IDMOTIVO'); ?>
        <?php echo $form->dropDownList($model, 'ATD_IDMOTIVO', GxHtml::listDataEx(Motivos::model()->findAll('(MOT_TIPO = "2" or MOT_TIPO = "5") and MOT_TIPOCONTATO like "%' . $model->ATD_TIPO . '%" and MOT_SITUACAO="A" order by MOT_DESCRICAO')), array('prompt' => 'Selecione')); ?>
        <?php echo $form->error($model, 'ATD_IDMOTIVO'); ?>
    </div><!-- row -->
    <div class="row">
        <?php echo $form->labelEx($model, 'ATD_IDMOTIVO_FN', array('required' => true)); ?>
        <?php echo $form->dropDownList($model, 'ATD_IDMOTIVO_FN', CacheUtil::getMotivosFnList(), array('prompt' => 'Selecione')); ?>
        <?php echo $form->error($model, 'ATD_IDMOTIVO_FN'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model, 'ATD_NIVELINTERESSE'); ?>
        <?php
        if ($model->ATD_NIVELINTERESSE == 4) {
            echo $form->dropDownList($model, 'ATD_NIVELINTERESSE', array('4' => 'Cliente Ativo'));
        } else {
            echo $form->dropDownList($model, 'ATD_NIVELINTERESSE', Util::getNiveisInteresseList(array('4')), array('prompt' => 'Selecione'));
        }
        ?>
        <?php echo $form->error($model, 'ATD_NIVELINTERESSE'); ?>
    </div><!-- row -->

    <?php if (in_array(User::getPrivilegio(), array(4, 10, 16, 17, 19, 21))) { ?>
        <div class="row">
            <?php echo $form->hiddenField($model, 'ATD_IDFUNCIONARIO', array('type' => "hidden", 'value' => User::getId())); ?>
        </div><!-- row -->
    <?php } else { ?>
        <div class="row">
            <?php echo $form->labelEx($model, 'ATD_IDFUNCIONARIO'); ?>
            <?php
            $funcionarios = User::getPrivilegio() == 11 ? User::getFuncionariosComercialAdministrativo() : User::getFuncionariosComercial();
            echo $form->dropDownList($model, 'ATD_IDFUNCIONARIO', $funcionarios, array('prompt' => 'Selecione'));
            ?>
            <?php echo $form->error($model, 'ATD_IDFUNCIONARIO'); ?>
        </div><!-- row -->
    <?php } ?>

    <div class="row">
        <?php echo $form->labelEx($model, 'ATD_IDVEICULO'); ?>
        <?php echo $form->dropDownList($model, 'ATD_IDVEICULO', GxHtml::listDataEx(Veiculos::model()->findAll('VEI_SITUACAO = "A" AND VEI_IDREVENDA IN (' . User::getRevendas(true) . ') order by VEI_DESCRICAO')), array('prompt' => 'Selecione')); ?>
        <?php echo $form->error($model, 'ATD_IDVEICULO'); ?>
    </div><!-- row -->

    <div class="row">
        <?php echo $form->labelEx($model, 'ATD_KMINICIAL'); ?>
        <?php echo $form->textField($model, 'ATD_KMINICIAL'); ?>
        <?php echo $form->error($model, 'ATD_KMINICIAL'); ?>
    </div><!-- row -->
    <div class="row">
        <?php echo $form->labelEx($model, 'ATD_KMFINAL'); ?>
        <?php echo $form->textField($model, 'ATD_KMFINAL'); ?>
        <?php echo $form->error($model, 'ATD_KMFINAL'); ?>
    </div><!-- row -->

    <div class="row">
        <label>Brinde</label>
        <?php
        if (!$model->isNewRecord) {
            $BrindesAtendimentos = BrindesAtendimentos::model()->findAll("BRIATD = " . $model->ATD_IDATENDIMENTO);
            foreach ($BrindesAtendimentos as $row) {
                $Brinde = $row->BRICOD;
                ?>
                <div id="clone">
                    Qtd. <input name="Qtd[]" id="Qtd[]" class="qtd" type="text" size="3" maxlength="3" style="width:40px" value="<?php echo $row->BRIQTD; ?>" />
                    <?php echo $form->dropDownList($model, 'Brinde[]', GxHtml::listDataEx(Brindes::model()->findAll('BRI_STATUS="A" order by BRI_DESC')), array('prompt' => 'Selecione', 'style' => 'width:380px', 'options' => array($Brinde => array('selected' => true)))); ?>
                </div>
                <?php
            }
        }
        if ($model->isNewRecord or count($BrindesAtendimentos) == 0) { ?>
            <div id="clone">
                Qtd. <input name="Qtd[]" id="Qtd[]" class="qtd" type="text" size="3" maxlength="3" style="width:40px" value="" />
                <?php echo $form->dropDownList($model, 'Brinde[]', GxHtml::listDataEx(Brindes::model()->findAll('BRI_STATUS="A" order by BRI_DESC')), array('prompt' => 'Selecione', 'style' => 'width:380px')); ?>
            </div>
        <?php } ?>
        <a href="javascript:void(0);" id="link_clone">+ Adicionar brindes</a>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model, 'ATD_CONTATO'); ?>
        <?php echo $form->textField($model, 'ATD_CONTATO'); ?>
        <?php echo $form->error($model, 'ATD_CONTATO'); ?>
    </div><!-- row -->
    <div class="row atd_obs">
        <?php echo $form->labelEx($model, 'ATD_OBS'); ?>
        <?php
        if (empty($model->ATD_IDATENDIMENTO)) {
            echo $form->textArea($model, 'ATD_OBS');
        } else {
            echo '<div class="textarea-like-disabled">'.$model->ATD_OBS.'</div>';
        }
        ?>
        <?php echo $form->error($model, 'ATD_OBS'); ?>
    </div><!-- row -->
    <?php echo $form->hiddenField($model, 'ATD_IDUSUARIO', array('type' => "hidden", 'value' => ($model->isNewRecord ? User::getId() : $model->ATD_IDUSUARIO))); ?>

    <?php
    echo FaHtml::saveButton();
    $this->endWidget();
    ?>
</div><!-- form -->

<?php
Yii::app()->clientScript->registerScript('clone', '
$(document).ready(function(){
  $("#link_clone").click(function(){
    $("#clone").clone().insertBefore("#link_clone");
	$(".qtd").last().val("");
  });
});
'); ?>
<script type="text/javascript">
    $(document).ready(function () {
        function toggleMotivoFN() {
            if($("#Atendimentos_ATD_IDMOTIVO").val() == '83') {
                $('#Atendimentos_ATD_IDMOTIVO_FN').parent().show();
            } else {
                $('#Atendimentos_ATD_IDMOTIVO_FN').parent().hide();
            }
        }

        $("#Atendimentos_ATD_IDMOTIVO").change(function () {
            toggleMotivoFN();
        });
        toggleMotivoFN();
    });
</script>