<?php
// phpcs:ignoreFile

$configuredBaseUrl = getenv('LASUCA_FEED_URL');

$roundedid = [];
$rounded = [];
$rounded1 = [];
$ID = [];
$value = [];

$endpointDataLoaded = false;
$endpointErrorMessage = null;

try {
    if ($configuredBaseUrl !== false && $configuredBaseUrl !== '') {
        $baseUrl = rtrim($configuredBaseUrl, '/');
    } else {
        $baseUrl = 'https://192.168.0.10';
    }

    $endpointUrl = rtrim($baseUrl, '/') . '/shared-endpoint/public/index.php';
    $scheme = parse_url($endpointUrl, PHP_URL_SCHEME) ?: 'http';

    $context = null;
    if (strcasecmp($scheme, 'https') === 0) {
        $host = parse_url($endpointUrl, PHP_URL_HOST) ?: 'localhost';
        if (in_array($host, ['localhost', '127.0.0.1', '192.168.0.10'], true)) {
            $context = stream_context_create([
                'ssl' => [
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                ],
            ]);
        }
    }

    $response = @file_get_contents($endpointUrl, false, $context ?: null);

    if ($response === false && strcasecmp($scheme, 'https') === 0) {
        $fallbackUrl = preg_replace('#^https#i', 'http', $endpointUrl);
        $response = @file_get_contents($fallbackUrl);
        if ($response !== false) {
            $endpointUrl = $fallbackUrl;
        }
    }

    if ($response === false) {
        throw new RuntimeException('Unable to reach shared endpoint: ' . $endpointUrl);
    }

    try {
        $payload = json_decode($response, true, flags: JSON_THROW_ON_ERROR);
    } catch (JsonException $exception) {
        throw new RuntimeException('Malformed JSON from shared endpoint', 0, $exception);
    }

    if (($payload['status'] ?? null) !== 'ok') {
        $message = $payload['message'] ?? 'unknown error';
        throw new RuntimeException('Shared endpoint returned an error: ' . $message);
    }

    if (!isset($payload['items']) || !is_array($payload['items'])) {
        $snippet = substr(strip_tags($response), 0, 200);
        throw new RuntimeException('Shared endpoint response missing items. Snippet: ' . $snippet);
    }

    foreach ($payload['items'] as $item) {
        $tagKey = str_pad((string) $item['tagId'], 5, '0', STR_PAD_LEFT);
        $value[$item['name']] = $item['value'];
        $rounded[$item['name']] = $item['rounded1'];
        $rounded1[$item['name']] = $item['rounded2'];
        $roundedid[$tagKey] = $item['rounded2'];
        $ID[$tagKey] = $item['value'];
    }

    $endpointDataLoaded = true;
} catch (Throwable $exception) {
    $endpointErrorMessage = $exception->getMessage();
    error_log('Milling shared endpoint failed: ' . $endpointErrorMessage);
}

$dataSourceLabel = 'Shared endpoint';

