
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Import functions
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//

function ShowImportOptions() {

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

    event.preventDefault();

    AjaxBuildImportMenuItems(event);
}


function AjaxBuildImportMenuItems(event) {

    var params = {
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxBuildImportMenuItems",
        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("AjaxBuildImportMenuItems", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText;
            ToastError("AjaxBuildImportMenuItems", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });

}




/**
 * Upload FormData with Ajax
 * @param {any} formdata
 */
function AjaxUploadImportFile(formdata) {

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

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxUploadImportFile",
        type: "POST",
        data: formdata,
        cache: false,
        contentType: false,
        processData: false,
        success: function (response) {

            ClosePleaseWaitDialog();

            if (response.Success) {

                RefreshSelectedContent();

                ToastMessage("File Import", response.ImportResults);

                // TODO: P7 Select the content page most appropriate for this import (E.g. PROJECTSGRID, TIMELOGGRID)

                if (response.RefreshReportsList) {
                    // Refill the entire reports div
                    SetupReportFilesInReportFilesDiv();
                }

            }
            else {
                ToastError("File Import", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {

            ClosePleaseWaitDialog();

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

}


function FileOpenAndUploadForImport(recordtype, dotextstring) {

    $('#ImportFileControl').attr("accept", dotextstring);

    $('#ImportFileControl').on('change', function () {

        $('#ImportFileControl').off('change');

        // Import into the selected record
        var SelectedTableId = GetSelectedRecordTableId();
        var SelectedRecordId = GetSelectedRecordId();

        // Upload FormData with Ajax
        var formdata = new FormData();
        var ImportFileControl = $('#ImportFileControl').get(0);
        var files = ImportFileControl.files;
        formdata.append(recordtype, files[0]);
        formdata.append("SelectedTableId", SelectedTableId);
        formdata.append("SelectedRecordId", SelectedRecordId);
        AjaxUploadImportFile(formdata);

        // Clear this so the 'change' event is triggered again
        $('#ImportFileControl').val("");
    });

    // Open the File Open dialog
    $('#ImportFileControl').trigger('click');
}



function OnImportProjectTasks() {
    FileOpenAndUploadForImport("stdata.ProjectTaskSet", ".csv,.xlsx");
}

function OnImportTimelogs() {
    FileOpenAndUploadForImport("stdata.TaskSet", ".csv,.xlsx");
}

function OnImportExpenses() {
    FileOpenAndUploadForImport("stdata.ExpenseSet", ".csv,.xlsx");
}

function OnImportUsers() {
    FileOpenAndUploadForImport("stdata.UserSet", ".csv,.xlsx");
}

function OnImportClients() {
    FileOpenAndUploadForImport("stdata.ClientSet", ".csv,.xlsx");
}

function OnImportProjects() {
    FileOpenAndUploadForImport("stdata.ProjectSet", ".csv,.xlsx");
}

function OnImportInventory() {
    FileOpenAndUploadForImport("stdata.InventorySet", ".csv,.xlsx");
}

function OnImportMsProjectXml() {
    FileOpenAndUploadForImport("xml-project", ".xml");
}

function OnImportMsProjectMpp() {
    FileOpenAndUploadForImport("MPP", ".mpp");
}

function OnImportQuickBooksIif() {
    FileOpenAndUploadForImport("IIF", ".iif");
}




