<?php
cs()->registerScript(
    'alertAutoClose',
    'window.setTimeout(function() { $(".alert").alert("close"); }, 5000)'
);
$this->widget(
    'ext.PlNavbar',
    array(
        'brand' => __('SPEED_DIAL_KEYS_HEADER_TITLE'),
        'brandUrl' => false,
        'fixed' => false,
        'htmlOptions' => array(
            'data-translation-id' => 'SPEED_DIAL_KEYS_HEADER_TITLE',
        ),
        'items' => array(
            array(
                'class' => 'bootstrap.widgets.TbMenu',
                'htmlOptions' => array('class' => 'pull-right'),
                'items' => array(
                    array(
                        'label' => __('SPEED_DIAL_EXPORT_IMPORT_BUTTON_TITLE'),
                        'url' => '#',
                        'icon' => 'wrench',
                        'linkOptions' => array(
                            'data-translation-id' => 'SPEED_DIAL_EXPORT_IMPORT_BUTTON_TITLE',
                        ),
                        'items' => array(
                            array(
                                'label' => __('SPEED_DIAL_EXPORT_BUTTON_TITLE'),
                                'url' => url('phonesettingsSpeedDial/default/export'),
                                'linkOptions' => array(
                                    'data-translation-id' => 'SPEED_DIAL_EXPORT_BUTTON_TITLE',
                                ),
                            ),
                            array(
                                
                                'label' => __('SPEED_DIAL_IMPORT_BUTTON_TITLE'),
                                'url' => url('phonesettingsSpeedDial/default/import'),
                                'linkOptions' => array(
                                    'id' => 'importSpeedDialList',
                                    'data-translation-id' => 'SPEED_DIAL_IMPORT_BUTTON_TITLE',
                                ),
                            ),
                            array(
                                'label' => __('SPEED_DIAL_PDF_EXPORT'),
                                'url' => url('phonesettingsSpeedDial/default/generatePDF'),
                                'linkOptions' => array(
                                    'target' => '_blank',
                                    'data-translation-id' => 'SPEED_DIAL_PDF_EXPORT',
                                ),
                            ),
                        )
                    ),
                ),
            ),
        )
    )
);

$this->widget('bootstrap.widgets.TbAlert', array(
    'block' => true, // display a larger alert block?
    'fade' => true, // use transitions?
    'closeText' => '&times;', // close link text - if set to false, no close link is displayed
    'alerts' => array(// configurations per alert type
        'success' => array( // success, info, warning, error or danger
            'block' => true,
            'fade' => true,
            'closeText' => '&times;'
        ),
        'info' => array( // success, info, warning, error or danger
            'block' => true,
            'fade' => true,
            'closeText' => '&times;'
        ),
        'danger' => array(// success, info, warning, error or danger
            'block' => true,
            'fade' => true,
            'closeText' => '&times;'
        ),
    ),
));

$this->widget('bootstrap.widgets.TbAlert', array(
    'block' => true, // display a larger alert block?
    'fade' => true, // use transitions?
    'closeText' => '&times;', // close link text - if set to false, no close link is displayed
    'alerts' => array(// configurations per alert type
        'error' => array( // success, info, warning, error or danger
            'block' => true,
            'fade' => true,
            'closeText' => '&times;'
        ),
    ),
));

