<!DOCTYPE html>
<html data-bs-theme="dark" lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title>Auction - FireWallet</title>
    <link rel="icon" type="image/png" sizes="900x768" href="/assets/img/favicon.png">
    <link rel="icon" type="image/png" sizes="900x768" href="/assets/img/favicon.png">
    <link rel="icon" type="image/png" sizes="900x768" href="/assets/img/favicon.png">
    <link rel="icon" type="image/png" sizes="900x768" href="/assets/img/favicon.png">
    <link rel="icon" type="image/png" sizes="900x768" href="/assets/img/favicon.png">
    <link rel="stylesheet" href="/assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i&amp;display=swap">
    <link rel="stylesheet" href="/assets/fonts/fontawesome-all.min.css">
    <link rel="stylesheet" href="/assets/fonts/font-awesome.min.css">
    <link rel="stylesheet" href="/assets/fonts/material-icons.min.css">
    <link rel="stylesheet" href="/assets/fonts/fontawesome5-overrides.min.css">
    <link rel="stylesheet" href="/assets/css/styles.min.css">
</head>

<body id="page-top">
    <div id="wrapper">
        <nav class="navbar align-items-start p-0 sidebar sidebar-dark accordion bg-gradient-primary navbar-dark toggled" style="background: var(--bs-primary-border-subtle);">
            <div class="container-fluid d-flex flex-column p-0"><a class="navbar-brand d-flex justify-content-center align-items-center m-0 sidebar-brand" href="/">
                    <div class="sidebar-brand-icon"><img src="/assets/img/favicon.png" width="44"></div>
                    <div class="mx-3 sidebar-brand-text"><span>FireWallet</span></div>
                </a>
                <hr class="my-0 sidebar-divider">
                <ul class="navbar-nav text-light" id="accordionSidebar">
                    <li class="nav-item"><a class="nav-link" href="/"><i class="fas fa-tachometer-alt"></i><span>Dashboard</span></a></li>
                    <li class="nav-item"><a class="nav-link" href="/tx"><i class="fas fa-table"></i><span>Transactions</span></a></li>
                    <li class="nav-item"><a class="nav-link" href="/auctions"><i class="fa fa-gavel"></i><span>Auctions</span></a></li>
                    <li class="nav-item"><a class="nav-link" href="/send"><i class="material-icons">send</i><span>Send HNS</span></a></li>
                    <li class="nav-item"><a class="nav-link" href="/receive"><i class="material-icons">call_received</i><span>Receive</span></a></li>
                    <li class="nav-item"><a class="nav-link" href="/plugins"><i class="material-icons">code</i><span>Plugins</span></a><a class="nav-link" href="/settings"><i class="material-icons">settings</i><span>Settings</span></a></li>
                </ul>
            </div>
        </nav>
        <div class="d-flex flex-column" id="content-wrapper" style="background: var(--bs-primary);">
            <div id="content">
                <nav class="navbar navbar-expand shadow mb-4 topbar static-top navbar-light" style="background: var(--bs-primary-text-emphasis);">
                    <div class="container-fluid"><button class="btn btn-link d-md-none me-3 rounded-circle" id="sidebarToggleTop" type="button"><i class="fas fa-bars"></i></button>
                        <form class="d-none d-sm-inline-block mw-100 ms-md-3 me-auto my-2 my-md-0 navbar-search" action="/search" method="get">
                            <div class="input-group"><input class="bg-light form-control border-0 small" type="text" placeholder="Search for domain" name="q" value="{{search_term}}" style="color: var(--bs-dark-text-emphasis);background: var(--bs-light);"><button class="btn btn-primary py-0" type="submit"><i class="fas fa-search"></i></button></div>
                        </form><span class="d-inline d-print-none d-sm-inline d-md-inline d-lg-none d-xl-none d-xxl-none" style="color: var(--bs-dark);"><span id="hsd-sync-mobile">{{sync}}</span></span><span class="d-inline d-print-none d-sm-inline d-md-inline d-lg-none d-xl-none d-xxl-none" style="color: var(--bs-dark);margin-left: 10px;"><span id="wallet-sync-mobile">{{wallet_status}}</span></span><span class="d-none d-print-inline d-sm-none d-md-none d-lg-inline d-xl-inline d-xxl-inline" style="color: var(--bs-dark);">Sync: <span id="hsd-sync">{{sync}}</span>%</span><span class="d-none d-print-inline d-sm-none d-md-none d-lg-inline d-xl-inline d-xxl-inline" style="color: var(--bs-dark);margin-left: 10px;">Wallet:&nbsp;<span id="wallet-sync">{{wallet_status}}</span></span><span class="d-none d-print-inline d-sm-none d-md-none d-lg-inline d-xl-inline d-xxl-inline" style="color: var(--bs-dark);margin-left: 10px;">Height: <span id="hsd-height">{{height}}</span></span>
                        <ul class="navbar-nav flex-nowrap ms-auto">
                            <li class="nav-item dropdown d-sm-none no-arrow"><a class="dropdown-toggle nav-link" aria-expanded="false" data-bs-toggle="dropdown" href="#"><i class="fas fa-search"></i></a>
                                <div class="dropdown-menu p-3 dropdown-menu-end animated--grow-in" aria-labelledby="searchDropdown">
                                    <form class="w-100 me-auto navbar-search" action="/search" method="get">
                                        <div class="input-group"><input class="bg-light form-control border-0 small" type="text" placeholder="Search for ..." name="q" value="{{search_term}}">
                                            <div class="input-group-append"><button class="btn btn-primary py-0" type="button"><i class="fas fa-search"></i></button></div>
                                        </div>
                                    </form>
                                </div>
                            </li>
                            <li class="nav-item dropdown no-arrow">
                                <div class="nav-item dropdown no-arrow"><a class="dropdown-toggle nav-link" aria-expanded="false" data-bs-toggle="dropdown" href="#"><span class="d-none d-lg-inline me-2 small" style="color: var(--bs-dark);">{{account}}</span><img class="border rounded-circle img-profile" src="/api/v1/wallet/icon" id="wallet-icon"></a>
                                    <div class="dropdown-menu shadow dropdown-menu-end animated--grow-in"><a class="dropdown-item" href="/logout"><i class="fas fa-sign-out-alt me-2 fa-sm fa-fw text-gray-400"></i>&nbsp;Logout</a></div>
                                </div>
                            </li>
                        </ul>
                    </div>
                </nav>
                <h4 class="text-center" style="color: rgb(255,0,0);">{{error}}</h4>
                <div class="container-fluid">
                    <div class="card">
                        <div class="card-body">
                            <div id="next-action" class="stick-right">{{next_action|safe}}</div>
                            <h4 class="card-title">{{rendered}}</h4>
                            <h6 class="text-muted mb-2 card-subtitle" id="next">{{next | safe}}</h6>
                        </div>
                    </div>
                </div>
                <div class="container-fluid" style="margin-top: 50px;">
                    <div class="card">
                        <div class="card-body">
                            <h4 class="card-title">Bid</h4>
                            <form action="/auction/{{search_term}}/bid"><label class="form-label">Bid</label><input class="form-control" type="number" placeholder="0.00" min="0" name="bid" step="any"><label class="form-label" style="margin-top: 10px;">Optional blind</label><input class="form-control" type="number" name="blind" placeholder="0.00" min="0" step="any">
                                <div class="text-end" style="margin-top: 10px;"><input class="btn btn-primary" type="submit" value="Bid"></div>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="container-fluid" style="margin-top: 50px;">
                    <div class="card">
                        <div class="card-body">
                            <h4 class="card-title">Bids</h4><div class="table-responsive">
    <table class="table">
        <thead>
            <tr>
                <th>Bid+Blind</th>
                <th>Bid</th>
                <th>Blind</th>
                <th>Owner</th>
                <th></th>
            </tr>
        </thead>
        <tbody id="bids-tbody">
            <tr id="loading-row">
                <td colspan="5" class="text-center">
                    <div class="spinner-border spinner-border-sm me-2" role="status">
                        <span class="visually-hidden">Loading...</span>
                    </div>
                    Loading bids...
                </td>
            </tr>
        </tbody>
    </table>
