<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>URLShortnr Dashboard by Imtiyaz Alam</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="https://cdn.jsdelivr.net/npm/qrious/dist/qrious.min.js"></script>
<style>
  :root {
    --primary: #4361ee;
    --secondary: #3a0ca3;
    --accent: #4cc9f0;
    --light: #f8f9fa;
    --dark: #212529;
    --success: #4bb543;
    --danger: #e63946;
    --gradient: linear-gradient(135deg, var(--primary), var(--secondary));
  }
  
  body {
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    min-height: 100vh;
    padding-bottom: 2rem;
  }
  
  .header-title {
    text-align: center;
    background: var(--gradient);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    margin: 1.5rem 0;
    font-weight: 800;
    font-size: 2.5rem;
  }
  
  .creator {
    text-align: center;
    color: var(--secondary);
    margin-bottom: 2rem;
    font-weight: 500;
  }
  
  .url-form {
    background: white;
    border-radius: 16px;
    padding: 1.5rem;
    box-shadow: 0 10px 30px rgba(0,0,0,0.08);
    margin-bottom: 2rem;
    border: 1px solid rgba(67, 97, 238, 0.1);
  }
  
  .url-form .form-control {
    border: 2px solid #e9ecef;
    border-radius: 10px;
    padding: 0.75rem 1rem;
    transition: all 0.3s;
  }
  
  .url-form .form-control:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 0.2rem rgba(76, 201, 240, 0.25);
  }
  
  .url-form .btn {
    border-radius: 10px;
    padding: 0.75rem 1.5rem;
    font-weight: 600;
    background: var(--gradient);
    border: none;
    transition: transform 0.2s;
  }
  
  .url-form .btn:hover {
    transform: translateY(-2px);
  }
  
  .card-url {
    border-radius: 16px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.05);
    padding: 1.25rem;
    margin-bottom: 1.25rem;
    background-color: #fff;
    transition: transform 0.3s, box-shadow 0.3s;
    border-left: 4px solid var(--primary);
    position: relative;
    overflow: hidden;
  }
  
  .card-url::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 4px;
    background: var(--gradient);
    transform: scaleX(0);
    transform-origin: left;
    transition: transform 0.3s;
  }
  
  .card-url:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
  }
  
  .card-url:hover::before {
    transform: scaleX(1);
  }
  
  .url-badge {
    background: var(--gradient);
    color: white;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    margin-right: 10px;
    flex-shrink: 0;
  }
  
  .original-url {
    color: #6c757d;
    font-size: 0.9rem;
    word-break: break-all;
    margin-bottom: 0.5rem;
  }
  
  .short-url-container {
    display: flex;
    align-items: center;
    margin-bottom: 0.5rem;
  }
  
  .short-url {
    font-weight: 600;
    color: var(--primary);
    text-decoration: none;
    font-size: 1.1rem;
    transition: color 0.2s;
  }
  
  .short-url:hover {
    color: var(--secondary);
  }
  
  .actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.75rem;
  }
  
  .copy-btn, .delete-btn, .qr-btn {
    border: none;
    border-radius: 8px;
    padding: 0.4rem 0.8rem;
    font-size: 0.85rem;
    font-weight: 500;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 0.3rem;
  }
  
  .copy-btn {
    background-color: rgba(67, 97, 238, 0.1);
    color: var(--primary);
  }
  
  .copy-btn:hover {
    background-color: var(--primary);
    color: white;
    transform: translateY(-2px);
  }
  
  .delete-btn {
    background-color: rgba(230, 57, 70, 0.1);
    color: var(--danger);
  }
  
  .delete-btn:hover {
    background-color: var(--danger);
    color: white;
    transform: translateY(-2px);
  }
  
  .qr-btn {
    background-color: rgba(76, 201, 240, 0.1);
    color: var(--accent);
  }
  
  .qr-btn:hover {
    background-color: var(--accent);
    color: white;
    transform: translateY(-2px);
  }
  
  .qr-modal .modal-content {
    border-radius: 16px;
    border: none;
    box-shadow: 0 20px 40px rgba(0,0,0,0.1);
  }
  
  .qr-modal .modal-header {
    border-bottom: 1px solid rgba(0,0,0,0.05);
    background: var(--gradient);
    color: white;
    border-radius: 16px 16px 0 0;
  }
  
  .empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: #6c757d;
  }
  
  .empty-state i {
    font-size: 4rem;
    margin-bottom: 1rem;
    color: #dee2e6;
  }
  
  .empty-state h5 {
    margin-bottom: 0.5rem;
    font-weight: 600;
  }
  
  .stats-bar {
    display: flex;
    justify-content: space-between;
    background: white;
    border-radius: 12px;
    padding: 1rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 5px 15px rgba(0,0,0,0.05);
  }
  
  .stat-item {
    text-align: center;
    flex: 1;
  }
  
  .stat-number {
    font-size: 1.8rem;
    font-weight: 700;
    color: var(--primary);
  }
  
  .stat-label {
    font-size: 0.85rem;
    color: #6c757d;
  }
  
  @media (max-width: 768px) {
    .header-title {
      font-size: 2rem;
    }
    
    .stats-bar {
      flex-direction: column;
      gap: 1rem;
    }
    
    .original-url {
      font-size: 0.8rem;
    }
    
    .short-url {
      font-size: 1rem;
    }
  }
  
  @media (max-width: 576px) {
    .url-form .input-group {
      flex-direction: column;
    }
    
    .url-form .input-group .form-control {
      border-radius: 10px;
      margin-bottom: 0.5rem;
    }
    
    .url-form .input-group .btn {
      border-radius: 10px;
      width: 100%;
    }
    
    .actions {
      flex-wrap: wrap;
    }
  }
