fleet-contractor/Site/Maintenance/js/maintenancelogattachment.js
2023-04-28 12:22:26 +08:00

109 lines
4.2 KiB
JavaScript

function UpLoadMaintenanceLogAttachment() {
var file = $('<input type="file" style="display: none;" multiple="multiple" />');
file.change(function () {
var files = this.files;
for (var i = 0; i < files.length; i++) {
if (files[i].size == 0) {
alert(GetTextByKey("P_MRM_ATTACHMENTSTIPS", "Attachment size is 0kb, uploading failed."));
return false;
}
if (files[i].size > 50 * 1024 * 1024) {
alert(GetTextByKey("P_MRM_ATTACHMENTSTIPS1", "Attachment is too large. Maximum file size is 50 MB."));
return false;
}
}
if (!maintenanceid) {
OnSave(0, function () {
SaveMaintenanceLogAttachmentFiles(files);
});
return;
}
else {
SaveMaintenanceLogAttachmentFiles(files);
}
}).click();
}
function SaveMaintenanceLogAttachmentFiles(files) {
for (var i = 0; i < files.length; i++) {
var iconfile = files[i];
ChangeMaintenanceLogAttachmentFile(iconfile);
}
}
function ChangeMaintenanceLogAttachmentFile(file) {
var formData = new FormData();
formData.append("iconFile", file);
formData.append("MethodName", "AddAttachment");
formData.append("ClientData", maintenanceid);
$.ajax({
url: 'AddMaintenanceRecord.aspx',
type: 'POST',
dataType: 'json',
processData: false,
contentType: false,
data: formData,
async: true,
success: function (data) {
if (data !== 'OK') {
showAlert(data, GetTextByKey("P_MRM_ATTACHMENTS", 'Attachment File'));
} else {
getMaintenanceLogAttachments();
}
},
error: function (err) {
showAlert(err.statusText, GetTextByKey("P_MRM_ATTACHMENTS", 'Attachment File'));
}
});
}
function deleteAttachment(attID) {
if (confirm(GetTextByKey("P_MRM_DELETEATTACHMENTTIPS", "Are you sure you want to delete the attachment?"))) {
maintenancerequest("DeleteAttachment", attID, function (data) {
if (data !== 'OK') {
showAlert(data, GetTextByKey("P_MRM_DELETEATTACHMENT", 'Delete Attachment'));
}
else
getMaintenanceLogAttachments();
}, function (err) {
showAlert(GetTextByKey("P_MRM_FAILEDDELETEATTACHMENT",'Failed to delete this attachment.'), GetTextByKey("P_MRM_DELETEATTACHMENT", 'Delete Attachment'));
});
}
}
function getMaintenanceLogAttachments() {
if (maintenanceid) {
$('#tbAttas').empty();
maintenancerequest('GetAttachments', maintenanceid, function (data) {
if (data && data.length > 0) {
$('#tabAttachments').show();
var flist = data;
for (var i = 0; i < flist.length; i++) {
var trrecoard = $('<tr></tr>').attr('id', flist[i].ID);
var tdfile = $('<td style=""></td>');
var filename = $("<label style='border-bottom: 1px solid RGB(30, 144, 255);color: RGB(30,144,255);cursor:pointer;'></label>").text(flist[i].FileName).click(function () {
window.open("../filesvc.ashx?attchid=" + this.parentElement.parentElement.id + "&sourceType=workorderattachment");
});
var span = $('<span></span>').text(flist[i].AddedByUserName).css("margin-right", 5);
var spanby = $('<span></span>').text(flist[i].AddedOnStr);
tdfile.append(filename, span, spanby);
if (isshowdelete) {
var tdimg = $('<td style="width:18px;border:none;text-align:center;"></td>');
var imgDom = $('<span class="sbutton icondelete" style="padding:0;" ></span>').click(function () {
deleteAttachment(this.parentElement.parentElement.id);
});
tdimg.append(imgDom);
trrecoard.append(tdimg);
}
trrecoard.append(tdfile).appendTo($('#tbAttas'));
}
}
});
}
}