if (!$endpointDataLoaded) {
    require __DIR__ . '/../items.php';
    require __DIR__ . '/../items2dec.php';
    $dataSourceLabel = 'Legacy items.php data';
}
// Helper function for rendering data rows
function renderDataRow($label, $value, $unit = "") {
    $safeValue = htmlspecialchars($value);
    return "<tr>
        <td id=\"vtitle\">$label</td>
        <td id=\"sum-count\">$safeValue</td>
        <td id=\"vtitle\">$unit</td>
    </tr>";
}
?>
<?php
//if ($value['BC09ONOFF'] == 0) {
//$grindingrate=$value['WTONSHR'];
//}
//else if ($value['BC09ONOFF'] == 1) {
//$grindingrate="0";
//}
?>
<table width="100%">
	<tr>
		<td width="50%">
			<table class="tftable2">
				<tr>
					<td width="50%" align="center" colspan="5" border="0">
						<font color="#00BFFF"><i>EAST MILL TANDEM</i></font>
					</td>
				</tr>
			</table>

			<table class="tftableEAST">
				<tr>
					<td align="center">
						<font color="#e0e0e0"></font>
					</td>
					<td align="center">
						<font color="#e0e0e0">Level</font>
					</td>
					<td align="center">
						<font color="#e0e0e0">SP</font>
					</td>
					<td align="center">
						<font color="#e0e0e0">RPM</font>
					</td>
					<td align="left" colspan="1">
						<font color="#e0e0e0">Mill 1 OP</font>
					</td>
					<td align="right">
						<font color="#00FF00"><?php echo $value['MILL1OUTPUT']; ?></font>
					</td>
					<td align="left" colspan="1">
						<font color="#e0e0e0">%</font>
					</td>
					<?php
					if ($value['MILL1AUTOMAN'] == 0) {
						echo "<td colspan=\"1\" align=\"center\" bgcolor=\"#0B3B17\"><font color=\"#e0e0e0\">A</font></td>";
					}
					if ($value['MILL1AUTOMAN'] == 1) {
						echo "<td colspan=\"1\" align=\"center\" bgcolor=\"#8A0808\"><font color=\"#e0e0e0\">M</font></td>";
					}
					?>
				</tr>

				<tr>
					<td bgcolor="#0B3B17" align="center">
						<font color="#e0e0e0">1</font>
					</td>
					<td bgcolor="#0B3B17" align="left"><progress id="progressmills" data-label="<?php echo $value['MILL1LVLPV']; ?>%" max="100" value="<?php echo $value['MILL1LVLPV']; ?>"></progress></td>
					<td bgcolor="#0B3B17" align="center">
						<font color="#e0e0e0"><?php echo $value['MILL1SP']; ?></font>
					</td>
					<td bgcolor="#0B3B17" align="center">
						<font color="#00FF00"><?php echo $value['EMILL1RPM']; ?></font>
					</td>
					<td align="left" colspan="1">
						<font color="#e0e0e0">MCC FPM</font>
					</td>
					<td align="right">
						<font color="#00FF00"><?php echo $value['MAINSPD']; ?></font>
					</td>
					<td colspan="2" align="left">
						<font color="#e0e0e0">FPM</font>
					</td>

				</tr>
				<tr>
					<td bgcolor="#D7DF01" align="center">
						<font color="#000000">2</font>
					</td>
					<td bgcolor="#D7DF01" align="left"><progress id="progressmills" data-label="<?php echo $value['M2LVL']; ?>%" max="100" value="<?php echo $value['M2LVL']; ?>"></progress></td>
					<td bgcolor="#D7DF01" align="center">
						<font color="#ffffff"><?php echo $value['MILL2SP']; ?></font>
					</td>
					<td bgcolor="#D7DF01" align="center">
						<font color="#298A08"><?php echo $value['EMILL2RPM']; ?></font>
					</td>
					<td align="left" colspan="1">
						<font color="#e0e0e0">MCC Output</font>
					</td>
					<td align="right">
						<font color="#00FF00"><?php echo $value['MCCOUTPUT']; ?></font>
					</td>
					<td colspan="2" align="left">
						<font color="#e0e0e0">%</font>
					</td>
				</tr>
				<tr>
					<td bgcolor="#04B404" align="center">
						<font color="#e0e0e0">3</font>
					</td>
					<td bgcolor="#04B404" align="left"><progress id="progressmills" data-label="<?php echo $value['MIL3LVL']; ?>%" max="100" value="<?php echo $value['MIL3LVL']; ?>"></progress></td>
					<td bgcolor="#04B404" align="center">
						<font color="#e0e0e0"><?php echo $value['MILL3SP']; ?></font>
					</td>
					<td bgcolor="#04B404" align="center">
						<font color="#29522b"><?php echo $value['EMILL3RPM']; ?></font>
					</td>
					<td align="left" colspan="1">
						<font color="#e0e0e0">MCC Tons</font>
					</td>
					<td align="right">
						<font color="#00FF00"><?php echo $rounded1['MCCTONS']; ?></font>
					</td>
					<td colspan="2" align="left">
						<font color="#e0e0e0">Tons</font>
					</td>
				</tr>
				<tr>
					<td bgcolor="#DF7401" align="center">
						<font color="#e0e0e0">4</font>
					</td>
					<td bgcolor="#DF7401" align="left"><progress id="progressmills" data-label="<?php echo $value['MIL4LVL']; ?>%" max="100" value="<?php echo $value['MIL4LVL']; ?>"></progress></td>
					<td bgcolor="#DF7401" align="center">
						<font color="#e0e0e0"><?php echo $value['MILL4SP']; ?></font>
					</td>
					<td bgcolor="#DF7401" align="center">
						<font color="#00FF00"><?php echo $value['EMILL4RPM']; ?></font>
					</td>
					<td colspan="1" align="left">
						<font color="#e0e0e0">Mill 1 SP</font>
					</td>
					<?php
					if ($value['LEVEL_THROUGHPUT_SWITCH'] == 1) {
						echo "<td align=\"right\"><font color=\"00FF00\"><b> " . $value['WEIGHT_WSP'] . " </b></font></td>";
					}
					if ($value['LEVEL_THROUGHPUT_SWITCH'] == 0) {
						echo "<td align=\"right\"><font color=\"00FF00\"><b> " . $value['WEIGHT_WSP'] . " </b></font></td>";
					}
					?>
					<?php
					if ($value['LEVEL_THROUGHPUT_SWITCH'] == 1) {
						echo "<td colspan=\"1\"><font color=\"#e0e0e0\">Tons/Hr</font></td>";
					}
					if ($value['LEVEL_THROUGHPUT_SWITCH'] == 0) {
						echo "<td colspan=\"1\"><font color=\"#e0e0e0\">%</font></td>";
					}
					?>
					<?php
					if ($value['MILL1AUTOMAN1'] == 1) {
						echo "<td colspan=\"1\" align=\"center\" bgcolor=\"#0B3B17\"><font color=\"#e0e0e0\">A</font></td>";
					}
					if ($value['MILL1AUTOMAN1'] == 0) {
						echo "<td colspan=\"1\" align=\"center\" bgcolor=\"#8A0808\"><font color=\"#e0e0e0\">M</font></td>";
					}
					?>
				</tr>
				<tr>
					<td bgcolor="#5858FA" align="center">
						<font color="#e0e0e0">5</font>
					</td>
					<td bgcolor="#5858FA" align="left"><progress id="progressmills" data-label="<?php echo $value['MIL5LVL']; ?>%" max="100" value="<?php echo $value['MIL5LVL']; ?>"></progress></td>
					<td bgcolor="#5858FA" align="center">
						<font color="#e0e0e0"><?php echo $value['MILL5SP']; ?></font>
					</td>
					<td bgcolor="#5858FA" align="center">
						<font color="#00FF00"><?php echo $value['EMILL5RPM']; ?></font>
					</td>
					<td align="left" colspan="1">
						<font color="#e0e0e0">Grinding Rate</font>
					</td>
					<td align="right">
						<font color="#00FF00"><?php echo $ID['00560']; ?></font>
					</td>
					<td colspan="2" align="left">
						<font color="#e0e0e0">Tons/Hr</font>
					</td>
				</tr>
				<tr>
					<td bgcolor="#8A0808" align="center">
						<font color="#e0e0e0">6</font>
					</td>
					<td bgcolor="#8A0808" align="left"><progress id="progressmills" data-label="<?php echo $value['MIL6LVL']; ?>%" max="100" value="<?php echo $value['MIL6LVL']; ?>"></progress></td>
					<td bgcolor="#8A0808" align="center">
						<font color="#e0e0e0"><?php echo $value['MILL6SP']; ?></font>
					</td>
					<td bgcolor="#8A0808" align="center">
						<font color="#00FF00"><?php echo $value['EMILL6RPM']; ?></font>
					</td>
					<td align="left" colspan="1">
						<font color="#e0e0e0">Average Rate</font>
					</td>
					<td align="right">
						<font color="#00FF00"><?php echo $value['AVG_TONS_PER_HOUR']; ?></font>
					</td>
					<td colspan="2" align="left">
						<font color="#e0e0e0">Tons/15/Min</font>
					</td>

				</tr>
				<tr>
					<td bgcolor="#2E2E2E" colspan="2">
						<font color="#e0e0e0">Imibition 6:</font>
					</td>
					<td bgcolor="#2E2E2E" align="right" id="lsp">
						<font color="00FF00"><b><?php echo $value['IMB6Flow']; ?></b></font>
					</td>
					<td bgcolor="#2E2E2E">
						<font color="#e0e0e0">GPM</font>
					</td>

					<td colspan="1">
						<font color="#e0e0e0">Imibition 6 SP:</font>
					</td>
					<td align="right" id="lsp">
						<font color="00FF00"><b><?php echo $value['IMB6SP']; ?></b></font>
					</td>
					<td colspan="2">
						<font color="#e0e0e0">GPM</font>
					</td>
				</tr>
				<tr>
					<td bgcolor="#2E2E2E" colspan="2">
						<font color="#e0e0e0">Imibition 5:</font>
					</td>
					<td bgcolor="#2E2E2E" align="right" id="lsp">
						<font color="00FF00"><b><?php echo $value['IMB5Flow']; ?></b></font>
					</td>
					<td bgcolor="#2E2E2E">
						<font color="#e0e0e0">GPM</font>
					</td>

					<td colspan="1">
						<font color="#e0e0e0">Imibition 5 SP:</font>
					</td>
					<td align="right" id="lsp">
						<font color="00FF00"><b><?php echo $ID['00164']; ?></b></font>
					</td>
					<td colspan="2">
						<font color="#e0e0e0">GPM</font>
					</td>
				</tr>
			</table>
		</td>

		<td width="50%">
			<table class="tftable2">
				<tr>
					<td width="50%" align="center" colspan="6" border="0">
						<font color="#00BFFF"><i>WEST MILL TANDEM</i></font>
					</td>
				</tr>
			</table>
			<table class="tftableWEST">
				<tr>
					<td align="center">
						<font color="#e0e0e0"></font>
					</td>
					<td align="center">
						<font color="#e0e0e0">Level</font>
					</td>
					<td align="center">
						<font color="#e0e0e0">SP</font>
					</td>
					<td align="center">
						<font color="#e0e0e0">RPM</font>
					</td>
					<td colspan="4" align="center">
						<font color="#e0e0e0"></font>
					</td>
				</tr>
				<tr>
					<td bgcolor="#0B3B17" align="center">
						<font color="#e0e0e0">1</font>
					</td>
					<td bgcolor="#0B3B17" align="left"><progress id="progressmills" data-label="<?php echo $ID['00914']; ?>%" max="100" value="<?php echo $ID['00654']; ?>"></progress></td>
					<td bgcolor="#0B3B17" align="center">
						<font color="#e0e0e0"></font>
					</td>
					<td bgcolor="#0B3B17" align="center">
						<font color="#00FF00"><?php echo $ID['00926']; ?></font>
					</td>
					<td align="left">
						<font color="#e0e0e0">BC-09 FPM</font>
					</td>
					<td align="right">
						<font color="#00FF00"><?php echo $value['Feet/Minute']; ?></font>
					</td>
					<td align="left">
						<font color="#e0e0e0">FPM</font>
					</td>
				</tr>
				<tr>
					<td bgcolor="#D7DF01" align="center">
						<font color="#000000">2</font>
					</td>
					<td bgcolor="#D7DF01" align="left"><progress id="progressmills" data-label="<?php echo $ID['00916']; ?>%" max="100" value="<?php echo $ID['00656']; ?>"></progress></td>
					<td bgcolor="#D7DF01" align="center">
						<font color="#ffffff"></font>
					</td>
					<td bgcolor="#D7DF01" align="center">
						<font color="#298A08"><?php echo $ID['00930']; ?></font>
					</td>
					<td align="left">
						<font color="#e0e0e0">BC-09 Output</font>
					</td>
					<td align="right">
						<font color="#00FF00"><?php echo $ID['00690']; ?></font>
					</td>
					<td align="left">
						<font color="#e0e0e0"></font>
					</td>
				</tr>
				<tr>
					<td bgcolor="#04B404" align="center">
						<font color="#e0e0e0">3</font>
					</td>
					<td bgcolor="#04B404" align="left"><progress id="progressmills" data-label="<?php echo $ID['00962']; ?>" max="100" value="0"></progress></td>
					<td bgcolor="#04B404" align="center">
						<font color="#e0e0e0"></font>
					</td>
					<td bgcolor="#04B404" align="center">
						<font color="#00FF00"><?php echo $ID['01018']; ?></font>
					</td>
					<td align="left">
						<font color="#e0e0e0">BC-09 Tons</font>
					</td>
					<td align="right">
						<font color="#00FF00"></font>
					</td>
					<td align="left">
						<font color="#e0e0e0">Tons</font>
					</td>
				</tr>
				<tr>
					<td bgcolor="#DF7401" align="center">
						<font color="#e0e0e0">4</font>
					</td>
					<td bgcolor="#DF7401" align="left"><progress id="progressmills" data-label="<?php echo $ID['00918']; ?>%" max="100" value="<?php echo $ID['00658']; ?>"></progress></td>
					<td bgcolor="#DF7401" align="center">
						<font color="#e0e0e0"></font>
					</td>
					<td bgcolor="#DF7401" align="center">
						<font color="#00FF00"><?php echo $ID['00934']; ?></font>
					</td>
					<td align="left">
						<font color="#e0e0e0">Mill 1 SP</font>
					</td>
					<td align="right">
						<font color="#00FF00"></font>
					</td>
					<td align="left">
						<font color="#e0e0e0">%</font>
					</td>
				</tr>
				<tr>
					<td bgcolor="#5858FA" align="center">
						<font color="#e0e0e0">5</font>
					</td>
					<td bgcolor="#5858FA" align="left"><progress id="progressmills" data-label="<?php echo $ID['00920']; ?>%" max="100" value="<?php echo $ID['00660']; ?>"></progress></td>
					<td bgcolor="#5858FA" align="center">
						<font color="#e0e0e0"></font>
					</td>
					<td bgcolor="#5858FA" align="center">
						<font color="#00FF00"><?php echo $ID['00940']; ?></font>
					</td>
					<td align="left">
						<font color="#e0e0e0">Grinding Rate</font>
					</td>
					<td align="right">
						<font color="#00FF00">
							<font><?php echo $value['RATE']; ?></font>
					</td>
					<td align="left">
						<font color="#e0e0e0">Tons/Hr</font>
					</td>
				</tr>
				<tr>
					<td bgcolor="#8A0808" colspan="4" align="center">
						<font color="#e0e0e0"></font>
					</td>
					<td align="left">
						<font color="#e0e0e0">Average Rate</font>
					</td>
					<td align="right">
						<font color="#00FF00"><?php require "../includes/west15minavg.php"; ?></font>
					</td>
					<td align="left">
						<font color="#e0e0e0">Tons/15Min</font>
					</td>
				</tr>
				<tr>
					<td bgcolor="#2E2E2E" colspan="2">
						<font color="#e0e0e0">Imibition:</font>
					</td>
					<td bgcolor="#2E2E2E" align="right" id="lsp">
						<font color="00FF00"><b><?php echo $value['IMB5Flow']; ?></b></font>
					</td>
					<td bgcolor="#2E2E2E">
						<font color="#e0e0e0">GPM</font>
					</td>

					<td colspan="1">
						<font color="#e0e0e0">Imibition SP:</font>
					</td>
					<td align="right" id="lsp">
						<font color="00FF00"><b><?php echo $ID['00164']; ?></b></font>
					</td>
					<td>
						<font color="#e0e0e0">GPM</font>
					</td>
				</tr>
				<tr>
					<td colspan="9">
						<font color="#e0e0e0">&nbsp</font>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
<?php

?>