<?php
// Database connection details
$servername = "localhost";
$username = "flheaeax_Thuupqx"; // Replace with your MySQL username
$password = "flheaeax_Thuupqx"; // Replace with your MySQL password
$dbname = "flheaeax_Thuupqx"; // Replace with your database name
// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Search functionality
$search_shonu = $search_khatesankhye = "";
$sql = "SELECT * FROM khate WHERE 1";  // Default query with no filter

// Check if search fields are filled and build the query dynamically
if (isset($_POST['search_shonu']) || isset($_POST['search_khatesankhye'])) {
    if (!empty($_POST['search_shonu'])) {
        $search_shonu = $_POST['search_shonu'];
        $sql .= " AND shonu LIKE '%$search_shonu%'";
    }
    if (!empty($_POST['search_khatesankhye'])) {
        $search_khatesankhye = $_POST['search_khatesankhye'];
        $sql .= " AND khatesankhye LIKE '%$search_khatesankhye%'";
    }
}

$result = $conn->query($sql);

// Check if there are any records in the table
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Record Management</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f7f6;
            color: #333;
            margin: 0;
            padding: 0;
        }
        
        h1 {
            text-align: center;
            padding: 20px;
            background-color: #007BFF;
            color: white;
            margin: 0;
        }

        .back-btn {
            position: absolute;
            top: 20px;
            left: 20px;
            background-color: #007BFF;
            color: white;
            padding: 10px 15px;
            text-decoration: none;
            border-radius: 5px;
            font-size: 16px;
        }

        .back-btn:hover {
            background-color: #0056b3;
        }

        table {
            width: 90%;
            margin: 30px auto;
            border-collapse: collapse;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        th, td {
            padding: 12px 15px;
            text-align: left;
            border: 1px solid #ddd;
        }

        th {
            background-color: #007BFF;
            color: white;
        }

        tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        tr:hover {
            background-color: #f1f1f1;
        }

        .search-container {
            text-align: center;
            margin-top: 20px;
        }

        .search-container input[type="text"] {
            padding: 10px;
            width: 250px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        .search-container input[type="submit"] {
            padding: 10px 15px;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin-left: 10px;
        }

        .search-container input[type="submit"]:hover {
            background-color: #218838;
        }

        a {
            text-decoration: none;
            color: #007BFF;
        }

        a:hover {
            color: #0056b3;
        }

        form {
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        form input[type="text"] {
            width: 100%;
            padding: 10px;
            margin: 8px 0;
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        form input[type="submit"] {
            padding: 12px 20px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            width: 100%;
        }

        form input[type="submit"]:hover {
            background-color: #0056b3;
        }
    </style>

    <!-- JavaScript to trigger the form submission on keyup (typing) -->
    <script>
        function triggerSearch() {
            document.getElementById("search-form").submit();
        }
    </script>
</head>
<body>

<!-- Back Button -->
<a href="javascript:history.back()" class="back-btn">Back</a>

<h1>Record Management</h1>

<!-- Search Form -->
<div class="search-container">
    <form id="search-form" method="POST" action="" oninput="triggerSearch()">

        
        <label for="search_khatesankhye">Search account no.:</label>
        <input type="text" name="search_khatesankhye" value="<?php echo htmlspecialchars ($search_khatesankhye); ?>" placeholder="Enter Khatesankhye">
        
        <input type="submit" value="Search">
    </form>
</div>

<?php
if ($result->num_rows > 0) {
    echo "<table>
            <tr>
                <th>Shonu</th>
                <th>Byabaharkarta</th>
                <th>Khatesankhye</th>
                <th>Khatakrama</th>
                <th>Phalanubhavi</th>
                <th>Kodprakara</th>
                <th>Daka</th>
                <th>Kod</th>
                <th>Khatehesaru</th>
                <th>Duravani</th>
                <th>Sthiti</th>
                <th>Actions</th>
            </tr>";

    while ($row = $result->fetch_assoc()) {
        echo "<tr>
                <td>" . $row["shonu"] . "</td>
                <td>" . $row["byabaharkarta"] . "</td>
                <td>" . $row["khatesankhye"] . "</td>
                <td>" . $row["khatakrama"] . "</td>
                <td>" . $row["phalanubhavi"] . "</td>
                <td>" . $row["kodprakara"] . "</td>
                <td>" . $row["daka"] . "</td>
                <td>" . $row["kod"] . "</td>
                <td>" . $row["khatehesaru"] . "</td>
                <td>" . $row["duravani"] . "</td>
                <td>" . $row["sthiti"] . "</td>
                <td><a href='edit.php?shonu=" . $row['shonu'] . "'>Edit</a></td>
              </tr>";
    }

    echo "</table>";
} else {
    echo "<p>No results found</p>";
}

$conn->close();
?>

</body>
</html>
