<?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\SignupCriteria;
use Signteer\Core\Criteria\TaskCriteria;
use Signteer\Core\Finder\SheetFinder;
use Signteer\Core\Finder\SignupFinder;
use Signteer\Core\Finder\TaskFinder;
use Signteer\Core\Model\SignupFieldFinder;

final class ListSignupsPage
{
    public $finder = SignupFinder::class;
    public $taskFinder = TaskFinder::class;
    public $signupFinder = SignupFinder::class;
    public $sheetFinder = SheetFinder::class;

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

        $sheet = $this->sheetFinder->get($sheetId);
        if (null === $sheet) {
            return new Reqirect('admin/sheet');
        }

        $c = (new SignupCriteria())->whereSheetId('=', $sheetId);
        $signups = $this->signupFinder->findWithVolunteer($c);

        $c = (new TaskCriteria())->whereSheetId('=', $sheetId);
        $tasks = $this->taskFinder->find($c);

        $html = new Html();

        $ret = $html->blockList()->setSpacing(3);

    // permalink
        $wpPostId = $sheet->getId();
        $permalink = get_permalink($wpPostId);
        $permalink = $html->link()->setFull($permalink)->setLabel($permalink)
            // ->withAttr('target', '_blank')
            ;
        // $v = new Card($permalink);
        $v = $permalink;
        $ret->add($html->dl('__Permalink__', $v)->asTable());

        foreach ($tasks as $task) {
            $signups_ = array_filter($signups, fn($e) => ($e->getTaskId() === $task->getId()));

            $ret_ = $html->table();
            // $ret_->setCaption($task->getTitle());
            $ret_->withHeadCell('head', 'no')->setContent('#')->withAttr('class', 'pw-col-align-center')->setWidth(1);
            $ret_->withHeadCell('head', 'title')->setContent('__Volunteer__');
            $ret_->withHeadCell('head', 'email')->setContent('__E-mail__');
            $ret_->withHeadCell('head', 'phone')->setContent('__Phone__');

            $totalSpots = $task->getSpots();
            $signedUpCount = 0;

            for ($i = 1; $i <= $totalSpots; $i++) {
                $ret_->withCell($i, 'no')->setContent($i)->withAttr('class', 'pw-col-align-center');
                $signup_ = ($signups_) ? array_shift($signups_) : null;
                if ($signup_) {
                    ++$signedUpCount;
                    $id = $signup_->getId();
                    $volunteer_ = $signup_->getVolunteer();
                    $vid = $volunteer_->getId();
                    $tid = $task->getId();

                    $v = $volunteer_->getTitle();
                    $v = $html->link('admin/sheet/' . $sheetId . '/signup/' . $id, [], $v);
                    $ret_->withCell($i, 'title')->setContent($v);

                    $v = $volunteer_->getEmail();
                    $ret_->withCell($i, 'email')->setContent($v);

                    $v = $volunteer_->getPhone();
                    $ret_->withCell($i, 'phone')->setContent($v);
                } else {
                    $v = '__(Empty)__';
                    $v = $html->el('strong', $v);
                    $ret_->withCell($i, 'title')->setContent($v)->withAttr('colspan', 3);
                }
            }

            $taskId = $task->getId();
            $head_ = $html->menu()
                ->add($html->el('h3', $task->getTitle()))
                ;

            if ($signedUpCount < $totalSpots) {
                $head_->add($html->link('admin/sheet/' . $sheetId . '/assign', ['task' => $taskId], '__Assign volunteer__'));
            }

            $ret_ = $html->blockList($head_, $ret_);

            $ret->add($ret_);
        }

        $pageTitle = $sheet->getTitle();
        $ret = $html->main($ret);
        $ret->title = $html->el('h1', $pageTitle);
        $ret->menu->add($html->link('admin/sheet/' . $sheetId . '/edit', [], '__Edit sheet details__'));
        $ret->menu->add($html->link('admin/sheet/' . $sheetId . '/task', [], '__Manage sheet tasks__'));

        return $ret;
    }
}
