<?php 

function getSites2($s,$c){
    $stmt = $GLOBALS['con']->prepare("SELECT `locnumber`,`locname`,`postcode` FROM `locations` WHERE `clientnumber`=?")or die($error=mysqli_error($GLOBALS['con']));
    $stmt->bind_param('i',$s);$stmt->execute();$res=$stmt->get_result();$stmt->close();
    $ret='<select class="form-control form-control-sm" id="locnumber2" name="locnumber" data-placeholder="Select Location">';
    $ret.='<option value="">Select Location</option>';
    $count=mysqli_num_rows($res);
    while($d=mysqli_fetch_array($res)){
    if($c==$d['locnumber']){$s='SELECTED';}else{$s='';}
    $ret.='<option value="'.$d['locnumber'].'" '.$s.'>'.$d['postcode'].' - '.$d['locname'].'</option>';
    }
    
    $ret.='</select>';
    return $ret;
    }
    



function getItemName($itemnumber){
$stmt = $GLOBALS['con']->prepare("SELECT `desc` FROM `itemdetails` WHERE `itemnumber`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$itemnumber);$stmt->execute();$rc=$stmt->get_result();$stmt->close();	
$cd=mysqli_fetch_array($rc);	
return $cd['desc'];
}

function getLEVData($itemnumber){
    $stmt = $GLOBALS['con']->prepare("SELECT `process` FROM `itemdetails` WHERE `itemnumber`=?")or die(mysqli_error($GLOBALS['con']));
    $stmt->bind_param('i',$itemnumber);$stmt->execute();$rc=$stmt->get_result();$stmt->close();
    $cd=mysqli_fetch_array($rc);
    if(empty($cd['process'])){return 'None found';}

}

function getPSSRData($itemnumber){
 $stmt = $GLOBALS['con']->prepare("SELECT `system_type` FROM `itemdetails` WHERE `itemnumber`=?")or die(mysqli_error($GLOBALS['con']));
    $stmt->bind_param('i',$itemnumber);$stmt->execute();$rc=$stmt->get_result();$stmt->close();
    $cd=mysqli_fetch_array($rc);
    if(empty($cd['system_type'])){return 'None found';}

}

function isWSItem($itemnumber){
$stmt = $GLOBALS['con']->prepare("SELECT `system_type` FROM `itemdetails` 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['system_type'])){return "";}
else{return "checked";}
}

function getWSData($itemnumber){
//check for instance
$stmt = $GLOBALS['con']->prepare("SELECT `id`,`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();
$c=mysqli_num_rows($res);$d=mysqli_fetch_array($res);
if($c>0){
$stmt = $GLOBALS['con']->prepare("SELECT `description`,`no` FROM `writtenscheme` WHERE `id`=?")or die($error=mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$d['wsnumber']);$stmt->execute();$res2=$stmt->get_result();$stmt->close();
$e=mysqli_fetch_array($res2);
return '<div class="row">
<div class="col-md-7">Item attached to Written Scheme <strong>'.$e['no'].'</strong></div>
<div class="col-md-4"><a href="javascript:void(0)" class="btn btn-secondary btn-sm" onClick="editWSC(\''.$d['wsnumber'].'\')" data-toggle="tooltip" data-placement="left" title="View '.$e['description'].' details"><i class="fad fa-search"></i></a>&nbsp;&nbsp;&nbsp;
<a href="javascript:void(0)" class="btn btn-secondary btn-sm" onClick="printWSC(\''.$d['wsnumber'].'\')" data-toggle="tooltip" data-placement="left" title="Print '.$e['description'].'"><i class="fad fa-print"></i></a>&nbsp;&nbsp;&nbsp;
<a href="javascript:void(0)" class="btn btn-secondary btn-sm" onClick="emailWSC(\''.$d['wsnumber'].'\')" data-toggle="tooltip" data-placement="left" title="Email '.$e['description'].'"><i class="fad fa-envelope"></i></a>
</div><input type="hidden" name="isWS" id="isWS" value="1" /><input type="hidden" name="wsnumber" id="wsnumber" value="'.$d['wsnumber'].'" /></div>';}
else{return 'None found';}
}

function getPssrNumber($itemnumber){
//check for instance
$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);
return($d['wsnumber']);
}

function getPssrEno($itemnumber){
//check for instance
$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);
$stmt2 = $GLOBALS['con']->prepare("SELECT `no` FROM `writtenscheme` WHERE `id`='".$d['wsnumber']."'")or die($error=mysqli_error($GLOBALS['con']));
$stmt2->execute();$resx=$stmt2->get_result();$stmt2->close();
$e=mysqli_fetch_array($resx);

if(empty($d['wsnumber'])){return '';}
else{return $e['no'];}
}

function listWSE($client){
    $stmt = $GLOBALS['con']->prepare("SELECT * FROM `writtenscheme` WHERE `clientnumber`='".$client."' ORDER BY abs(no) ASC ")or die(mysqli_error($GLOBALS['con']));
    $stmt->execute();$cc=$stmt->get_result();$stmt->close();
    $ret='<div class="col-4"><select id="wsSelect" name="wsSelect" class="form-control" data-mdb-select-init data-mdb-filter="true" >';
    $ret.='<option value="">SELECT SCHEME</option>';
    while($cd=mysqli_fetch_array($cc)){
        $ret.='<option value="'.$cd['id'].'"><small>'.$cd['no'].'</small>:'.$cd['title'].'</option>';
    }
    $ret.='</select></div>';
    return $ret;
}

function getPssrNo($itemnumber,$clientnumber){
//check for instance
$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);
$stmt2 = $GLOBALS['con']->prepare("SELECT `no` FROM `writtenscheme` WHERE `id`='".$d['wsnumber']."'")or die($error=mysqli_error($GLOBALS['con']));
$stmt2->execute();$resx=$stmt2->get_result();$stmt2->close();
$e=mysqli_fetch_array($resx);

if(empty($d['wsnumber'])){return '<div class="row"><div class="col-8 alert alert-warning">Not attached to WS yet, please complete the item registration first.</div>'.listWSE($clientnumber).'</div>';}
else{return '<div class="alert alert-success">Item is attached to the Written Scheme <strong>'.$e['no'].'</strong></div>';}

}

function getPssrReviews($wsnumber){
    $ret='';
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `wsrevisions` WHERE `wsnumber`=? ORDER BY abs(date) DESC")or die($error=mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$wsnumber);$stmt->execute();$res=$stmt->get_result();$stmt->close();
while($d=mysqli_fetch_array($res)){
$stmt = $GLOBALS['con']->prepare("SELECT `forename`,`surname` FROM `surveyor` WHERE `id`=?")or die($error=mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$d['surveyor']);$stmt->execute();$res2=$stmt->get_result();$stmt->close();
$e=mysqli_fetch_array($res2);
if($_SESSION['usertype']=="admins"){$del='<a href="javascript:void(0)" class="btn btn-danger btn-sm" onClick="remrevConfirm(\''.$d['id'].'\')" data-toggle="tooltip" data-placement="left" title="Remove this revision entry"><i class="fad fa-times"></i></a>';}
$ret.='<tr><td>'.date("d-m-Y",$d['date']).' <small>'.date("H:m:i",$d['date']).'</small></td><td>'.$d['description'].'</td><td>'.$e['forename'].' '.$e['surname'].'</td><td>'.$del.'</td></tr>';	
}
return $ret;
}

function isLWSItem($itemnumber){
$stmt = $GLOBALS['con']->prepare("SELECT `process` FROM `itemdetails` 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['process'])){return "";}
else{return "checked";}
}


function getLWSData($itemnumber){
//check for instance
    $stmt = $GLOBALS['con']->prepare("SELECT `id`,`lwsnumber` FROM `lwsitems` WHERE `itemnumber`=?")or die($error=mysqli_error($GLOBALS['con']));
    $stmt->bind_param('i',$itemnumber);$stmt->execute();$res=$stmt->get_result();$stmt->close();
    $c=mysqli_num_rows($res);$d=mysqli_fetch_array($res);
    if($c>0){
        $stmt = $GLOBALS['con']->prepare("SELECT `description`,`no` FROM `lwrittenscheme` WHERE `id`=?")or die($error=mysqli_error($GLOBALS['con']));
        $stmt->bind_param('i',$d['lwsnumber']);$stmt->execute();$res2=$stmt->get_result();$stmt->close();
        $e=mysqli_fetch_array($res2);
        return '<div class="row">
<div class="col-md-7">Item attached to LOLER Written Scheme <strong>'.$e['no'].'</strong></div>
<div class="col-md-4"><a href="javascript:void(0)" class="btn btn-secondary btn-sm" onClick="editLWSC(\''.$d['lwsnumber'].'\')" data-toggle="tooltip" data-placement="left" title="View '.$e['description'].' details"><i class="fad fa-search"></i></a>&nbsp;&nbsp;&nbsp;
<a href="javascript:void(0)" class="btn btn-secondary btn-sm" onClick="printLWSC(\''.$d['lwsnumber'].'\')" data-toggle="tooltip" data-placement="left" title="Print '.$e['description'].'"><i class="fad fa-print"></i></a>&nbsp;&nbsp;&nbsp;
<a href="javascript:void(0)" class="btn btn-secondary btn-sm" onClick="emailLWSC(\''.$d['lwsnumber'].'\')" data-toggle="tooltip" data-placement="left" title="Email '.$e['description'].'"><i class="fad fa-envelope"></i></a>
</div><input type="hidden" name="isLWS" id="isLWS" value="1" /><input type="hidden" name="lwsnumber" id="lwsnumber" value="'.$d['lwsnumber'].'" /></div>';}
else{return 'None found';}
}

function getLWSNumber($itemnumber){
//check for instance
    $stmt = $GLOBALS['con']->prepare("SELECT `lwsnumber` FROM `lwsitems` 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);
    return($d['lwsnumber']);
}

function getLWSReviews($wsnumber){
    $ret='';
    $stmt = $GLOBALS['con']->prepare("SELECT * FROM `lwsrevisions` WHERE `lwsnumber`=? ORDER BY abs(date) DESC")or die($error=mysqli_error($GLOBALS['con']));
    $stmt->bind_param('i',$wsnumber);$stmt->execute();$res=$stmt->get_result();$stmt->close();
    while($d=mysqli_fetch_array($res)){
        $stmt = $GLOBALS['con']->prepare("SELECT `forename`,`surname` FROM `surveyor` WHERE `id`=?")or die($error=mysqli_error($GLOBALS['con']));
        $stmt->bind_param('i',$d['surveyor']);$stmt->execute();$res2=$stmt->get_result();$stmt->close();
        $e=mysqli_fetch_array($res2);
        if($_SESSION['usertype']=="admins"){$del='<a href="javascript:void(0)" class="btn btn-danger btn-sm" onClick="remrevConfirm(\''.$d['id'].'\')" data-toggle="tooltip" data-placement="left" title="Remove this revision entry"><i class="fad fa-times"></i></a>';}
        $ret.='<tr><td>'.date("d-m-Y",$d['date']).' <small>'.date("H:m:i",$d['date']).'</small></td><td>'.$d['description'].'</td><td>'.$e['forename'].' '.$e['surname'].'</td><td>'.$del.'</td></tr>';
    }
    return $ret;
}

function getItemList($clientnumber,$current){
    $ret='';
//check for instance
$stmt = $GLOBALS['con']->prepare("SELECT `itemnumber`,`desc`,`serialnumber` FROM `itemdetails` WHERE `clientnumber`=? AND `statuslive`='1'")or die($error=mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$clientnumber);$stmt->execute();$res=$stmt->get_result();$stmt->close();
while($d=mysqli_fetch_array($res)){
if($current==$d['itemnumber']){$s="SELECTED";}else{$s="";}
$ret.='<option value="'.$d['itemnumber'].'" '.$s.'>'.$d['desc'].' - '.$d['serialnumber'].'</option>';
}
return $ret;
}

?>