<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';


$production_id = intval($_GET['id'] ?? 0);
if($production_id <= 0){
    die("Invalid Production ID");
}

// Fetch production item
$prod = $conn->query("SELECT * FROM walkin_production WHERE id=$production_id")->fetch_assoc();
if(!$prod){
    die("Production not found");
}

// Handle form submission
if($_SERVER['REQUEST_METHOD'] === 'POST'){
    $actual_qty = floatval($_POST['actual_qty'] ?? 0);
    $remarks = $_POST['remarks'] ?? '';
    $audited_by = $_SESSION['username'] ?? 'Unknown';

    $stmt = $conn->prepare("INSERT INTO walkin_audit_log 
        (production_id, audited_by, original_qty, actual_qty, remarks) VALUES (?,?,?,?,?)");
    $stmt->bind_param("isdds", $production_id, $audited_by, $prod['total_qty'], $actual_qty, $remarks);
    $stmt->execute();

    echo "<div class='alert alert-success'>Audit saved successfully!</div>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Audit & Balance Production</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { padding: 20px; font-family: Arial, sans-serif; font-size: 14px; }
        h2 { margin-bottom: 20px; }
        label { font-weight: bold; }
    </style>
</head>
<body>
    <div class="container mt-4">
          <button class="btn btn-secondary" onclick="history.back()">
            <i class="fa fa-arrow-left"></i> Back
        </button>
    <h2>Audit & Balance Production</h2>
    <a href="kitchen_reports.php" class="btn btn-secondary mb-3"><i class="fa fa-arrow-left"></i> Back to Productions</a>

    <div class="card mb-3">
        <div class="card-header bg-dark text-white">
            Production Details
        </div>
        <div class="card-body">
            <p><strong>Item Name:</strong> <?= htmlspecialchars($prod['item_name']) ?></p>
            <p><strong>Type:</strong> <?= ucfirst($prod['item_type']) ?></p>
            <p><strong>Production Date:</strong> <?= $prod['production_date'] ?></p>
            <p><strong>People Count:</strong> <?= $prod['people_count'] ?></p>
            <p><strong>Total Quantity:</strong> <?= $prod['total_qty'] ?> <?= $prod['unit'] ?? '' ?></p>
        </div>
    </div>

    <form method="post">
        <div class="mb-3">
            <label for="actual_qty">Actual Quantity Used:</label>
            <input type="number" step="0.01" class="form-control w-50" name="actual_qty" id="actual_qty" value="<?= $prod['total_qty'] ?>" required>
        </div>

        <div class="mb-3">
            <label for="remarks">Remarks / Notes:</label>
            <textarea class="form-control w-50" name="remarks" id="remarks" rows="3"></textarea>
        </div>

        <button type="submit" class="btn btn-success"><i class="fa fa-save"></i> Save Audit</button>
    </form>

    <hr>

    <h5>Previous Audits for this Item</h5>
    <table class="table table-bordered table-striped">
        <thead class="table-dark">
            <tr>
                <th>#</th>
                <th>Audit Date</th>
                <th>Audited By</th>
                <th>Original Qty</th>
                <th>Actual Qty</th>
                <th>Adjustment</th>
                <th>Remarks</th>
            </tr>
        </thead>
        <tbody>
        <?php
        $audits = $conn->query("SELECT * FROM walkin_audit_log WHERE production_id=$production_id ORDER BY audit_date DESC");
        $i = 1;
        while($a = $audits->fetch_assoc()):
        ?>
            <tr>
                <td><?= $i ?></td>
                <td><?= $a['audit_date'] ?></td>
                <td><?= htmlspecialchars($a['audited_by']) ?></td>
                <td><?= $a['original_qty'] ?></td>
                <td><?= $a['actual_qty'] ?></td>
                <td><?= $a['adjustment'] ?></td>
                <td><?= htmlspecialchars($a['remarks']) ?></td>
            </tr>
        <?php $i++; endwhile; ?>
        </tbody>
    </table>
</body>
</html>