<?php
$pageTitle = 'Work Form — ' . e($request['request_number']);
include __DIR__ . '/../shared/header.php';

// Decode existing data if editing
$meas   = json_decode($form['measurement_data']    ?? '{}', true);
$refEq  = json_decode($form['reference_equipment'] ?? '[]', true);
$rows   = $meas['rows'] ?? [];
?>

<div class="page-header">
  <div>
    <div class="page-title"><i class="bi bi-pencil-square me-2"></i>Laboratory Work Form</div>
    <div class="page-subtitle">
      <code><?= e($request['request_number']) ?></code> &mdash;
      <?= e($request['equipment_name']) ?> &mdash;
      <span class="text-primary"><?= e($request['lab_name']) ?></span>
    </div>
  </div>
  <a href="<?= APP_URL ?>/index.php?page=requests&action=view&id=<?= $request['request_id'] ?>" class="btn btn-outline-secondary">
    <i class="bi bi-arrow-left me-1"></i>Back to Request
  </a>
</div>

<!-- Equipment Summary Bar -->
<div class="alert alert-light border mb-4">
  <div class="row g-2 small">
    <div class="col-md-3"><span class="text-muted">Equipment:</span> <strong><?= e($request['equipment_name']) ?></strong></div>
    <div class="col-md-2"><span class="text-muted">Manufacturer:</span> <?= e($request['manufacturer']) ?></div>
    <div class="col-md-2"><span class="text-muted">Model:</span> <?= e($request['model']) ?></div>
    <div class="col-md-2"><span class="text-muted">Serial:</span> <code><?= e($request['serial_number']) ?></code></div>
    <div class="col-md-3"><span class="text-muted">Company:</span> <?= e($request['company_name']) ?></div>
  </div>
</div>

