<?php
session_start();
if(!isset($_SESSION['user']) || empty($_SESSION['user'])) {
    header("location: ../login.php"); exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Batcher Formulas</title>
<style type="text/css" media="screen">
table tr td {
    text-align: right;
    padding: 0 15px;
}
table tr th {
	text-align: right;
}
td:nth-child(1) { width: 70px; }
th:nth-child(1) { width: 70px; }
td:nth-child(2) { width: 50px; }
th:nth-child(2) { width: 50px; }
td:nth-child(3) { width: 100px; }
th:nth-child(3) { width: 100px; }
td:nth-child(4) { width: 100px; }
th:nth-child(4) { width: 100px; }

.left-text { text-align: left; }

body {
    font-family: Arial, sans-serif;
    background-color: #f4f6f8;
    padding: 20px;
}

table {
    width: 100%;
    border-collapse: collapse;
    background-color: #fff;
    border-radius: 8px;
    overflow: hidden; /* Ensures rounded corners work */
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

thead {
    background-color: #4CAF50;
    color: white;
}

th, td {
    padding: 12px 15px;
    text-align: left;
}

tbody tr:nth-child(even) {
    background-color: #f9f9f9;
}

tbody tr:hover {
    background-color: #f1f1f1;
    transition: background-color 0.3s ease;
}

th {
    font-weight: bold;
}

/* Responsive table for small screens */
@media (max-width: 600px) {
    table, thead, tbody, th, td, tr {
        display: block;
        width: 100%;
    }
    thead tr {
        display: none; /* Hide table headers */
    }
    td {
        padding: 10px;
        text-align: right;
        position: relative;
    }
    td::before {
        content: attr(data-label);
        position: absolute;
        left: 10px;
        text-align: left;
        font-weight: bold;
        color: #555;
    }
}
</style>
</head>
<body>

<?php


$jsonFile = '/mnt/nand/apps/batcher/db.json';
$jsonContent = file_get_contents($jsonFile);
$data = json_decode($jsonContent, true);
if (!$data) {
    echo "DFC/Batcher database not configured";
} else {
   // echo "User: " . $data['user'];
  //  echo "Password: " . $data['passw'];
 //  echo "dbtype: " . $data['dbtype'];

   if ($data['dbtype'] != 'mysql') {
      echo "DFC/Batcher database type must by MySQL for reporting";
   } else {


        $conn = mysqli_connect($data['server'], $data['user'], $data['passw'], $data['database']);
        if(!$conn) {
            echo 'Connection error: ' . mysqli_connect_error();
        } else {

            echo "<h2>Formula Report</h2>";

            $sql = 'SELECT FormulaName, StepNum, Bin, TargetWt, TrimWt FROM Formulas AS a INNER JOIN Steps AS b ON a.FormulaId = b.FormulaId ORDER BY FormulaName, StepNum';
            $result = $conn->query($sql);
            $prvName = "";

            $cnt = 0;
            if($result->num_rows > 0) {
                echo '<table>';
                while($row = $result->fetch_assoc()) {
                    $name = $row['FormulaName'];
                    if($name != $prvName) {
                        if($cnt != 0) {
                            echo '<tr><td colspan="5">  &nbsp; </td><tr>';
                        }
                        echo '<tr><th class="left-text" colspan="5">' . $name . '</th></tr>';
                        echo '<tr><th>StepNum</th><th>Bin</th><th>Target Weight</th><th>Trim Weight</th><th></th></tr>';
                        $prvName = $name;
                    }
                    $stepnum = $row['StepNum'];
                    $bin = $row['Bin'];
                    $targetwt = $row['TargetWt'];
                    $trimwt = $row['TrimWt'];

                    echo '<tr><td>' . $stepnum . '</td><td>' . $bin . '</td><td>' . $targetwt . '</td><td>' . $trimwt . '</td><td></td></tr>';
                    $cnt++;
                }
                echo '</table>';
            }
        }
    }
}

?>
</body>
</html>
