<?php
include "dbconnect.php";  // Database connection

// Start session if not already started
session_start();

// Capture Razorpay payment details from URL parameters
if (isset($_GET['razorpay_payment_id'], $_GET['razorpay_order_id'], $_GET['razorpay_signature'])) {
    $payment_id = $_GET['razorpay_payment_id'];
    $order_id = $_GET['razorpay_order_id'];
    $signature = $_GET['razorpay_signature'];
} else {
    die("Missing payment details.");
}

// Capture user details from URL parameters
$username = isset($_GET['username']) ? htmlspecialchars($_GET['username']) : '';
$email = isset($_GET['email']) ? htmlspecialchars($_GET['email']) : '';
$mobile = isset($_GET['mobile']) ? htmlspecialchars($_GET['mobile']) : '';

// Razorpay secret key to verify the signature
$secret_key = 'FFaibhB1IGG67p7tHSukZRMX';  // Replace with your Razorpay secret key

// Construct the data for signature verification
$data = $order_id . "|" . $payment_id;

// Generate the expected signature
$generated_signature = hash_hmac('sha256', $data, $secret_key);

// Verify the signature
if ($generated_signature != $signature) {
    die("Payment verification failed! Signature mismatch.");
}

// Insert payment data into database
if ($generated_signature == $signature) {
    // Sanitize input data before inserting into the database
    $username = mysqli_real_escape_string($con, $username);
    $email = mysqli_real_escape_string($con, $email);
    $mobile = mysqli_real_escape_string($con, $mobile);

    // Original amount is ₹29,500 (₹29500 in paise)
    $amount = 29500; // ₹29500 in paise
    $gst_percentage = 18;
    $gst_amount = ($amount * $gst_percentage) / 100;
    $total_amount = $amount + $gst_amount; // Total amount including GST
    $status = 'Completed'; // Assuming successful payment

    // Prepare the SQL query to insert payment details
    $sql = "INSERT INTO payments (order_id, payment_id, username, email, mobile, amount, gst_amount, total_amount, status) 
            VALUES ('$order_id', '$payment_id', '$username', '$email', '$mobile', '$amount', '$gst_amount', '$total_amount', '$status')";

    // Execute the query
    if (mysqli_query($con, $sql)) {
        echo "Payment details have been successfully saved.";
    } else {
        echo "Error: " . mysqli_error($con);
    }
}

// Fetch and display the saved payment details from the database
$query = "SELECT * FROM payments WHERE payment_id = '$payment_id'";
$result = mysqli_query($con, $query);

if ($result) {
    $payment = mysqli_fetch_assoc($result);
    // Store the payment data in variables
    $username = $payment['username'];
    $email = $payment['email'];
    $mobile = $payment['mobile'];
    $order_id = $payment['order_id'];
    $payment_id = $payment['payment_id'];
    $amount = $payment['amount'];
    $gst_amount = $payment['gst_amount'];
    $total_amount = $payment['total_amount'];
    $status = $payment['status'];
} else {
    echo "Error: " . mysqli_error($con);
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bill Slip</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(45deg, #73C8A9, #373B44); /* Smooth gradient background */
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .bill-container {
            width: 75%;
            max-width: 800px;
            padding: 30px;
            background: white;
            border-radius: 12px;
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
            box-sizing: border-box;
        }

        .bill-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
        }

        .bill-header h2 {
            font-size: 30px;
            color: #1D1E1C;
            margin: 0;
            font-weight: bold;
        }

        .company-logo {
            width: 150px; /* Increased logo size */
            height: auto;
        }

        .company-info {
            font-size: 14px;
            color: #555;
        }

        .client-info {
            margin-top: 20px;
            margin-bottom: 30px;
        }

        .client-info h3 {
            font-size: 22px;
            margin: 0 0 10px 0;
            color: #1D1E1C;
        }

        .client-info p {
            margin: 5px 0;
            color: #555;
        }

        table {
            width: 100%;
            margin-top: 20px;
            border-collapse: collapse;
            font-size: 16px;
            margin-bottom: 30px;
        }

        th, td {
            padding: 12px 18px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }

        th {
            background-color: #45B8AC;
            color: #fff;
        }

        tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        .footer {
            margin-top: 30px;
            text-align: center;
            font-size: 14px;
            color: #888;
        }

        .print-button {
            display: inline-block;
            margin-top: 20px;
            padding: 12px 24px;
            background-color: #45B8AC;
            color: white;
            font-size: 16px;
            text-align: center;
            border-radius: 25px;
            border: none;
            cursor: pointer;
            transition: background-color 0.3s ease, transform 0.3s ease;
        }

        .print-button:hover {
            background-color: #36A79B;
            transform: scale(1.05);
        }

        .print-button:focus {
            outline: none;
        }

        #current-date {
            font-size: 14px;
            color: #888;
        }

        /* Print Specific Styles */
        @media print {
            .bill-header h2 {
                font-size: 24px; /* Reduced the size of the company name */
            }

            .company-logo {
                width: 100px; /* Reduced logo size */
            }

            .footer {
                display: none; /* Hide footer on print */
            }

            .print-button, #login-button {
                display: none; /* Hide print and login buttons */
            }

            table {
                margin-top: 30px;
            }

            td, th {
                font-size: 14px; /* Reduce font size for better print layout */
            }
        }
    </style>
