<?php

namespace Signteer\Web\Admin\Page\Sheet;

use Plainwork\Core\App;
use Plainwork\Http\Request;
use Plainwork\Ui\Html;
use Signteer\Core\Criteria\SheetCriteria as Criteria;
use Signteer\Core\Finder\SheetFinder;

final class ListSheetsPage
{
    public $finder = SheetFinder::class;

    public function __invoke(Request $request)
    {
        $c = (new Criteria());
        $ms = $this->finder->findWithTasks($c);

        $html = new Html;

        $ret = $html->menu()->setVertical();
        foreach ($ms as $m) {
            $id = $m->getId();
            $tasks = $m->getTasks();
            $countTasks = count($tasks);
            $countSpots = array_sum(array_map(fn($e) => $e->getSpots(), $tasks));

            $ret_ = $html->inlineList();
            $ret_->add($html->el('b', $m->getTitle()));
            $ret_->add('__Tasks__' . ': ' . $countTasks);
            $ret_->add('__Spots__' . ': ' . $countSpots);
            $ret_->add('__Filled spots__' . ': ' . $countSpots);

            // $ret_ = (new BlockList())->setSpacing(0)
                // ->add($m->getTitle())
                // ->add('__Spots__' . ': ' . $countSpots)
                // ;
            $ret_ = $html->link('./' . $id, [], $ret_);
            $ret->add($ret_);
        }

        $ret = $html->main($ret);
        $ret->title = $html->el('h1', '__Sign-up sheets__');
        $ret->titleMenu->add($html->link('./create', [], '__Add new__'));
        $ret->titleMenu->add($html->link('./download', [], $html->icon()->down(), '__Download sign-ups__')->withAttr('target', '_blank'));

        return $ret;
    }
}
