<?php
cs()->registerScript('alertAutoClose', 'window.setTimeout(function() { $(".alert").alert("close"); }, 5000)');

$exportLink = array(
    'label' => __('SPEED_DIAL_EXPORT_BUTTON_TITLE'),
    'url' => url('adminSpeedDialList/default/export', array('profile_id' => isset($model->id) ? $model->id : "")),
    'linkOptions' => array(
        'data-translation-id' => 'SPEED_DIAL_EXPORT_BUTTON_TITLE',
    ),
);
$exportPDFLink = array(
    'label' => __('SPEED_DIAL_PDF_EXPORT'),
    'url' => url('adminSpeedDialList/default/generatePDF', array('profile_id' => isset($model->id) ? $model->id : "")),
    'linkOptions' => array(
    'target' => $model->isNewRecord ? "" : '_blank',
        'data-translation-id' => 'SPEED_DIAL_PDF_EXPORT',
    )
);
if ($model->isNewRecord()) {
    $exportLink["disabled"] = true;
    $exportPDFLink["disabled"] = true;
} 

$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(
                            $exportLink,
                            array(
                                'label' => __('SPEED_DIAL_IMPORT_BUTTON_TITLE'),
                                'url' => url('adminSpeedDialList/default/import', array('profile_id' => $model->id)),
                                'linkOptions' => array(
                                    "id" => "importSpeedDialList",
                                    'data-translation-id' => 'SPEED_DIAL_IMPORT_BUTTON_TITLE',
                                ),
                            ),
                            $exportPDFLink,
                        )
                    ),
                ),
            ),
        )
         * 
         */
    )
);

$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('block'=>true, 'fade'=>true, 'closeText'=>'&times;'), // success, info, warning, error or danger
    ),
));

$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
        'id'=>'speed-dial-list-form',
        'enableAjaxValidation'=>true,
        'type'=>'horizontal',
        'action' => $model->isNewRecord() ? array('create') : array("update&id=".$model->id)
    ));
    
echo '<div class="well">';
echo '<h4 data-translation-id="PROFILE_FORM_PROFILE_ATTRIBUTES">'.__('PROFILE_FORM_PROFILE_ATTRIBUTES').'</h4>';
echo $form->textFieldRow($model,'name',array(
        'data-translation-id'=>'SPEED_DIAL_MODEL_NAME'
    ));
echo $form->textFieldRow($model,'descr',array(
        'data-translation-id'=>'SPEED_DIAL_MODEL_DESCR'
    ));
echo '</div>';

echo CHtml::beginForm();



$this->widget('ext.PlGrid.PlGridView', array(
    'id' => 'speed-dial-grid',
    'headerAttribute' => 'name',
    "showNavbar" => false,
    'modalSize' => 'large', // null, 'large', 'small', 'huge'
    'dataProvider' => $dataProvider,
    'summaryText' => false,
    'summaryText' => false,
    'type' => 'striped bordered condensed', //change color from columns in 'white' and 'grey' responsively
    'itemsCssClass' => 'plTable items', //change design of the table, removed search bar
    'selectableRows' => 2,
    'columns' => array(
        array(
            'name' => 'key',
            'htmlOptions' => array('width' => '40'),
            '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' => '{delete}',
            'htmlOptions' => array(
                'data-translation-target' => 'USER_ERROR_MESSAGE_TRANSLATIONS',
                'class' => 'buttonColumnIcons',
            ),
            'buttons' => array(
                'delete' => array(
                    'url' => '',
                    'label' => "",
                    'visible' => 'true', //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();
$this->endWidget();
?>
<style>
    .grid-view {
        padding-top: 0;
    }

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

<script>
    var formHasChanged;
$('#speed-dial-list-form').on('change',function() {
    formHasChanged = true;
});
    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) {
                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;
    });
    
    $(".navbar-inner").on("click", "li.disabled a", function() {
    
        $("#SpeedDialList_name").focus();
        return false;
    });
    
</script>