<style>

    #myTable thead th {

        position: sticky;

        top: 0;

        z-index: 1;

    }

</style>

<table id="myTable" data-last-order-identifier="customers"

    class="table table-clients number-index-2 dataTable no-footer">

    <thead>

        <tr>

            <th class="sorting_disabled not-export" rowspan="2" colspan="1" aria-label=" - "><span class="hide"> -

                </span>

                <div class="checkbox mass_select_all_wrap"><input type="checkbox" id="mass_select_all"

                        data-to-table="clients"><label></label></div>

            </th>

            <th rowspan="2">Tên KH</th>

            <th rowspan="2">Số cont</th>

            <th rowspan="2">Ngày ghi sổ</th>

            <th rowspan="2">Điểm Lấy Rỗng</th>

            <th rowspan="2">Điểm đi</th>

            <th rowspan="2">Điểm đến</th>

            <th rowspan="2">Xuất/Nhập</th>

            <th rowspan="2">Loại Cont</th>

            <th rowspan="2">Giá vận chuyển</th>

            <th rowspan="2">VAT</th>

            <th colspan="8">Phí</th>

            <th rowspan="2">Phải Thu</th>

            <th rowspan="2">Đã Thu</th>

            <th colspan="2">Thuê xe ngoài</th>

            <th colspan="2">Xe nhà</th>

            <th rowspan="2">Chi phí phát sinh</th>

            <th rowspan="2">Hoa hồng</th>

            <th rowspan="2">Tổng chi phí</th>

            <th rowspan="2"> Ghi Chú Thêm </th>

        </tr>

        <tr class="sticky_head">

            <th>Phí Bốc Xếp</th>

            <th>Phí nâng</th>

            <th>Phí hạ</th>

            <th>Vệ Sinh</th>

            <th>Cân</th>

            <th>Cược cont</th>

            <th>Neo</th>

            <th>Phí khác</th>

            <th>Nhà xe</th>

            <th>Giá</th>

            <th>Tên</th>

            <th>Số xe</th>

        </tr>

    </thead>

    <tbody>

        <?php 

            foreach ($paginated_data as $index => $row): 

            $current_date = date('Y-m-d');

            $record_date = date('Y-m-d', strtotime($row['Ngay_ghi_so']));

            $is_future_date = $record_date > $current_date;

        ?>

        <tr data-id="<?php echo $row['id']; ?>" style="background-color: <?php echo $row['note_color']; ?>;">

            <td>

                <div class="checkbox"> <input type="checkbox" class="rowselect"

                        value="<?php echo $index; ?>"><label></label></div>

            </td>

            <td class="truncate" data-truncate="<?php echo $row['Ten_KH']; ?>" style="text-align: left"><a

                    href="/perfex_crm/admin/clients/client/<?php echo $row['MA_KH'];?>"><?php echo $row['Ten_KH']; ?></a>

                <div class="row-options">

                         <a href="#" class="edit-button" data-id="<?php echo $row['id']; ?>" style="color:#e57909">Chỉnh sửa </a> | <a

                        href="<?php echo site_url('congno/delete/' . $row['id']); ?>"

                        class="text-danger _delete" onclick="return confirm('Bạn có chắc muốn xóa không?');">Xóa </a></div>

            </td>

            <td><?php echo $row['So_cont']; ?></td>

            <td>

                <?php if ($is_future_date): ?>

                    <span style="color: red;">● </span>

                <?php endif; ?>

                <?= date('d/m/Y', strtotime($row['Ngay_ghi_so'])); ?>

            </td>

            <td><?php echo $row['Diem_lay_rong']; ?></td>

            <td><?php echo $row['Diem_di']; ?></td>

            <td><?php echo $row['Diem_den']; ?></td>

            <td><?php echo $row['xuat_nhap']; ?></td>

            <td><?php echo $row['Loai_cont']; ?></td>

            <td style="color: rgb(37 99 235)"><?php echo number_format($row['Gia_van_chuyen']); ?></td>

            <td>

                <?php 

                    $gia_van_chuyen = $row['Gia_van_chuyen'];

                    $vat = $row['VAT'];

                        if(is_numeric($gia_van_chuyen) && is_numeric($vat)) {

                            echo number_format($gia_van_chuyen * $vat);

                            echo " (".($vat * 100)."%)";

                        } else {

                            echo "0 (0%)";

                        }

                ?>

            </td>

            <td><?php echo number_format($row['gia_cuoc']); ?></td>

            <td><?php echo number_format($row['Phi_nang']); ?></td>

            <td><?php echo number_format($row['Phi_ha']); ?></td>

            <td><?php echo number_format($row['Ve_sinh_can']); ?></td>

            <td><?php echo number_format($row['can']); ?></td>

            <td><?php echo number_format($row['Cuoc_cont']); ?></td>

            <td><?php echo number_format($row['Neo']); ?></td>

            <td><?php echo number_format($row['Phi_khac']); ?></td>

            <td><?php echo number_format($row['Phai_thu']); ?></td>

            <td><?php echo number_format($row['Da_thu']); ?></td>

            <td>

                <?php foreach ($status as $xengoai) {

                    if ($xengoai['id'] == $row['Nha_xe']) {

                        echo '<a href="' . admin_url('leads/statuses') . '">' . $xengoai['name'] . '</a>';

                        break;

                    }

                }?>

                        

            </td>

            <td><?php echo number_format($row['Gia']); ?></td>

            <td>

                <?php

                    foreach ($staff as $staffs) {

                        if ($staffs['staffid'] == $row['So_xe']) {

                            echo $staffs['staff_identifi'];

                    }

                }

                ?>

            </td>

            <td>

                <?php

                    foreach ($xenha as $xenha1) {

                        if ($xenha1['id'] == $row['bien_so_xe']) {

                            echo $xenha1['name'];

                        }

                    }

                ?>

            </td>

            <td><?php echo number_format($row['Chi_phi_phat_sinh']); ?></td>

            <td><?php echo number_format($row['Hoa_hong']); ?></td>

            <?php $vat = isset($vat) && !empty($vat) ? $vat : 0; ?>

            <td>

                <?php

                    if ($gia_van_chuyen == 0){

                        echo "0";

                    }

                    else{

                       echo number_format($gia_van_chuyen + ($vat * $gia_van_chuyen) + $row['gia_cuoc'] + $row['Phi_nang'] + $row['Phi_ha'] + $row['Ve_sinh_can'] + $row['can'] + $row['Cuoc_cont'] + $row['Neo'] + $row['Phi_khac'] + $row['Chi_phi_phat_sinh']); 

                    }

                ?>

            </td>

            <td><?php echo $row['Ghi_chu_them']; ?></td>

        </tr>

        <?php endforeach; ?>

        

    </tbody>

