<?php
// Database connection details
$host = 'localhost';
$user = 'flheaeax_Thuupqx'; // Database username
$pass = 'flheaeax_Thuupqx'; // Database password
$dbname = 'flheaeax_Thuupqx'; // Database name

// Create connection
$conn = new mysqli($host, $user, $pass, $dbname);

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

// Fetch data from the 'khate' table based on a unique identifier (e.g., 'shonu')
if (isset($_GET['shonu'])) {
    $shonu = $_GET['shonu'];  // Get the value of 'shonu' from the URL

    // Query to get data for a specific 'shonu'
    $query = "SELECT * FROM khate WHERE shonu = ?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("i", $shonu);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $data = $result->fetch_assoc();
    } else {
        echo "कोई डेटा नहीं मिला।";
        exit();
    }
} else {
    echo "शोनु का चयन नहीं किया गया।";
    exit();
}

// Update data if form is submitted
if (isset($_POST['submit'])) {
    $shonu = $_POST['shonu'];
    $byabaharkarta = $_POST['byabaharkarta'];
    $khatesankhye = $_POST['khatesankhye'];
    $khatakrama = $_POST['khatakrama'];
    $phalanubhavi = $_POST['phalanubhavi'];
    $kodprakara = $_POST['kodprakara'];
    $daka = $_POST['daka'];
    $kod = $_POST['kod'];
    $khatehesaru = $_POST['khatehesaru'];
    $duravani = $_POST['duravani'];
    $sthiti = $_POST['sthiti'];

    // Update the data in the database
    $updateQuery = "UPDATE khate SET byabaharkarta=?, khatesankhye=?, khatakrama=?, phalanubhavi=?, kodprakara=?, daka=?, kod=?, khatehesaru=?, duravani=?, sthiti=? WHERE shonu=?";
    $stmt = $conn->prepare($updateQuery);
    $stmt->bind_param("isisiisssii", $byabaharkarta, $khatesankhye, $khatakrama, $phalanubhavi, $kodprakara, $daka, $kod, $khatehesaru, $duravani, $sthiti, $shonu);

    if ($stmt->execute()) {
        $successMessage = "रिकॉर्ड सफलतापूर्वक अपडेट हो गया!";
    } else {
        $errorMessage = "रिकॉर्ड अपडेट करने में त्रुटि: " . $stmt->error;
    }
}
?>

<!DOCTYPE html>
<html lang="hi">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>खाते का डेटा अपडेट करें</title>
    <style>
        /* Main body style */
        body {
            font-family: 'Arial', sans-serif;
            background: linear-gradient(135deg, #6a11cb, #2575fc);
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            color: #333;
        }

        /* Container for the form */
        .container {
            width: 100%;
            max-width: 600px;
            background: #ffffff;
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0px 10px 30px rgba(0, 0, 0, 0.2);
            box-sizing: border-box;
        }

        /* Back Button */
        .back-icon {
            font-size: 24px;
            color: #2575fc;
            cursor: pointer;
            margin-bottom: 20px;
            display: inline-block;
            text-decoration: none;
        }

        .back-icon:hover {
            color: #6a11cb;
        }

        /* Heading Style */
        h2 {
            text-align: center;
            color: #2575fc;
            font-size: 28px;
            margin-bottom: 30px;
            font-weight: bold;
        }

        /* Form Label Style */
        label {
            font-size: 14px;
            color: #555;
            margin-bottom: 8px;
            display: block;
            font-weight: bold;
        }

        /* Input field style */
        input[type="text"] {
            width: 100%;
            padding: 12px;
            margin-bottom: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            color: #333;
            box-sizing: border-box;
            background-color: #f9f9f9;
            transition: all 0.3s ease;
        }

        input[type="text"]:focus {
            border-color: #2575fc;
            background-color: #ffffff;
            outline: none;
            box-shadow: 0 0 8px rgba(37, 117, 252, 0.3);
        }

        /* Button Style */
        button {
            background: linear-gradient(135deg, #2575fc, #6a11cb);
            color: white;
            border: none;
            padding: 15px 20px;
            font-size: 18px;
            cursor: pointer;
            border-radius: 8px;
            width: 100%;
            margin-top: 20px;
            transition: all 0.3s ease;
        }

        button:hover {
            background: linear-gradient(135deg, #6a11cb, #2575fc);
            transform: translateY(-2px);
        }

        /* Modal Styles */
        .modal-overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 999;
        }

        .modal {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0px 5px 20px rgba(0, 0, 0, 0.2);
            z-index: 1000;
            text-align: center;
        }

        .modal-content {
            font-size: 18px;
            color: #28a745;
            margin-bottom: 20px;
        }

        .modal-close {
            padding: 10px 20px;
            background-color: #2575fc;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .modal-close:hover {
            background-color: #6a11cb;
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- Back Icon -->
        <a href="javascript:history.back()" class="back-icon">
            &larr; वापस जाएं
        </a>
        <h2>खाते का डेटा अपडेट करें</h2>

        <form action="" method="POST">
            <label for="shonu">शोनु:</label>
            <input type="text" name="shonu" value="<?php echo $data['shonu']; ?>" readonly><br><br>

            <label for="byabaharkarta">व्यवहारकर्ता:</label>
            <input type="text" name="byabaharkarta" value="<?php echo $data['byabaharkarta']; ?>"><br><br>

            <label for="khatesankhye">खाते संख्यe:</label>
            <input type="text" name="khatesankhye" value="<?php echo $data['khatesankhye']; ?>"><br><br>

            <label for="khatakrama">खाता क्रम:</label>
            <input type="text" name="khatakrama" value="<?php echo $data['khatakrama']; ?>"><br><br>

            <label for="phalanubhavi">फलानुभवी:</label>
            <input type="text" name="phalanubhavi" value="<?php echo $data['phalanubhavi']; ?>"><br><br>

            <label for="kodprakara">कोड प्रकार:</label>
            <input type="text" name="kodprakara" value="<?php echo $data['kodprakara']; ?>"><br><br>

            <label for="daka">दाका:</label>
            <input type="text" name="daka" value="<?php echo $data['daka']; ?>"><br><br>

            <label for="kod">कोड:</label>
            <input type="text" name="kod" value="<?php echo $data['kod']; ?>"><br><br>

            <label for="khatehesaru">खातेहसारु:</label>
            <input type="text" name="khatehesaru" value="<?php echo $data['khatehesaru']; ?>"><br><br>

            <label for="duravani">दुरावानी:</label>
            <input type="text" name="duravani" value="<?php echo $data['duravani']; ?>"><br><br>

            <label for="sthiti">स्थिति:</label>
            <input type="text" name="sthiti" value="<?php echo $data['sthiti']; ?>"><br><br>

            <button type="submit" name="submit">अपडेट करें</button>
        </form>
    </div>

    <!-- Modal for Success Message -->
    <div class="modal-overlay" id="modalOverlay">
        <div class="modal" id="successModal">
            <div class="modal-content">
                <?php echo $successMessage ?? ''; ?>
            </div>
            <button class="modal-close" onclick="closeModal()">बंद करें</button>
        </div>
    </div>

    <script>
        // Function to show the modal
        function showModal() {
            document.getElementById('modalOverlay').style.display = 'block';
            document.getElementById('successModal').style.display = 'block';
        }

        // Function to close the modal
        function closeModal() {
            document.getElementById('modalOverlay').style.display = 'none';
            document.getElementById('successModal').style.display = 'none';
        }

        // Show modal if form is submitted successfully
        <?php if (isset($successMessage)): ?>
            showModal();
        <?php endif; ?>
    </script>
</body>
</html>

<?php
$conn->close();
?>