<?php $isEdit = !empty($template); ?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-route me-2 text-primary"></i><?= $isEdit ? 'Edit Round' : 'New Round' ?></h5>
    <a href="<?= url('/Rounds') ?>" 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 mb-3">
<form method="post" action="<?= url('/Rounds/saveTemplate') ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="round_template_id" value="<?= (int) ($template['round_template_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($template['name'] ?? '') ?>" placeholder="Morning Plant Sweep">
        </div>
        <div class="col-md-3"><label class="form-label">Frequency</label>
            <select name="frequency" class="form-select form-select-sm">
                <?php foreach (\App\Models\Round::FREQUENCIES as $f): ?>
                    <option value="<?= $f ?>" <?= ($template['frequency'] ?? 'daily') === $f ? 'selected' : '' ?>><?= $f ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-3 d-flex align-items-end">
            <div class="form-check">
                <input type="checkbox" class="form-check-input" name="is_active" value="1" id="rActive" <?= !isset($template) || $template['is_active'] ? 'checked' : '' ?>>
                <label for="rActive" class="form-check-label">Active</label>
            </div>
        </div>
        <div class="col-12"><label class="form-label">Description</label>
            <textarea class="form-control form-control-sm" name="description" rows="2" placeholder="What is this round for?"><?= e($template['description'] ?? '') ?></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>
        <?php if ($isEdit): ?>
            <form method="post" action="<?= url('/Rounds/deleteTemplate/' . (int) $template['round_template_id']) ?>"
                  class="d-inline ms-auto rs-confirm-submit"
                  data-confirm-title="Delete round?"
                  data-confirm-body="Soft-delete the round <strong><?= e($template['name']) ?></strong> and its stops? Previous runs remain in history."
                  data-confirm-ok="Delete">
                <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash me-1"></i>Delete round</button>
            </form>
        <?php endif; ?>
    </div>
</form>
</div>

<?php if ($isEdit): ?>
<div class="rs-card p-3">
    <h6 class="fw-bold mb-2"><i class="fas fa-map-signs me-1"></i>Stops (walked in order)</h6>
    <?php if (empty($stops)): ?>
        <p class="text-muted small">No stops yet — add assets below.</p>
    <?php else: ?>
        <table class="table table-sm table-hover mb-3 align-middle">
            <thead><tr><th style="width:14px;">#</th><th>Asset</th><th>Reading key</th><th class="text-center">Unit</th><th class="text-center">Min OK</th><th class="text-center">Max OK</th><th>Instruction</th><th></th></tr></thead>
            <tbody>
                <?php foreach ($stops as $s): ?>
                    <tr>
                        <td class="small text-muted"><?= (int) $s['sort_order'] ?></td>
                        <td><strong><?= e($s['asset_name']) ?></strong><div class="small text-muted"><?= e($s['asset_type_name'] ?? '') ?></div></td>
                        <td class="small"><?= e($s['reading_key'] ?? '—') ?></td>
                        <td class="text-center small"><?= e($s['reading_unit'] ?? '') ?></td>
                        <td class="text-center small"><?= $s['min_ok'] !== null ? e($s['min_ok']) : '—' ?></td>
                        <td class="text-center small"><?= $s['max_ok'] !== null ? e($s['max_ok']) : '—' ?></td>
                        <td class="small"><?= e($s['instruction'] ?? '') ?></td>
                        <td class="text-end">
                            <form method="post" action="<?= url('/Rounds/deleteStop/' . (int) $s['stop_id']) ?>"
                                  class="d-inline rs-confirm-submit"
                                  data-confirm-title="Remove stop?"
                                  data-confirm-body="Remove <strong><?= e($s['asset_name']) ?></strong> from this round?"
                                  data-confirm-ok="Remove">
                                <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
                                <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-times"></i></button>
                            </form>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>

    <form method="post" action="<?= url('/Rounds/addStop/' . (int) $template['round_template_id']) ?>">
        <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
        <div class="row g-2">
            <div class="col-md-3"><label class="form-label">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'] ?>"><?= e($a['name']) ?></option><?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2"><label class="form-label">Reading key</label>
                <input type="text" class="form-control form-control-sm" name="reading_key" placeholder="temp / rpm / bar">
            </div>
            <div class="col-md-1"><label class="form-label">Unit</label>
                <input type="text" class="form-control form-control-sm" name="reading_unit" placeholder="°C">
            </div>
            <div class="col-md-1"><label class="form-label">Min OK</label>
                <input type="number" step="any" class="form-control form-control-sm" name="min_ok">
            </div>
            <div class="col-md-1"><label class="form-label">Max OK</label>
                <input type="number" step="any" class="form-control form-control-sm" name="max_ok">
            </div>
            <div class="col-md-3"><label class="form-label">Instruction (optional)</label>
                <input type="text" class="form-control form-control-sm" name="instruction" placeholder="Check for leaks">
            </div>
            <div class="col-md-1 d-flex align-items-end"><button type="submit" class="btn btn-sm btn-primary w-100"><i class="fas fa-plus"></i></button></div>
        </div>
    </form>
</div>
<?php endif; ?>
