<?php
Yii::app()->clientScript->registerCoreScript('jquery.ui');
cs()->registerScriptFile(Yii::app()->baseUrl . '/js/plDialog/plDialog.js', CClientScript::POS_HEAD);
$this->widget('ext.PlNavbar', array(
    'brand' => __('ADMIN_LDAP_INTERFACE_SECTION_TITLE'),
    'helpBtnUrl' => "adminLdapInterface-view",
    'brandUrl' => url('adminLdapInterface'),
    'htmlOptions' => array(
        'data-translation-id' => 'ADMIN_LDAP_INTERFACE_SECTION_TITLE',
    ),
    'items' => array(
        array(
            'class' => 'bootstrap.widgets.TbMenu',
            'items' => array(
                '---',
                array(
                    'label' => __('GLOBAL_BACK'),
                    'url' => url("adminLdapInterface/default/index"),
                    'icon' => 'arrow-left',
                    'linkOptions' => array(
                        'id' => 'back_button',
                        'data-target' => '#',
                        'data-translation-id' => 'GLOBAL_BACK'
                    ),
                ),
            ),
        ),
    )
));

$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
    'id' => 'ldap-import-form',
    'enableAjaxValidation' => true,
    'enableClientValidation' => true,
    'action' => $model->isNewRecord ? array('create') : array()
        ));
echo $form->errorSummary($model);
echo $form->textFieldRow(
        $model, 'server', array(
    'class' => 'span3',
    'data-translation-id' => 'ADMIN_LDAP_INTERFACE_SERVER'
        )
);
echo $form->textFieldRow(
        $model, 'username', array(
    'class' => 'span3',
    'data-translation-id' => 'ADMIN_LDAP_INTERFACE_USERNAME'
        )
);
echo $form->passwordFieldRow(
        $model, 'password', array(
    'class' => 'span3',
    'data-translation-id' => 'ADMIN_LDAP_INTERFACE_PASSWORD'
        )
);

echo $form->dropDownListRow(
        $model, 'connectedModel', $model->getAvailableModelTypes(), array(
    'class' => 'span3',
    'data-translation-id' => 'ADMIN_LDAP_INTERFACE_CONNECTED_MODEL',
    'onchange' => 'changeDropdownVisibility()'
        )
);

echo $form->dropDownListRow(
        $model, 'addressBookID', $availableAddressBooks, array(
    'class' => 'span3',
    'data-translation-id' => 'ADMIN_LDAP_INTERFACE_ADDRESS_BOOK_ID'
        )
);

echo $form->textFieldRow(
        $model, 'baseDN', array(
    'class' => 'span3',
    'data-translation-id' => 'ADMIN_LDAP_INTERFACE_BASE_DN',
        )
);
echo $form->textFieldRow(
        $model, 'filter', array(
    'class' => 'span3',
    'data-translation-id' => 'ADMIN_LDAP_INTERFACE_FILTER',
        )
);

echo $form->checkBoxRow($model, 'cronJobEnabled', array(
    'data-translation-id' => 'ADMIN_LDAP_INTERFACE_LDAP_CRON_JOB_ENABLED',
    'onchange' => 'changeTextFieldVisibility()'
));
echo $form->textFieldRow(
        $model, 'cronRule', array(
    'class' => 'span3',
    'type' => "hidden",
    'data-translation-id' => 'ADMIN_LDAP_INTERFACE_LDAP_CRON_RULE',
        )
);
echo '<br>';
$this->widget('bootstrap.widgets.TbButton', array(
    'buttonType' => 'button',
    'type' => 'primary',
    'label' => __('ADMIN_LDAP_INTERFACE_SELECT_ATTRIBUTES_BTN'),
    'htmlOptions' => array(
        'id' => 'LdapInterfaceAssignBtn',
        'data-translation-id' => 'ADMIN_LDAP_INTERFACE_SELECT_ATTRIBUTES_BTN',
        'onclick' => "showWarningIfFilterChanged()"
    )
));
echo CHtml::submitButton('save', array('id' => 'hidenLdapInterfaceAssignBtn', 'style' => 'display:none'));

$this->endWidget();
?>
<style>
    #LdapInterfaceAssignBtn{
        margin-right:20px;
        margin-top:20px;
    }
</style>
<script>
    changeDropdownVisibility();
    changeTextFieldVisibility();

    function changeTextFieldVisibility() {
        if ($('#LdapInterface_cronJobEnabled').prop("checked")) {
            hideDropdown('LdapInterface_cronRule');
        } else {
            showDropdown('LdapInterface_cronRule');
        }
    }

    function changeDropdownVisibility() {
        var addressBookTag = '<?php echo __('ADMIN_LDAP_INTERFACE_ADDRESSES_MODEL'); ?>';
        if ($('#LdapInterface_connectedModel').find('option:selected').text() === addressBookTag) {
            addRequiredAttribute();
            hideDropdown('LdapInterface_addressBookID');
        } else {
            removeRequiredAttribute();
            showDropdown('LdapInterface_addressBookID');
        }
    }

    function hideDropdown(id) {
        $('#' + id).prop("disabled", false);
        $("label[for='" + id + "']").show();
        $('#' + id).show();
    }

    function addRequiredAttribute() {
        if (!$("label[for='LdapInterface_addressBookID']").hasClass('required')) {
            $("label[for='LdapInterface_addressBookID']").attr('required', true);
            $("label[for='LdapInterface_addressBookID']").append("<span id=LdapInterface_addressBookID_reqired class='required'> *</span>");
        }
    }

    function showDropdown(id) {
        $('#' + id).prop("disabled", true);
        $("label[for='" + id + "']").hide();
        $('#' + id).hide();
    }

    function removeRequiredAttribute() {
        $("label[for='LdapInterface_addressBookID']").removeAttr('required');
        $('#LdapInterface_addressBookID_reqired').remove();
    }

    function showWarningIfFilterChanged() {
        var oldFilter = '<?php echo $model->filter; ?>';
        $("#LdapInterfaceAssignBtn").button("loading");
        if (oldFilter !== $('#LdapInterface_filter').val()) {
            createFilterChangedDialog();
        } else {
            $('#hidenLdapInterfaceAssignBtn').click();
        }
    }

    function createFilterChangedDialog() {
        message = "<?php echo __('ADMIN_LDAP_INTERFACE_FILTER_CHANGED_MESSAGE'); ?>";
        var options = getDialogOptions(message);
        var plDialog = new $.plDialog(options);
        plDialog.showConfirmDialog();
    }

    function getDialogOptions(message) {
        return {
            confirmMessage: "<div id='connected-account-warning'class='alert alert-info'>" + message + "<div><br />",
            dialogHeader: "<?php echo __('ADMIN_LDAP_INTERFACE_FILTER_DIALOGE_HEADER'); ?>",
            confirmText: "<?php echo __('GLOBAL_CONTINUE_BUTTON'); ?>",
            cancelText: "<?php echo __('GLOBAL_CANCEL_BUTTON'); ?>",
            height: 'auto',
            width: '700',
            position: ['center', 200],
            confirmCmd: function () {
                $('#dialog-confirm.modal-body').dialog("close");
                $('#hidenLdapInterfaceAssignBtn').click();
            },
            cancelCmd: function () {
                $('#dialog-confirm.modal-body').dialog("close");
                $("#LdapInterfaceAssignBtn").button("reset");
            }
        };
    }
</script>