<?php
/**
 * Animal Dietary Balance results display page.
 * Loads a single animal_records row by rid + rand params.
 */

require_once __DIR__ . '/../../../config/database.php';
require_once __DIR__ . '/../../../lib/auth.php';

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

requireLogin();

$pdo    = getDBConnection();
$userId = getCurrentUserId();

$recordId = (int)   ($_GET['rid']  ?? 0);
$randId   = (float) ($_GET['rand'] ?? 0);

$row = null;
if ($recordId > 0) {
    $stmt = $pdo->prepare('SELECT * FROM animal_records WHERE id = ? AND rand = ? LIMIT 1');
    $stmt->execute([$recordId, $randId]);
    $row = $stmt->fetch();
}

$h = fn($v) => htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8');
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Animal Dietary Balance | Crop Monitor</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.css" crossorigin="anonymous" rel="stylesheet">
    <link href="/client-assets/css/dashboard-2021.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.9.3/html2pdf.bundle.min.js" crossorigin="anonymous"></script>
    <style>
        @media print {
            @page { size: A4 portrait; margin: 0.5cm; }
            .d-print-none { display: none !important; }
        }
        .progress { border-radius: 0 !important; }
    </style>
</head>
<body>
<div class="container" id="content">

    <?php if (!$row): ?>
        <div class="alert alert-danger mt-4">Record not found or access denied.</div>
    <?php else: ?>

    <div class="row mb-3">
        <div class="col-md-3">
            <img class="img-fluid" src="/client-assets/images/crop-monitor.png" alt="Crop Monitor">
        </div>
    </div>

    <div class="d-print-none mb-3">
        <button class="btn btn-success btn-sm downloadPDF">
            <i class="fas fa-download me-1"></i>Download PDF
        </button>
    </div>

    <div class="row">
        <div class="col-md-12 text-center fw-bold h4">ANIMAL DIETARY MINERAL BALANCE</div>
    </div>

    <hr class="p-1 m-1">

    <table class="table table-bordered table-sm">
        <tbody>
            <tr>
                <td class="fw-bold text-end">Client:</td>
                <td><?= $h($row['client_name'] ?? '') ?></td>
                <td class="fw-bold text-end">Sample ID:</td>
                <td><?= $h($row['sample_id'] ?? '') ?></td>
            </tr>
            <tr>
                <td class="fw-bold text-end">Lab No:</td>
                <td><?= $h($row['lab_no'] ?? '') ?></td>
                <td class="fw-bold text-end">Date Sampled:</td>
                <td><?= $h($row['date_sampled'] ?? '') ?></td>
            </tr>
            <tr>
                <td class="fw-bold text-end">Analysis Type:</td>
                <td><?= $h($row['analysis_type'] ?? '') ?></td>
                <td class="fw-bold text-end">Site ID:</td>
                <td><?= $h($row['site_id'] ?? '') ?></td>
            </tr>
        </tbody>
    </table>

    <!-- Analysis calculation rows — pending PHP migration of animalAnalysisCalcs -->
    <div class="alert alert-info mt-3">
        <i class="fas fa-info-circle me-1"></i>
        Animal dietary analysis calculation display is pending full PHP migration.
    </div>

    <p>Note: The above assessment is for individual nutrient levels. The influence of nutrient interactions is not considered above.</p>

    <p><b>References:</b></p>
    <ul>
        <li>AFIA Laboratory Methods Manual v7 Appendix 2.2R(1), p93</li>
        <li>NRC Nutrient Requirements of Dairy Cattle. National Academy Press, Washington DC. 7th Revised Edition, 2001</li>
        <li>Milk Production from Pasture – Principles and Practices. Massey University, NZ, 2002</li>
    </ul>

    <?php endif; ?>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script>
document.querySelector('.downloadPDF') && document.querySelector('.downloadPDF').addEventListener('click', function () {
    var opt = {
        margin: 3,
        filename: 'animal-dietary-balance.pdf',
        image: { type: 'jpeg', quality: 1.0 },
        html2canvas: { scale: 2, letterRendering: true, windowWidth: 1024 },
        jsPDF: { orientation: 'portrait', unit: 'mm', format: 'a4' }
    };
    html2pdf().from(document.getElementById('content')).set(opt).save();
});
</script>
</body>
</html>
