<?php
Yii::app()->clientScript->registerScript('submit', '
    $(document).ready( function() {
        $("#submit_button").click(function() {
            $(this).prop("disabled",true)
            .parents("form").submit();
        });
    })
');

$Contrato = Contratos::model()->findByPk($ATD_IDCONTRATO);
if (!$Contrato) {
    User::blockAction('Contrato inválido!');
}

$model->ATD_DTCADASTRO = date('d/m/Y');
$model->ATD_HORAINICIO = date('H:i');

?>
<div class="form">
    <?php
    $form = $this->beginWidget('GxActiveForm', array(
        'id' => 'atendimentos-form',
        'enableAjaxValidation' => false,
        'focus' => array($model, 'ATD_IDFUNCIONARIO'),
        'htmlOptions' => array('class'=>'col-xs-12 form-cadastro')
        )
    );
    $this->widget('application.extensions.emultiselect.EMultiSelect');
    ?>
    <?php
    echo FaHtml::getFormInfo($form, $model);
    echo $form->hiddenField($model, 'ATD_IDFUNCIONARIO', array('type' => "hidden", 'value' => User::getId()));
    echo $form->hiddenField($model, 'ATD_IDCLIENTE', array('type' => "hidden", 'value' => $ATD_IDCLIENTE));
    echo $form->hiddenField($model, 'ATD_IDCONTRATO', array('type' => "hidden", 'value' => $ATD_IDCONTRATO));
    echo $form->hiddenField($model, 'ATD_SITUACAO', array('type' => "hidden", 'value' => 'R'));
    echo $form->hiddenField($model, 'ATD_DTCADASTRO', array('type' => "hidden", 'value' => date('d/m/Y')));
    echo $form->hiddenField($model, 'ATD_HORAINICIO', array('type' => "hidden", 'value' => date('H:i:s')));
    echo $form->hiddenField($model, 'ATD_HORAFINAL', array('type' => "hidden", 'value' => date('H:i:s', strtotime('+1 minute'))));
    echo $form->hiddenField($model, 'ATD_TOTALHORAS', array('type' => "hidden", 'value' => '00:01:00'));
    echo $form->hiddenField($model, 'ATD_TIPO', array('type' => "hidden", 'value' => 'D'));
    ?>
    <?php if($Contrato->COT_STATUS == 3 or $Contrato->COT_STATUS == 6) { ?>
        <div class="form-group alert-form-group col-xs-12 col-sm-12 col-md-9">
            <?php echo !empty($Contrato->getExcecoes()) ? FaHtml::alert(implode('<br />', $Contrato->getExcecoes()), 'danger', false, false) : FaHtml::alert('Nenhuma exceção!', 'success', false, false); ?>
        </div>
    <?php } ?>
    <br />
    <fieldset class="no-border border-box">
        <div class="form-group col-xs-12 col-sm-12 col-md-12">
            <?php echo $form->labelEx($model, 'Local');?>

            <?php
            $Locais = LocaisDocumentos::getLocais($Contrato->COT_IDCONTRATO);
            echo $form->dropDownList($model, 'ATD_IDMOTIVO', $Locais, array('prompt' => 'Selecione'));
            ?>

            <?php echo $form->error($model, 'ATD_IDMOTIVO');?>
        </div><!-- row -->
        <div class="form-group col-xs-12 col-sm-12 col-md-12">
            <?php echo $form->labelEx($model, 'ATD_OBS');?>
            <?php echo $form->textArea($model, 'ATD_OBS');?>
            <?php echo $form->error($model, 'ATD_OBS');?>
        </div><!-- row -->
    </fieldset>
    <fieldset class="no-border border-box">
    <?php if (User::getUser()->FUN_PRM_EXCECOES == true) { ?>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <?php echo $form->labelEx($model, 'ATD_IDOBS');?>
            <?php
            echo CHtml::dropDownList('ATD_IDOBS[]', array(), GxHtml::listDataEx(Observacoes::model()->findAll('OBS_STATUS="A" order by OBS_ASSUNTO'), 'OBS_IDOBSERVACAO', 'OBS_ASSUNTO'), array(
                'multiple' => 'multiple',
                'key'      => 'ATD_IDOBS',
                'size'      => '14',
                'class'    => 'multiselect',
                'ajax'     => array(
                    'type'    => 'POST',
                    'url'     => CController::createUrl('observacoes/dynamicObservacoes'),
                    'update'  => '#Atendimentos_ATD_OBS'
                    )
                )
            );
            ?>
            <?php echo $form->error($model, 'ATD_IDOBS');?>
        </div><!-- row -->
    <?php }?>
    </fieldset>
    <div class="clearfix"></div>
    <?php
    echo FaHtml::saveButton();
    $this->endWidget();
    ?>
</div><!-- form -->

<div class="clear"></div>

<div id="div-atividades">
    <?php
    $criteria = new CDbCriteria();
    $criteria->compare('ATD_IDCLIENTE', $ATD_IDCLIENTE);
    $criteria->compare('ATD_IDCONTRATO', $ATD_IDCONTRATO);
    $dataProvider = new CActiveDataProvider('Atendimentos', array(
        'criteria'      => $criteria,
        'sort'          => array(
            'defaultOrder' => 'ATD_IDATENDIMENTO DESC, ATD_DTCADASTRO DESC, ATD_HORAINICIO DESC',
            ),
        )
    );

    $this->widget('zii.widgets.CListView', array(
        'dataProvider' => $dataProvider,
        'itemView'     => '_dview',
        )
    );
    ?>
</div>

<script>
    function checkObservacao() {
        if($('#Atendimentos_ATD_IDMOTIVO').val() == -1) {
            $('label[for=Atendimentos_ATD_OBS]').html("Observações <span class='required'>*</span>")
        } else {
            $('label[for=Atendimentos_ATD_OBS]').html("Observações")
        }
    }
    $('#Atendimentos_ATD_IDMOTIVO').change(function() {
        checkObservacao();
    });
</script>
<style type="text/css">
    @media(max-width:800px){
        .form-cadastro fieldset {
            width: 100%;
            box-sizing: border-box;
            float: left;
            display: inline;
        }
    }
    <?php if (User::getUser()->FUN_PRM_EXCECOES == true) { ?>
    @media(min-width:801px){
        .form-cadastro fieldset {
            width: 30%;
            box-sizing: border-box;
            float: left;
            display: inline;
        }
    }
    <?php } else { ?>
    @media(min-width:700px){
        .form-cadastro fieldset {
            width: 60%;
            box-sizing: border-box;
            float: left;
            display: inline;
        }
    }
    <?php } ?>
    @media(min-width:989px){
        .form-cadastro fieldset {
            width: 45%;
            box-sizing: border-box;
            float: left;
            display: inline;
        }
    }
    .form-cadastro .form-group textarea {
        height: 193px;
    }
    .form-cadastro .form-group textarea,
    .form-cadastro .form-group select {
        width: 100%!important;
        max-width: 100%!important;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
    }
    .form-cadastro .form-group {
        position: relative;
        margin-right: 0;
        margin-left: 0;
        padding-right: 0;
        padding-left: 0;
    }
    .no-border {
        border: none!important;
    }
    .ui-multiselect {
        display: block;
        max-height: 250px!important;
    }
    .ui-multiselect ul.available, .ui-multiselect ul.selected {
        height: 225px;
    }
    .ui-multiselect .actions {
        height: 26px!important;
    }
    .form-group.alert-form-group,
    .form-group.alert-form-group .alert {
        margin-bottom: 0!important;
    }
</style>