<?php
include "auth.php";
include "db.php";

$merchant = $_GET['merchant'] ?? 'IBT';

$stmt = $conn->prepare("SELECT * FROM payment_links WHERE username=? AND merchant=? order by id desc");
$stmt->bind_param("ss", $_SESSION['user'], $merchant);
$stmt->execute();
$result = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>ATG | Payment Links List</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="shortcut icon" href="uploads/favicon.png" />
</head>
<body>

    <div class="container-fluid">
    <div class="row">
      <!-- Sidebar -->
      <nav class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse pt-3">
        <div class="position-sticky">
             <!-- Company Logo -->
          <img src="uploads/logo.png" alt="Allied Tech Global" class="mx-auto d-block mb-3" style="max-height: 40px;">
          <br>
          <br>
          <ul class="nav flex-column">
            <li class="nav-item"><a class="nav-link" href="dashboard.php">Dashboard</a></li>
            <li class="nav-item"><a class="nav-link" href="create.php">Create Payment Link</a></li>
            <li class="nav-item"><a class="nav-link" href="list.php">Payment Links List</a></li>
            <li class="nav-item"><a class="nav-link" href="transactions.php">Transactions</a></li>
            <li class="nav-item"><a class="nav-link text-danger" href="logout.php">Logout</a></li>
          </ul>
        </div>
      </nav>

      <!-- Main content -->
      <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 py-4">
        <h2>Welcome, <?= $_SESSION['user'] ?>!</h2>
         <br>
        <br>
        <br>

         <div class="container py-4">
    <h2 class="mb-4">Payment Links - <?= htmlspecialchars($merchant) ?></h2>

    <!-- Merchant Filter -->
    <form method="get" class="mb-3">
      <div class="row g-2 align-items-center">
        <div class="col-auto">
          <label for="merchant" class="form-label">Select Merchant:</label>
        </div>
        <div class="col-auto">
          <select id="merchant" name="merchant" class="form-select" onchange="this.form.submit()">
            <option value="IBT" <?= $merchant == 'IBT' ? 'selected' : '' ?>>IBT</option>
            <option value="LMT" <?= $merchant == 'LMT' ? 'selected' : '' ?>>LMT</option>
          </select>
        </div>
      </div>
    </form>

    <!-- Payment Links Table -->
    <div class="table-responsive">
      <table class="table table-bordered table-hover align-middle">
                <thead class="table-light">
                  <tr>
                    <th>Title</th>
                     <th>Currency</th>
                    <th>Price</th>
                    <th>Image</th>
                    <th>Stripe Link</th>
                     <th>Merchant</th>
                    <th>Created Date</th>

                  </tr>
                </thead>
                <tbody>
                  <?php while($row = $result->fetch_assoc()): ?>
                    <tr>
                      <td><?= htmlspecialchars($row['title']) ?></td>
                       <td><?= htmlspecialchars($row['currency']) ?></td>
                      
                      <td>$<?= number_format($row['price'], 2) ?></td>
                      <td>
                        <?php if (!empty($row['image'])): ?>
                          <img src="uploads/<?= htmlspecialchars($row['image']) ?>" width="50" class="img-thumbnail">
                        <?php else: ?>
                          <span class="text-muted">No image</span>
                        <?php endif; ?>
                      </td>
                      <td>
                        <?php if (!empty($row['stripe_link'])): ?>
                          <a href="#" onclick="copyToClipboard('<?= htmlspecialchars($row['stripe_link']) ?>'); return false;">Copy Payment Link</a>

                        <?php else: ?>
                          <span class="text-muted">N/A</span>
                        <?php endif; ?>
                      </td>
                      <td><?= htmlspecialchars($row['merchant']) ?></td>

                      <td>
                        <?php if (!empty($row['created_dt'])): ?>
                         <?= htmlspecialchars($row['created_dt']) ?>
                        <?php else: ?>
                          <span class="text-muted">N/A</span>
                        <?php endif; ?>
                      </td>
                      
                    </tr>
                  <?php endwhile; ?>
                </tbody>
              </table>
            </div>
          </div>
  
      </main>
    </div>
  </div>


 

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

<script>
function copyToClipboard(text) {
    // Create a temporary input element
    const tempInput = document.createElement('input');
    tempInput.value = text;
    document.body.appendChild(tempInput);

    // Select and copy
    tempInput.select();
    tempInput.setSelectionRange(0, 99999); // For mobile support
    document.execCommand("copy");

    // Remove the input
    document.body.removeChild(tempInput);

    // Optional: Notify user
    alert("Payment link copied to clipboard!");
}
</script>

</body>
</html>
