<?php

declare(strict_types=1);

/**
 * 會員頁 — N 幣歷程分頁。
 *
 * @var array{earned:int,reverted:int,spent?:int,net:int,count:int} $ncoinSummary
 * @var list<array<string,mixed>> $ncoinHistory
 * @var int $ncoinPage
 * @var int $ncoinPages
 * @var int $ncoinTotal
 * @var int $ncoinRangeFrom
 * @var int $ncoinRangeTo
 * @var list<int|null> $ncoinPagerItems
 * @var Closure $buildNcoinPageUrl
 * @var string $ncoinNameEsc
 */

?>
<div class="member-profile-tab-panel__inner">
    <div class="member-profile-ncoin-sum">
        <span>累計取得 <b>+<?= htmlspecialchars(number_format($ncoinSummary['earned']), ENT_QUOTES, 'UTF-8') ?></b></span>
        <?php if (($ncoinSummary['spent'] ?? 0) > 0): ?>
            <span>累計支出 <b class="member-profile-ncoin-sum__bad">-<?= htmlspecialchars(number_format((int) $ncoinSummary['spent']), ENT_QUOTES, 'UTF-8') ?></b></span>
        <?php endif; ?>
        <?php if ($ncoinSummary['reverted'] > 0): ?>
            <span>已扣回 <b class="member-profile-ncoin-sum__bad">-<?= htmlspecialchars(number_format($ncoinSummary['reverted']), ENT_QUOTES, 'UTF-8') ?></b></span>
        <?php endif; ?>
        <span>淨額 <b><?= htmlspecialchars(number_format($ncoinSummary['net']), ENT_QUOTES, 'UTF-8') ?></b></span>
    </div>
    <?php if ($ncoinHistory === []): ?>
        <p class="member-profile-empty">尚無 <?= $ncoinNameEsc ?>紀錄。完成推薦技師、評論或點讚即可獲得 <?= $ncoinNameEsc ?>。</p>
    <?php else: ?>
        <p class="member-profile-ncoin-range" role="status">
            顯示第 <?= number_format($ncoinRangeFrom) ?>–<?= number_format($ncoinRangeTo) ?> 筆，共 <?= number_format($ncoinTotal) ?> 筆
            <?php if ($ncoinPages > 1): ?>
                <span class="member-profile-ncoin-range__sep" aria-hidden="true">·</span>
                第 <?= (int) $ncoinPage ?>／<?= (int) $ncoinPages ?> 頁
            <?php endif; ?>
        </p>
        <?php
        $ncoinPagerPosition = 'top';
        require __DIR__ . DIRECTORY_SEPARATOR . 'member_profile_ncoin_pager.php';
        ?>
        <div class="member-profile-table-wrap">
            <table class="member-profile-ncoin-hist">
                <thead>
                    <tr><th>時間</th><th>項目</th><th class="member-profile-ncoin-hist__amt"><?= $ncoinNameEsc ?></th></tr>
                </thead>
                <tbody>
                    <?php foreach ($ncoinHistory as $h): ?>
                        <?php $reverted = !empty($h['reverted']); ?>
                        <tr class="<?= $reverted ? 'is-reverted' : '' ?>">
                            <td class="member-profile-ncoin-hist__when" data-label="時間"><?= htmlspecialchars(substr((string) ($h['created_at'] ?? ''), 0, 16), ENT_QUOTES, 'UTF-8') ?></td>
                            <td data-label="項目">
                                <?= htmlspecialchars(fate_ncoin_action_label((string) ($h['action'] ?? '')), ENT_QUOTES, 'UTF-8') ?>
                                <?php if (!empty($h['note'])): ?>
                                    <span class="member-profile-ncoin-hist__note"><?= htmlspecialchars((string) $h['note'], ENT_QUOTES, 'UTF-8') ?></span>
                                <?php endif; ?>
                            </td>
                            <td class="member-profile-ncoin-hist__amt member-profile-ncoin-hist__amt--<?= htmlspecialchars(fate_ncoin_ledger_amount_kind($h), ENT_QUOTES, 'UTF-8') ?>" data-label="<?= $ncoinNameEsc ?>">
                                <?= htmlspecialchars(fate_ncoin_format_ledger_amount($h), ENT_QUOTES, 'UTF-8') ?>
                                <?php if ($reverted): ?><span class="member-profile-ncoin-hist__reverted">已扣回</span><?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php
        $ncoinPagerPosition = 'bottom';
        require __DIR__ . DIRECTORY_SEPARATOR . 'member_profile_ncoin_pager.php';
        ?>
    <?php endif; ?>
</div>
