<?php
/**
 * components/soilAnalysisForm.php
 *
 * Soil analysis data entry form component.
 * Replaces modX [[$soilAnalysisForm]] chunk.
 */

// Form field definitions with validation rules
$formSections = [
    'Client Information' => [
        'name' => ['type' => 'text', 'label' => 'Client Name', 'required' => true, 'placeholder' => 'Enter client name'],
        'company' => ['type' => 'text', 'label' => 'Company Name', 'required' => false, 'placeholder' => 'Enter company name'],
        'email' => ['type' => 'email', 'label' => 'Email Address', 'required' => true, 'placeholder' => 'client@example.com'],
        'site_address' => ['type' => 'text', 'label' => 'Site Address', 'required' => true, 'placeholder' => 'Enter site address'],
        'state_postcode' => ['type' => 'text', 'label' => 'State & Postcode', 'required' => true, 'placeholder' => 'State, Postcode'],
    ],
    'Sample Information' => [
        'lab_no' => ['type' => 'text', 'label' => 'Lab Number', 'required' => true, 'placeholder' => 'Lab reference number'],
        'batch_no' => ['type' => 'text', 'label' => 'Batch Number', 'required' => false, 'placeholder' => 'Batch reference'],
        'sample_id' => ['type' => 'text', 'label' => 'Sample ID', 'required' => true, 'placeholder' => 'Sample identifier'],
        'site_id' => ['type' => 'text', 'label' => 'Site ID', 'required' => true, 'placeholder' => 'Site identifier'],
        'crop_type' => ['type' => 'text', 'label' => 'Crop Type', 'required' => true, 'placeholder' => 'Type of crop'],
        'soil_type' => ['type' => 'select', 'label' => 'Soil Type', 'required' => true, 'options' => ['Sandy', 'Loamy', 'Clay', 'Silt', 'Peat', 'Chalk']],
        'date_sampled' => ['type' => 'date', 'label' => 'Date Sampled', 'required' => true],
    ],
    'Physical Properties' => [
        'texture' => ['type' => 'text', 'label' => 'Texture', 'required' => false, 'placeholder' => 'Soil texture description'],
        'gravel' => ['type' => 'number', 'label' => 'Gravel (%)', 'required' => false, 'step' => '0.1', 'min' => '0', 'max' => '100'],
        'colour' => ['type' => 'text', 'label' => 'Colour', 'required' => false, 'placeholder' => 'Soil colour'],
        'ocarbon' => ['type' => 'number', 'label' => 'Organic Carbon (%)', 'required' => false, 'step' => '0.01', 'min' => '0'],
        'omatter' => ['type' => 'number', 'label' => 'Organic Matter (%)', 'required' => false, 'step' => '0.01', 'min' => '0'],
    ],
    'Chemical Properties' => [
        'ph_cacl2' => ['type' => 'number', 'label' => 'pH (CaCl₂)', 'required' => false, 'step' => '0.1', 'min' => '0', 'max' => '14'],
        'ph_h2o' => ['type' => 'number', 'label' => 'pH (H₂O)', 'required' => false, 'step' => '0.1', 'min' => '0', 'max' => '14'],
        'paramag' => ['type' => 'number', 'label' => 'Paramagnetic', 'required' => false, 'step' => '0.01'],
        'ec' => ['type' => 'number', 'label' => 'EC (dS/m)', 'required' => false, 'step' => '0.01', 'min' => '0'],
    ],
    'Nutrient Analysis' => [
        'NO3_N' => ['type' => 'number', 'label' => 'NO₃-N (mg/kg)', 'required' => false, 'step' => '0.1', 'min' => '0'],
        'NH3_N' => ['type' => 'number', 'label' => 'NH₃-N (mg/kg)', 'required' => false, 'step' => '0.1', 'min' => '0'],
        'p_mehlick' => ['type' => 'number', 'label' => 'P Mehlich (mg/kg)', 'required' => false, 'step' => '0.1', 'min' => '0'],
        'p_bray2' => ['type' => 'number', 'label' => 'P Bray-2 (mg/kg)', 'required' => false, 'step' => '0.1', 'min' => '0'],
        'p_morgan' => ['type' => 'number', 'label' => 'P Morgan (mg/kg)', 'required' => false, 'step' => '0.1', 'min' => '0'],
        'k_morgan' => ['type' => 'number', 'label' => 'K Morgan (mg/kg)', 'required' => false, 'step' => '0.1', 'min' => '0'],
        'ca_morgan' => ['type' => 'number', 'label' => 'Ca Morgan (mg/kg)', 'required' => false, 'step' => '0.1', 'min' => '0'],
        'mg_morgan' => ['type' => 'number', 'label' => 'Mg Morgan (mg/kg)', 'required' => false, 'step' => '0.1', 'min' => '0'],
        'na_morgan' => ['type' => 'number', 'label' => 'Na Morgan (mg/kg)', 'required' => false, 'step' => '0.1', 'min' => '0'],
        's_morgan' => ['type' => 'number', 'label' => 'S Morgan (mg/kg)', 'required' => false, 'step' => '0.1', 'min' => '0'],
    ],
    'Micronutrients' => [
        'b_cacl2' => ['type' => 'number', 'label' => 'B (CaCl₂) (mg/kg)', 'required' => false, 'step' => '0.01', 'min' => '0'],
        'mn_dtpa' => ['type' => 'number', 'label' => 'Mn (DTPA) (mg/kg)', 'required' => false, 'step' => '0.01', 'min' => '0'],
        'zn_dtpa' => ['type' => 'number', 'label' => 'Zn (DTPA) (mg/kg)', 'required' => false, 'step' => '0.01', 'min' => '0'],
        'fe_dtpa' => ['type' => 'number', 'label' => 'Fe (DTPA) (mg/kg)', 'required' => false, 'step' => '0.01', 'min' => '0'],
        'cu_dtpa' => ['type' => 'number', 'label' => 'Cu (DTPA) (mg/kg)', 'required' => false, 'step' => '0.01', 'min' => '0'],
        'al' => ['type' => 'number', 'label' => 'Al (mg/kg)', 'required' => false, 'step' => '0.01', 'min' => '0'],
        'se' => ['type' => 'number', 'label' => 'Se (mg/kg)', 'required' => false, 'step' => '0.01', 'min' => '0'],
    ],
    'Base Saturation' => [
        'tec' => ['type' => 'number', 'label' => 'TEC', 'required' => false, 'step' => '0.01'],
        'cec' => ['type' => 'number', 'label' => 'CEC', 'required' => false, 'step' => '0.01'],
        'ca_mehlick3' => ['type' => 'number', 'label' => 'Ca Mehlich-3', 'required' => false, 'step' => '0.01'],
        'mg_mehlick3' => ['type' => 'number', 'label' => 'Mg Mehlich-3', 'required' => false, 'step' => '0.01'],
        'k_mehlick3' => ['type' => 'number', 'label' => 'K Mehlich-3', 'required' => false, 'step' => '0.01'],
        'na_mehlick3' => ['type' => 'number', 'label' => 'Na Mehlich-3', 'required' => false, 'step' => '0.01'],
        'al_mehlick3' => ['type' => 'number', 'label' => 'Al Mehlich-3', 'required' => false, 'step' => '0.01'],
    ],
];
?>

