<?php

$phoneList = $this->renderPhoneNamesDropdown();
if (isset($_GET['phone'])) {
    $selectedPhone = $_GET['phone'];
}
$phonesDropdown = array(
    'label'=>$phoneList[$selectedPhone]['label'],
    'url'=>'#',
    'items'=>$phoneList,
    'linkOptions'=>array(
        'onclick'=>'throwAlert(this)',
        'data-translation-id'=>"PROVISIONING_PROFILE_FORM_CHANGE_PHONE_CONFIRM_MESSAGE",
         'data-translation-target'=>'PROVISIONING_PROFILE_FORM_NAVBAR_TITLE',
    ),
);


$this->widget('ext.PlNavbar', array(
        'collapse'=>false, // requires bootstrap-responsive.css
        // 'brand'=>__('PROVISIONING_PROFILE_FORM_NAVBAR_TITLE'),
        'brand'=>$profile->isNewRecord() ? __('PROVISIONING_PROFILE_KEY_CREATE_NEW_PROFILE') : __('PROVISIONING_PROFILE_KEY_UPDATE_NEW_PROFILE') . ': ' . CHtml::encode($profile->name),
        'htmlOptions'=>array(
            'data-translation-target'=>'PROVISIONING_PROFILE_FORM_NAVBAR_TITLE',
        ),
        'items'=>array(
            array(
                'class'=>'bootstrap.widgets.TbMenu',
                
                'items'=>array(
                    '---',
                    $phonesDropdown,
                    
                ),
            ),
        ),
    ));

$this->widget('bootstrap.widgets.TbAlert', array(
        'block'=>true, // display a larger alert block?
        'fade'=>true, // use transitions?
        'closeText'=>'&times;', // close link text - if set to false, no close link is displayed
    ));
    
    
Yii::app()->clientScript->registerScript(
   'autoHideAlert',
   'setTimeout(function(){$(".alert").alert("close")}, 3000);',
   CClientScript::POS_READY
);



$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
        'id'=>'provisioning-key-profile-form',
        'enableAjaxValidation'=>true,
        'type'=>'horizontal'
    ));
    
echo '<div class="well">';
echo '<h4 data-translation-id="PROFILE_FORM_PROFILE_ATTRIBUTES">'.__('PROFILE_FORM_PROFILE_ATTRIBUTES').'</h4>';
echo $form->textFieldRow($profile,'name',array(
        'data-translation-id'=>'PROVISIONING_KEY_PROFILE_MODEL_NAME'
    ));
echo $form->textFieldRow($profile,'descr',array(
        'data-translation-id'=>'PROVISIONING_KEY_PROFILE_MODEL_DESCR'
    ));
echo '</div>';


//FIXME :
$this->renderPartial('defaultGrid',array(
        'defaultParameter'=>$defaultParameter,
        'profile'=>$profile,
        'selectedPhone'=>$selectedPhone,
		'buffer'=>$buffer
    ));

$this->widget('bootstrap.widgets.TbButton', array(
        'buttonType'=>'submit',
        'type'=>'primary',
        'label'=>__('GLOBAL_SAVE_BUTTON'),
        'loadingText'=>'loading...',
        'htmlOptions'=>array(
            'id'=>'KeyProfileSaveBtn',
            'data-translation-id'=>'GLOBAL_SAVE_BUTTON',
        ),

    ));


$this->endWidget();

if (Yii::app()->user->translateMode) {
    echo '<i data-translation-target="PROVISIONING_PROFILE_FORM_NAVBAR_TITLE" data-translation-id="PROVISIONING_PROFILE_KEY_CREATE_NEW_PROFILE"></i>';
    echo '<i data-translation-target="PROVISIONING_PROFILE_FORM_NAVBAR_TITLE" data-translation-id="PROVISIONING_PROFILE_KEY_UPDATE_NEW_PROFILE"></i>';
    echo '<i data-translation-target="PROVISIONING_PROFILE_FORM_NAVBAR_TITLE" data-translation-id="CHANGES_SAVED_SUCCSESSFULLY"></i>';
    echo '<i data-translation-target="PROVISIONING_PROFILE_FORM_NAVBAR_TITLE" data-translation-id="CHANGES_NOT_SAVED_CAUSE_OF_AN_ERROR"></i>';
}

if  (Yii::app()->user->translateMode) {
    $options = $defaultParameter->getOptions(false);
    foreach($options as $id => $value){
        echo '<i data-translation-id="'.$value['label'].'" data-translation-target="TEST"></i>';
    }
}
?>
<script>
var formHasChanged;
$('#provisioning-key-profile-form').on('change',function() {
    formHasChanged = true;
});

$('#KeyProfileSaveBtn').click(function() {
    $('#saveBtn').click();
    $(this).button('loading');
});

function throwAlert(e) {
    if (formHasChanged) {
        alert("<?php echo __("PROVISIONING_PROFILE_FORM_CHANGE_PHONE_CONFIRM_MESSAGE");?>");
    }
}
</script>