<form method="POST" id="workFormMain">
  <input type="hidden" name="csrf_token" value="<?= csrfToken() ?>">

  <!-- 1. Environmental Conditions -->
  <div class="form-section">
    <div class="form-section-title"><i class="bi bi-thermometer-half me-2"></i>1. Environmental Conditions</div>
    <div class="row g-3">
      <div class="col-md-4">
        <label class="form-label">Place of Test</label>
        <input type="text" name="place_of_test" class="form-control" value="<?= e($form['place_of_test'] ?? '') ?>" placeholder="e.g. GSA Temperature Laboratory">
      </div>
      <div class="col-md-2">
        <label class="form-label">Temp. Min (°C)</label>
        <input type="number" step="0.01" name="temperature_min" class="form-control" value="<?= e($form['temperature_min'] ?? '') ?>" placeholder="20.0">
      </div>
      <div class="col-md-2">
        <label class="form-label">Temp. Max (°C)</label>
        <input type="number" step="0.01" name="temperature_max" class="form-control" value="<?= e($form['temperature_max'] ?? '') ?>" placeholder="23.0">
      </div>
      <div class="col-md-2">
        <label class="form-label">Humidity Min (%RH)</label>
        <input type="number" step="0.1" name="humidity_min" class="form-control" value="<?= e($form['humidity_min'] ?? '') ?>" placeholder="45">
      </div>
      <div class="col-md-2">
        <label class="form-label">Humidity Max (%RH)</label>
        <input type="number" step="0.1" name="humidity_max" class="form-control" value="<?= e($form['humidity_max'] ?? '') ?>" placeholder="65">
      </div>
      <div class="col-md-2">
        <label class="form-label">Pressure Min (hPa)</label>
        <input type="number" step="0.1" name="pressure_min" class="form-control" value="<?= e($form['pressure_min'] ?? '') ?>" placeholder="1010">
      </div>
      <div class="col-md-2">
        <label class="form-label">Pressure Max (hPa)</label>
        <input type="number" step="0.1" name="pressure_max" class="form-control" value="<?= e($form['pressure_max'] ?? '') ?>" placeholder="1025">
      </div>
    </div>
  </div>

  <!-- 2. Reference Standards -->
  <div class="form-section">
    <div class="form-section-title"><i class="bi bi-shield-check me-2"></i>2. Reference Standards / Equipment Used</div>
    <div class="table-responsive mb-2">
      <table class="table table-bordered table-sm mb-0">
        <thead class="table-light">
          <tr>
            <th style="min-width:250px">Equipment Name</th>
            <th style="min-width:160px">Serial No.</th>
            <th style="min-width:200px">Certificate No.</th>
            <th style="width:60px"></th>
          </tr>
        </thead>
        <tbody id="refTableBody">
          <?php foreach ($refEq as $i => $r): ?>
          <tr>
            <td><input type="text" name="ref_equipment[<?= $i ?>][name]"   class="form-control form-control-sm" value="<?= e($r['name']  ?? '') ?>"></td>
            <td><input type="text" name="ref_equipment[<?= $i ?>][serial]" class="form-control form-control-sm" value="<?= e($r['serial'] ?? '') ?>"></td>
            <td><input type="text" name="ref_equipment[<?= $i ?>][cert]"   class="form-control form-control-sm" value="<?= e($r['cert']   ?? '') ?>"></td>
            <td><button type="button" class="btn btn-sm btn-outline-danger remove-ref"><i class="bi bi-trash"></i></button></td>
          </tr>
          <?php endforeach; ?>
          <?php if (empty($refEq)): ?>
          <tr>
            <td><input type="text" name="ref_equipment[0][name]"   class="form-control form-control-sm" placeholder="e.g. Platinum Resistance Thermometer"></td>
            <td><input type="text" name="ref_equipment[0][serial]" class="form-control form-control-sm" placeholder="SN-12345"></td>
            <td><input type="text" name="ref_equipment[0][cert]"   class="form-control form-control-sm" placeholder="GSA/THL/2025/001"></td>
            <td><button type="button" class="btn btn-sm btn-outline-danger remove-ref"><i class="bi bi-trash"></i></button></td>
          </tr>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
    <button type="button" id="addRefRow" class="btn btn-sm btn-outline-secondary">
      <i class="bi bi-plus-circle me-1"></i>Add Reference Equipment
    </button>
  </div>

  <!-- 3. Calibration Procedure -->
  <div class="form-section">
    <div class="form-section-title"><i class="bi bi-journal-code me-2"></i>3. Calibration Procedure</div>
    <div class="row g-3">
      <div class="col-md-6">
        <label class="form-label">Procedure Used</label>
        <input type="text" name="procedure_used" class="form-control" value="<?= e($form['procedure_used'] ?? '') ?>" placeholder="e.g. GSA-SM-THL-P001">
      </div>
      <div class="col-md-6">
        <label class="form-label">Standard / Norm</label>
        <input type="text" name="norm_standard" class="form-control" value="<?= e($form['norm_standard'] ?? '') ?>" placeholder="e.g. EURAMET cg-11, IEC 60751">
      </div>
    </div>
  </div>

  <!-- 4. Measurement Results -->
  <div class="form-section">
    <div class="form-section-title"><i class="bi bi-table me-2"></i>4. Measurement Results</div>
    <div class="table-responsive mb-2">
      <table class="table table-bordered table-sm mb-0">
        <thead class="table-light">
          <tr>
            <th style="min-width:180px">Measurement / Point</th>
            <th style="min-width:120px">Nominal Value</th>
            <th style="min-width:120px">Measured Value</th>
            <th style="min-width:120px">Error</th>
            <th style="min-width:140px">Expanded Uncertainty</th>
            <th style="min-width:80px">Unit</th>
            <th style="width:60px"></th>
          </tr>
        </thead>
        <tbody id="measTableBody">
          <?php foreach ($rows as $i => $row): ?>
          <tr>
            <td><input type="text"   name="meas_rows[<?= $i ?>][name]"        class="form-control form-control-sm" value="<?= e($row['name']        ?? '') ?>" placeholder="e.g. Repeatability"></td>
            <td><input type="number" name="meas_rows[<?= $i ?>][nominal]"     class="form-control form-control-sm" step="any" value="<?= e($row['nominal']     ?? '') ?>"></td>
            <td><input type="number" name="meas_rows[<?= $i ?>][measured]"    class="form-control form-control-sm" step="any" value="<?= e($row['measured']    ?? '') ?>"></td>
            <td><input type="number" name="meas_rows[<?= $i ?>][error]"       class="form-control form-control-sm" step="any" value="<?= e($row['error']       ?? '') ?>"></td>
            <td><input type="number" name="meas_rows[<?= $i ?>][uncertainty]" class="form-control form-control-sm" step="any" value="<?= e($row['uncertainty'] ?? '') ?>"></td>
            <td><input type="text"   name="meas_rows[<?= $i ?>][unit]"        class="form-control form-control-sm" value="<?= e($row['unit']        ?? '') ?>" placeholder="°C"></td>
            <td><button type="button" class="btn btn-sm btn-outline-danger remove-row"><i class="bi bi-trash"></i></button></td>
          </tr>
          <?php endforeach; ?>
          <?php if (empty($rows)): // default 3 empty rows ?>
          <?php for ($i=0; $i<3; $i++): ?>
          <tr>
            <td><input type="text"   name="meas_rows[<?= $i ?>][name]"        class="form-control form-control-sm" placeholder="Point <?= $i+1 ?>"></td>
            <td><input type="number" name="meas_rows[<?= $i ?>][nominal]"     class="form-control form-control-sm" step="any"></td>
            <td><input type="number" name="meas_rows[<?= $i ?>][measured]"    class="form-control form-control-sm" step="any"></td>
            <td><input type="number" name="meas_rows[<?= $i ?>][error]"       class="form-control form-control-sm" step="any"></td>
            <td><input type="number" name="meas_rows[<?= $i ?>][uncertainty]" class="form-control form-control-sm" step="any"></td>
            <td><input type="text"   name="meas_rows[<?= $i ?>][unit]"        class="form-control form-control-sm" placeholder="°C"></td>
            <td><button type="button" class="btn btn-sm btn-outline-danger remove-row"><i class="bi bi-trash"></i></button></td>
          </tr>
          <?php endfor; endif; ?>
        </tbody>
      </table>
    </div>
    <button type="button" id="addMeasRow" class="btn btn-sm btn-outline-secondary">
      <i class="bi bi-plus-circle me-1"></i>Add Row
    </button>
  </div>

  <!-- 5. Uncertainty -->
  <div class="form-section">
    <div class="form-section-title"><i class="bi bi-calculator me-2"></i>5. Measurement Uncertainty</div>
    <div class="row g-3">
      <div class="col-md-8">
        <label class="form-label">Uncertainty Statement</label>
        <textarea name="uncertainty_statement" class="form-control" rows="4" placeholder="The reported expanded uncertainty of measurement is stated as the standard uncertainty of measurement multiplied by the coverage factor k = 2, which for a normal distribution corresponds to a coverage probability of approximately 95%..."><?= e($form['uncertainty_statement'] ?? '') ?></textarea>
      </div>
      <div class="col-md-2">
        <label class="form-label">Coverage Factor (k)</label>
        <input type="number" step="0.01" name="coverage_factor" class="form-control" value="<?= e($form['coverage_factor'] ?? '2.00') ?>">
      </div>
      <div class="col-md-2">
        <label class="form-label">Confidence Level</label>
        <input type="text" name="confidence_level" class="form-control" value="<?= e($form['confidence_level'] ?? '95%') ?>">
      </div>
    </div>
  </div>

  <!-- 6. Remarks -->
  <div class="form-section">
    <div class="form-section-title"><i class="bi bi-chat-text me-2"></i>6. Remarks &amp; Observations</div>
    <textarea name="remarks" class="form-control" rows="3" placeholder="Any additional remarks, observations, or conditions affecting the calibration..."><?= e($form['remarks'] ?? '') ?></textarea>
  </div>

  <!-- Action Buttons -->
  <div class="d-flex gap-3 align-items-center">
    <button type="submit" name="form_status" value="draft" class="btn btn-outline-primary btn-lg">
      <i class="bi bi-floppy me-2"></i>Save as Draft
    </button>
    <button type="submit" name="form_status" value="submitted" class="btn btn-success btn-lg">
      <i class="bi bi-check-circle me-2"></i>Submit &amp; Generate Certificate
    </button>
    <a href="<?= APP_URL ?>/index.php?page=requests&action=view&id=<?= $request['request_id'] ?>" class="btn btn-outline-secondary">Cancel</a>
  </div>
</form>

<?php include __DIR__ . '/../shared/footer.php'; ?>