</table>

<script>

function deleteSelectedRows() {

    const selectedIds = Array.from(document.querySelectorAll('.rowselect:checked'))

        .map(checkbox => checkbox.closest('tr').getAttribute('data-id'));

    if (selectedIds.length === 0) {

        alert("Vui lòng chọn ít nhất 1 dòng để xóa!");

        return;

    }

    var confirmation = confirm(selectedIds.length + " dòng được chọn. Bạn có muốn xóa không?");

    if (confirmation) {

        $.ajax({

            url: "<?php echo site_url('congno/delete_selected'); ?>",

            type: "POST",

            data: {

                ids: selectedIds

            },

            success: function() {

                location.reload();

            },

            error: function() {

                alert("Có lỗi xảy ra trong quá trình xóa!");

            }

        });

    }

}

</script>

<script>

        const inputFields = document.querySelectorAll('.form-control-format');

        const forms = document.querySelectorAll('#myForm, #editForm1');



        inputFields.forEach(field => {

            field.addEventListener('input', formatNumber);

        });



        forms.forEach(form => {

            form.addEventListener('submit', removeFormatBeforeSubmit);

        });



        function formatNumber(event) {

            const value = event.target.value.replace(/\D/g, '');

            if (value) {

                event.target.value = parseInt(value).toLocaleString('en-US');

            }

        }



        function removeFormatBeforeSubmit(event) {

            inputFields.forEach(field => {

                const value = field.value.replace(/,/g, '');

                field.value = value;

            });

        }

    </script>