</div>

<script>
    async function loadBids(initial = false) {
        const tbody = document.getElementById('bids-tbody');

        try {
            // Fetch all required data
            const response = await fetch(`/api/v1/wallet/domainBids?domain={{search_term}}`);
            const data = await response.json();
            if (initial) {
                if (response.ok && data.result) {
                    tbody.innerHTML = data.result;
                } else {
                    tbody.innerHTML = '<tr><td colspan="5" class="text-center text-muted">No bids found. <a href="/auction/{{search_term}}/scan">Rescan Auction</a></td></tr>';
                }
            }
            const mempoolResponse = await fetch('/api/v1/hsd/mempoolBids');
            const nextStateResponse = await fetch(`/api/v1/hsd/nextAuctionState?domain={{search_term}}`);

            if (!initial) {
                if (response.ok && data.result) {
                    tbody.innerHTML = data.result;
                } else {
                    tbody.innerHTML = '<tr><td colspan="5" class="text-center text-muted">No bids found. <a href="/auction/{{search_term}}/scan">Rescan Auction</a></td></tr>';
                }
            }



            const nextStateData = await nextStateResponse.json();

            if (nextStateResponse.ok && nextStateData.state) {
                document.getElementById('next').innerHTML = nextStateData.next;
                document.getElementById('next-action').innerHTML = nextStateData.next_action;
            } else {
                document.getElementById('next').innerHTML = 'Unknown';
                document.getElementById('next-action').innerHTML = '';
            }

            const mempoolData = await mempoolResponse.json();
            if (mempoolResponse.ok && mempoolData.result) {
                const domainBids = mempoolData.result['{{search_term}}'];
                if (domainBids && domainBids.length > 0) {
                    let mempoolRows = '';
                    domainBids.forEach(bid => {
                        const bidValue = bid.revealed ? `${(bid.value / 1000000).toFixed(2)} HNS` : 'Hidden until reveal';
                        const lockupValue = (bid.lockup / 1000000).toFixed(2);
                        const blindValue = bid.revealed ? `${((bid.lockup - bid.value) / 1000000).toFixed(2)} HNS` : 'Hidden until reveal';
                        const type = bid.revealed ? 'Reveal' : 'Bid';
                        mempoolRows += `<tr class="table-warning">
                            <td>${lockupValue} HNS</td>
                            <td>${bidValue}</td>
                            <td>${blindValue}</td>
                            <td>${bid.owner}</td>
                            <td><a class='text-decoration-none' style='color: var(--bs-table-color-state, var(--bs-table-color-type, var(--bs-table-color)));' target='_blank' href='https://shakeshift.com/transaction/${bid.txid}'>Mempool ${type} 🔗</a></td>
                        </tr>`;
                    });
                    tbody.innerHTML += mempoolRows;
                }
            }
        } catch (error) {
            console.error('Error loading bids:', error);
        }
    }

    // Load bids when page loads
    document.addEventListener('DOMContentLoaded', () => loadBids(true));

    // Auto-refresh bids every 20 seconds
    setInterval(() => loadBids(false), 20000);
</script>
                        </div>
                    </div>
                </div>
            </div>
            <footer class="sticky-footer" style="background: var(--bs-primary-text-emphasis);">
                <div class="container my-auto">
                    <div class="text-center my-auto copyright"><span>Copyright © FireWallet 2025</span></div>
                </div>
            </footer>
        </div><a class="border rounded d-inline scroll-to-top" href="#page-top"><i class="fas fa-angle-up"></i></a>
    </div>
    <script src="/assets/bootstrap/js/bootstrap.min.js"></script>
    <script src="/assets/js/script.min.js"></script>
</body>

</html>