<?php

use Fisharebest\Webtrees\Http\RequestHandlers\MessagePage;
use Fisharebest\Webtrees\Http\RequestHandlers\MessageSelect;
use Fisharebest\Webtrees\Http\RequestHandlers\UserPage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\ModuleInterface;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\UserService;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\User;
use Illuminate\Support\Collection;

/**
 * @var int                  $block_id
 * @var string               $context
 * @var ModuleInterface      $module
 * @var Tree                 $tree
 * @var Collection<stdClass> $messages
 * @var Collection<User>     $users
 * @var UserService          $user_service
 */

?>

<?php if ($users->isNotEmpty()): ?>
    <form method="post" action="<?= e(route(MessageSelect::class, ['tree' => $tree->name()])) ?>">
        <?= csrf_field() ?>
        <input type="hidden" name="url" value="<?= e(route(UserPage::class, ['tree' => $tree->name()])) ?>">
        <label for="to"><?= I18N::translate('Send a message') ?></label>

        <div class="input-group">
            <select class="form-control" id="to" name="to" required="required">
                <option value=""><?= I18N::translate('&lt;select&gt;') ?></option>
                <?php foreach ($users as $user): ?>
                    <option value="<?= e($user->userName()) ?>">
                        <?= e($user->realName()) ?> - <?= e($user->userName()) ?>
                    </option>
                <?php endforeach ?>
            </select>

            <button type="submit" class="btn btn-primary"><?= I18N::translate('Send') ?></button>
        </div>
        <br><br>
    </form>
<?php endif ?>

<form
    method="post"
    action="<?= e(route('module', ['action' => 'DeleteMessage', 'module' => $module->name(), 'context' => $context, 'tree' => $tree->name()])) ?>"
    data-wt-confirm="<?= I18N::translate('Are you sure you want to delete this message? It cannot be retrieved later.') ?>"
    onsubmit="return confirm(this.dataset.confirm);"
    id="messageform"
    name="messageform"
>
    <?= csrf_field() ?>

    <?php if ($messages->isNotEmpty()): ?>
        <div class="table-responsive">
            <table class="table-sm w-100">
                <tr>
                    <th class="wt-page-options-label">
                        <?= I18N::translate('Delete') ?>
                        <br>
                        <a href="#"
                           onclick="$('#block-<?= $block_id ?> :checkbox').prop('checked', true); return false;">
                            <?= I18N::translate('All') ?>
                        </a>
                    </th>
                    <th class="wt-page-options-label"><?= I18N::translate('Subject') ?></th>
                    <th class="wt-page-options-label"><?= I18N::translate('Date sent') ?></th>
                    <th class="wt-page-options-label"><?= I18N::translate('Email address') ?></th>
                </tr>

                <?php foreach ($messages as $message): ?>
                    <tr>
                        <td class="wt-page-options-value center">
                            <input type="checkbox" name="message_id[]" value="<?= $message->message_id ?>" id="cb_message<?= $message->message_id ?>">
                        </td>

                        <td class="wt-page-options-value">
                            <a href="#message<?= $message->message_id ?>" data-bs-toggle="collapse" role="button"
                               aria-expanded="false" aria-controls="message<?= $message->message_id ?>">
                                <?= view('icons/expand') ?>
                                <?= view('icons/collapse') ?>
                                <b dir="auto"><?= e($message->subject) ?></b>
                            </a>
                        </td>

                        <td class="wt-page-options-value">
                            <?= view('components/datetime', ['timestamp' => $message->created]) ?>
                        </td>

                        <td class="wt-page-options-value">
                            <?php $user = $user_service->findByIdentifier($message->sender) ?>

                            <?php if ($user instanceof User): ?>
                                <bdi><?= e($user->realName()) ?></bdi> - <bdi><?= $user->email() ?></bdi>
                            <?php else: ?>
                                <a href="mailto:<?= e($message->sender) ?>"><?= e($message->sender) ?></a>
                            <?php endif ?>
                        </td>
                    </tr>

                    <tr>
                        <td class="wt-page-options-value" colspan="4">
                            <div id="message<?= $message->message_id ?>" class="collapse">
                                <div dir="auto" style="white-space: pre-wrap;">
                                    <?= Registry::markdownFactory()->autolink()->convertToHtml($message->body) ?>
                                </div>
                                <br>

                                <?php
                                /* I18N: When replying to an email, the subject becomes “RE: subject” */
                                if (!str_starts_with($message->subject, I18N::translate('RE: '))) {
                                    $message->subject = I18N::translate('RE: ') . $message->subject;
                                }

                                // If this user still exists, show a reply link.
                                if ($user instanceof User) {
                                    $reply_url = route(MessagePage::class, [
                                        'subject' => $message->subject,
                                        'to'      => $user->userName(),
                                        'tree'    => $tree->name(),
                                        'url'     => route(UserPage::class, ['tree' => $tree->name()]),
                                    ]);
                                    ?>

                                    <a class="btn btn-primary" href="<?= e($reply_url) ?>"
                                       title="<?= I18N::translate('Reply') ?>">
                                        <?= I18N::translate('Reply') ?>
                                    </a>
                                <?php } ?>
                                <button type="button" class="btn btn-danger"
                                        data-wt-confirm="<?= I18N::translate('Are you sure you want to delete this message? It cannot be retrieved later.') ?>"
                                        onclick="if (confirm(this.dataset.confirm)) {$('#messageform :checkbox').prop('checked', false); $('#cb_message<?= $message->message_id ?>').prop('checked', true); document.messageform.submit();}">
                                    <?= I18N::translate('Delete') ?>
                                </button>
                            </div>
                        </td>
                    </tr>
                <?php endforeach ?>
            </table>
        </div>
        <p>
            <button type="submit"><?= I18N::translate('Delete selected messages') ?></button>
        </p>
    <?php endif ?>
</form>
