// ***********************************************************************************
// ** Modal dialog for multiline text                                            *****
// ***********************************************************************************

var bMultilineEditDialogIsOpen = false;
var sLastMultilineEditRecordId = "";
var sLastMultilineEditDbTable = "";
var sLastMultilineEditDbColumn = "";
var sLastMultilineEditProperty = "";

function OpenRecordsetMultilineModalDialog(sRecordId, sDbTable, sDbColumn, sTitle)
{
    // Remember these values to get/set value
    sLastMultilineEditRecordId = sRecordId;
    sLastMultilineEditDbTable = sDbTable;
    sLastMultilineEditDbColumn = sDbColumn;
    sLastMultilineEditProperty = sTitle;

    var nDlgWidth = 1200;
    var nDlgHeight = 800;
    var nWindowWidth = $(window).width();
    var nWindowHeight = $(window).height();
    if (nWindowWidth < nDlgWidth)
        nDlgWidth = nWindowWidth * 0.98;
    if (nWindowHeight < nDlgHeight)
        nDlgHeight = nWindowHeight * 0.95;

    // Open the dialog
    // igniteui.com/help/igdialog-property-reference
    $("#igDialogMultilineEdit").igDialog({ modal: true, headerText: sTitle, width: nDlgWidth, height: nDlgHeight, resizable: true, closeOnEscape: true, openAnimation: "", closeAnimation: "fade", stateChanged: function (event, ui) { OnMultilineEditStateChanged(event, ui); }/*, openAnimation: "fade"*/ });
    $("#igDialogMultilineEdit").igDialog("open");
    CenterDialog("igDialogMultilineEdit");

    // Set up controls
    bMultilineEditDialogIsOpen = true;

    // Clear stale text before loading so the previous value is not briefly visible
    $("#igDialogMultilineTextArea").val("");

    // Load the value by Ajax
    LoadMultilineEditValue();

    // Set focus
    var TextArea = $("#igDialogMultilineTextArea");
    TextArea.focus();
    TextArea[0].focus();


    // OK button click
    $("#igDialogMultilineOKButton").unbind().click(function () {
        // Save
        SaveMultilineEditValue();
        CloseMultilineEditDialog();

        // Show the changes
        RefreshSelectedContent();
    });

    // Cancel button click
    $("#igDialogMultilineCancelButton").unbind().click(function () {
        // Cancel
        CloseMultilineEditDialog();
    });

}

function LoadMultilineEditValue() {

    if (!sLastMultilineEditRecordId)
        return;
    if (!sLastMultilineEditDbTable)
        return;
    if (!sLastMultilineEditDbColumn)
        return;

    // Get the value using Ajax
    // Use a callback function to actually set the value
    AjaxGetContentPropertyValue("stdata.BaseDataSet", sLastMultilineEditDbTable, sLastMultilineEditRecordId, sLastMultilineEditDbColumn, GetContentPropertyValueSuccessCallback);
}

/**
 * Called when the record value has been retrieved by Ajax
 * @param {any} ContentType
 * @param {any} RecordTableName
 * @param {any} RecordId
 * @param {any} DbColumn
 * @param {any} PropertyValue
 */
function GetContentPropertyValueSuccessCallback(ContentType, RecordTableName, RecordId, DbColumn, PropertyValue) {

    var TextAreaObject = $("#igDialogMultilineTextArea");
    if (!TextAreaObject)
        return;
    if (!TextAreaObject.length)
        return;

    // Set the value
    TextAreaObject.val(PropertyValue);

    // Set focus
    TextAreaObject.focus();
    TextAreaObject[0].focus();
}

function SaveMultilineEditValue() {

    if (!sLastMultilineEditRecordId)
        return;
    if (!sLastMultilineEditDbTable)
        return;
    if (!sLastMultilineEditDbColumn)
        return;

    var TextAreaObject = $("#igDialogMultilineTextArea");
    if (!TextAreaObject)
        return;
    if (!TextAreaObject.length)
        return;

    // Set the value using Ajax
    var NewValue = TextAreaObject.val();
    AjaxSetContentPropertyValue(NewValue, sLastMultilineEditProperty, sLastMultilineEditDbColumn, "stdata.BaseDataSet", sLastMultilineEditDbTable, sLastMultilineEditRecordId);
}


function OnMultilineEditStateChanged(event, ui) {

    if (!ui)
        return;
    if (!ui.action)
        return;

    // Close event
    if (ui.action == 'close') {
        sLastMultilineEditRecordId = "";
        sLastMultilineEditDbTable = "";
        sLastMultilineEditDbColumn = "";
        sLastMultilineEditProperty = "";
    }
}



/**
 * Intercepts Tab in the multiline textarea and inserts a literal tab character
 * instead of moving focus to the next element.
 * @param {KeyboardEvent} e
 */
function OnMultilineTextAreaKeyDown(e) {
    if (e.key !== 'Tab')
        return;
    e.preventDefault();
    InsertTextAtCursor(e.target, '\t');
}

/**
 * Inserts sText at the current cursor position (or replaces the current selection)
 * in a textarea element, then places the cursor after the inserted text.
 * @param {HTMLTextAreaElement} textarea
 * @param {string} sText
 */
function InsertTextAtCursor(textarea, sText) {
    var nStart = textarea.selectionStart;
    var nEnd = textarea.selectionEnd;
    textarea.value = textarea.value.substring(0, nStart) + sText + textarea.value.substring(nEnd);
    textarea.selectionStart = textarea.selectionEnd = nStart + sText.length;
}


//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Close the Edit Record dialog
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
function CloseMultilineEditDialog() {
    $("#igDialogMultilineEdit").igDialog("close");
    bMultilineEditDialogIsOpen = false;

    sLastMultilineEditRecordId = "";
    sLastMultilineEditDbTable = "";
    sLastMultilineEditDbColumn = "";
    sLastMultilineEditProperty = "";
}

function IsMultilineEditDialogOpen() {
    return bMultilineEditDialogIsOpen;
}


