<?php
Yii::app()->clientScript->registerCoreScript('jquery.ui');
/**
 rename the following KEYs
 NumberTranslation              = name of your Model Class
 number-translation      = name of your gridViews and Form IDs (the "-form" / "#grid") is not needed
 NUMBER_TRANSLATION     = name of the PlGridView TranslationTags
 */

$columns = array();
$dontRender = array('sip_headers','sort');
$renderBool = array('active');


$columns[] = array(
    'value' => '"<i class=\"icon-fullscreen\"></i>"',
    'type'=>'raw'
);


foreach ($model->attributes as $attributeName=>$attributeValue) {
    if ($attributeName == 'scope') {
        $attributeName = 'translatedScope';
    }


    if (in_array($attributeName,$dontRender)) {
        continue;
    }
    if (in_array($attributeName,$renderBool)) {
        $columns[] = array(
            'name'=>$attributeName,
            'header' => $attributeName == $model->getAttributeLabel($attributeName),
            'type'=>'raw',
            'value'=>array($this,'renderBool'),
            'headerHtmlOptions'=>array('data-translation-id'=> strtoupper('NumberTranslation').'_MODEL_'.strtoupper($attributeName)),
        );
        continue;
    }
    $columns[] = array(
        'name'=>$attributeName,
        'header' => $model->getAttributeLabel($attributeName),
        'headerHtmlOptions'=>array('data-translation-id'=> strtoupper('NumberTranslation').'_MODEL_'.strtoupper($attributeName == 'translatedScope' ? 'scope' : $attributeName)),
    );
}
$columns[] = array(
    'class'=>'ext.PlGrid.PlCButtonColumn',
    'template'=>'{ajaxUpdate}{delete}',
);

$this->widget('ext.PlGrid.PlGridView', array(
        'id'=>'number-translation-grid',
        'headerTitle'=>'Header_title', // optional
        'addButtonTitle'=>'ADD_NEW_NUMBER_TRANSLATION', // optional
        'searchButtonTitle'=>'SEARCH_NUMBER_TRANSLATION', // optional
        'modalCreateHeader'=>'NUMBER_TRANSLATION_HEADER_CREATE', // optional
        'modalUpdateHeader'=>'NUMBER_TRANSLATION_HEADER_UPDATE', // optional
        'itemsCssClass'=>'plTable items',
        'modalSize' => 'large', // null, 'large', 'small', 'huge'
        'dataProvider'=>$model->search(),
        'filter'=>$model,
        'columns'=>$columns,
    )
);
echo "<div id='alertFlash'></div>";

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

    ));

?>
<script>
$('#saveNewSortOrder').click(function(){

var data = {};
  $('#number-translation-grid').find('input[type=hidden]').each(function(i,e) {
      data[e.name] = e.value;
  });

var url = '<?php echo url('systemNumberTranslator/default/saveNewSortOrder') ?>';

//   data[e.name] = e.value;
 $.post(
    url,
    { '<?php echo Yii::app()->request->csrfTokenName ?>':'<?php echo Yii::app()->request->csrfToken ?>', data:data } ,
    function(data){
      if (data) {
	var alertBlockTemplate = '<div class="alert alert-error alert-block fade in"><strong><?php echo __('CHANGES_NOT_SAVED_CAUSE_OF_AN_ERROR'); ?></strong><a class="close" data-dismiss="alert" href="#">&times;</a></div>';
	$('#alertFlash').html(alertBlockTemplate);
      } else {
	var alertBlockTemplate = '<div class="alert alert-success alert-block fade in"><strong><?php echo __('CHANGES_SAVED_SUCCSESSFULLY'); ?></strong><a class="close" data-dismiss="alert" href="#">&times;</a></div>';
	$('#alertFlash').html(alertBlockTemplate);
      }
      setTimeout(function(){$(".alert").alert('close');}, 6000);
    }, 'json');

});


$(function() {
  $("#number-translation-grid tbody").sortable({axis: 'y';});
  $( "#number-translation-grid tbody" ).on( "sortstop", function( event, ui ) {

    $.each($("#number-translation-grid tbody tr"),function(i,e) {
	$(e).find('input.hidenSort').val(i);
    });
  } );
});



function removeSipHeader(element) {
  $(element).parent().parent().remove();
  updateSipGridInputFields();
}

function updateSipGridInputFields() {
 $.each($("#sip-header-grid tbody tr"),function(i,e) {
	$(e).find('input.hidenIndex').val(i);

	$(e).find('input').each(function(index,element) {
	  if (index === 0 ) {
	    element.id = 'sip_headers_'+i+'_id';
	    element.name = 'sip_headers['+i+'][id]';
	  } else if (index === 1) {
	    element.id = 'sip_headers_'+i+'_header';
	    element.name = 'sip_headers['+i+'][header]';
	  } else if (index === 2 ) {
	    element.id = 'sip_headers_'+i+'_value';
	     element.name = 'sip_headers['+i+'][value]';
	  } else if (index === 3) {
	    element.id = 'sip_headers_'+i+'_new_value';
	    element.name = 'sip_headers['+i+'][new_value]';
	  }
	});
    });
}
</script>