</head>
<body>

    <div class="bill-container">
        <div class="bill-header">
            <img src="img/logo.jpg" alt="Company Logo" class="company-logo"> <!-- Add your logo image file path here -->
            <div>
                <h2>ChemTechie Engineering & Consulting Services Pvt. Ltd.</h2>
                <p class="company-info">Mobile: +91-9821960679</p>
                <p class="company-info">Address: U-18/3 Ground Floor, Pink Town House, DLF Phase III, Haryana, India - 122002</p>
                <p class="company-info">Email: info@chemtechieg.com</p>
            </div>
        </div>

        <p>Date: <span id="current-date"></span></p>

        <div class="client-info">
            <h3>Client Information</h3>
            <p>Name: <?php echo $username; ?></p>
            <p>Email: <?php echo $email; ?></p>
            <p>Mobile: <?php echo $mobile; ?></p>
        </div>

        <table>
            <tr>
                <th>Order ID</th>
                <td><?php echo $order_id; ?></td>
            </tr>
            <tr>
                <th>Payment ID</th>
                <td><?php echo $payment_id; ?></td>
            </tr>
            <tr>
                <th>Amount (without GST)</th>
                <td>₹<?php echo $amount / 100; ?> </td> <!-- Displaying ₹29,500 -->
            </tr>
            <tr>
                <th>GST (18%)</th>
                <td>₹<?php echo $gst_amount / 100; ?> </td> <!-- Displaying ₹5310 -->
            </tr>
            <tr>
                <th>Total Amount</th>
                <td>₹<?php echo $total_amount / 100; ?> </td> <!-- Displaying ₹34,810 -->
            </tr>
            <tr>
                <th>Status</th>
                <td><?php echo $status; ?></td>
            </tr>
        </table>

        <div class="footer">
            <p>Thank you for your business!</p>
        </div>

        <!-- Print Button -->
        <button class="print-button" onclick="window.print()">Print Bill</button>

        <!-- Login Button -->
        <a href="login.php" id="login-button">
            <button class="print-button">Login</button>
        </a>

        <!-- Display Total Amount with GST near the Pay button -->
        <div class="d-flex justify-content-center">
            <button class="print-button" id="razorpay-button">Pay ₹<?php echo $total_amount / 100; ?> Now</button>
        </div>
    </div>

    <script>
        // To show the current date in the bill
        const currentDate = new Date().toLocaleDateString();
        document.getElementById('current-date').textContent = currentDate;

        // Close the page after 2 minutes (120,000 milliseconds)
        setTimeout(function() {
            window.close();  // Close the page
        }, 120000); // 2 minutes (in milliseconds)
    </script>

</body>
</html>
