<?php

$this->widget('ext.PlNavbar', array(
    'brand'=> __('ADMINGROUPS_HEADER_TITLE'),
    'htmlOptions' => array(
        'data-translation-id'=>'ADMINGROUPS_HEADER_TITLE',
    ),
    'items'=>array(
        array(
            'class'=>'bootstrap.widgets.TbMenu',
            'items'=>array(
                array(
                    'label'=> __('ADMINGROUPS_ADD_NEW_GROUP'),
                    'linkOptions'=>array(
                        'id'=>'groups-gridCreateBtn',
                        'data-target'=>'#groups-grid_Modal',
                        'data-translation-id'=>'ADMINGROUPS_ADD_NEW_GROUP',
                    ),
                    'icon'=>'plus',
                    'url'=>'#'
                ),
            ),
        ),
    ),
));


$this->widget('ext.treetable.JTreeTable',array(
    'id'=>'treeTable',
    'primaryColumn'=>'id',
    'parentColumn'=>'parent',
    'sortField' => 'name',
    'expanded' => true,
    'columns'=>array(
        'name'=>array(
            'label'=>'extension',
            'htmlOptions'=>array('align'=>'center'),
            'header' => $model->getAttributeLabel('name'),
            'headerHtmlOptions'=>array('data-translation-id'=>'USERGROUPS_MODEL_NAME'),
        ),
        'extension'=>array(
            'label'=>'extension',
            'htmlOptions'=>array('align'=>'center'),
            'header' => __('USERGROUPS_MODEL_EXTENSION'),
            'headerHtmlOptions'=>array(
                'width'=>80,
                'data-translation-id'=>'USERGROUPS_MODEL_EXTENSION'
            ),
            'visible'=>'isset($data->extension)',
            'value'=>'$data->night_circut ? "<i class=\"icon-moon\"></i> ".$data->extension : "<i class=\"icon-moon\" style=\"visibility: hidden;\"></i> ".$data->extension'
        ),
        'userNumber'=>array(
            'label'=>'userNumber',
            'headerHtmlOptions'=>array(),
            'htmlOptions'=>array('align'=>'center'),
	        'header' => __('USERGROUPS_MODEL_USERNUMBER'),
	        'headerHtmlOptions'=>array(
	           'data-translation-id'=>'USERGROUPS_MODEL_USERNUMBER',
	           'width'=>80,
	       ),
        ),
        'edit'=>array(
            'label'=>'Edit',
            'header' =>'' /*__('USERGROUPS_MODEL_EDIT')*/,
            'headerHtmlOptions'=>array(
                'width'=>100,
                //'data-translation-id'=>'USERGROUPS_MODEL_EDIT'
            ),
            'htmlOptions'=>array('align'=>'center'),
            'content'=>'test',
            'template'=>'{ringmap}{ajaxUpdate}{delete}',
            'buttons' => array(
                'ringMap'=>array(
                    'label' => '<i class="icon-align-center"></i>',
                    'options'=>array(
                        'title'=>__('USERGROUPS_MODEL_RINGMAP_BUTTON'),
                    		'data-translation-id'=>'USERGROUPS_MODEL_RINGMAP_BUTTON',
                     ),
                         'url'=>'Yii::app()->createUrl("adminGroups/default/ringMap", array("id"=>"$data->id"))',
                ),
                'detail'=>array(
                    'label' => '<i class="icon-group"></i>',
                    'options'=>array(
                        'title'=>__('USERGROUPS_MODEL_DETAIL_BUTTON'),
                    		'data-translation-id'=>'USERGROUPS_MODEL_DETAIL_BUTTON',
                     ),
                     //'url'=>'Yii::app()->createUrl("adminUsers/default/index", array("groupId"=>"$data->id"))',
                        'url'=>'Yii::app()->createUrl(
                            "adminGroups/default/detail",
                            array(  
                                "id"=>"$data->id",
                                "extension"=>$data->extension
                            )
                        )',
                ),
                'ajaxUpdate'=>array(
                    'label' => '<i class="icon-pencil"></i>',
                    'options'=>array(
                        'title'=>__('USERGROUPS_MODEL_UPDATE_BUTTON'),
                    	'data-translation-id'=>'USERGROUPS_MODEL_UPDATE_BUTTON',
            			//'data-id'=>'$data->id',
                     ),
                     'ajax' => true,
                     'url'=>'Yii::app()->createUrl("adminGroups/default/update", array("id"=>"$data->primaryKey"))',
                     'success'=>'function(data){
                        $("#groups-grid_Modal .modal-header h4").html("'.__('USERGROUPS_HEADER_UPDATE').'")
                        $("#groups-grid_Modal .modal-body").html(data);
                        $("#groups-grid_Modal").modal("show");
                     }',
                ),
                'delete'=>array(
                    'label' => '<i class="icon-trash"></i>',
                    'visible'=>'$data->type != "allUsers"',
                    'options'=>array(
                        'title'=> __('USERGROUPS_DELETE_BUTTON'),
                    	'data-translation-target'=>'USERGROUPS_DELETE_TRANSLATIONS',
            			//'data-id'=>'$data->id',
                     ),
                     'ajax' => true,
                     'confirm' => __('USERGROUPS_DELETE_CONFIRM_MESSAGE'),
                     'type' => 'POST',
                     'url'=>'Yii::app()->createUrl("adminGroups/default/delete", array("id"=>"$data->primaryKey"))',
                     'success'=>'function(data){
                        location.reload();
                     }',
                ),
            ),
        ),
