<?php

namespace Signteer\Web\Admin\Page\Sheet;

use Plainwork\Core\App;
use Plainwork\Http\Request;
use Plainwork\Http\Redirect;
use Plainwork\Ui\Html;
use Signteer\Core\Criteria\TaskCriteria as Criteria;
use Signteer\Core\Finder\TaskFinder;

final class ListSheetTasksPage
{
    public $finder = TaskFinder::class;

    public function __invoke(Request $request)
    {
        $sheetId = $request->getQueryParam('sheet');
        if (null === $sheetId) {
            return new Reqirect('../..');
        }

        $c = (new Criteria())->whereSheetId('=', $sheetId);
        $ms = $this->finder->find($c);

        $html = new Html();

        $ret = $html->menu()->setVertical();

        foreach ($ms as $m) {
            $id = $m->getId();

            $ret_ = $html->inlineList();
            $ret_->add($html->el('b', $m->getTitle()));
            $ret_->add('__Number of spots__' . ': ' . $m->getSpots());
            $ret_ = $html->link('./' . $id, [], $ret_);

            $ret->add($ret_);
        }

        $ret = $html->main($ret);
        $ret->title = $html->el('h1', '__Sheet\'s tasks__');
        $ret->titleMenu->add($html->link('./create', [], '__Add new__'));

        return $ret;
    }
}