<?php foreach ($formSections as $sectionName => $fields): ?>
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="mb-0"><?= htmlspecialchars($sectionName, ENT_QUOTES, 'UTF-8') ?></h5>
        </div>
        <div class="card-body">
            <div class="row">
                <?php foreach ($fields as $fieldName => $fieldConfig): ?>
                    <div class="col-md-6 mb-3">
                        <label for="<?= htmlspecialchars($fieldName, ENT_QUOTES, 'UTF-8') ?>" class="form-label">
                            <?= htmlspecialchars($fieldConfig['label'], ENT_QUOTES, 'UTF-8') ?>
                            <?php if ($fieldConfig['required']): ?><span class="text-danger">*</span><?php endif; ?>
                        </label>

                        <?php if ($fieldConfig['type'] === 'select'): ?>
                            <select class="form-control" id="<?= htmlspecialchars($fieldName, ENT_QUOTES, 'UTF-8') ?>"
                                    name="<?= htmlspecialchars($fieldName, ENT_QUOTES, 'UTF-8') ?>"
                                    <?= $fieldConfig['required'] ? 'required' : '' ?>>
                                <option value="">Select...</option>
                                <?php foreach ($fieldConfig['options'] as $option): ?>
                                    <option value="<?= htmlspecialchars($option, ENT_QUOTES, 'UTF-8') ?>">
                                        <?= htmlspecialchars($option, ENT_QUOTES, 'UTF-8') ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        <?php else: ?>
                            <input type="<?= htmlspecialchars($fieldConfig['type'], ENT_QUOTES, 'UTF-8') ?>"
                                   class="form-control"
                                   id="<?= htmlspecialchars($fieldName, ENT_QUOTES, 'UTF-8') ?>"
                                   name="<?= htmlspecialchars($fieldName, ENT_QUOTES, 'UTF-8') ?>"
                                   placeholder="<?= htmlspecialchars($fieldConfig['placeholder'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
                                   <?= $fieldConfig['required'] ? 'required' : '' ?>
                                   <?php if (isset($fieldConfig['step'])): ?>step="<?= htmlspecialchars($fieldConfig['step'], ENT_QUOTES, 'UTF-8') ?>"<?php endif; ?>
                                   <?php if (isset($fieldConfig['min'])): ?>min="<?= htmlspecialchars($fieldConfig['min'], ENT_QUOTES, 'UTF-8') ?>"<?php endif; ?>
                                   <?php if (isset($fieldConfig['max'])): ?>max="<?= htmlspecialchars($fieldConfig['max'], ENT_QUOTES, 'UTF-8') ?>"<?php endif; ?>>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
<?php endforeach; ?>

<!-- Hidden fields for form processing -->
<input type="hidden" name="m_user" value="<?= htmlspecialchars($_SESSION['user_id'] ?? '1', ENT_QUOTES, 'UTF-8') ?>">