<?php
session_start();
require_once '../db.php'; 

// Check if user is logged in
if (!isset($_SESSION['Mid'])) {
    header("Location: login.php");
    exit();
}

$mid = $_SESSION['Mid'];
$name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);

if (($_SERVER["REQUEST_METHOD"] == "POST") && isset($_POST['submit'])) {
    $msgtype = $_POST["msgtype"];
    $message = $_POST["message"];
    $date = date('Y-d-m');
    
    if ($msgtype == 'complain') {
        $sql = "INSERT INTO `complain`( `complain`, `Mid`, `cdate`, `status`) VALUES ('$message', '$mid', '$date', 'onworking')";
    } elseif ($msgtype == 'Secretcomplain') {
        $sql = "INSERT INTO `complain`( `complain`, `Mid`, `cdate`, `status`) VALUES ('$message', '1', '$date', 'onworking')";
    } else {
        $sql = "INSERT INTO contact(Mid, message, cdate) VALUES ('$mid', '$message', '$date')";
    }
    
    if (mysqli_query($conn, $sql)) {
        $_SESSION['contact'] = "<script>alert('Thank you for touch with us')</script>";
        echo "<script>window.location.href='http://localhost/LoanApprovalPredction/Members/contact.php'</script>";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($name); ?></title>
    <link rel="icon" href="../images/Icon.png">
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen flex flex-col">
    <?php include '../includes/navbar.php'; ?>
    <div class="flex items-center justify-center flex-grow p-4">
        <div class="bg-white p-8 rounded-lg shadow-lg max-w-3xl w-full">
            <h1 class="text-2xl font-bold text-gray-800 mb-6 text-center">Contact Us</h1>
            
            <?php if (isset($_SESSION['contact'])): ?>
                <div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4 mb-6" role="alert">
                    <p>Thank you for getting in touch with us!</p>
                </div>
                <?php unset($_SESSION['contact']); ?>
            <?php endif; ?>
            
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" class="space-y-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Message Type:</label>
                    <div class="flex gap-4 flex-wrap">
                        <label class="flex items-center space-x-2">
                            <input type="radio" name="msgtype" value="complain" required 
                                   class="h-4 w-4 text-blue-600 border-gray-300 focus:ring-blue-500">
                            <span class="text-gray-700">Complain</span>
                        </label>
                        <label class="flex items-center space-x-2">
                            <input type="radio" name="msgtype" value="Secretcomplain" 
                                   class="h-4 w-4 text-blue-600 border-gray-300 focus:ring-blue-500">
                            <span class="text-gray-700">Secret Complain</span>
                        </label>
                        <label class="flex items-center space-x-2">
                            <input type="radio" name="msgtype" value="message" 
                                   class="h-4 w-4 text-blue-600 border-gray-300 focus:ring-blue-500">
                            <span class="text-gray-700">Message</span>
                        </label>
                    </div>
                </div>
                <div>
                    <label for="message" class="block text-sm font-medium text-gray-700">Write Your Message/Complain Here:</label>
                    <textarea id="message" name="message" required 
                              class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
                              rows="4" placeholder="Enter your message or complaint"></textarea>
                </div>
                <button type="submit" name="submit" 
                        class="w-full bg-green-600 text-white py-2 px-4 rounded-md hover:bg-green-700 transition duration-200">
                    Send Message
                </button>
            </form>
            
            <div class="mt-8">
                <h2 class="text-xl font-bold text-gray-800 mb-4 text-center">Your Complaints/Messages</h2>
                <?php 
                    $sql1 = "SELECT complain.Mid, complain.complain, complain.cdate, complain.status, members.Mid, members.MembersName 
                             FROM complain 
                             INNER JOIN members ON complain.Mid = members.Mid 
                             WHERE complain.Mid = '$mid' OR complain.Mid = '1'";
                    $result1 = mysqli_query($conn, $sql1);
                    if (mysqli_num_rows($result1) > 0) {
                        $id = 0;
                ?>
                    <div class="overflow-x-auto">
                        <table class="w-full border-collapse">
                            <caption class="bg-gray-200 text-lg font-semibold p-2 rounded-t">
                                Your complaints or secret complaints will display here
                            </caption>
                            <thead>
                                <tr class="bg-green-100">
                                    <th class="border border-gray-300 px-4 py-2 text-left">ID</th>
                                    <th class="border border-gray-300 px-4 py-2 text-left">Name</th>
                                    <th class="border border-gray-300 px-4 py-2 text-left">Complain Date</th>
                                    <th class="border border-gray-300 px-4 py-2 text-left">Status</th>
                                    <th class="border border-gray-300 px-4 py-2 text-left">Complain</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php while ($rows1 = mysqli_fetch_assoc($result1)) { $id++; ?>
                                    <tr class="hover:bg-gray-50">
                                        <td class="border border-gray-300 px-4 py-2"><?php echo $id; ?></td>
                                        <td class="border border-gray-300 px-4 py-2"><?php echo htmlspecialchars($rows1['MembersName']); ?></td>
                                        <td class="border border-gray-300 px-4 py-2"><?php echo $rows1['cdate']; ?></td>
                                        <td class="border border-gray-300 px-4 py-2"><?php echo $rows1['status']; ?></td>
                                        <td class="border border-gray-300 px-4 py-2"><?php echo htmlspecialchars($rows1['complain']); ?></td>
                                    </tr>
                                <?php } ?>
                            </tbody>
                        </table>
                    </div>
                <?php } else { ?>
                    <p class="text-gray-600 text-center">No complaints or messages found.</p>
                <?php } ?>
            </div>
        </div>
    </div>
</body>
</html>