<?php
class JournalsController extends Controller
{
    public function indexAction()
    {
        $jp   = new JournalPosting();
        $rows = $jp->list(
            (string)($_GET['source_type'] ?? ''),
            (string)($_GET['from'] ?? ''),
            (string)($_GET['to']   ?? '')
        );
        $this->view->setLayout($this->roleLayout());
        $this->view->render('Journals/index', [
            'page_title'  => 'Journals',
            'rows'        => $rows,
            'source_type' => (string)($_GET['source_type'] ?? ''),
            'from'        => (string)($_GET['from'] ?? ''),
            'to'          => (string)($_GET['to']   ?? ''),
        ]);
    }

    public function viewAction($id = 0)
    {
        $jp = new JournalPosting();
        $je = $jp->byId((int)$id);
        if (!$je) {
            $this->view->setLayout($this->roleLayout());
            $this->view->render('generals/fb_page', ['fb_msg' => '<div class="alert alert-danger">JE not found.</div>']);
            return;
        }
        $lines = $jp->linesForJe((int)$id);

        $this->view->setLayout($this->roleLayout());
        $this->view->render('Journals/view', [
            'page_title' => $je['je_no'],
            'je'         => $je,
            'lines'      => $lines,
        ]);
    }
}
