<?php $isEdit = !empty($schedule); ?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-ruler-combined me-2 text-primary"></i><?= $isEdit ? 'Edit' : 'New' ?> Calibration Schedule</h5>
    <a href="<?= url('/Calibration') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<div class="rs-card p-3">
<form method="post" action="<?= url('/Calibration/save') ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="cal_schedule_id" value="<?= (int) ($schedule['cal_schedule_id'] ?? 0) ?>">
    <div class="row g-2">
        <div class="col-md-6"><label class="form-label">Name *</label>
            <input type="text" class="form-control form-control-sm" name="name" required value="<?= e($schedule['name'] ?? '') ?>" placeholder="e.g. Boiler steam pressure gauge">
        </div>
        <div class="col-md-6"><label class="form-label">Instrument (asset) *</label>
            <select name="asset_id" class="form-select form-select-sm" required>
                <option value="">— pick asset —</option>
                <?php foreach ($assets as $a): ?>
                    <option value="<?= (int) $a['asset_id'] ?>" <?= (int) ($schedule['asset_id'] ?? 0) === (int) $a['asset_id'] ? 'selected' : '' ?>><?= e($a['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-4"><label class="form-label">Standard</label>
            <input type="text" class="form-control form-control-sm" name="standard" value="<?= e($schedule['standard'] ?? '') ?>" placeholder="e.g. ISO 17025 / NIST-traceable">
        </div>
        <div class="col-md-2"><label class="form-label">Interval (days) *</label>
            <input type="number" class="form-control form-control-sm" name="interval_days" min="1" value="<?= (int) ($schedule['interval_days'] ?? 365) ?>">
        </div>
        <div class="col-md-3"><label class="form-label">Tolerance</label>
            <input type="text" class="form-control form-control-sm" name="tolerance" value="<?= e($schedule['tolerance'] ?? '') ?>" placeholder="±0.5">
        </div>
        <div class="col-md-1"><label class="form-label">Unit</label>
            <input type="text" class="form-control form-control-sm" name="unit" value="<?= e($schedule['unit'] ?? '') ?>" placeholder="bar">
        </div>
        <div class="col-md-2 d-flex align-items-end">
            <div class="form-check">
                <input type="checkbox" class="form-check-input" name="is_active" value="1" id="calActive" <?= !$isEdit || $schedule['is_active'] ? 'checked' : '' ?>>
                <label for="calActive" class="form-check-label">Active</label>
            </div>
        </div>
        <?php if (!$isEdit): ?>
            <div class="col-md-3"><label class="form-label">Last calibrated</label>
                <input type="date" class="form-control form-control-sm" name="last_calibrated_at">
                <small class="text-muted">If set, next_due auto-computes.</small>
            </div>
            <div class="col-md-3"><label class="form-label">Next due (fallback)</label>
                <input type="date" class="form-control form-control-sm" name="next_due_at">
            </div>
        <?php endif; ?>
        <div class="col-12"><label class="form-label">Notes</label>
            <textarea class="form-control form-control-sm" name="notes" rows="2"><?= e($schedule['notes'] ?? '') ?></textarea>
        </div>
    </div>
    <div class="mt-3 d-flex gap-2">
        <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-save me-1"></i>Save</button>
    </div>
</form>
</div>

<?php if ($isEdit): ?>
    <form method="post" action="<?= url('/Calibration/delete/' . (int) $schedule['cal_schedule_id']) ?>"
          class="mt-3 rs-confirm-submit"
          data-confirm-title="Delete schedule?"
          data-confirm-body="Soft-delete <strong><?= e($schedule['name']) ?></strong>? Existing records stay in the database but the schedule stops driving next-due."
          data-confirm-ok="Delete">
        <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
        <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash me-1"></i>Delete schedule</button>
    </form>
<?php endif; ?>