/*
        array(
                'label'=>'',
                'class'=>'ext.PlGrid.PlCButtonColumn',
                'template'=>'{ajaxUpdate}{delete}',
            		'buttons'=>array(
            				//'ajaxUpdate' => array(
            				//    'label'=>'ajax',
            				//    'url'=>'Yii::app()->createUrl("ajaxUpdate", array("id"=>$data->id))',
            				//),
            				'delete'=>array(
            						'visible'=>'$data->type != "admins" && $data->type != "allUsers"',
            				),
            				'ajaxUpdate'=>array(
            						'label' => '<i class="icon-pencil"></i>',
            						'imageUrl'=> false,
            						'options'=>array(
            								'title'=>'Update',
            								//'data-id'=>'$data->id',
            						),
            						'url'=>'Yii::app()->createUrl("adminGroups/default/update", array("id"=>"$data->primaryKey"))',
            						'click'=>'js:function(){
				            						$.get(this.href, function(data){
				            						$(".modal-body").html(data);
				            						$("#modalHeader").html("'.__('USERGROUPS_HEADER_UPDATE').'");
				            						$("#newGroupModal").modal("show");
				            						return false;
				            						});
            										return false;
            						}',
            				),
            		),
            )
*/
    ),
    //'model' => 'Groups',
    'model' => Groups::model()->findByAttributes(array('parent' => '0')),
    'options'=>array(
        //'initialState'=>'expanded',
    ),
    'getChildrenUrl' => $this->createUrl('ajax/load', array()),
    'updateItemUrl' => $this->createUrl('ajax/update', array()),
));


