<!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 = 'product-list';
        include './includes/sidebar.php'; ?>

        <div class="page-wrapper">
            <div class="content">
                <div class="page-header">
                    <div class="add-item d-flex">
                        <div class="page-title">
                            <h4>Invoice Report </h4>
                            <h6>Manage Your Invoice Report</h6>
                        </div>
                    </div>
                    <ul class="table-top-head">
                        <li>
                            <a data-bs-toggle="tooltip" data-bs-placement="top" title="Pdf" onclick="generatePDF()"><img src="images/pdf.svg" alt="img"></a>
                        </li>
                        <li>
                            <a data-bs-toggle="tooltip" data-bs-placement="top" title="Excel"><img src="images/excel.svg" alt="img"></a>
                        </li>
                        <li>
                            <a data-bs-toggle="tooltip" data-bs-placement="top" title="Print"><i data-feather="printer" class="feather-rotate-ccw"></i></a>
                        </li>
                        <li>
                            <a data-bs-toggle="tooltip" data-bs-placement="top" title="Refresh"><i data-feather="rotate-ccw" class="feather-rotate-ccw"></i></a>
                        </li>
                        <li>
                            <a data-bs-toggle="tooltip" data-bs-placement="top" title="Collapse" id="collapse-header"><i data-feather="chevron-up" class="feather-chevron-up"></i></a>
                        </li>
                    </ul>
                </div>


                <!-- Invoices -->
                <div class="main-container" id="jobCardContent">
                    <div class="card">
                        <div class="card-body">
                            <div class="row justify-content-between align-items-center border-bottom mb-3">
                                <div class="col-md-6">
                                    <div class="mb-0">
                                        <img src="./images/Risav Dai a copy.png" alt="ArthaVidhi Logo" width="180" height="60">
                                    </div>

                                    <p>Gandak-14, Birgunj(Parsa), Nepal</p>
                                </div>
                                <div class="col-md-6">
                                    <div class="text-end mb-3">
                                        <h5 class="text-gray mb-1">Service and Maintenance</h5>
                                        <h1 class="text-primary fw-bold">JOB CARD</h1>
                                    </div>

                                </div>
                            </div>
                        </div>
                    </div>
                    <div>
                        <div class="table-responsive mb-3">
                            <table class="table">
                                <thead class="thead-light">
                                    <tr>
                                        <th>SN</th>
                                        <th>Particulars</th>
                                        <th>Rate (Rs)</th>
                                        <th>Quantity</th>
                                        <th>Net Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    require_once 'php/databaseConnection.php';

                                    // Database connection
                                    $mysqli = db_connect();
                                    if (!$mysqli) {
                                        die("Database connection failed: " . $mysqli->connect_error);
                                    }

                                    // Get job number dynamically
                                    $jobNumber = isset($_GET['jobNumber']) ? $mysqli->real_escape_string($_GET['jobNumber']) : 'JOBN7';

                                    // Prepare a secure query
                                    $stmt = $mysqli->prepare("SELECT lubType, lubQuan, lubPrice, lubDis, lubNet FROM lubricantsNew WHERE jobNumber = ?");
                                    $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 "<tr>";
                                            echo "<td>&#9679;</td>";
                                            echo "<td>" . htmlspecialchars($row['lubType']) . "</td>";
                                            echo "<td>" . htmlspecialchars($row['lubPrice']) . "</td>";
                                            echo "<td>" . htmlspecialchars($row['lubQuan']) . "</td>";
                                            echo "<td>" . htmlspecialchars($row['lubNet']) . "</td>";
                                            echo "</tr>";
                                        }
                                    }

                                    // Close the database connection
                                    $stmt->close();
                                    $mysqli->close();
                                    ?>
                                    <?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['jobNumber']) ? $mysqli->real_escape_string($_GET['jobNumber']) : 'JOBN7';

                                    // Prepare a secure query using a prepared statement
                                    $stmt = $mysqli->prepare("SELECT labourCode, labourQuan, labourPrice, labourDis, labourNet FROM labours WHERE jobNumber = ?");
                                    $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 "<tr>";
                                            echo "<td>&#9679;</td>";
                                            echo "<td>" . htmlspecialchars($row['labourCode']) . "</td>"; // Particulars
                                            echo "<td>" . htmlspecialchars($row['labourPrice']) . "</td>"; // Rate
                                            echo "<td>" . htmlspecialchars($row['labourQuan']) . "</td>";  // Quantity
                                            echo "<td>" . htmlspecialchars($row['labourNet']) . "</td>";   // Net Total
                                            echo "</tr>";
                                        }
                                    }

                                    // Close the database connection
                                    $stmt->close();
                                    $mysqli->close();
                                    ?>
                                    <?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['jobNumber']) ? $mysqli->real_escape_string($_GET['jobNumber']) : 'JOBN7';

                                    // Prepare a secure query using a prepared statement
                                    $stmt = $mysqli->prepare("SELECT dentType, dentQuan, dentPrice, dentDis, dentNet FROM dents WHERE jobNumber = ?");
                                    $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 "<tr>";
                                            echo "<td>&#9679;</td>";
                                            echo "<td>" . htmlspecialchars($row['dentType']) . "</td>";     // Particulars
                                            echo "<td>" . htmlspecialchars($row['dentPrice']) . "</td>";    // Rate
                                            echo "<td>" . htmlspecialchars($row['dentQuan']) . "</td>";     // Quantity
                                            echo "<td>" . htmlspecialchars($row['dentNet']) . "</td>";      // Net Total
                                            echo "</tr>";
                                        }
                                    }

                                    // Close the database connection
                                    $stmt->close();
                                    $mysqli->close();
                                    ?>
                                    <?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['jobNumber']) ? $mysqli->real_escape_string($_GET['jobNumber']) : 'JOBN7';

                                    // Prepare a secure query using a prepared statement
                                    $stmt = $mysqli->prepare("SELECT spareType, spareQuan, sparePrice, spareDis, spareNet FROM spares WHERE jobNumber = ?");
                                    $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 "<tr>";
                                            echo "<td>&#9679;</td>";
                                            echo "<td>" . htmlspecialchars($row['spareType']) . "</td>";     // Particulars
                                            echo "<td>" . htmlspecialchars($row['sparePrice']) . "</td>";    // Rate
                                            echo "<td>" . htmlspecialchars($row['spareQuan']) . "</td>";     // Quantity
                                            echo "<td>" . htmlspecialchars($row['spareNet']) . "</td>";      // Net Total
                                            echo "</tr>";
                                        }
                                    }

                                    // Close the database connection
                                    $stmt->close();
                                    $mysqli->close();
                                    ?>
                                    <?php
                                    require_once 'php/databaseConnection.php';

                                    // Database connection
                                    $mysqli = db_connect();
                                    if (!$mysqli) {
                                        die("Database connection failed: " . $mysqli->connect_error);
                                    }

                                    // Get job number
                                    $jobNumber = isset($_GET['jobNumber']) ? $mysqli->real_escape_string($_GET['jobNumber']) : 'JOBN7';

                                    // Prepare a secure query
                                    $stmt = $mysqli->prepare("SELECT type_code, price, quantity, discount, net_amount FROM customs WHERE jobNumber = ? AND category = 'Lubricant'");
                                    $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 "<tr>";
                                            echo "<td>&#9679;</td>";
                                            echo "<td>" . htmlspecialchars($row['type_code']) . "</td>";           // Particulars
                                            echo "<td>" . htmlspecialchars($row['price']) . "</td>";              // Rate
                                            echo "<td>" . htmlspecialchars($row['quantity']) . "</td>";           // Quantity
                                            echo "<td>" . htmlspecialchars($row['net_amount']) . "</td>";         // Net Total
                                            echo "</tr>";
                                        }
                                    }

                                    // Close the database connection
                                    $stmt->close();
                                    $mysqli->close();
                                    ?>
                                    <?php
                                    require_once 'php/databaseConnection.php';

                                    // Database connection
                                    $mysqli = db_connect();
                                    if (!$mysqli) {
                                        die("Database connection failed: " . $mysqli->connect_error);
                                    }

                                    // Get job number
                                    $jobNumber = isset($_GET['jobNumber']) ? $mysqli->real_escape_string($_GET['jobNumber']) : 'JOBN7';

                                    // Prepare a secure query using a prepared statement
                                    $stmt = $mysqli->prepare("SELECT type_code, price, quantity, discount, net_amount FROM customs WHERE jobNumber = ? AND category = 'Labour'");
                                    $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 "<tr>";
                                            echo "<td>&#9679;</td>";
                                            echo "<td>" . htmlspecialchars($row['type_code']) . "</td>";           // Particulars
                                            echo "<td>" . htmlspecialchars($row['price']) . "</td>";              // Rate
                                            echo "<td>" . htmlspecialchars($row['quantity']) . "</td>";           // Quantity
                                            echo "<td>" . htmlspecialchars($row['net_amount']) . "</td>";         // Net Total
                                            echo "</tr>";
                                        }
                                    }

                                    // Close the database connection
                                    $stmt->close();
                                    $mysqli->close();
                                    ?>
                                    <?php
                                    require_once 'php/databaseConnection.php';

                                    // Database connection
                                    $mysqli = db_connect();
                                    if (!$mysqli) {
                                        die("Database connection failed: " . $mysqli->connect_error);
                                    }

                                    // Get job number
                                    $jobNumber = isset($_GET['jobNumber']) ? $mysqli->real_escape_string($_GET['jobNumber']) : 'JOBN7';

                                    // Prepare a secure query using a prepared statement
                                    $stmt = $mysqli->prepare("SELECT type_code, price, quantity, discount, net_amount FROM customs WHERE jobNumber = ? AND category = 'Dent/Paint'");
                                    $stmt->bind_param("s", $jobNumber);
                                    $stmt->execute();
                                    $result = $stmt->get_result();

                                    // Check if records exist
                                    if ($result->num_rows > 0) {
                                        $count = 1;
                                        while ($row = $result->fetch_assoc()) {
                                            echo "<tr>";
                                            echo "<td>&#9679;</td>";  // <-- Here is the big bold dot
                                            echo "<td>" . htmlspecialchars($row['type_code']) . "</td>"; // Particulars
                                            echo "<td>" . htmlspecialchars($row['price']) . "</td>";    // Rate
                                            echo "<td>" . htmlspecialchars($row['quantity']) . "</td>"; // Quantity
                                            echo "<td>" . htmlspecialchars($row['net_amount']) . "</td>"; // Net Total
                                            echo "</tr>";
                                        }
                                    }

                                    // Close the database connection
                                    $stmt->close();
                                    $mysqli->close();
                                    ?>
                                    <?php
                                    require_once 'php/databaseConnection.php';

                                    // Database connection
                                    $mysqli = db_connect();
                                    if (!$mysqli) {
                                        die("Database connection failed: " . $mysqli->connect_error);
                                    }

                                    // Get job number
                                    $jobNumber = isset($_GET['jobNumber']) ? $mysqli->real_escape_string($_GET['jobNumber']) : 'JOBN7';

                                    // Prepare a secure query using a prepared statement
                                    $stmt = $mysqli->prepare("SELECT type_code, price, quantity, discount, net_amount FROM customs WHERE jobNumber = ? AND category = 'Spare Parts'");
                                    $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 "<tr>";
                                            echo "<td>&#9679;</td>"; // SN - Big bold bullet point
                                            echo "<td>" . htmlspecialchars($row['type_code']) . "</td>"; // Particulars
                                            echo "<td>" . htmlspecialchars($row['price']) . "</td>";    // Rate
                                            echo "<td>" . htmlspecialchars($row['quantity']) . "</td>"; // Quantity
                                            echo "<td>" . htmlspecialchars($row['net_amount']) . "</td>"; // Net Total
                                            echo "</tr>";
                                        }
                                    }

                                    // Close the database connection
                                    $stmt->close();
                                    $mysqli->close();
                                    ?>
                                    <!-- <tr>
										<td>2</td>
										<td>Labour Code Total</td>
										<td class="text-end">Rs &nbsp;<//?php echo $jobCard['labourBillAmount']; ?></td>
									</tr>
									<tr>
										<td>3</td>
										<td>Dent/Paint Type Total</td>
										<td class="text-end">Rs &nbsp;<//?php echo $jobCard['dentBillAmount']; ?></td>
									</tr>
									<tr>
										<td>4</td>
										<td>Spare Parts Total</td>
										<td class="text-end">Rs &nbsp;<//?php echo $jobCard['billAmount']; ?></td>
									</tr>
									<tr class="fw-bold">
										<td colspan="2" class="text-end">Net Total:</td>
										<td class="text-end">Rs &nbsp;<//?php echo $jobCard['lubricantBillAmount'] + $jobCard['labourBillAmount'] + $jobCard['dentBillAmount'] + $jobCard['billAmount']; ?></td>
									</tr> -->
                                </tbody>
                            </table>
                        </div>

                    </div>
                    <div class="row border-bottom mb-3">
                        <div class="col-md-5 ms-auto mb-3">
                            <?php
                            require_once 'php/databaseConnection.php';

                            // Database connection
                            $mysqli = db_connect();
                            if (!$mysqli) {
                                die("Database connection failed: " . $mysqli->connect_error);
                            }

                            // Get job number
                            $jobNumber = isset($_GET['jobNumber']) ? $mysqli->real_escape_string($_GET['jobNumber']) : 'JOBN7';

                            // Prepare and execute the statement
                            $stmt = $mysqli->prepare("SELECT TotalBillAmount, vatAmount, discountAmount, netTotal, billNumber, billRemarks, totalPaid, remainingAmount, paymentStatus FROM jobCard WHERE jobNumber = ?");
                            $stmt->bind_param("s", $jobNumber);
                            $stmt->execute();
                            $result = $stmt->get_result();

                            if ($result->num_rows > 0) {
                                $row = $result->fetch_assoc();
                            ?>
                                <div class="d-flex justify-content-between align-items-center border-bottom mb-2 pe-3">
                                    <p class="mb-0">Sub Total</p>
                                    <p class="text-dark fw-medium mb-2"><?php echo htmlspecialchars($row['TotalBillAmount']); ?></p>
                                </div>
                                <div class="d-flex justify-content-between align-items-center border-bottom mb-2 pe-3">
                                    <p class="mb-0">Discount (in Rs)</p>
                                    <p class="text-dark fw-medium mb-2"><?php echo htmlspecialchars($row['discountAmount']); ?></p>
                                </div>
                                <div class="d-flex justify-content-between align-items-center mb-2 pe-3">
                                    <p class="mb-0">VAT (13%)</p>
                                    <p class="text-dark fw-medium mb-2"><?php echo htmlspecialchars($row['vatAmount']); ?></p>
                                </div>
                                <div class="d-flex justify-content-between align-items-center mb-2 pe-3">
                                    <h5>Grand Total</h5>
                                    <h5><?php echo htmlspecialchars($row['netTotal']); ?></h5>
                                </div>
                                <div class="d-flex justify-content-between align-items-center mb-2 pe-3">
                                    <h5 class="text-success">Total Amount Paid</h5>
                                    <h5 class="text-success"><?php echo htmlspecialchars($row['totalPaid']); ?></h5>
                                </div>
                                <div class="d-flex justify-content-between align-items-center mb-2 pe-3">
                                    <h5 class="text-danger">Remaining Amount</h5>
                                    <h5 class="text-danger"><?php echo htmlspecialchars($row['remainingAmount']); ?></h5>
                                </div>
                            <?php
                            } else {
                                echo '<div class="d-flex justify-content-center"><p>No records found for this job number.</p></div>';
                            }

                            // Close connections
                            $stmt->close();
                            $mysqli->close();
                            ?>
                        </div>
                    </div>

                </div>
                <script>
                    function generatePDF() {
                        const {
                            jsPDF
                        } = window.jspdf;

                        // A4 page dimensions (mm)
                        const pdfWidth = 210;
                        const pdfHeight = 297;

                        const doc = new jsPDF("p", "mm", "a4");

                        html2canvas(document.querySelector("#jobCardContent"), {
                            scale: 2 // Increase quality
                        }).then(canvas => {
                            const imgData = canvas.toDataURL("image/png");

                            // Calculate height proportionally to fit inside A4
                            let imgWidth = pdfWidth;
                            let imgHeight = (canvas.height * pdfWidth) / canvas.width;

                            // Ensure it doesn't exceed A4 height
                            if (imgHeight > pdfHeight) {
                                imgHeight = pdfHeight;
                            }

                            // Add image to PDF at position (0,0)
                            doc.addImage(imgData, "PNG", 0, 0, imgWidth, imgHeight);

                            // Save PDF
                            doc.save("job_card.pdf");
                        });
                    }
                </script>

                <div class="text-center">
                    <div class="mb-0">
                        <svg width="180" height="65" viewBox="0 0 300 80" xmlns="http://www.w3.org/2000/svg">
                            <g fill="none" fill-rule="evenodd">
                                <!-- Shopping Bag Icon -->
                                <rect x="10" y="10" width="50" height="50" rx="10" fill="#FFA64D" />
                                <path d="M25 15a10 10 0 0 1 20 0" stroke="#0D1B40" stroke-width="3" fill="none" />
                                <circle cx="35" cy="35" r="14" fill="#0D1B40" />

                                <!-- Dollar Symbol Inside Circle -->
                                <text x="31" y="40" font-family="Arial, sans-serif" font-size="16" fill="white" font-weight="bold">$</text>

                                <!-- Text "ArthaVidhi" -->
                                <text x="70" y="50" font-family="Poppins, sans-serif" font-size="40" fill="#0D1B40" font-weight="bold">ArthaVidhi</text>

                                <!-- "SOFT" Text -->
                                <text x="230" y="10" font-family="Poppins, sans-serif" font-size="22" fill="#FFA64D" font-weight="900">SOFT</text>
                            </g>
                        </svg>
                    </div>
                </div>
            </div>
        </div>
        <!-- /Invoices -->
    </div>
    </div>
    </div>
    <!-- /Main Wrapper -->
    <div class="customizer-links" id="setdata">
        <ul class="sticky-sidebar">
            <li class="sidebar-icons">
                <a href="#" class="navigation-add" data-bs-toggle="tooltip" data-bs-placement="left" data-bs-original-title="Theme">
                    <i data-feather="settings" class="feather-five"></i>
                </a>
            </li>
        </ul>
    </div>


    <!-- jQuery -->
    <script src="js/jquery-3.7.1.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

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

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

    <!-- Datatable JS -->
    <script src="js/jquery.dataTables.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>
    <script src="js/dataTables.bootstrap5.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

    <!-- Datetimepicker JS -->
    <script src="js/moment.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>
    <script src="js/bootstrap-datetimepicker.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

    <!-- Daterangepikcer JS -->
    <script src="js/moment.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>
    <script src="js/daterangepicker.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

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

    <!-- Summernote JS -->
    <script src="js/summernote-bs4.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

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

    <!-- Sweetalert 2 -->
    <script src="js/sweetalert2.all.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>
    <script src="js/sweetalerts.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

    <!-- Custom JS -->
    <script src="js/script.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>


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

    <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
</body>

</html>