<?php require_once('../../includes/connection.php');
$master=array();
$output=array();
$final=array();
$multi=array();
$single=array();
$surveyors=array();
$repTypes=array("500","502","503","504","506","507","508","600","602","700","700","800","801","802","900");
$multiTypes=array("500","502","504","506");
$clientnumber=$_REQUEST['clientnumber'];
$chains=json_decode($_REQUEST['chainlist']); array_push($chains,"CA" ); foreach($chains as $chain){$master[$chain][]=array();}
$items=$_REQUEST['items']; $itemlist=explode("|",$items);

//SORT ITEMS INTO INDEX
foreach($itemlist as $item){
$itemset=explode("-",$item);
if( $itemset[4]=="undefined"){ $itemset[4]="CA";}
if(!empty( $itemset[2])){
$itemData =  array (
'chain' => $itemset[4],
'surveyor' => $itemset[3],
'file' => $itemset[0]."-".$itemset[1]."-".$itemset[2]."-".$itemset[3],
'type' => $itemset[2]
);
array_push($output,$itemData );
if(!empty($itemset[3])){$surveyors[] = $itemset[3];}
}
}
$surveyors = array_unique($surveyors);

//MOVE ITEMS INTO CHAIN GROUPS
foreach($output as $itemData){
foreach($chains as $chain){
if($itemData[chain]==$chain){
if(!in_array($itemData,$master[$chain])){$master[$chain][]=$itemData;}}
}
}

//CREATE ARRAYS
foreach($master as $chain => $items){$final[$chain]=array();
foreach($repTypes as $type){$final[$chain][$type]=array();
foreach($surveyors as $surveyor){$final[$chain][$type][$surveyor]=array();
}
}
}

//GROUP AS SURVEYOR
foreach($master as $chain => $items){
foreach($repTypes as $type){;
foreach($surveyors as $surveyor){
foreach($items as $item){
if($item[chain]==$chain && $item[type]==$type && $item[surveyor]==$surveyor){
    if(!in_array($item,$final[$chain][$type][$surveyor])){array_push($final[$chain][$type][$surveyor],$item);}
}
}
}
}
}

//SPLIT TO MULTI AND SINGLE
foreach($master as $chain => $items){
    foreach($repTypes as $type){;
    foreach($surveyors as $surveyor){

    $count=count($final[$chain][$type][$surveyor]);
    
    if($count>1 && in_array($type,$multiTypes)){ //echo $chain."-".$type."-".$surveyor."-".$count."\n";
        if(!in_array($multi,$final[$chain][$type][$surveyor]) && !in_array($single,$final[$chain][$type][$surveyor])){array_push($multi,$final[$chain][$type][$surveyor]);}
    }
    else{
        if(!in_array($multi,$final[$chain][$type][$surveyor]) && !in_array($single,$final[$chain][$type][$surveyor])){array_push($single,$final[$chain][$type][$surveyor]);}
    }

    }
    }
    }

$master = array_map('array_filter', $master);
$master = array_filter($master);
$single = array_map('array_filter', $single);
$single = array_filter($single);
$multi = array_map('array_filter', $multi);
$multi = array_filter($multi);

$finalmulti='';
foreach($multi as $set=>$vals){
foreach($vals as $i=>$v){
$finalmulti=$finalmulti.$v['file']."_".$v['chain'].",";
}
}

$finalsingle='';
foreach($single as $set=>$vals){
foreach($vals as $i=>$v){
$finalsingle=$finalsingle.$v['file']."_".$v['chain'].",";
}
}

//print_r($finalsingle);
echo $finalmulti."|".$finalsingle;
//echo json_encode($master)
?>