<?php 
if(file_exists('../../../'.$_REQUEST['tenant'].'/config.php')){require_once('../../../'.$_REQUEST['tenant'].'/config.php'); }
if(file_exists('/config.php')){require_once('/config.php'); }

require('../asset/asset_functions.php');
if (!function_exists('enrep_itemdetails_pressure_from_record')) {
    require_once __DIR__ . '/../../functions/itemdetails_json.php';
}


//LOAD WS DATA

$stmt = $GLOBALS['con']->prepare("SELECT * FROM `writtenscheme` WHERE `id`=? ")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['ws']);$stmt->execute();$s=$stmt->get_result();$stmt->close();
$n=mysqli_fetch_array($s);

// CHECK REV STATUS & SET SAVEMODE
if($n['rev']=="0"){$active="no"; $savemode="standalone";}
else{$savemode="revise";}
if($n['del']=="0"){$alertDelivery='none';}
else{$alertDelivery='block';}
//GET CLIENT DETAILS
$stmt = $GLOBALS['con']->prepare("SELECT `clientnumber`,`clientname`,`contract_number`,`email` FROM `clientdetails` WHERE `clientnumber`='".$n['clientnumber']."' ")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
$cd=mysqli_fetch_array($cc);

//LOAD WS CURRENT LIST
$ilist=array();
$itable=array();
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `wsitems` WHERE `wsnumber`=? AND `type`='1'")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$n['id']);$stmt->execute();$cic=$stmt->get_result();$stmt->close();
while($cicl=mysqli_fetch_array($cic)){$ilist[]=$cicl['itemnumber'];$itable[]=$cicl;}

foreach ($ilist as $key => $value) {
$istmt = $GLOBALS['con']->prepare("SELECT `pressure_data` FROM `itemdetails` WHERE `itemnumber`='".$value."'")or die(mysqli_error($GLOBALS['con']));
$istmt->execute();$iic=$istmt->get_result();$istmt->close();
$iicd=mysqli_fetch_array($iic);
$pressureData = is_array($iicd) ? enrep_itemdetails_pressure_from_record($iicd) : [];
if(!empty($pressureData['scheme_number_alt'])){$wsalt=$pressureData['scheme_number_alt'];}
}

//LOAD LWS CURRENT LIST
$lilist=array();
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `lwsitems` WHERE `lwsnumber`=? AND `type`='1'")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$n['id']);$stmt->execute();$cic=$stmt->get_result();$stmt->close();
while($cicl=mysqli_fetch_array($cic)){$lilist[]=$cicl['itemnumber'];}

//LOAD CLIENT ITEMS
$stmt = $GLOBALS['con']->prepare("
    SELECT i.`clientnumber`, i.`itemnumber`, i.`desc`, i.`serialnumber`, i.`plantnumber`, i.`locnumber`
    FROM `itemdetails` i
    WHERE i.`clientnumber`=?
      AND i.`statuslive`='1'
      AND TRIM(COALESCE(JSON_UNQUOTE(JSON_EXTRACT(i.`pressure_data`, '$.system_type')), '')) <> ''
      AND TRIM(COALESCE(JSON_UNQUOTE(JSON_EXTRACT(i.`pressure_data`, '$.system_type')), '')) <> '0'
      AND NOT EXISTS (
        SELECT 1
        FROM `wsitems` wi
        WHERE wi.`itemnumber` = i.`itemnumber`
      )
    ORDER BY i.`desc`, i.`serialnumber`, i.`plantnumber`
")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$n['clientnumber']);$stmt->execute();$ic=$stmt->get_result();$stmt->close();


if($n['wstype']==1){require('../ws/ws_existing.php');}
else{require('../ws/ws_enrep.php');}
?>
