<?php
$form = $this->beginWidget('ext.PlGrid.PlActiveForm', array(
    'id' => 'credential-iCloud',
    'gridId' => 'third-party-credentials-grid',
    'action' => $model->isNewRecord ? array('create') : array()
        ));

echo $form->dropDownListRow(
        $model, 'provider', $model->enabledProviders(), array(
    'data-translation-id' => 'THIRDPARTYCREDENTIAL_MODEL_PROVIDER',
    'onchange' => 'ajaxRenderForm()'));
echo $form->textFieldRow($model, 'title', array('data-translation-id' => 'THIRDPARTYCREDENTIAL_MODEL_TITLE'));
echo $form->textFieldRow($model, 'username', array('data-translation-id' => 'THIRDPARTYCREDENTIAL_MODEL_USERNAME'));
echo $form->passwordFieldRow($model, 'password', array('data-translation-id' => 'THIRDPARTYCREDENTIAL_MODEL_PASSWORD'));
echo $form->textFieldRow($model, 'serverURI', array('data-translation-id' => 'THIRDPARTYCREDENTIAL_MODEL_SERVERURI', 'readonly' => true));
echo $form->textFieldRow($model, 'addressBookURI', array('data-translation-id' => 'THIRDPARTYCREDENTIAL_MODEL_SERVERURI', 'readonly' => true));
echo $form->textFieldRow($model, 'iCloudCalendars', array('data-translation-id' => 'THIRDPARTYCREDENTIAL_MODEL_SERVERURI', 'readonly' => true));
echo "<div id='password-info'class='alert alert-info' data-translation-id='THIRDPARTYCREDENTIAL_MODEL_PASSWORD_INFO'>" . __("THIRDPARTYCREDENTIAL_MODEL_PASSWORD_INFO") . "</div>";

echo $form->errorSummary($model);

$this->endWidget();
?>
<script>
    $('#third-party-credentials-grid_Modal').removeClass('modal-large');
    $('#ThirdPartyCredential_addressBookURI').closest(".control-group").hide();
    $('#ThirdPartyCredential_iCloudCalendars').closest(".control-group").hide();
    $('#ThirdPartyCredential_serverURI').closest(".control-group").hide();
    $('#credential-iCloud').submit(validateIDandPassword);
    $('#calendar-grid').hide();

    function ajaxRenderForm() {
        $.ajax({
            url: "<?php echo url('phonesettingsThirdPartyCredentials/default/ajaxRenderModal'); ?>",
            type: "GET",
            dataType: "html",
            data: {provider: $('#ThirdPartyCredential_provider').val()},
            success: function (result) {
                $('#credential-iCloud').replaceWith(result);
            }
        });
    }

    function validateIDandPassword() {
        var copyThis = $(this);
        if (copyThis.data('requestRunning')) {
            return;
        }

        $.ajax({
            url: "<?php echo url('phonesettingsThirdPartyCredentials/default/validateICloudCredentials'); ?>",
            type: "POST",
            async: false,
            dataType: "json",
            data: {username: $('#ThirdPartyCredential_username').val(), password: $('#ThirdPartyCredential_password').val()},
            success: function (result) {
                if (result !== "false") {
                    copyThis.data('requestRunning', true);
                    $('#ThirdPartyCredential_serverURI').val(result);
                    $('#ThirdPartyCredential_addressBookURI').val($('#ThirdPartyCredential_serverURI').val());
//                    getCalendars();
                    renderTable();
                }
                return false;
            }
        });
    }

//    function getCalendars() {
//        $('#ThirdPartyCredential_addressBookURI').val($('#ThirdPartyCredential_serverURI').val());
//        $.ajax({
//            url: "<?php // echo url('phonesettingsThirdPartyCredentials/default/getICloudCalendars') ?>",
//            type: "POST",
//            async: false,
//            dataType: "json",
//            data: {username: $('#ThirdPartyCredential_username').val(), password: $('#ThirdPartyCredential_password').val()},
//            success: function (result) {
//                if (result !== "false") {
////                    iCloudCalendars = JSON.stringify(result);
////                    $('#ThirdPartyCredential_iCloudCalendars').val(iCloudCalendars);
//                    renderTable();
//                }
//                return false;
//            }
//        });
//    }

    function renderTable() {
        $.ajax({
            url: "<?php echo url('phonesettingsThirdPartyCredentials/default/renderTable'); ?>",
            type: "POST",
            data: {
                title: $('#ThirdPartyCredential_title').val(),
                username: $('#ThirdPartyCredential_username').val(),
                password: $('#ThirdPartyCredential_password').val(),
                addressBookURI: $('#ThirdPartyCredential_addressBookURI').val(),
                iCloudCalendars: $('#ThirdPartyCredential_iCloudCalendars').val(),
                scenario: $('#ThirdPartyCredential_provider').val()
            },
            success: function (result) {
                $('#credential-iCloud').replaceWith(result);
            }
        });
    }
</script>