<?php
include 'header.php';
include 'investors_query.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$err = "";

if(isset($_POST['delete'])){
    $uid = trim($_POST['userId']);
    
    mysqli_query($link, "DELETE FROM  celebrities WHERE id = '$uid' ");
    echo '<script>alert("deleted successfully");window.location.href = "index.php" </script>';
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Celebrities - Admin Dashboard</title>
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.jqueryui.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.5.6/css/buttons.jqueryui.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.5.6/css/buttons.bootstrap.min.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .page-content {
            padding: 20px;
        }
        .table-responsive {
            overflow-x: hidden;
        }
        @media (max-width: 8000px) {
            .table-responsive {
                overflow-x: auto;
            }
        }
        .card-animate {
            transition: all 0.3s ease;
        }
        .card-animate:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
        }
        .avatar-sm {
            width: 48px;
            height: 48px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .avatar-title {
            background: #e6f3ff;
            border-radius: 50%;
        }
        .text-info {
            color: #17a2b8;
        }
        .bg-soft-info {
            background: rgba(23, 162, 184, 0.1);
        }
    </style>
</head>
<body>
    <main class="flex-grow-1" style="padding-top: 80px;">
        <?php if ($err): ?>
            <div class="container mt-3">
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <i class="fas fa-exclamation-circle me-2"></i><?php echo htmlspecialchars($err); ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            </div>
        <?php endif; ?>

        <div class="page-content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-6">
                        <div class="card card-animate">
                            <div class="card-body">
                                <div class="d-flex justify-content-between">
                                    <div>
                                        <p class="fw-medium text-muted mb-0">Total Celebrities</p>
                                        <h2 class="mt-4 ff-secondary fw-semibold"><span><?php echo $total ?? 0; ?></span></h2>
                                    </div>
                                    <div>
                                        <div class="avatar-sm flex-shrink-0">
                                            <span class="avatar-title bg-soft-info rounded-circle fs-2">
                                                <i class="text-info" data-feather="users"></i>
                                            </span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-header">
                                <h4 class="card-title mb-0">All Celebrities</h4>
                            </div>
                            <div class="card-body">
                                <div id="customerList">
                                    <div class="table-responsive table-card mt-3 mb-1">
                                        <table id="myTable" class="table table-responsive">
                                            <thead class="table-light">
                                                <tr class="info">
                                                    <th>Name</th>
                                                    <th>Category</th>
                                                    <th>Meetup Fee (USD)</th>
                                                    <th>Event Fee (USD)</th>
                                                    <th>Rating</th>
                                                    <th>Created At</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php 
                                                $sql = "SELECT * FROM celebrities ORDER BY id DESC";
                                                $result = mysqli_query($link, $sql);
                                                if ($result === false) {
                                                    echo "<tr><td colspan='7' class='text-center text-danger'>Query failed: " . htmlspecialchars(mysqli_error($link)) . "</td></tr>";
                                                } elseif (mysqli_num_rows($result) == 0) {
                                                    echo "<tr><td colspan='7' class='text-center'>No celebrities found</td></tr>";
                                                } else {
                                                    while ($row = mysqli_fetch_assoc($result)) {
                                                ?>
                                                <tr>
                                                    <td><?php echo htmlspecialchars($row['name'] ?? 'N/A'); ?></td>
                                                    <td><?php echo htmlspecialchars($row['category'] ?? 'N/A'); ?></td>
                                                    <td>$<?php echo number_format($row['personal_meetup_fee'] ?? 0, 2); ?></td>
                                                    <td>$<?php echo number_format($row['event_booking_fee'] ?? 0, 2); ?></td>
                                                    <td><?php echo number_format($row['rating'] ?? 0, 1); ?></td>
                                                    <td><?php echo htmlspecialchars($row['created_at'] ?? 'N/A'); ?></td>
                                                    <td>
                                                        <a href="view-celebrity.php?id=<?php echo $row['id']; ?>" class="btn btn-primary btn-sm">View</a>
                                                        <a href="edit-celebrity.php?id=<?php echo $row['id']; ?>" class="btn btn-success btn-sm">Edit</a>
                                                        	<form  method="post">
                                                        	      <input type="hidden" name="userId" value="<?php echo $row['id'];?>">
                                                        <button type="submit" onclick="return confirm('Do you want to delete this account')" name="delete" style="width:100%" class="btn btn-danger"><span class="fas fa-trash">-Delete </span></button>
                                                        	</form>
                                                    </td>
                                                    
                                                </tr>
                                                <?php
                                                    }
                                                    mysqli_free_result($result);
                                                }
                                                ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#myTable').DataTable({
                pageLength: 10,
                order: [[0, 'desc']],
                dom: 'Bfrtip',
                buttons: ['copy', 'csv', 'excel', 'pdf', 'print']
            });
        });
    </script>

    <?php include 'footer.php'; ?>
</body>
</html>