<?php
echo "<h4 data-translation-id='PROFILE_FORM_PARAMETER_ATTRIBUTES'>".__('PROFILE_FORM_PARAMETER_ATTRIBUTES').': '.$phoneModel->getPhoneModelNameHumanReadable()."</h4>";
$columns = array();
$notInGrid = array('profileID','phoneModel');

foreach ($defaultParameter->attributes as $attributeName=>$attributeValue) {
    if (!in_array($attributeName,$notInGrid)) {
        $columns[] = array(
                    'name'=>$attributeName,
                    'header' => $defaultParameter->getAttributeLabel($attributeName),
                    'type'=>'raw',
                    'headerHtmlOptions'=>array('data-translation-id'=> 'PHONE_PARAMETERS_MODEL_'.strtoupper($attributeName)),
                    'value'=>array($this,'render_textField')
            );
    } 
}
$columns[2]['footer'] = '<span style="cursor: pointer;" data-translation-id="ADD_PHONE_PARAMETER" id="addPhoneParameterLabel">'.__('ADD_PHONE_PARAMETER').'</span>';
$columns[2]['footerHtmlOptions'] = array('style'=>'text-align: right;');
$columns[] = array(
                'class'=>'bootstrap.widgets.TbButtonColumn',                
                'htmlOptions'=>array('class'=>'buttonColumnIcons', 'style'=>'cursor: pointer;width:55px;'),
                'template'=>'{delete}',
                'buttons'=>array(
                    'delete'=>array(
                        'url'=>'',
                        'visible'=>'true',//FIXME
                        'click'=>'js:function(){                           
                                $(this).parent().parent().remove();
                        }',
                    ),
                ),
            'footer'=>'<div class="icon-plus addParameter" style="cursor: pointer;"
							data-toggle="tooltip" title="'.__("PROVISIONING_PROFILE_ADD_PARAMETER_TOOLTIP").'"
							data-translation-id="PROVISIONING_PROFILE_ADD_PARAMETER_TOOLTIP">
					  </div>',
            );


if ($buffer) {
	$dataProvider = $bufferParameterKeys;
	$hideRow = "";
	echo '<script>formHasChanged = true;</script>';
} else {
	$dataProvider = $defaultParameter->search($profileId,$selectedPhone);
	$hideRow = '$data->id ? "":"hideRow"';
	echo '<script>formHasChanged = false;</script>';
}

$this->widget('bootstrap.widgets.TbGridView', array(
        'id'=>'phone-parameter-grid',
        'itemsCssClass'=>'plTable items',
        'enableSorting'=>false,
        'summaryText'=>'',
        'type'=>'striped bordered condensed',  
        'dataProvider'=>$dataProvider,
        'rowCssClassExpression'=>$hideRow,
       // 'filter'=>$defaultParameter,
        'columns'=>$columns,
        'htmlOptions' => array(
            'class' => 'plTable'
        ),
    )
);


?> 
<style>
.hideRow{
    display:none;
}
.addParameter{
    background-color: #fff;
}
.table-bordered tfoot td{
        border-left-color: transparent;
}
.table-bordered tfoot td:last-child , .table-bordered tfoot td:first-child {
    border-left-color: #ddd;
}
</style>
<script>
$('#provisioningProfileSaveBtn').click(function() {
    $('#saveBtn').click();
    $(this).button('loading');
});

var rowTemplate = $('#phone-parameter-grid tbody tr').first();

$('#addPhoneParameterLabel,.addParameter').click(function(){
    
    var index = $('#phone-parameter-grid tbody').find('.icon-trash').length;    

    $('#phone-parameter-grid > table').append(rowTemplate.clone().addClass('new-item-animated'));
        $('#phone-parameter-grid tbody tr:last-child').show();
        $('#phone-parameter-grid tbody tr:last-child input').each(function(i,e){
            e.name = e.name.replace('0', index);
            e.id = e.id.replace('0', index);   
            e.value = '';
        })
})
</script>
