<?php require_once('header.php'); ?> 

<?php
// Handle delete
if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['id']) && isset($_GET['type'])) {
    $id = (int) $_GET['id'];
    $type = $_GET['type'];

    $table = "";
    if ($type == "inquiry") $table = "partnership_inquiries";
    if ($type == "application") $table = "partnership_applications";
    if ($type == "status") $table = "status_checks";

    if ($table) {
        $statement = $pdo->prepare("DELETE FROM $table WHERE id = ?");
        $statement->execute([$id]);
        $success_message = ucfirst($type) . " deleted successfully!";
    }
}
?>

<section class="content-header">
    <div class="content-header-left">
        <h1>View Partnership Leads</h1>
    </div>
</section>

<section class="content">
  <div class="row">
    <div class="col-md-12">

      

      <div class="box box-info">
        <div class="box-body table-responsive">
          <ul class="nav nav-tabs" role="tablist">
            <li class="active"><a href="#inquiries" role="tab" data-toggle="tab">Inquiries</a></li>
            <li><a href="#applications" role="tab" data-toggle="tab">Applications</a></li>
            <li><a href="#status" role="tab" data-toggle="tab">Status Requests</a></li>
          </ul>

          <div class="tab-content" style="margin-top:20px;">
            
            <!-- Inquiries -->
            <div class="tab-pane active" id="inquiries">
              <table class="table table-bordered table-hover table-striped">
                <thead>
                  <tr>
                    <th>#</th>
                    <th>Full Name</th>
                    <th>Phone</th>
                    <th>Email</th>
                    <th>Investment</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $i = 0;
                  $statement = $pdo->prepare("SELECT * FROM partnership_inquiries ORDER BY id DESC");
                  $statement->execute();
                  $inquiries = $statement->fetchAll(PDO::FETCH_ASSOC);
                  foreach ($inquiries as $row) {
                      $i++;
                      ?>
                      <tr>
                        <td><?= $i ?></td>
                        <td><?= htmlspecialchars($row['full_name']) ?></td>
                        <td><?= htmlspecialchars($row['phone']) ?></td>
                        <td><?= htmlspecialchars($row['email']) ?></td>
                        <td><?= htmlspecialchars($row['investment']) ?></td>
                        <td>
                          <button class="btn btn-info btn-xs view-btn" 
                                  data-type="inquiry" 
                                  data-details='<?= json_encode($row) ?>'>View</button>
                          <a href="dashboard-leads.php?action=delete&type=inquiry&id=<?= $row['id'] ?>" 
                             class="btn btn-danger btn-xs"
                             onclick="return confirm('Delete this inquiry?');">Delete</a>
                        </td>
                      </tr>
                      <?php
                  }
                  ?>
                </tbody>
              </table>
            </div>

            <!-- Applications -->
            <div class="tab-pane" id="applications">
              <table class="table table-bordered table-hover table-striped">
                <thead>
                  <tr>
                    <th>#</th>
                    <th>Full Name</th>
                    <th>Phone</th>
                    <th>Email</th>
                    <th>State</th>
                    <th>District</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $i = 0;
                  $statement = $pdo->prepare("SELECT * FROM partnership_applications ORDER BY id DESC");
                  $statement->execute();
                  $applications = $statement->fetchAll(PDO::FETCH_ASSOC);
                  foreach ($applications as $row) {
                      $i++;
                      ?>
                      <tr>
                        <td><?= $i ?></td>
                        <td><?= htmlspecialchars($row['full_name']) ?></td>
                        <td><?= htmlspecialchars($row['phone']) ?></td>
                        <td><?= htmlspecialchars($row['email']) ?></td>
                        <td><?= htmlspecialchars($row['state']) ?></td>
                        <td><?= htmlspecialchars($row['district']) ?></td>
                        <td>
                          <button class="btn btn-info btn-xs view-btn" 
                                  data-type="application" 
                                  data-details='<?= json_encode($row) ?>'>View</button>
                          <a href="dashboard-leads.php?action=delete&type=application&id=<?= $row['id'] ?>" 
                             class="btn btn-danger btn-xs"
                             onclick="return confirm('Delete this application?');">Delete</a>
                        </td>
                      </tr>
                      <?php
                  }
                  ?>
                </tbody>
              </table>
            </div>

            <!-- Status -->
            <div class="tab-pane" id="status">
              <table class="table table-bordered table-hover table-striped">
                <thead>
                  <tr>
                    <th>#</th>
                    <th>Aadhaar</th>
                    <th>Phone</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $i = 0;
                  $statement = $pdo->prepare("SELECT * FROM status_checks ORDER BY id DESC");
                  $statement->execute();
                  $statuses = $statement->fetchAll(PDO::FETCH_ASSOC);
                  foreach ($statuses as $row) {
                      $i++;
                      ?>
                      <tr>
                        <td><?= $i ?></td>
                        <td><?= htmlspecialchars($row['aadhaar']) ?></td>
                        <td><?= htmlspecialchars($row['phone']) ?></td>
                        <td>
                          <button class="btn btn-info btn-xs view-btn" 
                                  data-type="status" 
                                  data-details='<?= json_encode($row) ?>'>View</button>
                          <a href="dashboard-leads.php?action=delete&type=status&id=<?= $row['id'] ?>" 
                             class="btn btn-danger btn-xs"
                             onclick="return confirm('Delete this status request?');">Delete</a>
                        </td>
                      </tr>
                      <?php
                  }
                  ?>
                </tbody>
              </table>
            </div>

          </div><!-- /.tab-content -->
        </div>
      </div>
    </div>
  </div>
</section>

<!-- Modal for View -->
<div class="modal fade" id="viewModal" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Lead Details</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body" id="modal-body-content">
        <!-- Filled dynamically -->
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

<script>
document.querySelectorAll('.view-btn').forEach(btn => {
    btn.addEventListener('click', function() {
        let data = JSON.parse(this.dataset.details);
        let html = "<table class='table table-bordered'>";

        for (let key in data) {
            // Skip ID and empty fields
            if (key === "id" || !data[key]) continue;

            let label = key.replace(/_/g, " "); // nicer labels
            let value = data[key];

            // Check if value looks like an image path
            if (/\.(jpg|jpeg|png|gif|webp)$/i.test(value)) {
                value = `<img src="../${value}" alt="${label}"  height="200" style=" border:1px solid #ccc; border-radius:5px;">`;
            }

            html += `<tr><th style="width:200px; text-transform:capitalize;">${label}</th><td>${value}</td></tr>`;
        }

        html += "</table>";
        document.getElementById('modal-body-content').innerHTML = html;
        $('#viewModal').modal('show');
    });
});
</script>


<?php require_once('footer.php'); ?>