$this->beginWidget('bootstrap.widgets.TbModal', array(
            'id'=>'groups-grid_Modal',
            'htmlOptions'=>array(
                'class' => 'modal-large',
            ),
            'options'=>array(
            ),
        ));


        echo '<div class="modal-header">
                <a class="close" data-dismiss="modal">&times;</a>
                <h4>Modal header</h4>
            </div>
            <div class="modal-body">
                <p>One fine body...</p>
            </div>
            <div class="modal-footer">';

            
        $saveBtnId = 'groups-grid_Modal_saveBtn';
        $deleteBtnId = 'groups-grid_Modal_deleteBtn'; 
        

        $this->widget('bootstrap.widgets.TbButton', array(
                'buttonType'=>'button',
                'type'=>'primary',
                'label'=>__('GLOBAL_SAVE_BUTTON'),
                'loadingText'=>__('GLOBAL_LOADING_TEXT'),
                'htmlOptions'=>array('id'=>$saveBtnId, 'class'=>'modalSaveBtn'),
            ));            


        $this->widget('bootstrap.widgets.TbButton', array(
            'buttonType'=>'link',
            'icon' => 'icon-question',
            'url'=>Yii::app()->controller->getHelpUrl(null, true),
            'encodeLabel' => false,
            'htmlOptions'=>array(
                'data-translation-id' => 'GLOBAL_HELP_BTN',
                'title' => __('GLOBAL_HELP_BTN'),
                'target'=> "_blank",
                'class'=>'pull-left'
            ),
        ));
            
        Yii::app()->clientScript->registerScript('modalSaveBtn', "$('#".$saveBtnId."').click(function() {
            var btn = $(this);
            btn.button('loading');
            $('#groups-grid_hiddenSaveBtn').click();
        });");
        
        
                
        Yii::app()->clientScript->registerScript('modalDeleteBtn', "$('#".$deleteBtnId."').click(function() {
            if(confirm('".__('GLOBAL_DELETE_CONFIRM')."')){
                var btn = $(this);
                var hiddenBtn = $('#".$this->id."_hiddenSaveBtn');
                btn.button('loading');
                $('<input>').attr({name: 'deleteRequest', value:'true'}).insertAfter(hiddenBtn);
                hiddenBtn.click();
            }
            
        });");

        $this->widget('bootstrap.widgets.TbButton', array(
                'label'=>__('GLOBAL_CANCEL_BUTTON'),
                'url'=>'#',
                'htmlOptions'=>array('data-dismiss'=>'modal'),
            ));

        echo '</div>';

        $this->endWidget();

        if (Yii::app()->user->translateMode) {
        	echo '<i data-translation-target="USERGROUPS_HEADER_ADD_UPDATE_TRANSLATION" data-translation-id="USERGROUPS_HEADER_CREATE"></i>';
        	echo '<i data-translation-target="USERGROUPS_HEADER_ADD_UPDATE_TRANSLATION" data-translation-id="USERGROUPS_HEADER_UPDATE"></i>';
        	
        	echo '<i data-translation-target="USERGROUPS_DELETE_TRANSLATIONS" data-translation-id="USERGROUPS_DELETE_BUTTON"></i>';
        	echo '<i data-translation-target="USERGROUPS_DELETE_TRANSLATIONS" data-translation-id="USERGROUPS_DELETE_CONFIRM_MESSAGE"></i>';
        }        


        $gridviewres = Yii::getPathOfAlias('zii.widgets.assets.gridview');
        Yii::app()->assetManager->publish($gridviewres);
        $assetfolder = Yii::app()->assetManager->getPublishedUrl($gridviewres);
        Yii::app()->clientScript->registerScriptFile($assetfolder.'/jquery.yiigridview.js');

Yii::app()->clientScript->registerScript('addModalWindow', "
		$('#groups-gridCreateBtn').click(function(){
		    $('#groups-grid_Modal .modal-header h4').html('".__('USERGROUPS_HEADER_CREATE')."');
		    $('#groups-grid_Modal .modal-header h4').attr('data-translation-target','USERGROUPS_HEADER_ADD_UPDATE_TRANSLATION');
		    $('#groups-grid_Modal_deleteBtn').hide();
		    $.ajax({
		        url: '".$this->createUrl('create')."',
		    })
		    .success(function(data) {
		        $('#groups-grid_Modal .modal-body').html(data);
		        $('#groups-grid_Modal').modal('show');
		     });
		  });     
           $('#groups-grid_Modal').on('shown', function () {
		      $('#groups-grid_Modal').find('input[type=text],textarea,select').filter(':visible:first').focus();
		      $('.modal-backdrop').unbind('click');
          });

		", null);
