<?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,
             'action' => array("/badge/addAndAssign", 'badge_number' => $model->number, 'add' => false),
         ));

 // Set ID for use when cancel the asign operate and back to person view 
 if ($model->person_id != "" && ! is_numeric($model->person_id))
 {
     $attributes_array = explode("-", $model->person_id);
     $person_number = end($attributes_array);
     $person_model = Person::model()->findByAttributes(array('number' => $person_number));
     if ($person_model != null)
         $id = $person_model->id;
     else
         $id = null;
 }

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

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

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

 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
     echo CHtml::submitButton(Yii::t('default', 'Save'), array('class' => 'btn primary'));
    ?>
     &ensp; 
    <?php echo CHtml::link(Yii::t('default', 'Cancel'), array('/LDAPperson/view', '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="+"<?php echo $model->number ?>",
            cache: false,
            success: function(result) {
                $("[name*='Badge[activation_end_date]']").val(result);
            }
        });
    })
</script>