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

$this->breadcrumbs = array(
    yii::t('default', 'Badges') => array('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->labelEx($model, 'person_id');

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

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

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
    $badge = Badge::model()->findByPk($model->id);
    if ($badge->person_id != null && $badge->person_id != ''&& $badge->person_id != 0)
    {
        echo CHtml::submitButton(Yii::t('default', 'Save'),
                array('class' => 'btn primary',
                    "onClick" =>
                    "
								// The confirmation message of the ressignment shouldn't appear in case no people was selected	
								if($('#Badge_person_id').val() != ''){
									return confirm('" . yii::t('default', 'Are you sure you want to reassign the badge of number ({badge_number}) (which was previously assigned to {person_name}) to another person?', array('{badge_number}' => $model->number, '{person_name}' => $badge->person->name)) . "');
								}
							"
                )
        );
    }
    else
    {
        echo CHtml::submitButton(Yii::t('default', 'Save'), array('class' => 'btn primary'));
    }
    ?>
    &ensp;
    <?php echo CHtml::link(Yii::t('default', 'Cancel'), array('index'), 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="+"<?php echo $model->number ?>",
            cache: false,
            success: function(result) {
                $("[name*='Badge[activation_end_date]']").val(result);
            }
        });
    })
</script>