<?php $this->start('body');
    $rows = $this->data['rows'] ?? [];
?>
<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-shield-alt me-1 text-muted"></i> Callout Playbooks</h5>
        <div class="text-muted" style="font-size:.72rem;">
            SLA + escalation standards attached to Services projects for unplanned response.
        </div>
    </div>
    <button type="button" class="btn btn-sm btn-primary" onclick="cpNew()" style="background:#7A9E7E;border-color:#7A9E7E;color:#fff;font-weight:600;">
        <i class="fas fa-plus me-1"></i> New playbook
    </button>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-body p-0">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th style="width:120px;">Code</th>
                    <th>Name</th>
                    <th style="width:100px;">Status</th>
                    <th style="width:130px;">Created</th>
                    <th style="width:100px;" class="text-end">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r): ?>
                    <tr>
                        <td><code><?= htmlspecialchars($r['code']); ?></code></td>
                        <td>
                            <strong><?= htmlspecialchars($r['name']); ?></strong>
                            <?php if (!empty($r['description'])): ?><div class="text-muted small"><?= htmlspecialchars(mb_strimwidth($r['description'], 0, 120, '…')); ?></div><?php endif; ?>
                        </td>
                        <td>
                            <?php if ((int)$r['is_active'] === 1): ?>
                                <span class="badge" style="background:#22c55e;color:#fff;">Active</span>
                            <?php else: ?>
                                <span class="badge" style="background:#94a3b8;color:#fff;">Retired</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-muted small"><?= htmlspecialchars($r['created_on']); ?></td>
                        <td class="text-end">
                            <a href="<?= PROOT; ?>CalloutPlaybooks/edit/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-pen"></i> Edit</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                    <tr><td colspan="5" class="text-center text-muted py-4">No playbooks yet — click "New playbook" to author your first.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $this->end(); ?>

<?php $this->start('section_js'); ?>
<script>
function cpNew() {
    $.confirm({
        title: 'New playbook',
        content: `
            <div class="row g-2">
                <div class="col-md-4"><label class="form-label small">Code</label><input id="pb_code" class="form-control form-control-sm" placeholder="e.g. STD_TELCO"></div>
                <div class="col-md-8"><label class="form-label small">Name</label><input id="pb_name" class="form-control form-control-sm" placeholder="e.g. Standard telco site SLA"></div>
                <div class="col-12"><label class="form-label small">Description</label><textarea id="pb_desc" class="form-control form-control-sm" rows="2"></textarea></div>
            </div>`,
        type: 'green',
        buttons: {
            save: { text: 'Create', btnClass: 'btn-success', action: function () {
                var payload = {
                    code: $('#pb_code').val().trim(),
                    name: $('#pb_name').val().trim(),
                    description: $('#pb_desc').val(),
                };
                if (!payload.code || !payload.name) { rsToast('Code + name required.', 'error'); return false; }
                $.post('<?= PROOT; ?>CalloutPlaybooks/createAj', payload, 'json')
                    .done(function (r) {
                        if (r && r.ok) location.href = '<?= PROOT; ?>CalloutPlaybooks/edit/' + r.id;
                        else rsToast('Failed: ' + (r.msg || 'unknown'), 'error');
                    });
            }},
            cancel: { text: 'Cancel', btnClass: 'btn-light' }
        }
    });
}
</script>
<?php $this->end(); ?>
