
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// File attachments functions
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//


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

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

                RefreshPropertyEditorPartialView();
                RefreshSelectedContent();

                ToastMessage("Attachment", response.AttachFileName);
            }
            else {
                ToastError("Attachment", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText;
            ToastError("AjaxUploadAttachmentFile", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });

}


function FileOpenAndUploadForAttachment(sDbTable, sRecordId) {

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

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

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

        // Upload FormData with Ajax
        var formdata = new FormData();
        var ImportFileControl = $('#ImportFileControl').get(0);
        var files = ImportFileControl.files;
        formdata.append('dbtable', sDbTable);
        formdata.append('recordid', sRecordId);
        formdata.append('binarydata', files[0]);
        AjaxUploadAttachmentFile(formdata);

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

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


function AjaxDeleteAttachment(sDbTable, sRecordId, sAttachment) {

    if (!sDbTable)
        return;
    if (!sRecordId)
        return;
    if (!sAttachment)
        return;

    var params = {
        "DbTable": sDbTable,
        "RecordId": sRecordId,
        "Attachment": sAttachment
    };

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

                RefreshPropertyEditorPartialView();
                RefreshSelectedContent();

                ToastMessage("Deleted", response.AttachFileName);
            }
            else {
                ToastError("Delete", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText + "\r\nText: " + BarcodeText;
            ToastError("AjaxDeleteAttachment", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });
}



function AjaxViewAttachment(sAttachment) {

    if (!sAttachment)
        return;

    var params = {
        "Attachment": sAttachment
    };

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

                if (response.AttachImageBase64String) {
                    OpenViewImageDialog(response.AttachFileName, response.AttachImageBase64String)
                }
            }
            else {
                ToastError("View", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText + "\r\nText: " + BarcodeText;
            ToastError("AjaxViewAttachment", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });
}


function AjaxDownloadAttachment(sAttachment) {

    if (!sAttachment)
        return;

    var params = {
        "Attachment": sAttachment
    };

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

                if (response.AttachImageBase64String) {
                //    const bytes = atob(response.AttachImageBase64String);
                //    download(bytes, response.AttachFileName, response.AttachFileExt);
                    var blob = Base64StringToBlob(response.AttachImageBase64String);
                    download(blob, response.AttachFilename, response.AttachFileExt);
                }
            }
            else {
                ToastError("View", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText + "\r\nAttachment: " + sAttachment;
            ToastError("AjaxDownloadAttachment", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });
}
