
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Export functions
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//

function download(data, filename, type) {
    var file = new Blob([data], { type: type });
    if (window.navigator.msSaveOrOpenBlob) // IE10+
        window.navigator.msSaveOrOpenBlob(file, filename);
    else { // Others
        var a = document.createElement("a"),
            url = URL.createObjectURL(file);
        a.href = url;
        a.download = filename;
        document.body.appendChild(a);
        a.click();
        setTimeout(function () {
            document.body.removeChild(a);
            window.URL.revokeObjectURL(url);
        }, 0);
    }
}

function ShowExportOptions() {

    if (!event)
        return;
    if (!event.currentTarget)
        return;

    event.preventDefault();

    AjaxBuildExportMenuItems(event);
}


function AjaxBuildExportMenuItems(event) {

    var params = {
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxBuildExportMenuItems",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (response) {
            if (response.Success) {
                if (response.MenuItems) {

                    var menu = new ContextMenu(response.MenuItems);
                    menu.display(event);
                }
            }
            else {
                ToastError("AjaxBuildExportMenuItems", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText;
            ToastError("AjaxBuildExportMenuItems", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });

}

function AjaxExportSelectedContent(ExportType, ExportFileExt) {

    // Export the selected page info
    var ValueString = GetSelectedContentPaneValueString()
    var LayoutString = LoadSelectedPaneLayoutStringFromCookie();;
    var FilterJsonString = LoadSelectedPaneFilterJsonStringFromCookie();
    var SettingsJsonString = LoadSelectedPaneSettingsJsonStringFromCookie();
    var ContentType = GetSelectedContentType();
    var ContentTitle = GetSelectedContentTitle();
    AjaxExportContent(ContentType, ContentTitle, ValueString, LayoutString, FilterJsonString, SettingsJsonString, ExportType, ExportFileExt);
}


function AjaxExportContent(ContentType, ContentTitle, ValueString, LayoutString, FilterJsonString, SettingsJsonString, ExportType, ExportFileExt) {

    var params = {
        "ContentType": ContentType,
        "ContentTitle": ContentTitle,
        "ValueString": ValueString,
        "LayoutString": LayoutString,
        "FilterJsonString": FilterJsonString,
        "SettingsJsonString": SettingsJsonString,
        "ExportType": ExportType,
        "ExportFileExt": ExportFileExt
    };

    // Modeless dialog for lengthly operations
    OpenPleaseWaitDialog("Export");

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxExportContent",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (response) {

            ClosePleaseWaitDialog();

            if (response.Success) {

                // Save the file
                // Convert base64 string to binary data;
                if (response.ExportDataBase64String && response.ExportDataBase64String.length > 0) {
                            // There results were not correct for mpp files (binary)
                            // const bytes = atob(response.ExportDataBase64String);
                            // download(bytes, response.ExportFilename, response.ExportFileExt);
                            //var binary = atob(response.ExportDataBase64String)
                            //var array = new Uint8Array(binary.length)
                            //for (var i = 0; i < binary.length; i++) { array[i] = binary.charCodeAt(i) }
                            //var blob = new Blob([array]);
                    var blob = Base64StringToBlob(response.ExportDataBase64String);
                    download(blob, response.ExportFilename, response.ExportFileExt);
                }
                else {
                    ToastError("Export", "Hmmm, no data was generated for your file: " + response.ExportFilename);
                }

                // Show results
                if (response.ExportResults)
                    ToastMessage("Export", response.ExportResults);
            }
            else {
                ToastError("Export", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {

            ClosePleaseWaitDialog();

            var errormsg = "Error " + xhr.status + ", " + xhr.statusText;
            ToastError("AjaxExportContent", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });

}




function OnExportExcelXlsx() {

    // Export the selected page info
    AjaxExportSelectedContent("Excel"/*ExportType*/, "xlsx"/*ExportFileExt*/);
}

function OnExportExcelCsv() {

    // Export the selected page info
    AjaxExportSelectedContent("Excel"/*ExportType*/, "csv"/*ExportFileExt*/);
}

function OnExportWordDocx() {

    // Export the selected page info
    AjaxExportSelectedContent("Word"/*ExportType*/, "docx"/*ExportFileExt*/);
}

function OnExportTextTxt() {

    // Export the selected page info
    AjaxExportSelectedContent("Text"/*ExportType*/, "txt"/*ExportFileExt*/);
}

function OnExportBrowserHtml() {

    // Export the selected page info
    AjaxExportSelectedContent("Browser"/*ExportType*/, "html"/*ExportFileExt*/);
}

function OnExportQuickBooksTimer() {

    // Export the selected page info
    AjaxExportSelectedContent("QuickBooks Timer"/*ExportType*/, "iif"/*ExportFileExt*/);
}

function OnExportMsProjectXML() {

    // Export the selected page info
    AjaxExportSelectedContent("MS Project"/*ExportType*/, "xml"/*ExportFileExt*/);
}

function OnExportMsProjectMPP() {

    // Export the selected page info
    AjaxExportSelectedContent("MS Project"/*ExportType*/, "mpp"/*ExportFileExt*/);
}





