var listChains = new Array();

function splitter(m) {console.log('split')
$('#deliveryScreen').css('display', 'none');
$('#statusCount').html('Generating Report Covers and collating reports<br><i class="fad fa-blanket"></i>')
$('#loading').css('display', 'block')
var itemlist = '';
var str = '';
var x = '';
var y = '';

var pass = '';
var selected = '';

var selectedChains = $('#chainedSelects').text()
var outputMode = $("input[name='outputMode']:checked").val();
if (parent.$('#schAttach').is(":checked")) {
var schAttach = "attach";
}
if (parent.$('#wsAttach').is(":checked")) {
var wsAttach = "attach";
}

//DETECT CHAINS AND CREATE ARRAYS
$('input[type=checkbox]').each(function() {
if ($(this).is(":checked")) {
if ($(this).attr('name') == null) {} else {
itemlist = itemlist + ($(this).attr('name') + '_' + $(this).attr('data-link') + '|');
var link = $(this).attr('data-link')
if (link === undefined) {} else {
const {
length
} = listChains;
const id = length + 1;
const found = listChains.some(el => el === link);
if (!found) {
listChains.push(link);
}
}
}
}
})


//RUN COLLATION AS MULTI
//if(m=="multi"){
//var chainset=JSON.stringify(listChains)
//$.post('functions/deliveryModules/splitMultiGen.php',{clientnumber:'<?= $_REQUEST['clientnumber']?>',chainlist:chainset,items:itemlist,mode:outputMode,wsMode:wsAttach},function(ret){
//$('#delivery').css('display','block');
//var outputString=ret;
//$('#mailerContainer').load('includes/master/deliveryMailer.php?attachments='+outputString+'&clientnumber=<?= $_REQUEST['clientnumber']?>')
//$('#loading').css('display','none')
//ret=$.trim(ret);
//debug(ret)
//var list=ret.split('|');
//listChains.push('CA');
//var list=ret.split('|');
//var mList=list[0];
//var sList=list[1];
//mList=mList.split(',')

//$.each(listChains,function(key,chain){
//cArray=new Array;
//var iType='';
//$.each(mList,function(item,value){
//cItem=value.split('_');
//var iChain=cItem[1];
//var iItem=cItem[0];
//var ctype=iItem.split('-');
//if(ctype[2]!="undefined"){
//iType=ctype[2]; console.log(iType)
//}
//if(iChain==chain){cArray.push(iItem)}
//})
//var op=cArray.toString()
//console.log(op+'-'+iType)
//})


//})

//}

//RUN COLLATION AS SINGLES
//else{
var chainset = JSON.stringify(listChains)

$.post('functions/deliveryModules/splitGen.php', {
clientnumber: '<?= $_REQUEST['clientnumber']?>',
chainlist: chainset,
items: itemlist,
mode: outputMode,
wsMode: wsAttach,
schMode: schAttach
}, function(ret) {
$('#delivery').css('display', 'block');
var outputString = ret;
if (outputMode == "pdf") {

$('#mailerContainer').load('includes/master/deliveryMailer.php?attachments=' + outputString + '&clientnumber=<?= $_REQUEST['clientnumber']?>')
}
if (outputMode == "zip") {

$('#mailerContainer').load('includes/master/deliveryMailer.php?zipMode=split&zips=' + outputString + '&clientnumber=<?= $_REQUEST['clientnumber']?>')
}
$('#loading').css('display', 'none')
})
//}

}