<?php

if ( $profile &&  $defaultParameter->getPhoneKeysDataProviderForKeyProfile($profile, "")!= null) {

    $keyLevels = $defaultParameter->getAvailableKeyLevels();

    if (count($keyLevels) == 0) {
        $keyLevels[] = array();
    }

    foreach ($keyLevels as $keyLevel) {

        if(!array_key_exists('level',$keyLevel)) {
            continue;
        }

        if ($buffer) {
        	$dataProvider = $this->bufferPhoneKeys($profile,$defaultParameter,$keyLevel["level"]);
        	echo '<script>formHasChanged = true;</script>';
        } else {
        	$dataProvider = $defaultParameter->getPhoneKeysDataProviderForKeyProfile($profile, $keyLevel["level"]);
        	echo '<script>formHasChanged = false;</script>';
        }
        
         echo "<h4 data-translation-id=".@$keyLevel["name"].">".__(@$keyLevel["name"])."</h4>";
$this->widget('bootstrap.widgets.TbGridView', array(
                'id'=>'phone-key-grid',
                'dataProvider'=>$dataProvider,
                'type'=>'striped bordered condensed',                  //change color from columns in 'white' and 'grey' responsively
                'itemsCssClass'=>'plTable',
                'summaryText' => false,                           //change design of the table, removed search bar
                'columns'=>array(
                    array(
                        'name' => 'name',
                        'type'=>'raw',
                        'value' => array($this, 'renderKeyName'),
                        'header' => __('PHONEKEYS_MODEL_KEY'),
                        'headerHtmlOptions'=>array('data-translation-id'=>'PHONEKEYS_MODEL_KEY')
                    ),
                    /*array(
                        'name' => 'phone',
                        'type'=>'raw',
                        'header' => __('PHONEKEYS_MODEL_PHONE'),
                        'headerHtmlOptions'=>array('data-translation-id'=>'PHONEKEYS_MODEL_PHONE')
                    ),*/
                    array(
                        'name'=>'function',
                        'type'=>'raw',
                        'value'=>array($defaultParameter, 'renderFunction'),
                        'header' => __('PHONEKEYS_MODEL_FUNCTION'),
                        'headerHtmlOptions'=>array('data-translation-id'=>'PHONEKEYS_MODEL_FUNCTION')
                    ),
                    array(
                        'name'=>'number_data',
                        'type'=>'raw',
                        'value'=> array($this, 'renderTextField'),
                        'header' => __('PHONEKEYS_MODEL_NUMBER_DATA'),
                        'headerHtmlOptions'=>array('data-translation-id'=>'PHONEKEYS_MODEL_NUMBER_DATA')
                    ),
                    array(
                        'name'=>'label',
                        'type'=>'raw',
                        'value'=> array($this, 'renderTextField'),
                        'header' => __('PHONEKEYS_MODEL_LABEL_DATA'),
                        'headerHtmlOptions'=>array('data-translation-id'=>'PHONEKEYS_MODEL_LABEL_DATA')
                    ),
                   array(
                        'name'=>'locked',
                        'type'=>'raw',
                        'value'=>array($this,'renderLocked'),
                   		'header' => __('PHONEKEYS_MODEL_LOCKED_DATA'),
                   		'headerHtmlOptions'=>array('data-translation-id'=>'PHONEKEYS_MODEL_LOCKED_DATA')
                    ),
                ),
            )
        );
    }
}
    echo CHtml::dropDownList('Dummy[queue_list]', '', $this->getAllQueues(), array('style' => 'display:none'));
    echo CHtml::textField('Dummy[text_field]', '', array('style' => 'display:none'));
    echo CHtml::dropDownList('Dummy[callgroup_list]', '', $this->getAllCallGroup(), array('style' => 'display:none'));
?>
<script>
    function toggleKeySelection(field) {
        
        var dummyQueueList = $("#Dummy_queue_list");
        var dummyTextField = $("#Dummy_text_field");
        var dummyCallGroupList = $("#Dummy_callgroup_list");
        var id = $(field).attr('id').split('_function')[0];

        var value = $(field)[0].options[$(field)[0].selectedIndex].text;
        
        var currentField = $('#' + id + '_number_data');

        var valueNoOverwrite = "<?php echo __('PHONE_TIPTEL_FUNCTION_NOT_OVERRIDE'); ?>";
        var valueEmpty = "<?php echo __('PHONE_TIPTEL_FUNCTION_EMPTY'); ?>";
        var login_logout_queues = "<?php echo __('PHONE_TIPTEL_FUNCTION_QUEUE_LOGIN'); ?>";
        var login_logout_callgroup = "<?php echo __('PHONE_TIPTEL_FUNCTION_CALLGROUP_LOGIN'); ?>";

        var disableDataFieldList = [
            valueNoOverwrite,
            valueEmpty,
            "<?php echo __('PHONE_TIPTEL_FUNCTION_VOICEMAIL'); ?>",
            "<?php echo __('PHONE_TIPTEL_FUNCTION_DND'); ?>",
            "<?php echo __('PHONE_TIPTEL_FUNCTION_ALL_QUEUES_LOGOUT'); ?>",
            "<?php echo __('PHONE_TIPTEL_FUNCTION_ALL_QUEUES_LOGIN'); ?>",
        ];

        $('#' + id + '_label').removeAttr('disabled');
        
        if (value === login_logout_queues) {
            var newField = dummyQueueList.clone();
            changeReplaceCurrentFieldWithNew(id, currentField, newField);
        } else if (value === login_logout_callgroup) {
            var newField = dummyCallGroupList.clone();
            changeReplaceCurrentFieldWithNew(id, currentField, newField);
        } else {
            var newField = dummyTextField.clone();
            changeReplaceCurrentFieldWithNew(id, currentField, newField);
            if ($.inArray(value, disableDataFieldList) !== -1) {
                newField.attr('disabled', 'disabled');
            }
        }
        if (value === valueNoOverwrite || value === valueEmpty) {
            $('#' + id + '_label').val('');
            $('#' + id + '_label').attr('disabled', 'disabled');
        }
    }

    function changeReplaceCurrentFieldWithNew(id, currentField, newField) {
        newField.attr("id", id + '_number_data');
        newField.attr("name", currentField.attr('name'));
        currentField.replaceWith(newField);
        newField.show();
    }
</script>