echo CHtml::beginForm();
$this->widget('ext.PlGrid.PlGridView', array(
    'showNavbar' => false,
    'id' => 'speed-dial-grid',
    'itemsCssClass' => 'plTable ',
    'dataProvider' => $dataProvider,
    'summaryText' => false,
    'type' => 'striped bordered ', //condensed
    'columns' => array(
        array(
            'name' => 'key',
            'htmlOptions' => array('width' => '40', 'disabled' => '$data->profile_id == "" ? "disabled : ""'),
            'header' => __('SPEED_DIAL_PREFIX_PHONE_KEY'),
            'type' => 'raw',
            'headerHtmlOptions' => array('data-translation-id' => 'SPEED_DIAL_PREFIX_PHONE_KEY'),
            'value' => array($this, "renderSpeedDialInput"),
        ),
        array(
            'name' => 'phoneNumber',
            'header' => __('SPEED_DIAL_MODEL_PHONENUMBER'),
            'headerHtmlOptions' => array('data-translation-id' => 'SPEED_DIAL_MODEL_PHONENUMBER'),
            'type' => 'raw',
            'value' => array($this, "renderSpeedDialInput"),
        ),
        array(
            'name' => 'description',
            'header' => __('SPEED_DIAL_MODEL_DESCRIPTION'),
            'headerHtmlOptions' => array('data-translation-id' => 'SPEED_DIAL_MODEL_DESCRIPTION'),
            'type' => 'raw',
            'value' => array($this, "renderSpeedDialInput"),
        ),
        array(
            'class' => 'bootstrap.widgets.TbButtonColumn',
            'template' => '{view}{update}{delete}',
            'htmlOptions' => array(
                'data-translation-target' => 'USER_ERROR_MESSAGE_TRANSLATIONS',
                'class' => 'buttonColumnIcons',
            ),
            'buttons' => array(
                'view' => array(
                    'visible' => 'false',
                ),
                'update' => array(
                    'visible' => 'false',
                ),
                'delete' => array(
                    'url' => '',
                    'label' => "",
                    'visible' => '$data->profile_id == ""', //FIXME
                    'click' => 'js:function(){
                            $(this).parent().find(".tooltip-inner").remove();
                            $(this).parent().find(".tooltip-arrow").remove();
                            $(this).parent().parent().remove();
                            $("#speed-dial-grid tfoot").show();
                        }',
                ),
            ),
            'footer' => '<div style="cursor: pointer;" class="icon-plus addExternalNumber"
                                data-toggle="tooltip" title="' . __("SPEED_DIAL_ADD_NEW_RULE_TOOLTIP") . '"
                                data-translation-id="SPEED_DIAL_ADD_NEW_RULE_TOOLTIP">
                        </div>',
        ),
    ),
));

echo CHtml::hiddenField('SpeedDialEmpty');

$this->widget('bootstrap.widgets.TbButton', array(
    'buttonType' => 'submit',
    'type' => 'primary',
    'label' => __('GLOBAL_SAVE_BUTTON'),
    'htmlOptions' => array(
        "data-translation-id" => "GLOBAL_SAVE_BUTTON"
    )
));
echo CHtml::endForm();

if (Yii::app()->user->translateMode) {
    echo '<p '
        . 'data-translation-id="SPEED_DIAL_KEY_ISNOT_UNIQUE"'
        . '>'
        . __('SPEED_DIAL_KEY_ISNOT_UNIQUE')
        . '</p>';
    echo '<h6>'
        . 'PDF Export Header'
        . '</h6>';
    echo '<p '
        . 'data-translation-id="PHONALISA_SPEEDIALLIST"'
        . '>'
        . __('PHONALISA_SPEEDIALLIST')
        . '</p>';
    echo '<p '
        . 'data-translation-id="PHONALISA_SPEEDIALLIST_FOR_EXTENSION"'
        . '>'
        . __('PHONALISA_SPEEDIALLIST_FOR_EXTENSION')
        . '</p>';
}
?>
<style>
    .grid-view {
        padding-top: 0;
    }

    #presets-grid tr td:nth-child(3) input{
        width: 30px;
        text-align: center;
    }
</style>

<script>
    var firstRow = $('#speed-dial-grid tbody tr').first();
    $('#content').on("click", ".addExternalNumber", function () {
        var index = $('#speed-dial-grid tbody tr').length + 1;
        $('#speed-dial-grid  > table').append(firstRow.clone().addClass('new-item-animated'));
        $('#speed-dial-grid tbody tr:last-child input').each(function (i, e) {
            $(e).removeAttr('disabled');
            var id = e.name.substring(e.name.lastIndexOf("SpeedDial[") + 10, e.name.lastIndexOf("]["));
            e.name = e.name.replace(id, index);
            e.value = '';
        });
    });
    
    $("#importSpeedDialList").click(function () {
        $.ajax({
            url:this.href,
            type:"POST",
            dataType:"html",
            success:function(data) {
                $(".modal-body").html(data);
                $(".modal-header h4").text("<?php echo __("SPEED_DIAL_KEYS_HEADER_TITLE_IMPORT"); ?>");
                $("#speed-dial-grid_Modal").modal("show");
            },
            error:function (data) {
                console.log(data);
                alert("error");
            }
            
        })
        return false;
    });
</script>