</style>
</head>
<body>

<div class="container py-4">
  <h1 class="header-title">URLShortnr</h1>
  <p class="creator">by Imtiyaz Alam</p>
  
  <!-- Stats Bar -->
  <div class="stats-bar">
    <div class="stat-item">
      <div class="stat-number"><%= urls.length %></div>
      <div class="stat-label">Total URLs</div>
    </div>
    <div class="stat-item">
      <div class="stat-number">0</div>
      <div class="stat-label">Clicks Today</div>
    </div>
    <div class="stat-item">
      <div class="stat-number">0</div>
      <div class="stat-label">Top URL</div>
    </div>
  </div>
  
  <!-- URL Form -->
  <div class="url-form">
    <form action="<%= basePath %>/shorten" method="POST">
      <div class="input-group">
        <input type="url" name="original_url" class="form-control" placeholder="Paste your long URL here..." required>
        <button class="btn btn-primary"><i class="fas fa-link me-2"></i>Shorten</button>
      </div>
    </form>
  </div>

  <% if (urls.length === 0) { %>
    <div class="empty-state">
      <i class="fas fa-link"></i>
      <h5>No URLs yet</h5>
      <p>Start shortening your URLs to see them here!</p>
    </div>
  <% } else { %>
    <% urls.forEach((url, index) => { %>
      <div class="card-url">
        <div class="d-flex justify-content-between align-items-start">
          <div class="flex-grow-1">
            <div class="d-flex align-items-center mb-2">
              <div class="url-badge"><%= index + 1 %></div>
              <span class="original-url" title="<%= url.original_url %>"><%= url.original_url %></span>
            </div>
            <div class="short-url-container">
              <a href="<%= basePath %>/<%= url.short_code %>" target="_blank" class="short-url">
                <i class="fas fa-external-link-alt me-2"></i>https://imtiyazalam.work.gd<%= basePath %>/<%= url.short_code %>
              </a>
            </div>
            <div class="actions">
              <button type="button" class="btn btn-sm copy-btn" title="Copy Short URL" onclick="copyURL('url-<%= url.id %>')">
                <i class="fas fa-copy me-1"></i>Copy
              </button>
              <button type="button" class="btn btn-sm qr-btn" data-bs-toggle="modal" data-bs-target="#qrModal<%= url.id %>">
                <i class="fas fa-qrcode me-1"></i>QR Code
              </button>
              <button type="button" class="btn btn-sm delete-btn" onclick="confirmDelete(<%= url.id %>)">
                <i class="fas fa-trash me-1"></i>Delete
              </button>
              <input type="hidden" id="url-<%= url.id %>" value="https://imtiyazalam.work.gd<%= basePath %>/<%= url.short_code %>">
            </div>
          </div>
        </div>
      </div>
      
      <!-- QR Code Modal -->
      <div class="modal fade qr-modal" id="qrModal<%= url.id %>" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title">QR Code</h5>
              <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body text-center py-4">
              <canvas id="qr-<%= url.id %>"></canvas>
              <p class="mt-3 mb-0"><small>Scan to visit the shortened URL</small></p>
            </div>
          </div>
        </div>
      </div>
    <% }) %>
  <% } %>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"></script>
<script>
// Generate QR codes
<% urls.forEach(url => { %>
  new QRious({
    element: document.getElementById('qr-<%= url.id %>'),
    value: 'https://imtiyazalam.work.gd<%= basePath %>/<%= url.short_code %>',
    size: 200,
    background: 'transparent'
  });
<% }) %>

function copyURL(id) {
    const copyText = document.getElementById(id);
    navigator.clipboard.writeText(copyText.value).then(() => {
        Swal.fire({
            icon: 'success',
            title: 'Copied!',
            text: 'Short URL copied to clipboard',
            timer: 1500,
            showConfirmButton: false,
            position: 'top-end',
            toast: true
        });
    });
}

function confirmDelete(id) {
    Swal.fire({
        title: 'Enter PIN to Delete',
        input: 'password',
        inputPlaceholder: 'Enter PIN',
        showCancelButton: true,
        confirmButtonText: 'Delete',
        confirmButtonColor: '#e63946',
        icon: 'warning',
        inputAttributes: { autocapitalize: 'off', autocomplete: 'off' }
    }).then((result) => {
        if (result.isConfirmed && result.value) {
            const form = document.createElement('form');
            form.method = 'POST';
            form.action = '<%= basePath %>/delete/' + id;
            const input = document.createElement('input');
            input.type = 'hidden';
            input.name = 'pin';
            input.value = result.value;
            form.appendChild(input);
            document.body.appendChild(form);
            form.submit();
        }
    });
}

// One-time alert from query string
<% if (req.query.msg) { %>
Swal.fire({
    icon: '<%= req.query.type === "error" ? "error" : "success" %>',
    title: '<%= req.query.msg.replace(/\+/g, " ") %>',
    timer: 1800,
    showConfirmButton: false
}).then(() => { window.location.href = '<%= basePath %>/app'; });
<% } %>
</script>
</body>
</html>