<?php $this->start('body');
    $settings = $this->data['settings'] ?? [];
    $get = function (string $k, string $default = '') use ($settings): string {
        return isset($settings[$k]) ? (string)$settings[$k]['setting_value'] : $default;
    };
    $desc = function (string $k) use ($settings): string {
        return isset($settings[$k]) ? (string)($settings[$k]['description'] ?? '') : '';
    };
    $editedBy = function (string $k) use ($settings): string {
        if (empty($settings[$k])) return '';
        $s = $settings[$k];
        if (empty($s['edited_by_name'])) return '';
        return 'Last edited by ' . $s['edited_by_name'] . ' · ' . date('d M · H:i', strtotime((string)$s['edited_on']));
    };
?>

<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-sliders-h me-2 text-muted"></i>Finance Settings</h5>
        <div class="text-muted small mt-1" style="font-size:.72rem;">
            Finance-owned tunables. Only Finance Manager, Executive, and Administrator can edit.
        </div>
    </div>
</div>

<div class="card border-0 shadow-sm mb-3">
    <div class="card-header py-2 small" style="background:#F8FAFC;border-bottom:1px solid #E2E8F0;font-weight:600;color:#334155;">
        <i class="fas fa-binoculars me-1 text-muted"></i>Product Discovery
        <span class="text-muted ms-2" style="font-weight:400;font-size:.65rem;">Guards Procurement's Discovery cash requests against uncontrolled spend</span>
    </div>
    <div class="card-body p-3">
        <form id="fs_form" onsubmit="return false;">
            <div class="row g-3">
                <div class="col-md-6">
                    <label class="form-label small" style="font-weight:600;color:#334155;">Discovery cap (% of Client PO total)</label>
                    <div class="input-group input-group-sm" style="max-width:220px;">
                        <input type="number" step="0.01" min="0" max="100" name="discovery_cap_pct" class="form-control" value="<?= htmlspecialchars($get('discovery_cap_pct', '5')); ?>">
                        <span class="input-group-text">%</span>
                    </div>
                    <div class="text-muted mt-1" style="font-size:.65rem;"><?= htmlspecialchars($desc('discovery_cap_pct')); ?></div>
                    <?php $eb = $editedBy('discovery_cap_pct'); if ($eb): ?><div class="text-muted" style="font-size:.6rem;"><?= htmlspecialchars($eb); ?></div><?php endif; ?>
                </div>
                <div class="col-md-6">
                    <label class="form-label small" style="font-weight:600;color:#334155;">Exec-approval threshold (% of Client PO total)</label>
                    <div class="input-group input-group-sm" style="max-width:220px;">
                        <input type="number" step="0.01" min="0" max="100" name="discovery_exec_threshold_pct" class="form-control" value="<?= htmlspecialchars($get('discovery_exec_threshold_pct', '3')); ?>">
                        <span class="input-group-text">%</span>
                    </div>
                    <div class="text-muted mt-1" style="font-size:.65rem;"><?= htmlspecialchars($desc('discovery_exec_threshold_pct')); ?></div>
                    <?php $eb = $editedBy('discovery_exec_threshold_pct'); if ($eb): ?><div class="text-muted" style="font-size:.6rem;"><?= htmlspecialchars($eb); ?></div><?php endif; ?>
                </div>
            </div>
            <div class="alert py-2 mt-3 mb-0 small" style="background:#EEF2F6;border:1px solid #7A8BA1;color:#334155;font-size:.65rem;">
                <i class="fas fa-info-circle me-1"></i>
                Exec threshold must be ≤ cap. Requests above the Exec threshold route through the 4-step chain (PM → Accountant → FM → Exec). Below → 3-step (PM → Accountant → FM).
            </div>
            <div class="text-end mt-3">
                <button type="button" id="btn_save_fs" class="btn btn-sm" style="background:#7A9E7E;border-color:#7A9E7E;color:#fff;font-weight:600;">
                    <i class="fas fa-save me-1"></i>Save settings
                </button>
            </div>
        </form>
    </div>
</div>

<script>
(function () {
    document.getElementById('btn_save_fs').addEventListener('click', function () {
        var form = document.getElementById('fs_form');
        var payload = {
            discovery_cap_pct: form.discovery_cap_pct.value,
            discovery_exec_threshold_pct: form.discovery_exec_threshold_pct.value
        };
        $.post('<?= PROOT; ?>FinanceSettings/updateAj', payload, function (r) {
            if (r && r.ok) { rsToast('Saved.', 'success'); setTimeout(function(){location.reload();}, 400); }
            else $.alert({title:'Could not save', content:(r && r.msg) || 'Server error', type:'red'});
        }, 'json').fail(function () { $.alert({title:'Network error', type:'red'}); });
    });
})();
</script>

<?php $this->end(); ?>
