<?php
session_start();
require_once 'database/shikanisha.kts.php';
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Required meta tags-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="Kakashi Tech Solutions">
    <meta name="author" content="$Kakashi Tech Solutions">
    <meta name="keywords" content="Kakashi Tech Solutions">

    <!-- Title Page-->
    <title>Kakashi Tech Solutions</title>

    <!-- Favicon -->
    <link rel="icon" type="image/png" href="./logo.png">

    <!-- Icons font CSS-->
    <link href="./logs/vendor/mdi-font/css/material-design-iconic-font.min.css" rel="stylesheet" media="all">
    <link href="./logs/vendor/font-awesome-4.7/css/font-awesome.min.css" rel="stylesheet" media="all">
    
    <!-- Font special for pages-->
    <link href="https://fonts.googleapis.com/css?family=Poppins:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">

    <!-- Vendor CSS-->
    <link href="./logs/vendor/select2/select2.min.css" rel="stylesheet" media="all">
    <link href="./logs/vendor/datepicker/daterangepicker.css" rel="stylesheet" media="all">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">

    <!-- Main CSS-->
    <link href="./logs/css/main.css" rel="stylesheet" media="all">
</head>

<body>
    <div class="page-wrapper bg-gra-01 p-t-180 p-b-180 font-poppins">
        <div class="centered-heading">
            <h1 class="food-heading text-white"><i>Register Your Business</i></h1>
        </div>

        <div class="wrapper wrapper--w780">
            <div class="card card-3">
                <div class="card-heading food-heading"></div>

                <div class="card-body">
                    <h2 class="title">Business Registration</h2>
                    <form id="register-business">
                        <div class="input-group">
                            <input class="input--style-3" type="text" placeholder="Business Name" name="shopname" required>
                        </div>

                        <div class="input-group">
                            <input class="input--style-3" type="text" placeholder="Location" name="location" required>
                        </div>

                        <div class="input-group">
                            <input class="input--style-3" type="text" placeholder="Street" name="street" required>
                        </div>

                        <div class="input-group">
                            <input class="input--style-3" type="text" placeholder="Address" name="address" required>
                        </div>

                        <div class="input-group">
                            <input class="input--style-3" type="text" placeholder="PIN" name="pin" required>
                        </div>

                        <div class="input-group">
                            <input class="input--style-3" type="text" placeholder="Contact 1" name="contact1" required>
                        </div>

                        <div class="input-group">
                            <input class="input--style-3" type="text" placeholder="Contact 2 (Optional)" name="contact2">
                        </div>
                         <div class="input-group">
                            <input class="input--style-3" type="text" placeholder="Business Quote" name="quote">
                        </div>

                        <div class="p-t-10">
                            <button class="btn btn--pill btn--green" type="submit">Register Business</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- JS Scripts -->
    <script src="./js/jquery-3.6.4.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
    <script src="./logs/vendor/select2/select2.min.js"></script>

    <script>
    $(document).ready(function() {
        $('#register-business').on('submit', function(event) {
            event.preventDefault(); // Prevent form submission

            // Serialize form data
            var formData = $(this).serialize();

            // Send Ajax request
            $.ajax({
                type: 'POST',
                url: './database/register_business.kts.php', // Update with your PHP script location
                data: formData,
                success: function(response) {
                    var jsonResponse = JSON.parse(response);

                    if (jsonResponse.status === 'success') {
                        toastr.success(jsonResponse.message);
                        // Redirect or reset form after success
                        setTimeout(function() {
                            window.location.href = 'index.php'; // Update this to the success page
                        }, 2000);
                    } else {
                        toastr.error(jsonResponse.message);
                    }
                },
                error: function() {
                    toastr.error('An error occurred. Please try again.');
                }
            });
        });
    });
    </script>
            <script>
    // JavaScript code to fetch data and display it
    function fetchBusinessDetails() {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', './database/fetch_business.kts.php', true);
        xhr.onload = function() {
            if (this.status === 200) {
                var data = JSON.parse(this.responseText);
                
                console.log(data);  // Check if contact2 is present in the response
                
                // Check if no error
                if (!data.error) {
                    // Update the span elements with the fetched data
                    document.getElementById('shopname').innerText = data.shop_name || '';
                    document.getElementById('location').innerText = data.location || '';
                    document.getElementById('quote').innerText = data.quote || '';
                    document.getElementById('street').innerText = data.street || '';
                    document.getElementById('address').innerText = data.address || '';
                    document.getElementById('pin').innerText = data.pin || '';
                    document.getElementById('contact1').innerText = data.contact1 || '';
                    document.getElementById('contact2').innerText = data.contact2 || ''; // This should populate contact2
                } else {
                    console.error("Error: " + data.error);
                }
            } else {
                console.error("Failed to fetch data");
            }
        };
        xhr.send();
    }

    // Call the function as soon as the DOM is fully loaded
    document.addEventListener('DOMContentLoaded', fetchBusinessDetails);
</script>
</body>

</html>
