<?php
/**
 * PmTemplatesController — CRUD for fleet PM templates.
 * Replaces the rsflow asset-checklist controller of the same name; the
 * underlying pm_templates table was never imported into Pacific.
 */
class PmTemplatesController extends Controller
{
    public function indexAction()
    {
        $this->view->setLayout($this->roleLayout());
        $this->view->render('PmTemplates/index', [
            'page_title' => 'PM Templates',
            'templates'  => (new PmTemplates())->listAll(),
        ]);
    }

    public function addAction()
    {
        $this->view->setLayout($this->roleLayout());
        $this->view->render('PmTemplates/edit', [
            'page_title' => 'Add PM Template',
            'tpl'        => [],
            'form'       => [],
            'is_new'     => true,
            'fb_msg'     => '',
        ]);
    }

    public function saveAction()
    {
        $this->verifyCsrf();
        if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') { Router::redirect('PmTemplates/add'); return; }
        $r = (new PmTemplates())->create($_POST, $this->currentUserId());
        if (!$r['ok']) {
            $this->view->setLayout($this->roleLayout());
            $this->view->render('PmTemplates/edit', [
                'page_title' => 'Add PM Template',
                'tpl'        => [],
                'form'       => $_POST,
                'is_new'     => true,
                'fb_msg'     => '<div class="alert alert-danger">' . htmlspecialchars($r['msg']) . '</div>',
            ]);
            return;
        }
        $_SESSION['flash_tenant_msg']  = $r['msg'];
        $_SESSION['flash_tenant_type'] = 'success';
        Router::redirect('PmTemplates');
    }

    public function editAction($id = 0)
    {
        $tpl = (new PmTemplates())->byId((int)$id);
        if (!$tpl) { $this->notFound('Template'); return; }
        $this->view->setLayout($this->roleLayout());
        $this->view->render('PmTemplates/edit', [
            'page_title' => 'Edit · ' . $tpl['name'],
            'tpl'        => $tpl,
            'form'       => $tpl,
            'is_new'     => false,
            'fb_msg'     => '',
        ]);
    }

    public function updateAction($id = 0)
    {
        $this->verifyCsrf();
        if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') { Router::redirect('PmTemplates/edit/' . (int)$id); return; }
        $r = (new PmTemplates())->update((int)$id, $_POST, $this->currentUserId());
        $_SESSION['flash_tenant_msg']  = $r['msg'];
        $_SESSION['flash_tenant_type'] = $r['ok'] ? 'success' : 'danger';
        Router::redirect('PmTemplates');
    }

    public function toggleAction($id = 0)
    {
        $this->verifyCsrf();
        if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') { Router::redirect('PmTemplates'); return; }
        (new PmTemplates())->toggleActive((int)$id, $this->currentUserId());
        $_SESSION['flash_tenant_msg']  = 'Template toggled.';
        $_SESSION['flash_tenant_type'] = 'success';
        Router::redirect('PmTemplates');
    }

    public function deleteAction($id = 0)
    {
        $this->verifyCsrf();
        if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') { Router::redirect('PmTemplates'); return; }
        (new PmTemplates())->softDelete((int)$id, $this->currentUserId());
        $_SESSION['flash_tenant_msg']  = 'Template removed.';
        $_SESSION['flash_tenant_type'] = 'success';
        Router::redirect('PmTemplates');
    }

    private function notFound(string $what): void
    {
        $this->view->setLayout($this->roleLayout());
        $this->view->render('generals/fb_page', [
            'page_title' => 'Not found',
            'fb_msg'     => '<div class="alert alert-danger">' . htmlspecialchars($what) . ' not found.</div>',
        ]);
    }
}
