<!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">

        <?php include './includes/navbar.php'; ?>

        <?php $page = 'add-product';
        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>Edit Job Card</h4>
                            <h6>Update the JobCard</h6>
                        </div>
                    </div>
                    <ul class="table-top-head">
                        <li>
                            <div class="page-btn">
                                <a href="product-list.php" class="btn btn-secondary"><i data-feather="arrow-left" class="me-2"></i>Back to JobCards</a>
                            </div>
                        </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>
                <?php
                require_once 'php/databaseConnection.php';

                // Check if job_number is provided via URL
                if (isset($_GET['jobNumber'])) {
                    $jobNumber = $_GET['jobNumber'];

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

                    // Fetch jobCard data
                    $stmt = $mysqli->prepare("SELECT 
    jobNumber, jobDate, registerNumber, year, millage, vehicleBrand, 
    dateTimeIn, dateTimeOut, vehicleModel, vehicleColour, engineNumber, 
    vinNumber, purpose, fuelBatteryType, customerName, customerAddress, 
    contactNumber, reportedDefects, completedAction, contactPerson, 
    personContactNumber, technicianName, supervisor, TotalBillAmount, 
    vatAmount, discountAmount, AmountOfDiscount, netTotal, billNumber, 
    billRemarks, totalPaid, totalPaidInWords, remainingAmount, 
    paymentStatus, billAmount, lubricantBillAmount, labourBillAmount, 
    dentBillAmount, customerVAT
FROM jobCard
WHERE jobNumber = ?;
");
                    $stmt->bind_param("s", $jobNumber);
                    $stmt->execute();
                    $result = $stmt->get_result();

                    if ($result->num_rows > 0) {
                        $jobCard = $result->fetch_assoc();
                    } else {
                        die("No record found for job number: " . htmlspecialchars($jobNumber));
                    }
                    $stmt->close();

                    // Fetch Main Lubricants
                    $mainLubricants = [];
                    $mainStmt = $mysqli->prepare("SELECT type_code, price, quantity, discount, net_amount FROM mainParts WHERE jobNumber = ? AND category = 'Lubricant'");
                    $mainStmt->bind_param("s", $jobNumber);
                    $mainStmt->execute();
                    $mainResult = $mainStmt->get_result();
                    while ($row = $mainResult->fetch_assoc()) {
                        $mainLubricants[] = $row;
                    }
                    $mainStmt->close();

                    // Fetch Custom Lubricants
                    $customLubricants = [];
                    $customStmt = $mysqli->prepare("SELECT type_code, price, quantity, discount, net_amount FROM customs WHERE jobNumber = ? AND category = 'Lubricant'");
                    $customStmt->bind_param("s", $jobNumber);
                    $customStmt->execute();
                    $customResult = $customStmt->get_result();
                    while ($row = $customResult->fetch_assoc()) {
                        $customLubricants[] = $row;
                    }
                    $customStmt->close();

                    // Fetch Main Spares
                    $mainSpares = [];
                    $mainSpareStmt = $mysqli->prepare("SELECT type_code, price, quantity, discount, net_amount FROM mainParts WHERE jobNumber = ? AND category = 'Spare Parts'");
                    $mainSpareStmt->bind_param("s", $jobNumber);
                    $mainSpareStmt->execute();
                    $mainSpareResult = $mainSpareStmt->get_result();
                    while ($row = $mainSpareResult->fetch_assoc()) {
                        $mainSpares[] = $row;
                    }
                    $mainSpareStmt->close();

                    // Fetch Custom Spares
                    $customSpares = [];
                    $customSpareStmt = $mysqli->prepare("SELECT type_code, price, quantity, discount, net_amount FROM customs WHERE jobNumber = ? AND category = 'Spare Parts'");
                    $customSpareStmt->bind_param("s", $jobNumber);
                    $customSpareStmt->execute();
                    $customSpareResult = $customSpareStmt->get_result();
                    while ($row = $customSpareResult->fetch_assoc()) {
                        $customSpares[] = $row;
                    }
                    $customSpareStmt->close();

                    // Fetch Main Labours
                    $mainLabours = [];
                    $mainLabourStmt = $mysqli->prepare("SELECT type_code, price, quantity, discount, net_amount FROM mainParts WHERE jobNumber = ? AND category = 'Labour'");
                    $mainLabourStmt->bind_param("s", $jobNumber);
                    $mainLabourStmt->execute();
                    $mainLabourResult = $mainLabourStmt->get_result();
                    while ($row = $mainLabourResult->fetch_assoc()) {
                        $mainLabours[] = $row;
                    }
                    $mainLabourStmt->close();

                    // Fetch Custom Labours
                    $customLabours = [];
                    $customLabourStmt = $mysqli->prepare("SELECT type_code, price, quantity, discount, net_amount FROM customs WHERE jobNumber = ? AND category = 'Labour'");
                    $customLabourStmt->bind_param("s", $jobNumber);
                    $customLabourStmt->execute();
                    $customLabourResult = $customLabourStmt->get_result();
                    while ($row = $customLabourResult->fetch_assoc()) {
                        $customLabours[] = $row;
                    }
                    $customLabourStmt->close();

                    // === Fetch Main Dent/Paint ===
                    $mainDentPaints = [];
                    $dentMainStmt = $mysqli->prepare("SELECT type_code, price, quantity, discount, net_amount FROM mainParts WHERE jobNumber = ? AND category = 'Dent/Paint'");
                    $dentMainStmt->bind_param("s", $jobNumber);
                    $dentMainStmt->execute();
                    $dentMainResult = $dentMainStmt->get_result();
                    while ($row = $dentMainResult->fetch_assoc()) {
                        $mainDentPaints[] = $row;
                    }
                    $dentMainStmt->close();

                    // === Fetch Custom Dent/Paint ===
                    $customDentPaints = [];
                    $dentCustomStmt = $mysqli->prepare("SELECT type_code, price, quantity, discount, net_amount FROM customs WHERE jobNumber = ? AND category = 'Dent/Paint'");
                    $dentCustomStmt->bind_param("s", $jobNumber);
                    $dentCustomStmt->execute();
                    $dentCustomResult = $dentCustomStmt->get_result();
                    while ($row = $dentCustomResult->fetch_assoc()) {
                        $customDentPaints[] = $row;
                    }
                    $dentCustomStmt->close();
                    // Close connection
                    $mysqli->close();
                } else {
                    die("Job number is required.");
                }
                ?>
                <form action="php/updateProduct.php?jobNumber=<?php echo $jobCard['jobNumber']; ?>" method="post" enctype="multipart/form-data">
                    <div class="card">
                        <div class="card-body add-product pb-0">
                            <div class="accordion-card-one accordion" id="accordionExample">
                                <!-- here -->
                                <div class="accordion-item">
                                    <div class="accordion-header" id="headingOne">
                                        <div class="accordion-button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-controls="collapseOne">
                                            <div class="addproduct-icon">
                                                <h5><i data-feather="info" class="add-info"></i><span>Vehicle Information</span></h5>
                                                <a href="javascript:void(0);"><i data-feather="chevron-down" class="chevron-down-add"></i></a>
                                            </div>
                                        </div>
                                    </div>
                                    <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
                                        <div class="accordion-body">
                                            <div class="row">
                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="input-blocks add-product list">
                                                        <label class="form-label">Job Number</label>
                                                        <input type="text" class="form-control" value="<?= htmlspecialchars($jobCard['jobNumber']) ?>" readonly>
                                                        <button type="button" class="btn btn-primaryadd">
                                                            Generated
                                                        </button>
                                                    </div>
                                                </div>
                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Job Date</label>
                                                        <input type="date" name="jobDate" class="form-control" value="<?= htmlspecialchars($jobCard['jobDate']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-2 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Registration Number</label>
                                                        <input type="text" name="registerNumber" class="form-control" value="<?= htmlspecialchars($jobCard['registerNumber']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-2 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Vehicle Year</label>
                                                        <input type="text" name="year" class="form-control" value="<?= htmlspecialchars($jobCard['year']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-2 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Date/ Time In:</label>
                                                        <input type="text" name="dateTimeIn" class="form-control" value="<?= htmlspecialchars($jobCard['dateTimeIn']) ?>">
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Millage</label>
                                                        <input type="text" name="millage" class="form-control" value="<?= htmlspecialchars($jobCard['millage']) ?>">
                                                    </div>
                                                </div>
                                                <div class=" col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Vehicle Brand</label>
                                                        <input type="text" name="vehicleBrand" class="form-control" value="<?= htmlspecialchars($jobCard['vehicleBrand']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-2 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Vehicle Model</label>
                                                        <input type="text" name="vehicleModel" class="form-control" value="<?= htmlspecialchars($jobCard['vehicleModel']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-2 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Vehicle Colour</label>
                                                        <input type="text" name="vehicleColour" class="form-control" value="<?= htmlspecialchars($jobCard['vehicleColour']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-2 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Date/ Time Out:</label>
                                                        <input type="text" name="dateTimeOut" class="form-control" value="<?= htmlspecialchars($jobCard['dateTimeOut']) ?>">
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Engine/Motor No.</label>
                                                        <input type="text" name="engineNumber" class="form-control" value="<?= htmlspecialchars($jobCard['engineNumber']) ?>">
                                                    </div>
                                                </div>
                                                <div class=" col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">VIN No.</label>
                                                        <input type="text" name="vinNumber" class="form-control" value="<?= htmlspecialchars($jobCard['vinNumber']) ?>">
                                                    </div>
                                                </div>
                                                <div class=" col-lg-6 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Purpose Visit</label>
                                                        <textarea name="purpose" class="form-control"><?= htmlspecialchars($jobCard['purpose']) ?></textarea>
                                                    </div>
                                                </div>
                                                <div class=" col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Customer Name</label>
                                                        <input type="text" name="customerName" class="form-control" value="<?= htmlspecialchars($jobCard['customerName']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Address</label>
                                                        <input type="text" name="customerAddress" class="form-control" value="<?= htmlspecialchars($jobCard['customerAddress']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Customer VAT No.:</label>
                                                        <input type="text" name="customerVAT" class="form-control" value="<?= htmlspecialchars($jobCard['customerVAT']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Contact Number</label>
                                                        <input type="text" name="contactNumber" class="form-control" value="<?= htmlspecialchars($jobCard['contactNumber']) ?>">
                                                    </div>
                                                </div>
                                            </div>
                                            <!-- Editor -->
                                            <div class="row">
                                                <div class="col-lg-6">
                                                    <div class="input-blocks summer-description-box transfer mb-3">
                                                        <label>Reported Defects/Demanded Repairs</label>
                                                        <textarea class="form-control h-100" name="reportedDefects" rows="5"><?= htmlspecialchars($jobCard['reportedDefects']) ?></textarea>
                                                        <p class="mt-1">Maximum 60 Characters</p>
                                                    </div>
                                                </div>
                                                <div class="col-lg-6">
                                                    <div class="input-blocks summer-description-box transfer mb-3">
                                                        <label>Completed Actions</label>
                                                        <textarea class="form-control h-100" name="completedAction" rows="5"><?= htmlspecialchars($jobCard['completedAction']) ?></textarea>
                                                        <p class="mt-1">Maximum 60 Characters</p>
                                                    </div>
                                                </div>
                                            </div>
                                            <!-- /Editor -->
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="accordion-card-one accordion" id="accordionExample">
                                <div class="accordion-item">
                                    <div class="accordion-header" id="headingOne">
                                        <div class="accordion-button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-controls="collapseOne">
                                            <div class="addproduct-icon">
                                                <h5><i data-feather="info" class="add-info"></i><span>Privacy Information</span></h5>
                                                <a href="javascript:void(0);"><i data-feather="chevron-down" class="chevron-down-add"></i></a>
                                            </div>
                                        </div>
                                    </div>
                                    <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
                                        <div class="accordion-body">
                                            <div class="row">
                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Contact Person</label>
                                                        <input type="text" name="contactPerson" class="form-control" value="<?= htmlspecialchars($jobCard['contactPerson']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Contact Number</label>
                                                        <input type="text" name="personContactNumber" class="form-control" value="<?= htmlspecialchars($jobCard['personContactNumber']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Technician Name</label>
                                                        <input type="text" name="technicianName" class="form-control" value="<?= htmlspecialchars($jobCard['technicianName']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Supervisor</label>
                                                        <input type="text" name="supervisor" class="form-control" value="<?= htmlspecialchars($jobCard['supervisor']) ?>">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!--/here -->
                            <div class="accordion-card-one accordion" id="accordionExample2">
                                <div class="accordion-item">
                                    <div class="accordion-header" id="headingTwo">
                                        <div class="accordion-button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-controls="collapseTwo">
                                            <div class="text-editor add-list">
                                                <div class="addproduct-icon list icon">
                                                    <h5><i data-feather="life-buoy" class="add-info"></i><span>Lubricant Details</span></h5>
                                                    <a href="javascript:void(0);"><i data-feather="chevron-down" class="chevron-down-add"></i></a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div id="collapseTwo" class="accordion-collapse collapse show" aria-labelledby="headingTwo" data-bs-parent="#accordionExample2">
                                        <div class="accordion-body">
                                            <div class="input-blocks add-products">

                                                <!-- === MAIN LUBRICANTS === -->
                                                <div id="mainLubricantContainer">
                                                    <?php if (!empty($mainLubricants)) : ?>
                                                        <?php foreach ($mainLubricants as $row) : ?>
                                                            <div class="row mainlubricantRow">
                                                                <div class="col-lg-3 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Lubricant Type</label>
                                                                        <input type="text" name="lubType[]" class="lubricantSearch form-control" value="<?= htmlspecialchars($row['type_code']) ?>" placeholder="Search Lubricant">
                                                                        <div class="lubricantSearchResults search-results"></div>
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Rate</label>
                                                                        <input type="number" name="lubPrice[]" class="lubricantTotal form-control" value="<?= $row['price'] ?>" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Quantity</label>
                                                                        <input type="number" name="lubQuan[]" class="lubricantQuantity form-control" value="<?= $row['quantity'] ?>" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Discount (in %)</label>
                                                                        <input type="number" name="lubDis[]" class="lubricantDiscount form-control" value="<?= $row['discount'] ?>">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Net Amount</label>
                                                                        <input type="number" name="lubNet[]" class="lubricantNetAmount form-control" value="<?= $row['net_amount'] ?>" step="any" readonly>
                                                                    </div>
                                                                </div>
                                                                <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                    <button type="button" class="btn btn-danger btn-sm deleteMainLubricantRow">
                                                                        <i class="fas fa-trash"></i>
                                                                    </button>
                                                                </div>
                                                            </div>
                                                        <?php endforeach; ?>
                                                    <?php else : ?>
                                                        <!-- Blank row if no data -->
                                                        <div class="row mainlubricantRow">
                                                            <div class="col-lg-3 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Lubricant Type</label>
                                                                    <input type="text" name="lubType[]" class="lubricantSearch form-control" placeholder="Search Lubricant">
                                                                    <div class="lubricantSearchResults search-results"></div>
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Rate</label>
                                                                    <input type="number" name="lubPrice[]" class="lubricantTotal form-control" value="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Quantity</label>
                                                                    <input type="number" name="lubQuan[]" class="lubricantQuantity form-control" value="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Discount (in %)</label>
                                                                    <input type="number" name="lubDis[]" class="lubricantDiscount form-control" value="0">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Net Amount</label>
                                                                    <input type="number" name="lubNet[]" class="lubricantNetAmount form-control" value="0" step="any" readonly>
                                                                </div>
                                                            </div>
                                                            <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                <button type="button" class="btn btn-danger btn-sm deleteMainLubricantRow">
                                                                    <i class="fas fa-trash"></i>
                                                                </button>
                                                            </div>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>

                                                <!-- Add Button -->
                                                <div class="mb-3 col-lg-1 col-sm-6 col-12 d-flex align-items-end">
                                                    <button id="addMainLubricantRow" type="button" class="btn btn-primary btn-sm">
                                                        <i class="fas fa-plus"></i>
                                                    </button>
                                                </div>

                                                <!-- === CUSTOM LUBRICANTS === -->
                                                <div id="lubricantContainer">
                                                    <?php if (!empty($customLubricants)) : ?>
                                                        <?php foreach ($customLubricants as $row) : ?>
                                                            <div class="row lubricantRow">
                                                                <div class="col-lg-3 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Custom Lubricant Type</label>
                                                                        <input type="text" name="customLubType[]" class="form-control" value="<?= htmlspecialchars($row['type_code']) ?>" placeholder="Enter Custom Lubricant Type">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Rate</label>
                                                                        <input type="number" name="customLubPrice[]" class="form-control customLubricantAmount" value="<?= $row['price'] ?>" min="0" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Quantity</label>
                                                                        <input type="number" name="customLubQuantity[]" class="form-control customLubricantQuantity" value="<?= $row['quantity'] ?>" min="0" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Discount (in %)</label>
                                                                        <input type="number" name="customLubDis[]" class="form-control customLubricantDiscount" value="<?= $row['discount'] ?>" min="0" max="100">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Net Amount</label>
                                                                        <input type="text" class="form-control customLubricantNetAmount" name="customLubNet[]" value="<?= $row['net_amount'] ?>" step="any" readonly>
                                                                    </div>
                                                                </div>
                                                                <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                    <button type="button" class="btn btn-danger btn-sm deleteRow">
                                                                        <i class="fas fa-trash"></i>
                                                                    </button>
                                                                </div>
                                                            </div>
                                                        <?php endforeach; ?>
                                                    <?php else : ?>
                                                        <!-- Default Empty Custom Row -->
                                                        <div class="row lubricantRow">
                                                            <div class="col-lg-3 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Custom Lubricant Type</label>
                                                                    <input type="text" name="customLubType[]" class="form-control" placeholder="Enter Custom Lubricant Type">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Rate</label>
                                                                    <input type="number" name="customLubPrice[]" class="form-control customLubricantAmount" value="0" min="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Quantity</label>
                                                                    <input type="number" name="customLubQuantity[]" class="form-control customLubricantQuantity" value="0" min="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Discount (in %)</label>
                                                                    <input type="number" name="customLubDis[]" class="form-control customLubricantDiscount" value="0" min="0" max="100">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Net Amount</label>
                                                                    <input type="text" class="form-control customLubricantNetAmount" name="customLubNet[]" value="0" step="any" readonly>
                                                                </div>
                                                            </div>
                                                            <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                <button type="button" class="btn btn-danger btn-sm deleteRow">
                                                                    <i class="fas fa-trash"></i>
                                                                </button>
                                                            </div>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>

                                                <!-- Add Button -->
                                                <div class="mb-3 col-lg-1 col-sm-6 col-12 d-flex align-items-end">
                                                    <button id="toggleCustomButton" type="button" class="btn btn-primary btn-sm">
                                                        <i class="fas fa-plus"></i>
                                                    </button>
                                                </div>

                                                <?php include './includes/lubricantScript.php' ?>
                                                <?php include './includes/customLubricantDetailsScript.php'; ?>

                                                <!-- Total Net Input -->
                                                <div class="col-lg-5 col-sm-6 col-12">
                                                    <div class="input-blocks add-product">
                                                        <label>Total Net</label>
                                                        <input type="number" class="form-control" name="lubricantBillAmount" value="0" step="any" readonly>
                                                        <input type="hidden" id="totalCustomLubricant" value="0">
                                                        <input type="hidden" id="totalLubricantAjax" value="0">
                                                    </div>
                                                </div>
                                                <script>
                                                    document.addEventListener("DOMContentLoaded", function() {
                                                        // DELETE Main Lubricant Row
                                                        document.getElementById("mainLubricantContainer").addEventListener("click", function(e) {
                                                            if (e.target.closest(".deleteMainLubricantRow")) {
                                                                const row = e.target.closest(".mainlubricantRow");
                                                                if (row) {
                                                                    row.remove();
                                                                    calculateInitialLubricantTotals();
                                                                }
                                                            }
                                                        });

                                                        // DELETE Custom Lubricant Row
                                                        document.getElementById("lubricantContainer").addEventListener("click", function(e) {
                                                            if (e.target.closest(".deleteLubricantRow")) {
                                                                const row = e.target.closest(".lubricantRow");
                                                                if (row) {
                                                                    row.remove();
                                                                    calculateInitialLubricantTotals();
                                                                }
                                                            }
                                                        });
                                                    });
                                                </script>

                                                <script>
                                                    // Your existing functions
                                                    function updateFinalLubricantBillTotal() {
                                                        let totalCustomLubricant = parseFloat(document.getElementById("totalCustomLubricant").value) || 0;
                                                        let totalLubricantAjax = parseFloat(document.getElementById("totalLubricantAjax").value) || 0;
                                                        let finalLubricantBillAmount = totalCustomLubricant + totalLubricantAjax;
                                                        document.querySelector("[name='lubricantBillAmount']").value = finalLubricantBillAmount.toFixed(2);
                                                    }

                                                    function calculateInitialLubricantTotals() {
                                                        let mainTotal = 0;
                                                        document.querySelectorAll(".lubricantNetAmount").forEach(input => {
                                                            mainTotal += parseFloat(input.value) || 0;
                                                        });
                                                        document.getElementById("totalLubricantAjax").value = mainTotal.toFixed(2);

                                                        let customTotal = 0;
                                                        document.querySelectorAll(".customLubricantNetAmount").forEach(input => {
                                                            customTotal += parseFloat(input.value) || 0;
                                                        });
                                                        document.getElementById("totalCustomLubricant").value = customTotal.toFixed(2);

                                                        updateFinalLubricantBillTotal();
                                                    }

                                                    // New functions for per-row net calculation
                                                    function calculateLubricantRowNetAmount(row) {
                                                        if (!row) return;
                                                        const price = parseFloat(row.querySelector('.lubricantTotal').value) || 0;
                                                        const quantity = parseFloat(row.querySelector('.lubricantQuantity').value) || 0;
                                                        const discount = parseFloat(row.querySelector('.lubricantDiscount').value) || 0;

                                                        let net = price * quantity;
                                                        if (discount > 0) {
                                                            net = net - (net * discount / 100);
                                                        }

                                                        row.querySelector('.lubricantNetAmount').value = net.toFixed(2);
                                                    }

                                                    function calculateCustomLubricantRowNetAmount(row) {
                                                        if (!row) return;
                                                        const price = parseFloat(row.querySelector('.customLubricantAmount').value) || 0;
                                                        const quantity = parseFloat(row.querySelector('.customLubricantQuantity').value) || 0;
                                                        const discount = parseFloat(row.querySelector('.customLubricantDiscount').value) || 0;

                                                        let net = price * quantity;
                                                        if (discount > 0) {
                                                            net = net - (net * discount / 100);
                                                        }

                                                        row.querySelector('.customLubricantNetAmount').value = net.toFixed(2);
                                                    }

                                                    // Event delegation for main lubricants inputs
                                                    document.getElementById('mainLubricantContainer').addEventListener('input', function(e) {
                                                        if (e.target.classList.contains('lubricantQuantity') ||
                                                            e.target.classList.contains('lubricantTotal') ||
                                                            e.target.classList.contains('lubricantDiscount')) {

                                                            const row = e.target.closest('.mainlubricantRow');
                                                            calculateLubricantRowNetAmount(row);
                                                            calculateInitialLubricantTotals();
                                                        }
                                                    });

                                                    // Event delegation for custom lubricants inputs
                                                    document.getElementById('lubricantContainer').addEventListener('input', function(e) {
                                                        if (e.target.classList.contains('customLubricantQuantity') ||
                                                            e.target.classList.contains('customLubricantAmount') ||
                                                            e.target.classList.contains('customLubricantDiscount')) {

                                                            const row = e.target.closest('.lubricantRow');
                                                            calculateCustomLubricantRowNetAmount(row);
                                                            calculateInitialLubricantTotals();
                                                        }
                                                    });

                                                    // Initialize totals on DOM ready
                                                    document.addEventListener("DOMContentLoaded", function() {
                                                        calculateInitialLubricantTotals();
                                                    });
                                                </script>

                                            </div>

                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="accordion-card-one accordion" id="accordionExample2">
                                <div class="accordion-item">
                                    <div class="accordion-header" id="headingTwo">
                                        <div class="accordion-button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-controls="collapseTwo">
                                            <div class="text-editor add-list">
                                                <div class="addproduct-icon list icon">
                                                    <h5><i data-feather="life-buoy" class="add-info"></i><span>Spare Parts Details</span></h5>
                                                    <a href="javascript:void(0);"><i data-feather="chevron-down" class="chevron-down-add"></i></a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div id="collapseTwo" class="accordion-collapse collapse show" aria-labelledby="headingTwo" data-bs-parent="#accordionExample2">
                                        <div class="accordion-body">
                                            <style>
                                                .search-results {
                                                    position: absolute;
                                                    background: white;
                                                    border: 1px solid #ccc;
                                                    width: 100%;
                                                    max-height: 200px;
                                                    overflow-y: auto;
                                                    display: none;
                                                    z-index: 1000;
                                                }

                                                .search-results div {
                                                    padding: 10px;
                                                    cursor: pointer;
                                                }

                                                .search-results div:hover {
                                                    background: #f0f0f0;
                                                }
                                            </style>
                                            <div class="input-blocks add-products">

                                                <!-- === MAIN SPARE PARTS === -->
                                                <div id="mainSpareContainer">
                                                    <?php if (!empty($mainSpares)) : ?>
                                                        <?php foreach ($mainSpares as $row) : ?>
                                                            <div class="row mainSparePartsRow">
                                                                <div class="col-lg-3 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Spare Parts Type</label>
                                                                        <input type="text" name="spareType[]" class="form-control sparePartsSearch" value="<?= htmlspecialchars($row['type_code']) ?>" placeholder="Search Parts">
                                                                        <div class="search-results spareSearchResults"></div>
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Rate</label>
                                                                        <input type="number" name="sparePrice[]" class="form-control spareTotal" value="<?= $row['price'] ?>" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Quantity</label>
                                                                        <input type="number" name="spareQuan[]" class="form-control spareQuantity" value="<?= $row['quantity'] ?>" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Discount (in %)</label>
                                                                        <input type="number" name="spareDis[]" class="form-control spareDiscount" value="<?= $row['discount'] ?>">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Net Amount</label>
                                                                        <input type="number" name="spareNet[]" class="form-control spareNetAmount" value="<?= $row['net_amount'] ?>" step="any" readonly>
                                                                    </div>
                                                                </div>
                                                                <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                    <button type="button" class="btn btn-danger btn-sm deleteMainSparePartsRow">
                                                                        <i class="fas fa-trash"></i>
                                                                    </button>
                                                                </div>
                                                            </div>
                                                        <?php endforeach; ?>
                                                    <?php else : ?>
                                                        <!-- Blank row if no data -->
                                                        <div class="row mainSparePartsRow">
                                                            <div class="col-lg-3 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Spare Parts Type</label>
                                                                    <input type="text" name="spareType[]" class="form-control sparePartsSearch" placeholder="Search Parts">
                                                                    <div class="search-results spareSearchResults"></div>
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Rate</label>
                                                                    <input type="number" name="sparePrice[]" class="form-control spareTotal" value="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Quantity</label>
                                                                    <input type="number" name="spareQuan[]" class="form-control spareQuantity" value="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Discount (in %)</label>
                                                                    <input type="number" name="spareDis[]" class="form-control spareDiscount" value="0">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Net Amount</label>
                                                                    <input type="number" name="spareNet[]" class="form-control spareNetAmount" value="0" step="any" readonly>
                                                                </div>
                                                            </div>
                                                            <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                <button type="button" class="btn btn-danger btn-sm deleteMainSparePartsRow">
                                                                    <i class="fas fa-trash"></i>
                                                                </button>
                                                            </div>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>

                                                <!-- Add Row Button for Main -->
                                                <div class="mb-3 col-lg-1 col-sm-6 col-12 d-flex align-items-end">
                                                    <button id="addMainSparePartsRow" type="button" class="btn btn-primary btn-sm">
                                                        <i class="fas fa-plus"></i>
                                                    </button>
                                                </div>

                                                <!-- === CUSTOM SPARE PARTS === -->
                                                <div id="sparePartsContainer">
                                                    <?php if (!empty($customSpares)) : ?>
                                                        <?php foreach ($customSpares as $row) : ?>
                                                            <div class="row sparePartsRow">
                                                                <div class="col-lg-3 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Custom Spare Parts</label>
                                                                        <input type="text" class="form-control" name="customSpareType[]" value="<?= htmlspecialchars($row['type_code']) ?>" placeholder="Custom Spare Parts">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Rate</label>
                                                                        <input type="number" class="form-control customSparePartsAmount" name="customSparePrice[]" value="<?= $row['price'] ?>" min="0" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Quantity</label>
                                                                        <input type="number" class="form-control customSparePartsQuantity" name="customSpareQuantity[]" value="<?= $row['quantity'] ?>" min="0" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Discount (in %)</label>
                                                                        <input type="number" class="form-control customSparePartsDiscount" name="customSpareDis[]" value="<?= $row['discount'] ?>" min="0" max="100">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Net Amount</label>
                                                                        <input type="text" class="form-control customSparePartsNetAmount" name="customSpareNet[]" value="<?= $row['net_amount'] ?>" step="any" readonly>
                                                                    </div>
                                                                </div>
                                                                <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                    <button type="button" class="btn btn-danger btn-sm deleteSparePartsRow">
                                                                        <i class="fas fa-trash"></i>
                                                                    </button>
                                                                </div>
                                                            </div>
                                                        <?php endforeach; ?>
                                                    <?php else : ?>
                                                        <!-- Default Empty Custom Row -->
                                                        <div class="row sparePartsRow">
                                                            <div class="col-lg-3 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Custom Spare Parts</label>
                                                                    <input type="text" class="form-control" name="customSpareType[]" placeholder="Custom Spare Parts">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Rate</label>
                                                                    <input type="number" class="form-control customSparePartsAmount" name="customSparePrice[]" value="0" min="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Quantity</label>
                                                                    <input type="number" class="form-control customSparePartsQuantity" name="customSpareQuantity[]" value="0" min="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Discount (in %)</label>
                                                                    <input type="number" class="form-control customSparePartsDiscount" name="customSpareDis[]" value="0" min="0" max="100">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Net Amount</label>
                                                                    <input type="text" class="form-control customSparePartsNetAmount" name="customSpareNet[]" value="0" step="any" readonly>
                                                                </div>
                                                            </div>
                                                            <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                <button type="button" class="btn btn-danger btn-sm deleteSparePartsRow">
                                                                    <i class="fas fa-trash"></i>
                                                                </button>
                                                            </div>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>

                                                <!-- Add Row Button for Custom -->
                                                <div class="mb-3 col-lg-1 col-sm-6 col-12 d-flex align-items-end">
                                                    <button id="addSparePartsRow" type="button" class="btn btn-primary btn-sm">
                                                        <i class="fas fa-plus"></i>
                                                    </button>
                                                </div>

                                                <?php include './includes/spareScript.php' ?>
                                                <?php include './includes/customSparePartsScript.php' ?>

                                                <!-- Total Net Input -->
                                                <div class="col-lg-5 col-sm-6 col-12">
                                                    <div class="input-blocks add-product">
                                                        <label>Total Net</label>
                                                        <input type="number" class="form-control" name="billAmount" value="0" step="any" readonly>
                                                        <input type="hidden" id="totalCustomSpare" value="0">
                                                        <input type="hidden" id="totalSpareAjax" value="0">
                                                    </div>
                                                </div>
                                            </div>
                                            <script>
                                                document.addEventListener("DOMContentLoaded", function() {
                                                    // DELETE Main Spare Row
                                                    document.getElementById("mainSpareContainer").addEventListener("click", function(e) {
                                                        if (e.target.closest(".deleteMainSparePartsRow")) {
                                                            const row = e.target.closest(".mainSparePartsRow");
                                                            if (row) {
                                                                row.remove();
                                                                calculateInitialSpareTotals();
                                                            }
                                                        }
                                                    });

                                                    // DELETE Custom Spare Row
                                                    document.getElementById("sparePartsContainer").addEventListener("click", function(e) {
                                                        if (e.target.closest(".deleteSparePartsRow")) {
                                                            const row = e.target.closest(".sparePartsRow");
                                                            if (row) {
                                                                row.remove();
                                                                calculateInitialSpareTotals();
                                                            }
                                                        }
                                                    });
                                                });
                                            </script>

                                            <script>
                                                function updateFinalBillTotal() {
                                                    let totalSpareAjax = parseFloat(document.getElementById("totalSpareAjax").value) || 0;
                                                    let totalCustomSpare = parseFloat(document.getElementById("totalCustomSpare").value) || 0;
                                                    let finalBillAmount = totalSpareAjax + totalCustomSpare;
                                                    document.querySelector("[name='billAmount']").value = finalBillAmount.toFixed(2);
                                                }

                                                function calculateInitialSpareTotals() {
                                                    let mainTotal = 0;
                                                    document.querySelectorAll(".spareNetAmount").forEach(input => {
                                                        mainTotal += parseFloat(input.value) || 0;
                                                    });
                                                    document.getElementById("totalSpareAjax").value = mainTotal.toFixed(2);

                                                    let customTotal = 0;
                                                    document.querySelectorAll(".customSparePartsNetAmount").forEach(input => {
                                                        customTotal += parseFloat(input.value) || 0;
                                                    });
                                                    document.getElementById("totalCustomSpare").value = customTotal.toFixed(2);

                                                    updateFinalBillTotal();
                                                }

                                                function calculateMainSpareRowNetAmount(row) {
                                                    if (!row) return;
                                                    const price = parseFloat(row.querySelector('.spareTotal').value) || 0;
                                                    const quantity = parseFloat(row.querySelector('.spareQuantity').value) || 0;
                                                    const discount = parseFloat(row.querySelector('.spareDiscount').value) || 0;

                                                    let net = price * quantity;
                                                    if (discount > 0) {
                                                        net = net - (net * discount / 100);
                                                    }

                                                    row.querySelector('.spareNetAmount').value = net.toFixed(2);
                                                }

                                                function calculateCustomSpareRowNetAmount(row) {
                                                    if (!row) return;
                                                    const price = parseFloat(row.querySelector('.customSparePartsAmount').value) || 0;
                                                    const quantity = parseFloat(row.querySelector('.customSparePartsQuantity').value) || 0;
                                                    const discount = parseFloat(row.querySelector('.customSparePartsDiscount').value) || 0;

                                                    let net = price * quantity;
                                                    if (discount > 0) {
                                                        net = net - (net * discount / 100);
                                                    }

                                                    row.querySelector('.customSparePartsNetAmount').value = net.toFixed(2);
                                                }

                                                // INPUT HANDLERS for main spare
                                                document.getElementById("mainSpareContainer").addEventListener("input", function(e) {
                                                    if (
                                                        e.target.classList.contains("spareTotal") ||
                                                        e.target.classList.contains("spareQuantity") ||
                                                        e.target.classList.contains("spareDiscount")
                                                    ) {
                                                        const row = e.target.closest(".mainSparePartsRow");
                                                        calculateMainSpareRowNetAmount(row);
                                                        calculateInitialSpareTotals();
                                                    }
                                                });

                                                // INPUT HANDLERS for custom spare
                                                document.getElementById("sparePartsContainer").addEventListener("input", function(e) {
                                                    if (
                                                        e.target.classList.contains("customSparePartsAmount") ||
                                                        e.target.classList.contains("customSparePartsQuantity") ||
                                                        e.target.classList.contains("customSparePartsDiscount")
                                                    ) {
                                                        const row = e.target.closest(".sparePartsRow");
                                                        calculateCustomSpareRowNetAmount(row);
                                                        calculateInitialSpareTotals();
                                                    }
                                                });

                                                // INIT
                                                document.addEventListener("DOMContentLoaded", function() {
                                                    calculateInitialSpareTotals();
                                                });
                                            </script>

                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="accordion-card-one accordion" id="accordionExample2">
                                <div class="accordion-item">
                                    <div class="accordion-header" id="headingTwo">
                                        <div class="accordion-button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-controls="collapseTwo">
                                            <div class="text-editor add-list">
                                                <div class="addproduct-icon list icon">
                                                    <h5><i data-feather="life-buoy" class="add-info"></i><span>Labour Code Details</span></h5>
                                                    <a href="javascript:void(0);"><i data-feather="chevron-down" class="chevron-down-add"></i></a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div id="collapseTwo" class="accordion-collapse collapse show" aria-labelledby="headingTwo" data-bs-parent="#accordionExample2">
                                        <div class="accordion-body">
                                            <div class="input-blocks add-products">
                                                <!-- === MAIN LABOUR SECTION === -->
                                                <div class="mainLabourContainer">
                                                    <?php if (!empty($mainLabours)) : ?>
                                                        <?php foreach ($mainLabours as $row) : ?>
                                                            <div class="row mainLabourRow">
                                                                <div class="col-lg-3 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Labour Type</label>
                                                                        <input type="text" name="labourCode[]" class="labourCodeSearch form-control" value="<?= htmlspecialchars($row['type_code']) ?>" placeholder="Search Labour Type">
                                                                        <div class="labourSearchResults search-results"></div>
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Rate</label>
                                                                        <input type="number" name="labourPrice[]" class="labourTotal form-control" value="<?= $row['price'] ?>" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Quantity</label>
                                                                        <input type="number" name="labourQuan[]" class="labourQuantity form-control" value="<?= $row['quantity'] ?>" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Discount (in %)</label>
                                                                        <input type="number" name="labourDis[]" class="labourDiscount form-control" value="<?= $row['discount'] ?>">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Net Amount</label>
                                                                        <input type="number" name="labourNet[]" class="labourNetAmount form-control" value="<?= $row['net_amount'] ?>" step="any" readonly>
                                                                    </div>
                                                                </div>
                                                                <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                    <button type="button" class="btn btn-danger btn-sm deleteMainLabourRow">
                                                                        <i class="fas fa-trash"></i>
                                                                    </button>
                                                                </div>
                                                            </div>
                                                        <?php endforeach; ?>
                                                    <?php else : ?>
                                                        <!-- Default empty row -->
                                                        <div class="row mainLabourRow">
                                                            <div class="col-lg-3 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Labour Type</label>
                                                                    <input type="text" name="labourCode[]" class="labourCodeSearch form-control" placeholder="Search Labour Type">
                                                                    <div class="labourSearchResults search-results"></div>
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Rate</label>
                                                                    <input type="number" name="labourPrice[]" class="labourTotal form-control" value="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Quantity</label>
                                                                    <input type="number" name="labourQuan[]" class="labourQuantity form-control" value="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Discount (in %)</label>
                                                                    <input type="number" name="labourDis[]" class="labourDiscount form-control" value="0">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Net Amount</label>
                                                                    <input type="number" name="labourNet[]" class="labourNetAmount form-control" value="0" step="any" readonly>
                                                                </div>
                                                            </div>
                                                            <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                <button type="button" class="btn btn-danger btn-sm deleteMainLabourRow">
                                                                    <i class="fas fa-trash"></i>
                                                                </button>
                                                            </div>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>

                                                <!-- Add Row Button -->
                                                <div class="mb-3 col-lg-1 col-sm-6 col-12 d-flex align-items-end">
                                                    <button id="addMainLabourRow" type="button" class="btn btn-primary btn-sm">
                                                        <i class="fas fa-plus"></i>
                                                    </button>
                                                </div>

                                                <?php include './includes/labourScript.php'; ?>
                                                <?php include './includes/customLabourScript.php'; ?>

                                                <!-- === CUSTOM LABOUR SECTION === -->
                                                <div id="labourContainer">
                                                    <?php if (!empty($customLabours)) : ?>
                                                        <?php foreach ($customLabours as $row) : ?>
                                                            <div class="row labourRow">
                                                                <div class="col-lg-3 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Custom Labour Code</label>
                                                                        <input type="text" name="customLabourCode[]" class="form-control" value="<?= htmlspecialchars($row['type_code']) ?>" placeholder="Custom Labour Code">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Rate</label>
                                                                        <input type="number" name="customLabourPrice[]" class="form-control customLabourAmount" value="<?= $row['price'] ?>" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Quantity</label>
                                                                        <input type="number" name="customLabourQuantity[]" class="form-control customLabourQuantity" value="<?= $row['quantity'] ?>" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Discount (in %)</label>
                                                                        <input type="number" name="customLabourDis[]" class="form-control customLabourDiscount" value="<?= $row['discount'] ?>">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Net Amount</label>
                                                                        <input type="number" name="customLabourNet[]" class="form-control customLabourNetAmount" value="<?= $row['net_amount'] ?>" step="any" readonly>
                                                                    </div>
                                                                </div>
                                                                <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                    <button type="button" class="btn btn-danger btn-sm deleteLabourRow">
                                                                        <i class="fas fa-trash"></i>
                                                                    </button>
                                                                </div>
                                                            </div>
                                                        <?php endforeach; ?>
                                                    <?php else : ?>
                                                        <!-- Default empty row -->
                                                        <div class="row labourRow">
                                                            <div class="col-lg-3 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Custom Labour Code</label>
                                                                    <input type="text" name="customLabourCode[]" class="form-control" placeholder="Custom Labour Code">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Rate</label>
                                                                    <input type="number" name="customLabourPrice[]" class="form-control customLabourAmount" value="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Quantity</label>
                                                                    <input type="number" name="customLabourQuantity[]" class="form-control customLabourQuantity" value="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Discount (in %)</label>
                                                                    <input type="number" name="customLabourDis[]" class="form-control customLabourDiscount" value="0">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Net Amount</label>
                                                                    <input type="number" name="customLabourNet[]" class="form-control customLabourNetAmount" value="0" step="any" readonly>
                                                                </div>
                                                            </div>
                                                            <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                <button type="button" class="btn btn-danger btn-sm deleteLabourRow">
                                                                    <i class="fas fa-trash"></i>
                                                                </button>
                                                            </div>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>

                                                <!-- Add Row Button -->
                                                <div class="mb-3 col-lg-1 col-sm-6 col-12 d-flex align-items-end">
                                                    <button id="addLabourRow" type="button" class="btn btn-primary btn-sm">
                                                        <i class="fas fa-plus"></i>
                                                    </button>
                                                </div>

                                                <!-- Total Labour Bill -->
                                                <div class="col-lg-5 col-sm-6 col-12">
                                                    <div class="input-blocks add-product">
                                                        <label>Total Net</label>
                                                        <input type="number" class="form-control" name="labourBillAmount" value="0" step="any" readonly>
                                                        <input type="hidden" id="totalCustomLabour" value="0">
                                                        <input type="hidden" id="totalLabourAjax" value="0">
                                                    </div>
                                                </div>

                                                <script>
                                                    document.addEventListener("DOMContentLoaded", function() {
                                                        // DELETE Main Labour Row
                                                        document.querySelector(".mainLabourContainer").addEventListener("click", function(e) {
                                                            if (e.target.closest(".deleteMainLabourRow")) {
                                                                const row = e.target.closest(".mainLabourRow");
                                                                if (row) {
                                                                    row.remove();
                                                                    calculateInitialLabourTotals();
                                                                }
                                                            }
                                                        });

                                                        // DELETE Custom Labour Row
                                                        document.getElementById("labourContainer").addEventListener("click", function(e) {
                                                            if (e.target.closest(".deleteLabourRow")) {
                                                                const row = e.target.closest(".labourRow");
                                                                if (row) {
                                                                    row.remove();
                                                                    calculateInitialLabourTotals();
                                                                }
                                                            }
                                                        });
                                                    });
                                                </script>

                                                <script>
                                                    function updateFinalLabourBillTotal() {
                                                        let totalCustomLabour = parseFloat(document.getElementById("totalCustomLabour").value) || 0;
                                                        let totalLabourAjax = parseFloat(document.getElementById("totalLabourAjax").value) || 0;
                                                        let finalLabourAmount = totalCustomLabour + totalLabourAjax;
                                                        document.querySelector("[name='labourBillAmount']").value = finalLabourAmount.toFixed(2);
                                                    }

                                                    function calculateInitialLabourTotals() {
                                                        let mainTotal = 0;
                                                        document.querySelectorAll(".labourNetAmount").forEach(input => {
                                                            mainTotal += parseFloat(input.value) || 0;
                                                        });
                                                        document.getElementById("totalLabourAjax").value = mainTotal.toFixed(2);

                                                        let customTotal = 0;
                                                        document.querySelectorAll(".customLabourNetAmount").forEach(input => {
                                                            customTotal += parseFloat(input.value) || 0;
                                                        });
                                                        document.getElementById("totalCustomLabour").value = customTotal.toFixed(2);

                                                        updateFinalLabourBillTotal();
                                                    }

                                                    function calculateMainLabourRowNetAmount(row) {
                                                        if (!row) return;
                                                        const price = parseFloat(row.querySelector('.labourTotal').value) || 0;
                                                        const quantity = parseFloat(row.querySelector('.labourQuantity').value) || 0;
                                                        const discount = parseFloat(row.querySelector('.labourDiscount').value) || 0;

                                                        let net = price * quantity;
                                                        if (discount > 0) {
                                                            net = net - (net * discount / 100);
                                                        }

                                                        row.querySelector('.labourNetAmount').value = net.toFixed(2);
                                                    }

                                                    function calculateCustomLabourRowNetAmount(row) {
                                                        if (!row) return;
                                                        const price = parseFloat(row.querySelector('.customLabourAmount').value) || 0;
                                                        const quantity = parseFloat(row.querySelector('.customLabourQuantity').value) || 0;
                                                        const discount = parseFloat(row.querySelector('.customLabourDiscount').value) || 0;

                                                        let net = price * quantity;
                                                        if (discount > 0) {
                                                            net = net - (net * discount / 100);
                                                        }

                                                        row.querySelector('.customLabourNetAmount').value = net.toFixed(2);
                                                    }

                                                    // INPUT HANDLER for main labour rows
                                                    document.querySelector(".mainLabourContainer").addEventListener("input", function(e) {
                                                        if (
                                                            e.target.classList.contains("labourTotal") ||
                                                            e.target.classList.contains("labourQuantity") ||
                                                            e.target.classList.contains("labourDiscount")
                                                        ) {
                                                            const row = e.target.closest(".mainLabourRow");
                                                            calculateMainLabourRowNetAmount(row);
                                                            calculateInitialLabourTotals();
                                                        }
                                                    });

                                                    // INPUT HANDLER for custom labour rows
                                                    document.getElementById("labourContainer").addEventListener("input", function(e) {
                                                        if (
                                                            e.target.classList.contains("customLabourAmount") ||
                                                            e.target.classList.contains("customLabourQuantity") ||
                                                            e.target.classList.contains("customLabourDiscount")
                                                        ) {
                                                            const row = e.target.closest(".labourRow");
                                                            calculateCustomLabourRowNetAmount(row);
                                                            calculateInitialLabourTotals();
                                                        }
                                                    });

                                                    // INIT total calculation on page load
                                                    document.addEventListener("DOMContentLoaded", function() {
                                                        calculateInitialLabourTotals();
                                                    });
                                                </script>

                                            </div>

                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="accordion-card-one accordion" id="accordionExample2">
                                <div class="accordion-item">
                                    <div class="accordion-header" id="headingTwo">
                                        <div class="accordion-button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-controls="collapseTwo">
                                            <div class="text-editor add-list">
                                                <div class="addproduct-icon list icon">
                                                    <h5><i data-feather="life-buoy" class="add-info"></i><span>Dent/ Paint Details</span></h5>
                                                    <a href="javascript:void(0);"><i data-feather="chevron-down" class="chevron-down-add"></i></a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div id="collapseTwo" class="accordion-collapse collapse show" aria-labelledby="headingTwo" data-bs-parent="#accordionExample2">
                                        <div class="accordion-body">
                                            <div class="input-blocks add-products">
                                                <!-- === MAIN DENT/PAINT === -->
                                                <div id="mainDentPaintContainer">
                                                    <?php if (!empty($mainDentPaints)) : ?>
                                                        <?php foreach ($mainDentPaints as $row) : ?>
                                                            <div class="row mainDentPaintRow">
                                                                <div class="col-lg-3 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Dent/Paint Type</label>
                                                                        <input type="text" name="dentType[]" class="form-control dentTypeSearch" value="<?= htmlspecialchars($row['type_code']) ?>" placeholder="Search Parts">
                                                                        <div class="search-results dentSearchResults"></div>
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Rate</label>
                                                                        <input type="number" name="dentPrice[]" class="form-control dentTotal" value="<?= $row['price'] ?>" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Quantity</label>
                                                                        <input type="number" name="dentQuan[]" class="form-control dentQuantity" value="<?= $row['quantity'] ?>" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Discount (in %)</label>
                                                                        <input type="number" name="dentDis[]" class="form-control dentDiscount" value="<?= $row['discount'] ?>">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Net Amount</label>
                                                                        <input type="number" name="dentNet[]" class="form-control dentNetAmount" value="<?= $row['net_amount'] ?>" step="any" readonly>
                                                                    </div>
                                                                </div>
                                                                <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                    <button type="button" class="btn btn-danger btn-sm deleteMainDentPaintRow">
                                                                        <i class="fas fa-trash"></i>
                                                                    </button>
                                                                </div>
                                                            </div>
                                                        <?php endforeach; ?>
                                                    <?php else : ?>
                                                        <div class="row mainDentPaintRow">
                                                            <div class="col-lg-3 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Dent/Paint Type</label>
                                                                    <input type="text" name="dentType[]" class="form-control dentTypeSearch" placeholder="Search Parts">
                                                                    <div class="search-results dentSearchResults"></div>
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Rate</label>
                                                                    <input type="number" name="dentPrice[]" class="form-control dentTotal" value="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Quantity</label>
                                                                    <input type="number" name="dentQuan[]" class="form-control dentQuantity" value="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Discount (in %)</label>
                                                                    <input type="number" name="dentDis[]" class="form-control dentDiscount" value="0">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Net Amount</label>
                                                                    <input type="number" name="dentNet[]" class="form-control dentNetAmount" value="0" step="any" readonly>
                                                                </div>
                                                            </div>
                                                            <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                <button type="button" class="btn btn-danger btn-sm deleteMainDentPaintRow">
                                                                    <i class="fas fa-trash"></i>
                                                                </button>
                                                            </div>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>

                                                <!-- Add Main Row Button -->
                                                <div class="mb-3 col-lg-1 col-sm-6 col-12 d-flex align-items-end">
                                                    <button id="addMainDentPaintRow" type="button" class="btn btn-primary btn-sm">
                                                        <i class="fas fa-plus"></i>
                                                    </button>
                                                </div>

                                                <!-- === CUSTOM DENT/PAINT === -->
                                                <div id="dentPaintContainer">
                                                    <?php if (!empty($customDentPaints)) : ?>
                                                        <?php foreach ($customDentPaints as $row) : ?>
                                                            <div class="row dentPaintRow">
                                                                <div class="col-lg-3 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Custom Dent/ Paint Name</label>
                                                                        <input type="text" class="form-control" name="customDentType[]" value="<?= htmlspecialchars($row['type_code']) ?>">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Rate</label>
                                                                        <input type="number" class="form-control customDentPaintAmount" name="customDentPrice[]" value="<?= $row['price'] ?>" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Quantity</label>
                                                                        <input type="number" class="form-control customDentPaintQuantity" name="customDentQuantity[]" value="<?= $row['quantity'] ?>" step="any">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Discount (in %)</label>
                                                                        <input type="number" class="form-control customDentPaintDiscount" name="customDentDis[]" value="<?= $row['discount'] ?>">
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-2 col-sm-6 col-12">
                                                                    <div class="input-blocks add-product">
                                                                        <label>Net Amount</label>
                                                                        <input type="text" class="form-control customDentPaintNetAmount" name="customDentNet[]" value="<?= $row['net_amount'] ?>" step="any" readonly>
                                                                    </div>
                                                                </div>
                                                                <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                    <button type="button" class="btn btn-danger btn-sm deleteDentPaintRow">
                                                                        <i class="fas fa-trash"></i>
                                                                    </button>
                                                                </div>
                                                            </div>
                                                        <?php endforeach; ?>
                                                    <?php else : ?>
                                                        <div class="row dentPaintRow">
                                                            <div class="col-lg-3 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Custom Dent/ Paint Name</label>
                                                                    <input type="text" class="form-control" name="customDentType[]" placeholder="Custom Dent/ Paint Name">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Rate</label>
                                                                    <input type="number" class="form-control customDentPaintAmount" name="customDentPrice[]" value="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Quantity</label>
                                                                    <input type="number" class="form-control customDentPaintQuantity" name="customDentQuantity[]" value="0" step="any">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Discount (in %)</label>
                                                                    <input type="number" class="form-control customDentPaintDiscount" name="customDentDis[]" value="0">
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-2 col-sm-6 col-12">
                                                                <div class="input-blocks add-product">
                                                                    <label>Net Amount</label>
                                                                    <input type="text" class="form-control customDentPaintNetAmount" name="customDentNet[]" value="0" step="any" readonly>
                                                                </div>
                                                            </div>
                                                            <div class="mt-4 col-lg-1 col-sm-6 col-12">
                                                                <button type="button" class="btn btn-danger btn-sm deleteDentPaintRow">
                                                                    <i class="fas fa-trash"></i>
                                                                </button>
                                                            </div>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>

                                                <!-- Add Custom Row Button -->
                                                <div class="mb-3 col-lg-1 col-sm-6 col-12 d-flex align-items-end">
                                                    <button id="addDentPaintRow" type="button" class="btn btn-primary btn-sm">
                                                        <i class="fas fa-plus"></i>
                                                    </button>
                                                </div>

                                                <?php include './includes/dentScript.php' ?>
                                                <?php include './includes/customDentPaintScript.php' ?>

                                                <!-- Total Net Input -->
                                                <div class="col-lg-5 col-sm-6 col-12">
                                                    <div class="input-blocks add-product">
                                                        <label>Total Net</label>
                                                        <input type="number" class="form-control" name="dentBillAmount" step="any" value="0" readonly>
                                                        <input type="hidden" id="totalCustomDent" value="0">
                                                        <input type="hidden" id="totalDentAjax" value="0">
                                                    </div>
                                                </div>
                                            </div>
                                            <script>
                                                document.addEventListener("DOMContentLoaded", function() {
                                                    // DELETE Main DentPaint Row
                                                    document.getElementById("mainDentPaintContainer").addEventListener("click", function(e) {
                                                        if (e.target.closest(".deleteMainDentPaintRow")) {
                                                            const row = e.target.closest(".mainDentPaintRow");
                                                            if (row) {
                                                                row.remove();
                                                                calculateInitialDentPaintTotals();
                                                            }
                                                        }
                                                    });

                                                    // DELETE Custom DentPaint Row
                                                    document.getElementById("dentPaintContainer").addEventListener("click", function(e) {
                                                        if (e.target.closest(".deleteDentPaintRow")) {
                                                            const row = e.target.closest(".dentPaintRow");
                                                            if (row) {
                                                                row.remove();
                                                                calculateInitialDentPaintTotals();
                                                            }
                                                        }
                                                    });
                                                });
                                            </script>

                                            <script>
                                                // Update Total Final
                                                function updateFinalDentBillTotal() {
                                                    let totalCustomDent = parseFloat(document.getElementById("totalCustomDent").value) || 0;
                                                    let totalDentAjax = parseFloat(document.getElementById("totalDentAjax").value) || 0;
                                                    let finalDentBillAmount = totalCustomDent + totalDentAjax;
                                                    document.querySelector("[name='dentBillAmount']").value = finalDentBillAmount.toFixed(2);
                                                }

                                                // Sum up values
                                                function calculateInitialDentPaintTotals() {
                                                    let mainTotal = 0;
                                                    document.querySelectorAll(".dentNetAmount").forEach(input => {
                                                        mainTotal += parseFloat(input.value) || 0;
                                                    });
                                                    document.getElementById("totalDentAjax").value = mainTotal.toFixed(2);

                                                    let customTotal = 0;
                                                    document.querySelectorAll(".customDentPaintNetAmount").forEach(input => {
                                                        customTotal += parseFloat(input.value) || 0;
                                                    });
                                                    document.getElementById("totalCustomDent").value = customTotal.toFixed(2);

                                                    updateFinalDentBillTotal();
                                                }

                                                // Calculate main row net
                                                function calculateMainDentPaintRowNetAmount(row) {
                                                    if (!row) return;
                                                    const price = parseFloat(row.querySelector('.dentTotal').value) || 0;
                                                    const quantity = parseFloat(row.querySelector('.dentQuantity').value) || 0;
                                                    const discount = parseFloat(row.querySelector('.dentDiscount').value) || 0;

                                                    let net = price * quantity;
                                                    if (discount > 0) {
                                                        net = net - (net * discount / 100);
                                                    }

                                                    row.querySelector('.dentNetAmount').value = net.toFixed(2);
                                                }

                                                // Calculate custom row net
                                                function calculateCustomDentPaintRowNetAmount(row) {
                                                    if (!row) return;
                                                    const price = parseFloat(row.querySelector('.customDentPaintAmount').value) || 0;
                                                    const quantity = parseFloat(row.querySelector('.customDentPaintQuantity').value) || 0;
                                                    const discount = parseFloat(row.querySelector('.customDentPaintDiscount').value) || 0;

                                                    let net = price * quantity;
                                                    if (discount > 0) {
                                                        net = net - (net * discount / 100);
                                                    }

                                                    row.querySelector('.customDentPaintNetAmount').value = net.toFixed(2);
                                                }

                                                // MAIN input event
                                                document.getElementById("mainDentPaintContainer").addEventListener("input", function(e) {
                                                    if (
                                                        e.target.classList.contains("dentTotal") ||
                                                        e.target.classList.contains("dentQuantity") ||
                                                        e.target.classList.contains("dentDiscount")
                                                    ) {
                                                        const row = e.target.closest(".mainDentPaintRow");
                                                        calculateMainDentPaintRowNetAmount(row);
                                                        calculateInitialDentPaintTotals();
                                                    }
                                                });

                                                // CUSTOM input event
                                                document.getElementById("dentPaintContainer").addEventListener("input", function(e) {
                                                    if (
                                                        e.target.classList.contains("customDentPaintAmount") ||
                                                        e.target.classList.contains("customDentPaintQuantity") ||
                                                        e.target.classList.contains("customDentPaintDiscount")
                                                    ) {
                                                        const row = e.target.closest(".dentPaintRow");
                                                        calculateCustomDentPaintRowNetAmount(row);
                                                        calculateInitialDentPaintTotals();
                                                    }
                                                });

                                                // INIT on load
                                                document.addEventListener("DOMContentLoaded", function() {
                                                    calculateInitialDentPaintTotals();
                                                });
                                            </script>

                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="accordion-card-one accordion" id="accordionExample">
                                <div class="accordion-item">
                                    <div class="accordion-header" id="headingOne">
                                        <div class="accordion-button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-controls="collapseOne">
                                            <div class="addproduct-icon">
                                                <h5><i data-feather="credit-card" class="add-info"></i>
                                                    <span>Billing Apartment</span>
                                                </h5>
                                                <a href="javascript:void(0);"><i data-feather="chevron-down" class="chevron-down-add"></i></a>
                                            </div>
                                        </div>
                                    </div>
                                    <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
                                        <div class="accordion-body">
                                            <div class="row">
                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Bill Amount</label>
                                                        <input type="number" name="TotalBillAmount" class="form-control" value="0" step="any" readonly>

                                                    </div>
                                                </div>
                                                <script>
                                                    document.addEventListener("DOMContentLoaded", function() {
                                                        function updateFinalBillTotal() {
                                                            let totalLubricantAjax = parseFloat(document.getElementById("totalLubricantAjax")?.value) || 0;
                                                            let totalCustomLubricant = parseFloat(document.getElementById("totalCustomLubricant")?.value) || 0;
                                                            let totalLabourAjax = parseFloat(document.getElementById("totalLabourAjax")?.value) || 0;
                                                            let totalCustomLabour = parseFloat(document.getElementById("totalCustomLabour")?.value) || 0;
                                                            let totalDentAjax = parseFloat(document.getElementById("totalDentAjax")?.value) || 0;
                                                            let totalCustomDent = parseFloat(document.getElementById("totalCustomDent")?.value) || 0;
                                                            let totalSpareAjax = parseFloat(document.getElementById("totalSpareAjax")?.value) || 0;
                                                            let totalCustomSpare = parseFloat(document.getElementById("totalCustomSpare")?.value) || 0;

                                                            // Calculate individual bill amounts
                                                            let finalLubricantBillAmount = totalLubricantAjax + totalCustomLubricant;
                                                            let finalLabourBillAmount = totalLabourAjax + totalCustomLabour;
                                                            let finalDentBillAmount = totalDentAjax + totalCustomDent;
                                                            let finalSpareBillAmount = totalSpareAjax + totalCustomSpare;

                                                            // Set calculated values in respective fields
                                                            document.querySelector("[name='lubricantBillAmount']").value = finalLubricantBillAmount.toFixed(2);
                                                            document.querySelector("[name='labourBillAmount']").value = finalLabourBillAmount.toFixed(2);
                                                            document.querySelector("[name='dentBillAmount']").value = finalDentBillAmount.toFixed(2);
                                                            document.querySelector("[name='billAmount']").value = finalSpareBillAmount.toFixed(2);

                                                            // Update Total Bill Amount
                                                            updateTotalBillAmount();
                                                        }

                                                        function updateTotalBillAmount() {
                                                            let finalLubricantBillAmount = parseFloat(document.querySelector("[name='lubricantBillAmount']").value) || 0;
                                                            let finalLabourBillAmount = parseFloat(document.querySelector("[name='labourBillAmount']").value) || 0;
                                                            let finalDentBillAmount = parseFloat(document.querySelector("[name='dentBillAmount']").value) || 0;
                                                            let finalSpareBillAmount = parseFloat(document.querySelector("[name='billAmount']").value) || 0;

                                                            let totalBillAmount = finalLubricantBillAmount + finalLabourBillAmount + finalDentBillAmount + finalSpareBillAmount;
                                                            document.querySelector("[name='TotalBillAmount']").value = totalBillAmount.toFixed(2);

                                                            updateNetTotal(); // Ensure VAT and Discount updates
                                                        }

                                                        function updateNetTotal() {
                                                            let totalBillAmount = parseFloat(document.querySelector("[name='TotalBillAmount']").value) || 0;
                                                            let discountPercent = parseFloat(document.querySelector("[name='discountAmount']").value) || 0;

                                                            // Calculate discount amount from percentage
                                                            let discountAmount = (discountPercent / 100) * totalBillAmount;

                                                            // Add this line to display calculated discount in the input field
                                                            document.querySelector("[name='AmountOfDiscount']").value = discountAmount.toFixed(2);

                                                            let discountedAmount = totalBillAmount - discountAmount;
                                                            let vatAmount = discountedAmount * 0.13;

                                                            document.querySelector("[name='vatAmount']").value = vatAmount.toFixed(2);

                                                            let netTotal = discountedAmount + vatAmount;
                                                            document.querySelector("[name='netTotal']").value = netTotal.toFixed(2);

                                                            updateRemainingAmount();
                                                        }


                                                        function updateRemainingAmount() {
                                                            let netTotal = parseFloat(document.querySelector("[name='netTotal']").value) || 0;
                                                            let totalPaid = parseFloat(document.querySelector("[name='totalPaid']").value) || 0;
                                                            let remainingAmount = netTotal - totalPaid;

                                                            document.querySelector("[name='remainingAmount']").value = remainingAmount.toFixed(2);
                                                        }

                                                        // Attach event listeners
                                                        document.querySelectorAll("[name='totalCustomLubricant'], [name='totalLubricantAjax'], [name='totalCustomLabour'], [name='totalLabourAjax'], [name='totalCustomDent'], [name='totalDentAjax'], [name='totalCustomSpare'], [name='totalSpareAjax']").forEach(input => {
                                                            input.addEventListener("input", updateFinalBillTotal);
                                                        });

                                                        document.querySelector("[name='discountAmount']").addEventListener("input", updateTotalBillAmount);
                                                        document.querySelector("[name='totalPaid']").addEventListener("input", updateRemainingAmount);

                                                        // <-- This triggers initial calculation on page load -->
                                                        updateFinalBillTotal();
                                                    });
                                                </script>


                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Discount (in %)</label>
                                                        <input type="number" name="discountAmount" class="form-control" step="any" value="<?= htmlspecialchars($jobCard['discountAmount']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-1 col-sm-6 col-12" hidden>
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Amount</label>
                                                        <input type="number" name="AmountOfDiscount" class="form-control" step="any" value="<?= htmlspecialchars($jobCard['AmountOfDiscount']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">VAT Amount (13%)</label>
                                                        <input type="number" name="vatAmount" class="form-control" value="0" step="any" readonly>
                                                    </div>
                                                </div>
                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Net Total</label>
                                                        <input type="number" name="netTotal" class="form-control" value="0" step="any" readonly>
                                                    </div>
                                                </div>
                                                <div class="col-lg-9 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Remarks</label>
                                                        <textarea name="billRemarks" class="form-control"><?= htmlspecialchars($jobCard['billRemarks']) ?></textarea>
                                                    </div>
                                                </div>

                                                <div class="col-lg-3 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Bill No.</label>
                                                        <input type="text" class="form-control" name="billNumber" value="<?= htmlspecialchars($jobCard['billNumber']) ?>" readonly>
                                                    </div>
                                                </div>
                                                <div class="col-lg-4 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Total Paid</label>
                                                        <input type="number" name="totalPaid" class="form-control" step="any" value="<?= htmlspecialchars($jobCard['totalPaid']) ?>">
                                                    </div>
                                                </div>
                                                <div class="col-lg-4 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Remaining Amount</label>
                                                        <input type="number" name="remainingAmount" class="form-control" value="<?= htmlspecialchars($jobCard['remainingAmount']) ?>" step="any" readonly>
                                                    </div>
                                                </div>
                                                <div class="col-lg-4 col-sm-6 col-12">
                                                    <div class="mb-3 add-product">
                                                        <label class="form-label">Status</label>
                                                        <select class="select" name="paymentStatus">
                                                            <option value="">Select</option>
                                                            <option value="Pending" <?php if ($jobCard['paymentStatus'] == 'Pending') {
                                                                                        echo 'selected';
                                                                                    } ?>>Pending</option>
                                                            <option value="Paid" <?php if ($jobCard['paymentStatus'] == 'Paid') {
                                                                                        echo 'selected';
                                                                                    } ?>>Paid</option>
                                                        </select>
                                                    </div>
                                                </div>
                                            </div>

                                        </div>
                                    </div>
                                </div>
                            </div>
                            <style>
                                .search-results {
                                    position: absolute;
                                    background: white;
                                    border: 1px solid #ccc;
                                    width: 100%;
                                    max-height: 200px;
                                    overflow-y: auto;
                                    display: none;
                                    z-index: 1000;
                                }

                                .search-results div {
                                    padding: 10px;
                                    cursor: pointer;
                                }

                                .search-results div:hover {
                                    background: #f0f0f0;
                                }
                            </style>
                            <div class="col-lg-12">
                                <div class="btn-addproduct mb-4">
                                    <button type="button" class="btn btn-cancel me-2">Cancel</button>
                                    <button type="submit" class="btn btn-submit">Save JobCard</button>
                                </div>
                            </div>
                </form>
            </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="37d26908286f2aa006113b6d-text/javascript"></script>

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

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

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

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

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

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

    <!-- Bootstrap Tagsinput JS -->
    <script src="js/bootstrap-tagsinput.js" type="37d26908286f2aa006113b6d-text/javascript"></script>

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

    <!-- Custom JS -->

    <script src="js/script.js" type="37d26908286f2aa006113b6d-text/javascript"></script>


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

    <style>
        .fuel-battery-indicator {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .fuel-battery-indicator input {
            width: 100%;
        }
    </style>
    <style>
        .search-results {
            position: absolute;
            background: white;
            border: 1px solid #ccc;
            width: 100%;
            max-height: 150px;
            overflow-y: auto;
            display: none;
            z-index: 1000;
        }

        .dropdown-item {
            padding: 8px;
            cursor: pointer;
        }

        .dropdown-item:hover {
            background-color: #f0f0f0;
        }
    </style>

    <!-- Fuel Battery Indicator Ajax -->
    <?php include './includes/fuelBatteryAjax.php'; ?>
    <!-- /Fuel Battery Indicator Ajax -->

    <!-- Spare Parts -->
    <?php include './includes/spareAjax.php'; ?>
    <!-- /Spare Parts -->

    <!-- Dent/Paint Details -->
    <?php include './includes/dentPaintAjax.php'; ?>
    <!-- /Dent/Paint Details -->

    <!-- labour Details -->
    <?php include './includes/labourDetailsAjax.php'; ?>
    <!-- /labour Details -->

    <!-- Lubricant Details -->
    <?php include './includes/lubricantDetailsAjax.php'; ?>
    <!-- /Lubricant Details -->

</body>

</html>