<?php $_REQUEST['src']='ext2';require_once('../../includes/connection.php');?>

<div id="list" class="row">
    <?php
function getClientName($clientnumber){
    $stmt = $GLOBALS['con']->prepare("SELECT `clientname` FROM `clientdetails` WHERE `clientnumber`=?")or die(mysqli_error($GLOBALS['con']));
    $stmt->bind_param('i',$clientnumber);$stmt->execute();$rc=$stmt->get_result();$stmt->close();	
    $cd=mysqli_fetch_array($rc);	
    return $cd['clientname'];
    }

    function getLocation($l){
        $r=mysqli_query($GLOBALS['con'],"SELECT `locname`,`postcode` FROM `locations` WHERE `locnumber`='".$l."'"); 	
        $d=mysqli_fetch_array($r);
        return $d['locname']." - ".$d['postcode'];
        mysqli_close($GLOBALS['con']);
        }
        
    
    $ret='';
    $itemData=array();
    $stmt = $GLOBALS['con']->prepare("SELECT DISTINCT `client_id` FROM `surveyor_assignments` WHERE `surveyor`=? ")or die(mysqli_error($GLOBALS['con']));
    $stmt->bind_param('i',$_REQUEST['s']);$stmt->execute();$ic=$stmt->get_result();$stmt->close();
    while($id=mysqli_fetch_array($ic)){$itemData[]=$id;}
    foreach($itemData as $data){$locs='';
        $stmt = $GLOBALS['con']->prepare("SELECT DISTINCT `site_id` FROM `surveyor_assignments` WHERE `surveyor`=? AND `client_id`=? ")or die(mysqli_error($GLOBALS['con']));
    $stmt->bind_param('ii',$_REQUEST['s'],$data['client_id']);$stmt->execute();$ic=$stmt->get_result();$stmt->close();
    while($id=mysqli_fetch_array($ic)){$locs.='<p class="border-bottom"><small>'.getLocation($id['site_id']).'</small> </p>';}
    $ret.='
    <div class="col-12 border card">
        <div class="card-header m-0"><span>'.getClientName($data['client_id'],'name').'</span>
                
        </div>
        <div class="card-body">
            <div id="D'.$data['client_id'].'">
                <div class="card-body"><h4>Sites Assigned</h4>'.$locs.'</div>
            </div>
        </div>
    </div>
    ';
 }
 echo $ret;
?>
</div>
<div class="col-12 text-center"><a class="btn btn-info" href="javascript:void(0)" onclick="printList()">Print</a></div>
<script>
$('#popupBox .modal-header').html(
    '<a href="javascript:void(0)" class="sa">Surveyor Assignments</a><div class="pull-right"><button type="button" class="btn btn-secondary p-1" onclick="closePop()"><i class="fad fa-window-close fa-2x" aria-hidden="true"></i></button></div>'
);

function printList() {
    $("#list").print({
        addGlobalStyles: true,
        stylesheet: 'src/css/label.css',
        rejectWindow: true,
        noPrintSelector: ".no-print",
        iframe: true,

    });
}
</script>