<?php
echo CHtml::beginForm();
$this->widget('ext.PlGrid.PlGridView', array(
    'id' => 'enableDNSServer-grid',
    'showNavbar' => true,
    'showAlertBlock' => true,
    'alertAutoHide' => false,
    'hideHeaderButtons' => true,
    'useCustomTitle'=> true,
    'dataProvider' => $checboxDataProvider,
    'headerTitle' => 'DNS-Server',
    'columns' => array(
        array(
            'name' => 'DNS_SERVER_ENABLE_DNS_SERVER',
            'type' => 'raw',
            'value' => array($this, 'renderLabel'),
            'htmlOptions' => array(
                'data-translation-id' => 'DNS_SERVER_ENABLE_DNS_SERVER',
                'width' => '50%'
            ),
            'header' => __('DNS_SERVER_ENABLE_DNS_SERVER_GRID_HEADER'),
            'headerHtmlOptions' => array(
                'data-translation-id' => 'DNS_SERVER_ENABLE_DNS_SERVER_GRID_HEADER',
            )
        ),
        array(
            'name' => 'enableDNSServer',
            'type' => 'raw',
            'header' => '',
            'value' => array($this, 'renderCheckbox')
        ),
    ),
        )
);


$this->widget('ext.PlGrid.PlGridView', array(
    'id' => 'dns-management-grid',
    'showNavbar' => false,
    'dataProvider' => $dataProvider,
    'hideHeader' => false,
    'columns' => array(
        array(
            'name' => 'DNS_SERVER_UPSTREAM_DNS_SERVER',
            'type' => 'raw',
            'value' => array($this, 'renderLabel'),
            'htmlOptions' => array(
                'data-translation-id' => 'DNS_SERVER_UPSTREAM_DNS_SERVER',
                'width' => '50%'
            ),
            'header' => __('DNS_SERVER_MANAGEMENT_GRID_HEADER'),
            'headerHtmlOptions' => array(
                'data-translation-id' => 'DNS_SERVER_MANAGEMENT_GRID_HEADER',
            )
        ),
        array(
            'name' => 'upstreamDNSServer',
            'type' => 'raw',
            'header' => '',
            'value' => array($this, 'renderInput')
        ),
        array(
            'class' => 'ext.PlGrid.PlCButtonColumn',
            'template' => '{delete}',
            'buttons' => array(
                'delete' => array(
                    'url' => '',
                    'label' => "",
                    'click' => 'js:function(){                           
                                $(this).parent().parent().remove();
                        }',
                ),
            ),
            'footer' => '<div style="cursor: pointer;" class="icon-plus addDNSServer"
                                data-toggle="tooltip" title="' . __("GLOBAL_ADD_NEW_LINE") . '"
                                data-translation-id="GLOBAL_ADD_NEW_LINE">
                        </div>',
        )
    ),
        )
);


$this->widget('ext.PlGrid.PlGridView', array(
    'id' => 'dns-zone-grid',
    'showNavbar' => false,
    'dataProvider' => $dataProviderZone,
    'columns' => array(
        array(
            'name' => 'fqdn',
            'header' => __('DNS_ZONE_FQDN'),
            'type' => 'raw',
            'headerHtmlOptions' => array('data-translation-id' => 'DNS_ZONE_FQDN'),
            'value' => array($this, 'renderInput')
        ),
        array(
            'name' => 'ttl',
            'header' => __('DNS_ZONE_TTL'),
            'type' => 'raw',
            'headerHtmlOptions' => array('data-translation-id' => 'DNS_ZONE_TTL'),
            'value' => array($this, 'renderNumberField')
        ),
        array(
            'class' => 'ext.PlGrid.PlCButtonColumn',
            'header' => '&nbsp;',
            'template' => '{dnsRecords}',
            'buttons' => array(
                'dnsRecords' => array(
                    'label' => '<i class="icon-edit"></i>',
                    'imageUrl' => false,
                    'options' => array(
                        'data-translation-id' => 'DNS_ZONE_DNS_RECORD_TOOLTIP',
                        'title' => __('DNS_ZONE_DNS_RECORD_TOOLTIP'),
                    ),
                    'url' => 'Yii::app()->createUrl("networkDNS/default/dnsRecord", array("dnsZone"=>"$data->id"))',
                ),
            )
        ),
        array(
            'class' => 'ext.PlGrid.PlCButtonColumn',
            'template' => '{delete}',
            'buttons' => array(
                'delete' => array(
                    'url' => '',
                    'label' => "",
                    'click' => 'js:function(){                           
                                $(this).parent().parent().remove();
                        }',
                ),
            ),
            'footer' => '<div style="cursor: pointer;" class="icon-plus addDNSRecord"
                                data-toggle="tooltip" title="' . __("GLOBAL_ADD_NEW_LINE") . '"
                                data-translation-id="GLOBAL_ADD_NEW_LINE">
                        </div>',
        )
    ),
        )
);

$this->renderPartial('_saveBtn');
echo CHtml::endForm();
?>
<script>

    $('.addDNSServer').click(function () {
        var rowTemplate = $('#dns-management-grid tbody tr').first();
        addNewLineToGrid('#dns-management-grid', rowTemplate);
    });

    $(document).ready(function () {
        if ($('#DNSZone_0_dnsID').val().length === 0) {
            hideDNSRecordBtnIfNewColumn($('#DNSZone_0_dnsID').parent().parent().find('.buttonColumnIcons > a'));
        }
    });

    $('.addDNSRecord').click(function () {
        var rowTemplate = $('#dns-zone-grid tbody tr').first();
        addNewLineToGrid('#dns-zone-grid', rowTemplate);
        hideDNSRecordBtnIfNewColumn($('.new-item-animated .buttonColumnIcons > a'));
    });

    function hideDNSRecordBtnIfNewColumn(DNSRecordBtns) {
        DNSRecordBtns.each(function (i, e) {
            if (!$(e).hasClass('delete')) {
                e.remove();
            }
        });
    }

    function addNewLineToGrid(gridId, rowTemplate) {
        var index = $(gridId + ' tbody').find('.icon-trash').length;
        $(gridId + ' > table').append(rowTemplate.clone().addClass('new-item-animated'));
        $(gridId + ' tbody tr:last-child').show();
        $(gridId + ' tbody tr:last-child').find(':input').each(function (i, e) {
            e.name = e.name.replace('0', index);
            e.id = e.id.replace('0', index);
            e.value = '';
            if (e.id === 'DNSZone_' + index + '_ttl') {
                e.value = '<?php echo $defaultTtlValue; ?>';
            }
        });
    }
</script>
