<?php
/* @var $this BadgeController */
/* @var $model Badge */
/* @var $form CActiveForm */

$this->breadcrumbs = array(
    yii::t('default', 'Persons') => array('/person/index'),
    yii::t('default', 'Assign Badge to Person'),
);

$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
            'id' => 'badge-form',
            'enableAjaxValidation' => false,
        ));


$this->widget('bootstrap.widgets.TbAlert');

echo $form->uneditableRow($model, 'person_id');

echo $form->labelEx($model, 'number');

$form->widget('zii.widgets.jui.CJuiAutoComplete', array(
    'model' => $model,
    'attribute' => 'number',
    'source' => array_values(Badge::getArrayOfBadges('Free')),
    'options' => array(
        'minLength' => '0',
        'select' => 'js: function(request, response) {
                                 item_name = response.item.value;
                                 $.ajax({
                                   url: "' . $this->createUrl('badge/getMaxServedMeals') . '"+"&person_id="+' . $id . '+"&badge_number="+item_name+"",
                                   success: function (data) {
                                           $("#Badge_max_served_meals").val(data);
                                   }
                                })
                             }',
    ),));

echo $form->labelEx($model, 'activation_start_date');

$this->widget('application.extensions.timepicker.timepicker', array(
    'model' => $model,
    'name' => 'activation_start_date',
    'select' => 'date',
    'options' => array(
        'timeFormat' => 'hh:mm',
        'showOn' => 'focus',
        'showSecond' => false,
        'changeMonth' => false,
        'changeYear' => false,
        'tabularLevel' => null,
        'regional' => 'nl',
    ),
));

echo $form->error($model, 'activation_start_date');


echo $form->labelEx($model, 'activation_end_date');

$this->widget('application.extensions.timepicker.timepicker', array(
    'model' => $model,
    'name' => 'activation_end_date',
    'select' => 'date',
    'options' => array(
        'timeFormat' => 'hh:mm',
        'showOn' => 'focus',
        'showSecond' => false,
        'changeMonth' => false,
        'changeYear' => false,
        'tabularLevel' => null,
        'regional' => 'nl',
    ),
));

echo $form->error($model, 'activation_end_date');

echo $form->textFieldRow($model, 'max_served_meals');
?>
<div class="actions">
    <?php
    echo CHtml::submitButton(Yii::t('default', 'Save'), array('class' => 'btn primary'));
    ?>
    &ensp;
    <?php echo CHtml::link(Yii::t('default', 'Cancel'), array('/person/manageBadges', 'id' => $id), array('class' => 'btn primary')); ?>
</div>

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

    <script language="javascript">
        $("[name*='Badge[activation_start_date]']").change(function(e) {
            $.ajax({
                type: 'POST',
                url:"<?php echo $this->createUrl('badge/getActivatedPeriod') ?>&start_date="+ this.value+"&badge_number="+$('#Badge_number').val(),
            cache: false,
            success: function(result) {
                $("[name*='Badge[activation_end_date]']").val(result);
            }
        });
    })
</script>