function genPDO(selected, errordel) {console.log('pdf')
var output = new Array();
var wsWarnings = [];

function deliveryDetectWrittenSchemes(items, callback) {
$.post(ROOT_URL+'/includes/master/report_functions.php', {
FUNCTION: 'detectPWS',
items: items,
client: '<?= $_REQUEST['clientnumber']?>',
tenant:TENANT_URL,
format: 'json'
}, function(ret) {
var data = (typeof ret === 'object') ? ret : {};
if (typeof ret === 'string') {
try { data = JSON.parse(ret); } catch (e) { data = {files: ret.split('|').filter(Boolean), warnings: []}; }
}
callback(data.files || [], data.warnings || []);
});
}

function deliveryAppendWrittenSchemes(files, warnings) {
wsWarnings = Array.from(new Set((wsWarnings || []).concat(warnings || [])));
$.each(files || [], function(key, value) {
if (value != '') {
const found = output.some(el => el.file === value + '.pdf');
if (!found) {
output.push({
source: 'output/wsfiles/<?= $_REQUEST['clientnumber']?>',
file: value + '.pdf',
type: 'ws'
});
console.log(value + ' added')
}
}
});
}

function deliveryLoadMailerWithWarnings(payload) {
payload.deliveryWarnings = wsWarnings || [];
deliveryLoadMailer(payload);
}

if (parent.$('#schAttach').is(":checked")) {
output.push({
source: 'output/reports/<?= $_REQUEST['clientnumber']?>',
file: '<?= $_REQUEST['clientnumber']?>_schedule.pdf',
type: 'sch'
});
}

var str = deliveryListFromAny(selected);
deliveryAppendCoverAttachments(output);

if (parent.$('#wsAttach').is(":checked") || outputMode == "pdf") {
console.log('ws started')
var x = parent.$('#itemList').text()
console.log(x)
deliveryDetectWrittenSchemes(x, function(files, warnings) {
deliveryAppendWrittenSchemes(files, warnings);


$.each(str, function(key, value) {
if (value === '') return;
var val = value.split('-');
output.push({
source: 'output/reports/<?= $_REQUEST['clientnumber']?>',
file: val[0] + '_' + val[1] + '.pdf',
type: 'pdf'
});
});

$('#delivery').css('display', 'block');
var outputString = JSON.stringify(output);
console.log('1')
deliveryLoadMailerWithWarnings({attachments: output, clientnumber: '<?= $_REQUEST['clientnumber']?>'});
$('#loading').css('display', 'none')

})
} else {
$.each(str, function(key, value) {
if (value === '') return;
var val = value.split('-');
output.push({
source: 'output/reports/<?= $_REQUEST['clientnumber']?>',
file: val[0] + '_' + val[1] + '.pdf',
type: 'pdf'
});
});
$('#delivery').css('display', 'block');
var outputString = JSON.stringify(output);
console.log('2')
deliveryLoadMailerWithWarnings({attachments: output, clientnumber: '<?= $_REQUEST['clientnumber']?>'});

$('#loading').css('display', 'none')


}
}



function genMPDO(hse) {
var output = new Array();
var wsWarnings = [];

function deliveryDetectWrittenSchemes(items, callback) {
$.post(ROOT_URL+'/includes/master/report_functions.php', {
FUNCTION: 'detectPWS',
items: items,
client: '<?= $_REQUEST['clientnumber']?>',
tenant:TENANT_URL,
format: 'json'
}, function(ret) {
var data = (typeof ret === 'object') ? ret : {};
if (typeof ret === 'string') {
try { data = JSON.parse(ret); } catch (e) { data = {files: ret.split('|').filter(Boolean), warnings: []}; }
}
callback(data.files || [], data.warnings || []);
});
}

function deliveryAppendWrittenSchemes(files, warnings) {
wsWarnings = Array.from(new Set((wsWarnings || []).concat(warnings || [])));
$.each(files || [], function(key, value) {
if (value != '') {
const found = output.some(el => el.file === value + '.pdf');
if (!found) {
output.push({
source: 'output/wsfiles/<?= $_REQUEST['clientnumber']?>',
file: value + '.pdf',
type: 'ws'
});
}
}
});
}

function deliveryLoadMailerWithWarnings(payload) {
payload.deliveryWarnings = wsWarnings || [];
deliveryLoadMailer(payload);
}

if (parent.$('#schAttach').is(":checked")) {
output.push({
source: 'output/reports/<?= $_REQUEST['clientnumber']?>',
file: '<?= $_REQUEST['clientnumber']?>_schedule.pdf',
type: 'sch'
});
}

deliveryAppendCoverAttachments(output);


if (parent.$('#wsAttach').is(":checked") || outputMode == "pdf") {
var s = parent.$('#itemList').text()
//genMasterPDF()
deliveryDetectWrittenSchemes(s, function(files, warnings) { //debug(ret)
deliveryAppendWrittenSchemes(files, warnings);

var ml = $('#multiList').text()
var str = deliveryListFromAny(ml);
$.each(str, function(key, value) {
if (value === '') return;

if (value.indexOf('multi') !== -1) {
var found = output.filter(function(item) {
return item.file === value + '.pdf';
});
if (found != "") {} else {
output.push({
source: 'output/reports/<?= $_REQUEST['clientnumber']?>',
file: value + '.pdf',
type: 'mpdf'
});
}
} else {
var val = value.split('-');
var found = output.filter(function(item) {
return item.file === val[0] + '_' + val[1] + '.pdf';
});
if (found != "") {} else {
output.push({
source: 'output/reports/<?= $_REQUEST['clientnumber']?>',
file: val[0] + '_' + val[1] + '.pdf',
type: 'pdf'
});
}
}
});


$('#delivery').css('display', 'block');
var outputString = JSON.stringify(output);
console.log('3')
deliveryLoadMailerWithWarnings({attachments: output, clientnumber: '<?= $_REQUEST['clientnumber']?>'});

$('#loading').css('display', 'none')




})
} else {
var ml = $('#multiList').text()
var str = deliveryListFromAny(ml);
$.each(str, function(key, value) {
if (value === '') return;

if (value.indexOf('multi') !== -1) {
var found = output.filter(function(item) {
return item.file === value + '.pdf';
});
if (found != "") {} else {
output.push({
source: 'output/reports/<?= $_REQUEST['clientnumber']?>',
file: value + '.pdf',
type: 'mpdf'
});
}
} else {
var val = value.split('-');
var found = output.filter(function(item) {
return item.file === val[0] + '_' + val[1] + '.pdf';
});
if (found != "") {} else {
output.push({
source: 'output/reports/<?= $_REQUEST['clientnumber']?>',
file: val[0] + '_' + val[1] + '.pdf',
type: 'pdf'
});
}
}
});


$('#delivery').css('display', 'block');
var outputString = JSON.stringify(output);
console.log('4')
deliveryLoadMailerWithWarnings({attachments: output, clientnumber: '<?= $_REQUEST['clientnumber']?>'});

$('#loading').css('display', 'none')


}
if (hse === '') {}
else {

setTimeout(function() {
var defectListElement = document.getElementById('defectList');
if (defectListElement) {
console.log("defectList found, calling genHSEZip");
genHSEZip(parent.$('#itemList').text(), errordel); // Call genHSEZip after delay
} else {
console.error("defectList not found.");
}
}, 300);


}
}
