<?php
$this->widget('ext.PlGrid.PlGridView', array(
    'id' => 'cert-grid',
    'itemsCssClass' => 'plTable items',
    'headerTitle' => 'NETWORK_CERTIFICATE_SETTINGS_TITLE',
    'addButtonTitle' => 'NETWORK_CERTIFICATE_SETTINGS_CREATE',
    'modalCreateHeader' => 'NETWORK_CERTIFICATE_SETTINGS_CREATE',
    'modalUpdateHeader' => 'NETWORK_CERTIFICATE_SETTINGS_UPDATE',
    'showAlertBlock' => true,
    'alertAutoHide' => false,
    'summaryText' => false,
    'enableSearchButton' => false,
    'useCustomTitle' => true,
    'dataProvider' => $model->search(),
    'modalSize' => 'large', // null, 'large', 'small', 'huge'
    'customButtons' =>
    array(
        array(
            'label' => __('NETWORK_CERTIFICATE_SETTINGS_UPLOAD_CERT'),
            'icon' => 'icon-upload',
            'url' => '#',
            'linkOptions' => array(
                'data-translation-id' => 'NETWORK_CERTIFICATE_SETTINGS_UPLOAD_CERT',
                'id' => 'uploadCert',
                'onClick' => 'openUploadCertModal()'
            ),
        ),
        array(
            'label' => __('NETWORK_CERTIFICATE_SETTINGS_CA'),
            'icon' => 'file-text',
            'url' => '#',
            'position' => 'right',
            'linkOptions' => array(
                'data-translation-id' => 'NETWORK_CERTIFICATE_SETTINGS_CA',
            ),
            'items' => array(
                array(
                    'label' => __('NETWORK_CERTIFICATE_SETTINGS_CA_HEADER'),
                    'icon' => 'file-text',
                    'url' => '#',
                    'linkOptions' => array(
                        'id' => 'setCA',
                        'onClick' => 'openCaModal();'
                    ),
                ),
                array(
                    'label' => __('GLOBAL_DOWNLOAD_BUTTON'),
                    'icon' => 'icon-download',
                    'url' => url('networkCertificateSettings/default/downloadCA'),
                )
            ),
        )
    ),
    'columns' => array(
        array(
            'name' => 'countryName',
            'header' => __('NETWORK_CERTIFICATE_SETTINGS_COUNTRY_NAME'),
            'headerHtmlOptions' => array('data-translation-id' => 'NETWORK_CERTIFICATE_SETTINGS_COUNTRY_NAME'),
        ),
        array(
            'name' => 'stateName',
            'header' => __('NETWORK_CERTIFICATE_SETTINGS_STATE_NAME'),
            'headerHtmlOptions' => array('data-translation-id' => 'NETWORK_CERTIFICATE_SETTINGS_STATE_NAME'),
        ),
        array(
            'name' => 'cityName',
            'header' => __('NETWORK_CERTIFICATE_SETTINGS_CITY_NAME'),
            'headerHtmlOptions' => array('data-translation-id' => 'NETWORK_CERTIFICATE_SETTINGS_CITY_NAME'),
        ),
        array(
            'name' => 'companyName',
            'header' => __('NETWORK_CERTIFICATE_SETTINGS_COMPANY_NAME'),
            'headerHtmlOptions' => array('data-translation-id' => 'NETWORK_CERTIFICATE_SETTINGS_COMPANY_NAME'),
        ),
        array(
            'name' => 'orgUnitName',
            'header' => __('NETWORK_CERTIFICATE_SETTINGS_ORG_UNIT_NAME'),
            'headerHtmlOptions' => array('data-translation-id' => 'NETWORK_CERTIFICATE_SETTINGS_ORG_UNIT_NAME'),
        ),
        array(
            'name' => 'commonName',
            'header' => __('NETWORK_CERTIFICATE_SETTINGS_COMMON_NAME'),
            'headerHtmlOptions' => array('data-translation-id' => 'NETWORK_CERTIFICATE_SETTINGS_COMMON_NAME'),
        ),
        array(
            'name' => 'eMailAddress',
            'header' => __('NETWORK_CERTIFICATE_SETTINGS_EMAIL_ADDRESS'),
            'headerHtmlOptions' => array('data-translation-id' => 'NETWORK_CERTIFICATE_SETTINGS_EMAIL_ADDRESS'),
        ),
        array(
            'class' => 'ext.PlGrid.PlCButtonColumn',
            'template' => '{download}{ajaxUpdate}{delete}',
            'buttons' => array(
                'download' => array(
                    'label' => __('GLOBAL_DOWNLOAD_BUTTON'),
                    'icon' => 'icon-download',
                    'url' => 'Yii::app()->createUrl("networkCertificateSettings/default/downloadCert", array("id"=>"$data->id"))',
                )
            ),
        )
    ),
        )
);

if (Yii::app()->user->translateMode) {
    $this->renderPartial('_translationTags');
}
?>
<script>
    function openUploadCertModal() {
        var headerTxt = '<?php echo __('NETWORK_CERTIFICATE_SETTINGS_UPLOAD_CERT'); ?>';
        var translationTag = 'NETWORK_CERTIFICATE_SETTINGS_UPLOAD_CERT';
        var url = '<?php echo url('networkCertificateSettings/default/uploadCert'); ?>';
        return openModal(url, headerTxt, translationTag);
    }

    function openCaModal() {
        var headerTxt = '<?php echo __('NETWORK_CERTIFICATE_SETTINGS_CA_HEADER'); ?>';
        var translationTag = 'NETWORK_CERTIFICATE_SETTINGS_CA_HEADER';
        var url = '<?php echo url('networkCertificateSettings/default/createCA'); ?>';
        return openModal(url, headerTxt, translationTag);
    }

    function openModal(url, headerTxt, translationTag) {
        $('#cert-grid_Modal .modal-header h4').html(headerTxt);
        $('#cert-grid_Modal .modal-header h4').attr('data-translation-id', translationTag);
        $('#cert-grid_Modal_saveBtn').button("reset");
        $('#cert-grid_Modal_deleteBtn').hide();
        $.ajax({
            type: "GET",
            dataType: "html",
            url: url
        }).success(function (data) {
            $('#cert-grid_Modal .modal-body').html(data);
            $('#cert-grid_Modal').modal('show');
            $(".modal-body").find("input[type='password'], input[type='text']").first().focus();
            contextHelper.afterTranslate = function () {
                contextHelper.triggerContextHelp("#CertificateAuthority_passphrase");
            };
        });
    }

    function writeDataToCtrld(urlScenario, formId, id) {
        var urlAppend = '';
        if (typeof id !== 'undefined') {
            urlAppend = '&id=' + id;
        }

        var formData = $(formId).serialize();
        var url = '<?php echo url('networkCertificateSettings/default'); ?>' + '/' + urlScenario + urlAppend;
        $.ajax({
            type: "POST",
            dataType: "json",
            url: url,
            data: formData
        }).success(function (data) {
            if (data.status === "error") {
                var alertBlockContent = $('.alert-block').find('ul');
                $.each(data.errorMsg, function (i, e) {
                    alertBlockContent.append('<li>' + i + ': ' + e + '</li>');
                });
                $('.alert-block').show();
                $('#cert-grid_Modal_saveBtn').button("reset");
                return false;
            }
            if (data.status === 'ok') {
                $('#cert-grid_Modal').modal("hide");
                location.reload();
                return true;
            }
        });
    }
</script>
