<!DOCTYPE html>
<html lang="en">
<?php include 'includes/head.php'; ?>

<body>
    <div id="global-loader">
        <div class="whirly-loader"> </div>
    </div>
    <!-- Main Wrapper -->
    <div class="main-wrapper">

        <!-- Header -->
        <?php include './includes/navbar.php'; ?>
        <!-- /Header -->

        <?php $page = 'customer-list';
        include './includes/sidebar.php'; ?>

        <!-- /product list -->
        <div class="page-wrapper">
            <div class="content">
                <div class="page-header">
                    <div class="page-title">
                        <h4>Customer Details</h4>
                        <h6>Full details of a Customer</h6>
                    </div>
                </div>
                <!-- /add -->
                <div class="row">
                    <div class="col-lg-8 col-sm-12" id="jobDetails">
                        <div class="card">
                            <div class="card-body">
                                <div class="bar-code-view">
                                    <img src="images/barcode1.png" alt="barcode">
                                    <a class="printimg" id="downloadPDF" href="javascript:window.print()">
                                        <img src="images/printer.svg" alt="print">
                                    </a>
                                </div>
                                <div class="productdetails">
                                    <ul class="product-bar">
                                        <?php
                                        require_once 'php/databaseConnection.php';
                                        // Database connection
                                        $mysqli = db_connect();
                                        if (!$mysqli) {
                                            die("Database connection failed: " . $mysqli->connect_error);
                                        }

                                        // Get job number dynamically (fallback to a default value if not provided)
                                        $jobNumber = isset($_GET['job_number']) ? $mysqli->real_escape_string($_GET['job_number']) : 'JOBN7';

                                        // Secure query using prepared statement
                                        $stmt = $mysqli->prepare("SELECT * FROM customerField WHERE job_number = ?");
                                        $stmt->bind_param("s", $jobNumber);
                                        $stmt->execute();
                                        $result = $stmt->get_result();

                                        // Check if records exist
                                        if ($result->num_rows > 0) {
                                            while ($row = $result->fetch_assoc()) {
                                                echo '<li class="job-item">';
                                                echo '<h4>Job Number</h4><h6>' . htmlspecialchars($row['job_number'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Job Date</h4><h6>' . htmlspecialchars($row['job_date'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Registration Number</h4><h6>' . htmlspecialchars($row['register_number'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Year</h4><h6>' . htmlspecialchars($row['year'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Date/ Time In</h4><h6>' . htmlspecialchars($row['dateTimeIn'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Date/ Time Out</h4><h6>' . htmlspecialchars($row['dateTimeOut'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Mileage</h4><h6>' . htmlspecialchars($row['millage'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Vehicle Brand</h4><h6>' . htmlspecialchars($row['vehicle_brand'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Vehicle Colour</h4><h6>' . htmlspecialchars($row['vehicle_colour'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Engine Number</h4><h6>' . htmlspecialchars($row['engine_number'] ?? 'N/A') . '</h6>';
                                                echo '<h4>VIN Number</h4><h6>' . htmlspecialchars($row['vin_number'] ?? 'N/A') . '</h6>';
                                                echo '<h4>JObCard Status</h4><h6>' . htmlspecialchars($row['jobCardStatus'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Purpose of Visit</h4><h6>' . htmlspecialchars($row['purposeAll'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Fuel/ Battery Type</h4><h6>' . htmlspecialchars($row['fuelBatteryType'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Fuel Battery Level</h4><h6>' . htmlspecialchars($row['powerValue'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Customer Name</h4><h6>' . htmlspecialchars($row['customer_name'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Customer Number</h4><h6>' . htmlspecialchars($row['contact_number'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Address</h4><h6>' . htmlspecialchars($row['customer_address'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Reported Defects</h4><h6>' . htmlspecialchars($row['reported_defects'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Completed Action</h4><h6>' . htmlspecialchars($row['completed_action'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Technician Name</h4><h6>' . htmlspecialchars($row['technicianName'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Supervisor</h4><h6>' . htmlspecialchars($row['supervisor'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Contact Person</h4><h6>' . htmlspecialchars($row['contactPerson'] ?? 'N/A') . '</h6>';
                                                echo '<h4>Contact Person Number</h4><h6>' . htmlspecialchars($row['personContactNumber'] ?? 'N/A') . '</h6>';
                                                echo '</li>';                                                
                                            }
                                        } else {
                                            echo "<li><p>No job records found.</p></li>";
                                        }

                                        // Close connection
                                        $stmt->close();
                                        $mysqli->close();
                                        ?>
                                    </ul>

                                </div>
                            </div>
                        </div>
                       
                    </div>
                    <script>
                        document.getElementById('downloadPDF').addEventListener('click', function() {
                            const {
                                jsPDF
                            } = window.jspdf;
                            const doc = new jsPDF();

                            // Capture the content from the div with job details
                            doc.html(document.getElementById('jobDetails'), {
                                callback: function(doc) {
                                    doc.save('jobDetails.pdf'); // Save the generated PDF
                                },
                                margin: 10,
                                x: 10,
                                y: 10
                            });
                        });
                    </script>
                    <div class="col-lg-4 col-sm-12">
                        <div class="card">
                            <div class="card-body">
                                <?php

                                require_once 'php/databaseConnection.php';

                                // Get the jobNumber dynamically from the URL (use $_GET for URL parameters)
                                if (isset($_GET['job_number'])) {
                                    $job_number = $_GET['job_number']; // Retrieve the jobNumber from the URL
                                } else {
                                    echo "<p>Job number not provided!</p>";
                                    exit; // Exit if no jobNumber is provided
                                }

                                // Database connection
                                $mysqli = db_connect();
                                if (!$mysqli) {
                                    echo json_encode(["status" => "error", "message" => "Database connection failed: " . $mysqli->connect_error]);
                                    exit;
                                }

                                // SQL query to fetch imageOfCar and its filename from the jobCard table for the given jobNumber
                                $query = "SELECT imageOfCar FROM customerField WHERE job_number = ?";
                                $stmt = $mysqli->prepare($query);
                                if ($stmt === false) {
                                    echo json_encode(["status" => "error", "message" => "SQL preparation error: " . $mysqli->error]);
                                    exit;
                                }

                                // Bind parameters
                                $stmt->bind_param("s", $job_number);

                                // Execute the statement
                                $stmt->execute();

                                // Bind the result variable
                                $stmt->bind_result($imageOfCar);

                                // Fetch the result
                                if ($stmt->fetch()) {
                                    // Check if imageOfCar exists and display the HTML
                                    if ($imageOfCar) {
                                        // Get the image file name and size
                                        $image_file = basename($imageOfCar);  // Extract the file name from the full path
                                        $image_size = filesize($imageOfCar);  // Get the file size

                                        // Format size in KB
                                        $image_size_kb = round($image_size / 1024, 2) . ' KB';

                                        // Display the HTML structure
                                        echo '
            <div class="slider-product">
                <img src="' . $imageOfCar . '" alt="Car Image">
                <h4>' . $image_file . '</h4>
                <h6>' . $image_size_kb . '</h6>
            </div>';
                                    } else {
                                        echo "<p>No image found for job number " . htmlspecialchars($job_number) . "</p>";
                                    }
                                } else {
                                    echo "<p>No record found for job number " . htmlspecialchars($job_number) . "</p>";
                                }

                                $stmt->close();
                                $mysqli->close();

                                ?>
                            </div>

                        </div>
                    </div>
                </div>

                <!-- /add -->
            </div>
        </div>
        <!-- /product list -->
    </div>
    <!-- /Main Wrapper -->

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
    <!-- jQuery -->
    <script src="js/jquery-3.7.1.min.js" type="5587a3acf923afacc789f535-text/javascript"></script>

    <!-- Feather Icon JS -->
    <script src="js/feather.min.js" type="5587a3acf923afacc789f535-text/javascript"></script>

    <!-- Slimscroll JS -->
    <script src="js/jquery.slimscroll.min.js" type="5587a3acf923afacc789f535-text/javascript"></script>

    <!-- Bootstrap Core JS -->
    <script src="js/bootstrap.bundle.min.js" type="5587a3acf923afacc789f535-text/javascript"></script>

    <!-- Owl JS -->
    <script src="js/owl.carousel.min.js" type="5587a3acf923afacc789f535-text/javascript"></script>

    <!-- Select2 JS -->
    <script src="js/select2.min.js" type="5587a3acf923afacc789f535-text/javascript"></script>

    <!-- Color Picker JS -->
    <script src="js/pickr.es5.min.js" type="5587a3acf923afacc789f535-text/javascript"></script>

    <!-- Custom JS -->
    <script src="js/theme-colorpicker.js" type="5587a3acf923afacc789f535-text/javascript"></script>
    <script src="js/script.js" type="5587a3acf923afacc789f535-text/javascript"></script>


    <script src="js/rocket-loader.min.js" data-cf-settings="5587a3acf923afacc789f535-|49" defer=""></script>


</body>

</html>