<?php
require_once __DIR__ . '/../../app/helpers/functions.php';
require_once __DIR__ . '/../../app/helpers/csrf.php';
require_once __DIR__ . '/../../app/middleware/auth.php';

$conn = require __DIR__ . '/../../app/config/database.php';
require_login();

$rows = [];
$res = $conn->query("
  SELECT m.*, a.symbol
  FROM crypto_multisig m
  JOIN crypto_assets a ON a.id=m.asset_id
  WHERE m.status='pending'
  ORDER BY m.id DESC
");
while($r=$res->fetch_assoc()) $rows[]=$r;

$title='Multisig Queue';
include __DIR__ . '/../../views/partials/head.php';
include __DIR__ . '/../../views/partials/admin_sidebar.php';
?>

<main class="flex-grow-1" style="margin-left:240px; min-height:100vh;">
<div class="container-fluid py-4">

  <div class="d-flex justify-content-between align-items-center mb-3">
    <div>
      <h3 class="fw-bold mb-0">Multi-sig Approvals</h3>
      <div class="ab-muted">2-of-3 simulation. Approve or reject.</div>
    </div>
    <a class="btn btn-outline-secondary" href="crypto_admin_dashboard.php">Back</a>
  </div>

  <?php if($m=flash('msg')): ?><div class="alert alert-success ab-card"><?= e($m) ?></div><?php endif; ?>
  <?php if($m=flash('err')): ?><div class="alert alert-danger ab-card"><?= e($m) ?></div><?php endif; ?>

  <div class="card ab-card p-3">
    <div class="table-responsive">
      <table class="table align-middle mb-0">
        <thead>
          <tr><th>Ref</th><th>User</th><th>Asset</th><th>Amount</th><th>Dest</th><th>Needed</th><th>Action</th></tr>
        </thead>
        <tbody>
        <?php foreach($rows as $m): ?>
          <tr>
            <td class="fw-bold"><?= e($m['ref']) ?></td>
            <td><?= (int)$m['user_id'] ?></td>
            <td><?= e($m['symbol']) ?></td>
            <td><?= number_format((float)$m['amount'],8) ?></td>
            <td style="max-width:280px;word-break:break-all;"><?= e($m['destination']) ?></td>
            <td><?= (int)$m['approvals_needed'] ?></td>
            <td>
              <form method="post" action="crypto_multisig_action.php" class="d-flex gap-2">
                <?= csrf_field() ?>
                <input type="hidden" name="ref" value="<?= e($m['ref']) ?>">
                <button class="btn btn-sm btn-success" name="do" value="approve">Approve</button>
                <button class="btn btn-sm btn-danger" name="do" value="reject">Reject</button>
              </form>
            </td>
          </tr>
        <?php endforeach; ?>

        <?php if(!$rows): ?>
          <tr><td colspan="7" class="text-center ab-muted py-4">No pending multisig requests.</td></tr>
        <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>

</div>
</main>

<?php include __DIR__ . '/../../views/partials/foot.php'; ?>
