<?php require_once('../../includes/connection.php');
$clientnumber='';
$chains='';
$items='';
$itemlist='';
$link='';
$arrays=array();
$arrays['catchall']=array();
$clientnumber=$_REQUEST['clientnumber'];
$chains=json_decode($_REQUEST['chainlist']);
$items=$_REQUEST['items'];
$itemlist=explode("|",$items);
$outputmode=$_REQUEST['mode'];
foreach($chains as $chain){
$link=$chain;
$arrays[$link]=array();
foreach($itemlist as $item){
$itemBreakdown=explode("_",$item);
$itemData=$itemBreakdown[0];
$itemLink=$itemBreakdown[1];
if($itemLink==$chain){
    if(!in_array($itemData,$arrays[$link])){$arrays[$link][]=$itemData;}
}
if($itemLink=='undefined'){
    if(!in_array($itemData,$arrays['catchall'])){$arrays['catchall'][]=$itemData;}
}
}
}

$output = array();
$outputZip = array();

// REPORT COVER GENERATION

$_REQUEST['mode']="split";
$split='../';
$_REQUEST['src']='ext4'; require_once('../global.php'); require_once('../../includes/master/form_functions.php');
$pmode=7;
if($pmode==6){include($split.'../packages/mpd/mpdf.php');}
if($pmode==7){require_once $split.'../packages/mpd/vendor/autoload.php';}

function getPssrNo($itemnumber){
$stmt = $GLOBALS['con']->prepare("SELECT `wsnumber` FROM `wsitems` WHERE `itemnumber`=?")or die($error=mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$itemnumber);$stmt->execute();$res=$stmt->get_result();$stmt->close();
$d=mysqli_fetch_array($res);
if(empty($d['wsnumber'])){return 'None found';}
else{return $d['wsnumber'];}
}

foreach($arrays as $name=>$array){
$_REQUEST['items']='';
$_REQUEST['items']=$array;
include('../genCover.php');
$coverData =  array (
'source' => 'output/reports/'.$_REQUEST['clientnumber'],
'file' => 'report_cover_'.$name.'.pdf',
'type' => 'rc'
);
array_push($output,$coverData );


// SCHEDULE CHECK AND ATTACH

if($_REQUEST['schMode']=="attach"){
    include('../genShed.php');
    $schedData =  array (
    'source' => 'output/reports/'.$_REQUEST['clientnumber'],
    'file' => $_REQUEST['clientnumber'].'_schedule.pdf',
    'type' => 'sch'
    );
    if(!in_array($schedData,$output)){array_push($output,$schedData );}
    if(!in_array($schedData,$outputZip)){array_push($outputZip,$schedData );}

}

// WS CHECK AND ATTACH

if($_REQUEST['wsMode']=="attach"){
    foreach($array as $value){
    $item= explode("-",$value); 
    $pssr=getPssrNo($item[1]);
    if($pssr=="None found"){}
    else{
    if(file_exists('../../output/wsfiles/'.$_REQUEST['clientnumber'].'/WS'.$pssr.'.pdf')){  
    $pssrData =  array (
    'source' => 'output/wsfiles/'.$_REQUEST['clientnumber'],
    'file' => 'WS'.$pssr.'.pdf',
    'type' => 'ws'
    );
    if(!in_array($pssrData,$output)){array_push($output,$pssrData );}
  
    else{}
    }
    }
    }
    }

//FILE ATTACHMENT

foreach($array as $value){
$v=explode("-",$value);
$file=$v[0]."_".$v[1].".pdf"; 
if(file_exists('../../output/reports/'.$_REQUEST['clientnumber'].'/'.$file)){
$itemData =  array (
'source' => 'output/reports/'.$_REQUEST['clientnumber'],
'file' => $file,
'type' => 'pdf'
);
array_push($output,$itemData );
}
}



if($outputmode=="pdf"){array_push($output,$itemData );}

if($outputmode=="zip"){
$path='../../output/reports/'.$_REQUEST['clientnumber'].'/';
$wspath='../../output/wsfiles/'.$_REQUEST['clientnumber'].'/';

$theZip=$_REQUEST['clientnumber'].'_'.$name.'.zip';
$zip = new ZipArchive;
if ($zip->open($path.$theZip, ZipArchive::CREATE) === TRUE)
{
$zip->addFile($path.'report_cover_'.$name.'.pdf','report_cover_'.$name.'.pdf');


if($_REQUEST['wsMode']=="attach"){
foreach($array as $value){
$item= explode("-",$value); 
$pssr=getPssrNo($item[1]);
if($pssr=="None found"){}
else{
if(file_exists('../../output/wsfiles/'.$_REQUEST['clientnumber'].'/WS'.$pssr.'.pdf')){  
$zip->addFile($wspath.'WS'.$pssr.'.pdf','WS'.$pssr.'.pdf');
}
else{}
}
}
}


foreach($array as $value){
    $v=explode("-",$value);
    $file=$v[0]."_".$v[1].".pdf"; 
    if(file_exists('../../output/reports/'.$_REQUEST['clientnumber'].'/'.$file)){
$zip->addFile($path.$file,$file);

}
}
$zip->close();
$itemData =  array (
    'source' => 'output/reports/'.$_REQUEST['clientnumber'],
    'file' => $theZip,
    'type' => 'zip'
    );
    array_push($outputZip,$itemData );

}

}
}

if($outputmode=="pdf"){echo json_encode($output);}

if($outputmode=="zip"){echo json_encode($outputZip);}

?>