<?php
include '../db.php';
// Start session and DB
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION['mpid']) || $_SESSION['mpid'] !== 1) {
    header("Location: http://localhost/LoanApprovalPredction/admin.php");
    exit();
}

// Handle actions
if (isset($_GET['cnid'])) {
    $id = intval($_GET['cnid']);
    $conn->query("DELETE FROM contact WHERE contact_id = $id");
    $_SESSION['delco'] = "<p class='text-green-600 text-center mb-4'>Contact deleted successfully.</p>";
    header("Location: " . $_SERVER['PHP_SELF']);
    exit();
}
if (isset($_GET['cpid'])) {
    $id = intval($_GET['cpid']);
    $conn->query("DELETE FROM complain WHERE complain_id = $id");
    $_SESSION['delcp'] = "<p class='text-green-600 text-center mb-4'>Complaint deleted successfully.</p>";
    header("Location: " . $_SERVER['PHP_SELF']);
    exit();
}
if (isset($_GET['scpid'])) {
    $id = intval($_GET['scpid']);
    $conn->query("UPDATE complain SET status = 'Solved' WHERE complain_id = $id");
    $_SESSION['csolve'] = "<p class='text-blue-600 text-center mb-4'>Complaint marked as solved.</p>";
    header("Location: " . $_SERVER['PHP_SELF']);
    exit();
}

// Flash messages
$msg = "";
if (isset($_SESSION['delcp'])) { $msg .= $_SESSION['delcp']; unset($_SESSION['delcp']); }
if (isset($_SESSION['delco'])) { $msg .= $_SESSION['delco']; unset($_SESSION['delco']); }
if (isset($_SESSION['csolve'])) { $msg .= $_SESSION['csolve']; unset($_SESSION['csolve']); }

$name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?= htmlspecialchars($name) ?> - Admin Panel</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="icon" href="../images/Icon.png">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body { font-family: 'Inter', sans-serif; }
        .table-container { max-height: 500px; overflow-y: auto; }
        .table-container thead { position: sticky; top: 0; z-index: 10; }
    </style>
</head>
<body class="bg-gray-50 min-h-screen">
   
