<?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_ALERT_MESSAGE",
    	'data-translation-target'=>'PROVISIONING_PROFILE_FORM_NAVBAR_TITLE',
    ),
);


$this->widget('ext.PlNavbar', array(
        'brand'=>$profile->isNewRecord() ? __('PROVISIONING_PROFILE_CREATE_NEW_PROFILE') : __('PROVISIONING_PROFILE_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(
                    //array('label'=>'Admin', 'url'=>'', 'active'=>true, 'icon'=>'list'),
                    '---',
                    $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
    ));

$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
        'id'=>'provisioning-profile-form',
        'enableAjaxValidation'=>true,
        'type'=>'horizontal',
        'htmlOptions' => array('enctype' => 'multipart/form-data'),
    ));

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_PROFILE_MODEL_NAME'
    ));
echo $form->textFieldRow($profile,'descr',array(
        'data-translation-id'=>'PROVISIONING_PROFILE_MODEL_DESCR'
    ));

echo '</div>';

$this->renderPartial('defaultGrid',array(
        'phoneModel' => $phoneModel,
        'defaultParameter'=>$defaultParameter,
        'profileId'=>$profile->id,
        'selectedPhone'=>$selectedPhone,
		'buffer'=>$buffer,
		'bufferParameterKeys'=>$bufferParameterKeys
    ));

if(count($phoneModel->getUploadableFiles()) > 0){
    $this->renderPartial('filesGrid',array(
        'profile'=>$profile,
        'defaultParameter'=>$defaultParameter,
        'profileId'=>$profile->id,
        'selectedPhone'=>$selectedPhone,
    	'buffer'=>$buffer,
    	'bufferParameterKeys'=>$bufferParameterKeys,
    	'phoneModel' => $phoneModel
    ));    
}


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

));

if (Yii::app()->user->translateMode) {
    echo '<i data-translation-target="PROVISIONING_PROFILE_FORM_NAVBAR_TITLE" data-translation-id="PROVISIONING_PROFILE_CREATE_NEW_PROFILE"></i>';
    echo '<i data-translation-target="PROVISIONING_PROFILE_FORM_NAVBAR_TITLE" data-translation-id="PROVISIONING_PROFILE_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>';
}
$this->endWidget();

?>

<script>

var formHasChanged;
$('#provisioning-profile-form').on('change',function() {
    formHasChanged = true;
});

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

</script>