<?php
require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../../lib/auth.php';
require_once __DIR__ . '/../../lib/csrf.php';

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

requireLogin();

$pageTitle = 'Soil Recommendations';
$siteName  = 'Crop Monitor';

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

$errors  = [];
$success = false;

$h = fn($v) => htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8');

// Load fertiliser_specifications for this user
$stmt = $pdo->prepare('SELECT * FROM fertiliser_specifications WHERE modx_user_id = ? ORDER BY id DESC');
$stmt->execute([$userId]);
$products = $stmt->fetchAll();

include __DIR__ . '/../../layouts/header.php';
include __DIR__ . '/../../layouts/navbar.php';
?>

<div id="layoutSidenav">
    <div id="layoutSidenav_nav">
        <?php include __DIR__ . '/../../layouts/sidebar.php'; ?>
    </div>
    <div id="layoutSidenav_content">
        <main>
            <div class="container-fluid px-4">
                <h1 class="mt-4"><?= $h($pageTitle) ?></h1>
                <ol class="breadcrumb mb-4">
                    <li class="breadcrumb-item"><a href="/dashboard/dashboard.php">Dashboard</a></li>
                    <li class="breadcrumb-item active">Soil Recommendations</li>
                </ol>

                <?php foreach ($errors as $err): ?>
                    <div class="alert alert-danger"><?= $h($err) ?></div>
                <?php endforeach; ?>
                <?php if ($success): ?>
                    <div class="alert alert-success">Product added successfully.</div>
                <?php endif; ?>

                <div class="row">
                    <div class="col-12">
                        <h5>Soil Analysis</h5>
                        <p class="text-muted">Variables used in Soil Analysis recommendation programs.</p>

                        <button type="button" class="btn btn-warning mb-3"
                                data-bs-toggle="modal" data-bs-target="#addProductModal">
                            Add Soil Recommendation
                        </button>

                        <?php if (empty($products)): ?>
                            <p class="text-muted">No products found. Add one using the button above.</p>
                        <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-sm table-striped table-hover table-bordered">
                                <thead class="table-dark">
                                    <tr>
                                        <th>Product</th>
                                        <th>Chemical</th>
                                        <th>N</th><th>P</th><th>K</th><th>Na</th>
                                        <th>Ca</th><th>Mg</th><th>B</th><th>Zn</th>
                                        <th>Cu</th><th>Mn</th><th>Fe</th><th>Co</th><th>Mo</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php foreach ($products as $prod): ?>
                                    <tr>
                                        <td><?= $h($prod['name'] ?? '') ?></td>
                                        <td><?= $h($prod['chemical'] ?? '') ?></td>
                                        <td><?= $h($prod['N'] ?? '0') ?></td>
                                        <td><?= $h($prod['P'] ?? '0') ?></td>
                                        <td><?= $h($prod['K'] ?? '0') ?></td>
                                        <td><?= $h($prod['Na'] ?? '0') ?></td>
                                        <td><?= $h($prod['Ca'] ?? '0') ?></td>
                                        <td><?= $h($prod['Mg'] ?? '0') ?></td>
                                        <td><?= $h($prod['B'] ?? '0') ?></td>
                                        <td><?= $h($prod['Zn'] ?? '0') ?></td>
                                        <td><?= $h($prod['Cu'] ?? '0') ?></td>
                                        <td><?= $h($prod['Mn'] ?? '0') ?></td>
                                        <td><?= $h($prod['Fe'] ?? '0') ?></td>
                                        <td><?= $h($prod['Co'] ?? '0') ?></td>
                                        <td><?= $h($prod['Mo'] ?? '0') ?></td>
                                    </tr>
                                <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </main>

        <?php include __DIR__ . '/../../layouts/footer.php'; ?>
    </div>
</div>

<!-- Add Product Modal -->
<div class="modal fade" id="addProductModal" tabindex="-1" aria-labelledby="addProductModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addProductModalLabel">Add New Product</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form method="post" action="/controllers/newProductSubmit.php">
                <input type="hidden" name="csrf_token" value="<?= $h(generateCsrfToken()) ?>">
                <div class="modal-body">
                    <div class="row mb-2">
                        <div class="col">
                            <label class="form-label">Product Name</label>
                            <input type="text" class="form-control form-control-sm" name="name" placeholder="Product Name" required>
                        </div>
                        <div class="col">
                            <label class="form-label">Chemical Symbol</label>
                            <input type="text" class="form-control form-control-sm" name="chemical" placeholder="Chemical">
                        </div>
                    </div>
                    <div class="row mb-2">
                        <div class="col">
                            <label class="form-label">Nitrogen (N)</label>
                            <input type="text" class="form-control form-control-sm" name="N" placeholder="0" required>
                        </div>
                        <div class="col">
                            <label class="form-label">Phosphorus (P)</label>
                            <input type="text" class="form-control form-control-sm" name="P" placeholder="0">
                        </div>
                    </div>
                    <div class="row mb-2">
                        <div class="col">
                            <label class="form-label">Potassium (K)</label>
                            <input type="text" class="form-control form-control-sm" name="K" placeholder="0">
                        </div>
                        <div class="col">
                            <label class="form-label">Sodium (Na)</label>
                            <input type="text" class="form-control form-control-sm" name="Na" placeholder="0">
                        </div>
                    </div>
                    <div class="row mb-2">
                        <div class="col">
                            <label class="form-label">Calcium (Ca)</label>
                            <input type="text" class="form-control form-control-sm" name="Ca" placeholder="0" required>
                        </div>
                        <div class="col">
                            <label class="form-label">Magnesium (Mg)</label>
                            <input type="text" class="form-control form-control-sm" name="Mg" placeholder="0" required>
                        </div>
                    </div>
                    <div class="row mb-2">
                        <div class="col">
                            <label class="form-label">Boron (B)</label>
                            <input type="text" class="form-control form-control-sm" name="B" placeholder="0">
                        </div>
                        <div class="col">
                            <label class="form-label">Zinc (Zn)</label>
                            <input type="text" class="form-control form-control-sm" name="Zn" placeholder="0">
                        </div>
                    </div>
                    <div class="row mb-2">
                        <div class="col">
                            <label class="form-label">Copper (Cu)</label>
                            <input type="text" class="form-control form-control-sm" name="Cu" placeholder="0">
                        </div>
                        <div class="col">
                            <label class="form-label">Manganese (Mn)</label>
                            <input type="text" class="form-control form-control-sm" name="Mn" placeholder="0">
                        </div>
                    </div>
                    <div class="row mb-2">
                        <div class="col">
                            <label class="form-label">Iron (Fe)</label>
                            <input type="text" class="form-control form-control-sm" name="Fe" placeholder="0">
                        </div>
                        <div class="col">
                            <label class="form-label">Cobalt (Co)</label>
                            <input type="text" class="form-control form-control-sm" name="Co" placeholder="0">
                        </div>
                    </div>
                    <div class="row mb-2">
                        <div class="col-6">
                            <label class="form-label">Molybdenum (Mo)</label>
                            <input type="text" class="form-control form-control-sm" name="Mo" placeholder="0">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Save Product</button>
                </div>
            </form>
        </div>
    </div>
</div>
