<?php
// Register script files
cs()->registerScriptFile(Yii::app()->baseUrl . '/js/rangeSlider/jquery-ui-1.8.16.custom.min.js', CClientScript::POS_HEAD);
cs()->registerScriptFile(Yii::app()->baseUrl . '/js/rangeSlider/jQAllRangeSliders-withRuler-min.js', CClientScript::POS_HEAD);
cs()->registerScriptFile(Yii::app()->baseUrl . '/js/rangeSlider/sliderAdminGroups.js', CClientScript::POS_HEAD);
cs()->registerCssFile(Yii::app()->baseUrl . '/js/rangeSlider/css/Phonalisa.css');


if (Yii::app()->user->translateMode) {
    echo '<i data-translation-target="RING_DELAY_CONFIRM_LEAVE_WITHOUT_CHANGES" data-translation-id="RING_DELAY_CONFIRM_LEAVE_WITHOUT_CHANGES">RING_DELAY_CONFIRM_LEAVE_WITHOUT_CHANGES: ';
    echo __('RING_DELAY_CONFIRM_LEAVE_WITHOUT_CHANGES'). '</i><br><br>';
} 


$this->widget('ext.PlNavbar', array(
    'brand'=> Yii::t(
        'messages',
        'RING_DELAY_FOR_GROUP',
        array(
            '{group}' => CHtml::encode($group->name),
            '{extension}' => CHtml::encode($group->extension ? ' < '.$group->extension.' > ' :$group->extension )
            )
    ),
    'htmlOptions'=>array(
        'data-translation-id'=>'RING_DELAY_FOR_GROUP',
    ),
    'items'=>array(
        array(
            'class'=>'bootstrap.widgets.TbMenu',
            'items'=>array(
                '---',
                array(
                    'label'=>__('GLOBAL_BACK'),
                    'url'=>url('adminGroups'),
                    'icon'=>'arrow-left',
                ),
            ),
            'htmlOptions'=>array(
                'data-translation-id'=>'GLOBAL_BACK',
            ),
        ),
    ),
));

echo CHtml::beginForm();

$this->widget('bootstrap.widgets.TbGridView', array(
    'id'=>'user-grid',
    'itemsCssClass'=>'table plTable items',
    'type'=>'striped bordered condensed',
    'dataProvider'=>$userDataProvider,
//     'filter'=>$userModel,
    'summaryText' => false,
    'enableSorting' => false,
    'columns'=>array(
        array(
            'name' => 'extension',
            'header' => '&nbsp;<i class="icon-fixed-width icon-phone"></i>',
            'headerHtmlOptions'=>array('data-translation-id'=>'USERS_MODEL_EXTENSION', 'style'=>'width:30px;'),
        ),
        array(
            'name' => 'name',
            'header' => $userModel->getAttributeLabel('name'),
            'headerHtmlOptions'=>array('data-translation-id'=>'USERS_MODEL_NAME', 'style'=>'width:150px;'),
        ),
        array(
            'name' => 'firstname',
            'header' => $userModel->getAttributeLabel('firstname'),
            'headerHtmlOptions'=>array('data-translation-id'=>'USERS_MODEL_FIRSTNAME', 'style'=>'width:150px;'),
        ),
        array(
            'name' => 'lastname',
            'header' => $userModel->getAttributeLabel('lastname'),
            'headerHtmlOptions'=>array('data-translation-id'=>'USERS_MODEL_LASTNAME', 'style'=>'width:150px;'),
        ),
        array(
            'visible'=>$group->extension,
            'value'=>array($this,'renderRingTimeSlider'),
            'htmlOptions' => array('class' => 'sliderCell'),
        ),
    ),
));

// Display groups grid if child groups found
if(!empty($groupsDataProvider->rawData)){
    $this->widget('bootstrap.widgets.TbGridView', array(
        'id'=>'groups-grid',
        'itemsCssClass'=>'table plTable items',
        'type'=>'striped bordered condensed',
        'summaryText' => false,
        'dataProvider'=>$groupsDataProvider,
        'columns'=>array(
            array(
                'name' => 'extension',
                'header' => '&nbsp;<i class="icon-fixed-width icon-phone"></i>',
                'headerHtmlOptions'=>array('data-translation-id'=>'USERS_MODEL_EXTENSION', 'style'=>'width:30px;'),
            ),
            array(
                'name' => 'nestedName',
                'header' => $groupsModel->getAttributeLabel('name'),
                'headerHtmlOptions'=>array('data-translation-id'=>'USERS_MODEL_NAME', 'style'=>'width:472px;'),
            ),
            array(
                'value'=>array($this,'renderRingTimeSlider'),
                'htmlOptions' => array('class' => 'sliderCell'),
            ),
        )
    ));   
}

$this->widget('bootstrap.widgets.TbButton', array(
    'label'=>__('GLOBAL_SAVE_BUTTON'),
    'buttonType' => 'submit',
    'type'=>'primary',
    'htmlOptions'=> array(
        'onClick' => 'sliderValuesChanged = false'
    )
));

echo CHtml::endForm();
?>
<script>
    var sliderValuesChanged = false;
    runGroupSlider();

    function runGroupSlider() {
        var options = {
            maxRingTime: <?php echo $group->maxRingTime; ?>
        };

        groupSlider = new $.groupSlider(options);
        groupSlider.run();
    }

    $("#content .ringTimeSlider").bind("userValuesChanged", function (e, data) {
        sliderValuesChanged = true;
        $(this).prev().val(data.values.min);
        $(this).next().val(data.values.max);
    });

    $(window).bind('beforeunload', function () {
        if (sliderValuesChanged) {
            return '<?php echo __("RING_DELAY_CONFIRM_LEAVE_WITHOUT_CHANGES"); ?>';
        }
    });
</script>