<?php
$form = $this->beginWidget('ext.PlGrid.PlActiveForm', array(
    'id' => 'licence-form',
    'gridId' => 'licence-grid',
    'displayProgressBar' => false,
    'enableAjaxValidation' => true,
    "onSubmitError" => "function(jqXHR, textStatus, errorThrown) {
        window.location = '/index.php?r=systemLicence';
    }",
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
        ));

if ($licenseIsValid) {

    $radioList = array(
        'selectActivation' => __('LICENSE_MODEL_SELECT_LICENSE_ACTIVATION'),
        'selectUpgrade' => __('LICENSE_MODEL_SELECT_LICENSE_UPGRADE')
    );
    echo '<div class="licenceActivation-modal">';
    echo CHtml::radioButtonList('licenseTypeSelector', 'selectUpgrade', $radioList, array(
        'class' => 'control-label controls',
        'data-translation-target' => 'LICENSE_MODEL_SELECT_TYPE_TRANSLATION',
        'onChange' => 'licenseTypeSwitcher();'
            )
    );
    echo '<div class="controls selectActivation">';
}

$this->renderPartial('_activateLicense', array('model' => $model, 'form' => $form), false, true);

if ($licenseIsValid) {
    echo '</div>';
    echo '<div class="controls selectUpgrade">';
    $this->renderPartial('_activateUpgrade', array('model' => $model, 'form' => $form), false, true);
    echo '</div>';

    echo '</div>';
}

echo '<div id="readPrivacy" onClick="readPrivacy(); return false" >';
echo $form->checkboxRow($model, 'agree_privacy', array(
    'data-translation-id' => 'WIZARD_AGREE_PRIVACY',
    'checked' => $model->agree_privacy
        )
);
echo '</div>';

$this->endWidget();
?>
<script>
    var defaultLicenseKey = '<?php echo PhonalisaLicenseManager::DEFAULT_LICENSE_KEY; ?>';
    licenseTypeSwitcher();

    function licenseTypeSwitcher() {
        if ($('[value="selectActivation"]').is(':checked')) {
            setPlaceholderValue('.selectUpgrade');
            removePlaceholderValue('.selectActivation');

            $('.selectUpgrade').hide();
            $('.selectActivation').show();
        } else {
            setPlaceholderValue('.selectActivation');
            removePlaceholderValue('.selectUpgrade');

            $('.selectActivation').hide();
            $('.selectUpgrade').show();
        }
    }

    function setPlaceholderValue(selector) {
        $(selector + " :input").each(function (i, e) {
            if ($(e).val() === '') {
                $(e).val(defaultLicenseKey);
            }
        });
    }

    function removePlaceholderValue(selector) {
        $(selector + " :input").each(function (i, e) {
            if ($(e).val() === defaultLicenseKey) {
                $(e).val('');
            }
        });
    }

    function readPrivacy() {
        if ($('#Licence_agree_privacy').attr('checked')) {
            $('#Licence_agree_privacy').removeAttr('checked');
        }
        showPrivacy();
    }

    function showPrivacy() {
        var options = {
            confirmMessage: '<?php echo PrivacyProvider::text(); ?>',
            dialogHeader: "<?php echo __('PRIVACY_POLICY_LINK'); ?>",
            confirmText: "<?php echo __('LICENSE_PRIVACY_ACKNOWLEDGE'); ?>",
            cancelText: "<?php echo __('LICENSE_PRIVACY_DENY'); ?>",
            height: 1000,
            width: 1000,
            confirmCmd: function () {
                if ($('#dialog-confirm_confirm').hasClass('disabled')) {
                    return false;
                }
                $('#Licence_agree_privacy').attr('checked', 'checked');
                $('#dialog-confirm.modal-body').dialog("close");
                $('#dialog-confirm.modal-body').remove();
            },
            cancelCmd: function () {
                $('#dialog-confirm.modal-body').dialog("close");
                $('#dialog-confirm.modal-body').remove();
            }
        };

        var plDialog = new $.plDialog(options);
        plDialog.showConfirmDialog();
        changeDialogView();
        switchButtons();
        changeLinkTargets();
    }

    function changeDialogView() {
        $('.dialog-backdrop').css('z-index', 2500);
        $('.ui-dialog').css('z-index', 3000);
        $('#dialog-confirm_confirm').addClass('disabled');
        $('#dialog-confirm_confirm').attr('title', '<?php echo __('WIZARD_AGREE_TEXT_TOOLTIP'); ?>');
        $('#dialog-confirm_confirm').attr('data-toggle', 'tooltip');
        $('[data-toggle="tooltip"]').tooltip();
        $('#dialog-confirm_cancel').attr('style', 'margin-left:5px;');
    }

    function switchButtons() {
        $("#dialog-confirm").scroll(function () {
            if ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight - 40) {
                $('#dialog-confirm_confirm').removeClass('disabled');
                $('#dialog-confirm_confirm').removeAttr('data-original-title');
            }
        });
    }

    function changeLinkTargets() {
        $('#dialog-confirm').find("a").each(function (i, e) {
            if (!e.href.startsWith('mailto:')) {
                $(e).attr("target", "_blank");
            }
        });
    }
</script>