var cc = '';

function genFiles(selected, errordel, forceGen, hse) {console.log('merging')

var outputMode = parent.$("input[name='outputMode']:checked").val();
$('#deliveryScreen').css('display', 'none');
var varray = selected.split('|');

if (parent.$('#coverOutput').is(":checked")) {

if (parent.$('#schAttach').is(":checked")) {
$('#loading').css('display', 'block')
$('#statusCount').html('Generating Schedule<br><i class="fad clipboard-list"></i>')
$.post(ROOT_URL+'/functions/generators/genShed.php', {
clientnumber: '<?= $_REQUEST['clientnumber']?>',
tenant:TENANT_URL
}, function(reta) {
$('#statusCount').html('Generating Report Cover<br><i class="fad fa-blanket"></i>')
$.post(ROOT_URL+'/functions/generators/genCover.php', {
items: varray,
clientnumber: '<?= $_REQUEST['clientnumber']?>',
site: '<?= $_REQUEST['site']?>'
}, function(ret) {

genCvr(selected, errordel);

})
$('#string').val(selected)

})
} else {

$('#statusCount').html('Generating Report Cover<br><i class="fad fa-blanket"></i>')
$('#loading').css('display', 'block')
$.post(ROOT_URL+'/functions/generators/genCover.php', {
items: varray,
clientnumber: '<?= $_REQUEST['clientnumber']?>',
site: '<?= $_REQUEST['site']?>',
tenant:TENANT_URL
}, function(ret) {
//debug(ret)
genCvr(selected, errordel);


})
$('#string').val(selected)

}

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

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


} else {
if (parent.$('#schAttach').is(":checked")) {
$('#loading').css('display', 'block')
$('#statusCount').html('Generating Schedule<br><i class="fad clipboard-list"></i>')
$.post(ROOT_URL+'/functions/generators/genShed.php', {
clientnumber: '<?= $_REQUEST['clientnumber']?>',
tenant:TENANT_URL
}, function(reta) { //debug(reta)
$('#statusCount').html('Generating Report Cover<br><i class="fad fa-blanket"></i>')
$.post(ROOT_URL+'/functions/generators/genCover.php', {
items: varray,
clientnumber: '<?= $_REQUEST['clientnumber']?>',
site: '<?= $_REQUEST['site']?>',
tenant:TENANT_URL
}, function(ret) { //debug(ret)
$('#statusCount').text(varray.length + ' Items remaining')
for (var i = 0; i < varray.length; i++) {
  if (varray[i] !="" ) {
  var sarray=varray[i].split('-');
  $.post(ROOT_URL+'/functions/generators/checkPDF.php', {
  clientnumber: '<?= $_REQUEST['clientnumber']?>' ,
  repnumber: sarray[0],
  items: sarray[1],
  form: sarray[2],
  forceGen: forceGen,
  split: "split" ,
  tenant: TENANT_URL,
  filePath:"regen"
  }, function(ret) {//debug(ret)
  var nu=parseInt($('#statusCount').text())
  cc=nu - 1;
  $('#statusCount').html(cc + ' Items remaining<br><i class="fad fa-file-pdf"></i>' );
  if (cc==1) {

  if (outputMode=="zip" ) {
  genZip(selected, errordel);
  }
  if (outputMode=="pdf" ) {
  genPDO(selected, errordel);
  }
  }
  })
  }
  }
  })
  $('#string').val(selected)

  })
  } else {
  $('#statusCount').html('Generating Report Cover<br><i class="fad fa-blanket"></i>')
    $('#loading').css('display', 'block')
    $.post(ROOT_URL+'/functions/generators/genCover.php', {
    items: varray,
    clientnumber: '<?= $_REQUEST['clientnumber']?>',
    site: '<?= $_REQUEST['site']?>',
    tenant: TENANT_URL,
    filePath:"regen"
    }, function(ret) {//debug(ret) //debug cover generation
    $('#statusCount').text(varray.length + ' Items remaining')
    for (var i = 0; i < varray.length; i++) {
      if (varray[i] !="" ) {
      var sarray=varray[i].split('-');
      $.post(ROOT_URL+'/functions/generators/checkPDF.php', {
      clientnumber: '<?= $_REQUEST['clientnumber']?>' ,
      repnumber: sarray[0],
      items: sarray[1],
      form: sarray[2],
      forceGen: forceGen,
      split: "split" ,
      tenant: TENANT_URL,
      filePath:"regen"
      }, function(ret) {//debug(ret) //debug report generation
      var nu=parseInt($('#statusCount').text())
      cc=nu - 1;
      $('#statusCount').html(cc + ' Items remaining<br><i class="fad fa-file-pdf"></i>' );
      if (cc==1) {

      if (outputMode=="zip" ) {
      genZip(selected, errordel);
      }
      if (outputMode=="pdf" ) {
      genPDO(selected, errordel);
      }
      }
      })
      }
      }
      })
      $('#string').val(selected)

      }

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

      setTimeout(function() {
      var defectListElement=document.getElementById('defectList');
      if (defectListElement) {
      console.log("defectList found, calling genHSEZip");
      genHSEZip(selected, errordel); // Call genHSEZip after delay
      } else {
      console.error("defectList not found 2.");
      }
      }, 1400);


      }
      }
      }