<?php
/**
 * PricingDefaultsController — Slice 8R.
 * Single-page settings surface for org-wide pricing defaults.
 * Read + write: CRM / Finance Manager / Executive / Administrator.
 *
 *   /PricingDefaults        edit form
 *   /PricingDefaults/saveAj POST the fields
 */
class PricingDefaultsController extends Controller
{
    public function indexAction()
    {
        if (!class_exists('PricingDefaults')) require_once ROOT . '/app/models/PricingDefaults.php';
        $defaults = (new PricingDefaults())->get();

        $this->view->setLayout($this->roleLayout());
        $this->view->render('PricingDefaults/index', [
            'page_title' => 'Pricing Defaults',
            'defaults'   => $defaults,
        ]);
    }

    public function saveAjAction()
    {
        header('Content-Type: application/json');
        try {
            if (!class_exists('PricingDefaults')) require_once ROOT . '/app/models/PricingDefaults.php';
            $res = (new PricingDefaults())->save($_POST,
                (int)($_SESSION['user_id']   ?? 0),
                (string)($_SESSION['full_name'] ?? ''));
            echo json_encode($res);
        } catch (Throwable $e) { echo json_encode(['ok' => false, 'msg' => $e->getMessage()]); }
    }
}