<?php include '../includes/newnavbar.php'; ?>


    <main class="container mx-auto px-4 py-8">
        <?= $msg ?>

        <div class="text-center mb-8">
            <p class="text-xl md:text-2xl font-semibold text-gray-800 bg-green-100 p-4 rounded-lg shadow-sm">
                In this field, you will get complaints and messages from members
            </p>
        </div>

        <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
            <!-- Contact Info -->
            <section class="bg-white rounded-lg shadow-md p-6">
                <h2 class="text-2xl font-semibold text-gray-800 mb-4">Contact Information</h2>
                <?php
                $res = $conn->query("SELECT contact.contact_id, contact.message, contact.cdate, members.MembersName, members.Email 
                                    FROM contact JOIN members ON contact.Mid = members.Mid");
                if ($res->num_rows > 0) {
                    $i = 0;
                ?>
                <div class="table-container">
                    <table class="min-w-full divide-y divide-gray-200">
                        <caption class="text-lg font-medium text-gray-600 mb-2">Contact Messages</caption>
                        <thead class="bg-green-100">
                            <tr>
                                <th class="px-4 py-3 text-left text-sm font-medium text-gray-800">#</th>
                                <th class="px-4 py-3 text-left text-sm font-medium text-gray-800">Name</th>
                                <th class="px-4 py-3 text-left text-sm font-medium text-gray-800">Email</th>
                                <th class="px-4 py-3 text-left text-sm font-medium text-gray-800">Message</th>
                                <th class="px-4 py-3 text-left text-sm font-medium text-gray-800">Date</th>
                                <th class="px-4 py-3 text-left text-sm font-medium text-gray-800">Action</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200">
                            <?php while ($row = $res->fetch_assoc()) { $i++; ?>
                            <tr class="hover:bg-gray-50">
                                <td class="px-4 py-3 text-sm text-gray-600"><?= $i ?></td>
                                <td class="px-4 py-3 text-sm text-gray-600"><?= htmlspecialchars($row['MembersName']) ?></td>
                                <td class="px-4 py-3 text-sm text-gray-600"><?= htmlspecialchars($row['Email']) ?></td>
                                <td class="px-4 py-3 text-sm text-gray-600"><?= htmlspecialchars($row['message']) ?></td>
                                <td class="px-4 py-3 text-sm text-gray-600"><?= date('M d, Y', strtotime($row['cdate'])) ?></td>
                                <td class="px-4 py-3 text-sm">
                                    <a href="?cnid=<?= $row['contact_id'] ?>" class="text-red-600 hover:text-red-800 font-medium" onclick="return del()">Delete</a>
                                </td>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
                <?php } else { ?>
                    <p class="text-gray-500 text-center py-4">No contact messages found.</p>
                <?php } ?>
            </section>

            <!-- Complaint Info -->
            <section class="bg-white rounded-lg shadow-md p-6">
                <h2 class="text-2xl font-semibold text-gray-800 mb-4">Complaint Information</h2>
                <?php
                $res1 = $conn->query("SELECT complain.complain_id, complain.complain, complain.cdate, complain.status, members.MembersName, members.Email 
                                     FROM complain JOIN members ON complain.Mid = members.Mid");
                if ($res1->num_rows > 0) {
                    $j = 0;
                ?>
                <div class="table-container">
                    <table class="min-w-full divide-y divide-gray-200">
                        <caption class="text-lg font-medium text-gray-600 mb-2">Complaints</caption>
                        <thead class="bg-green-100">
                            <tr>
                                <th class="px-4 py-3 text-left text-sm font-medium text-gray-800">#</th>
                                <th class="px-4 py-3 text-left text-sm font-medium text-gray-800">Name</th>
                                <th class="px-4 py-3 text-left text-sm font-medium text-gray-800">Email</th>
                                <th class="px-4 py-3 text-left text-sm font-medium text-gray-800">Complaint</th>
                                <th class="px-4 py-3 text-left text-sm font-medium text-gray-800">Date</th>
                                <th class="px-4 py-3 text-left text-sm font-medium text-gray-800">Status</th>
                                <th class="px-4 py-3 text-left text-sm font-medium text-gray-800">Action</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200">
                            <?php while ($row1 = $res1->fetch_assoc()) { $j++; ?>
                            <tr class="hover:bg-gray-50">
                                <td class="px-4 py-3 text-sm text-gray-600"><?= $j ?></td>
                                <td class="px-4 py-3 text-sm text-gray-600"><?= htmlspecialchars($row1['MembersName']) ?></td>
                                <td class="px-4 py-3 text-sm text-gray-600"><?= htmlspecialchars($row1['Email']) ?></td>
                                <td class="px-4 py-3 text-sm text-gray-600"><?= htmlspecialchars($row1['complain']) ?></td>
                                <td class="px-4 py-3 text-sm text-gray-600"><?= date('M d, Y', strtotime($row1['cdate'])) ?></td>
                                <td class="px-4 py-3 text-sm <?= $row1['status'] == 'Solved' ? 'text-green-600' : 'text-yellow-600' ?>">
                                    <?= htmlspecialchars($row1['status']) ?>
                                </td>
                                <td class="px-4 py-3 text-sm">
                                    <?php if ($row1['status'] == 'Solved') { ?>
                                        <a href="?cpid=<?= $row1['complain_id'] ?>" class="text-red-600 hover:text-red-800 font-medium" onclick="return del()">Delete</a>
                                    <?php } else { ?>
                                        <a href="?scpid=<?= $row1['complain_id'] ?>" class="text-blue-600 hover:text-blue-800 font-medium">Solved</a>
                                    <?php } ?>
                                </td>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
                <?php } else { ?>
                    <p class="text-gray-500 text-center py-4">No complaints found.</p>
                <?php } ?>
            </section>
        </div>
    </main>

    <script>
        function del() {
            return confirm("Are you sure you want to delete this?");
        }
    </script>
</body>
</html>
