<table class="table table-sm table-bordered">
    <thead>
        <tr class="table-warning">
            <th style='text-align: center;' rowspan="2">#</th>
            <th style='text-align: center;' rowspan="2">No POL</th>
            <th style='text-align: center;' rowspan="2">Sopir</th>
            <th style='text-align: center;' colspan="2">Berangkat</th>
            <th style='text-align: center;' colspan="2">Kembali</th>
            <th style='text-align: center;' rowspan="2">Jarak</th>
            <th width="110px" style='text-align: center;' rowspan="2">Opsi</th>
        </tr>
        <tr class="table-warning">
            <th style='text-align: center;'>Tgl</th>
            <th style='text-align: center;'>Kilomtr</th>
            <th style='text-align: center;'>Tgl</th>
            <th style='text-align: center;'>Kilomtr</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 1;
        foreach ($data as $d) {
            $datasj = $this->M_gudang->tb_sj($d['id_rit']);
            echo "<tr>";
            echo "<td>" . $i . "</td>";
            echo "<td><b>" . $d['no_pol'] . "</b></td>";
            echo "<td><b>" . $d['sopir'] . "</b></td>";
            echo "<td><b>" . $d['tanggal_pergi'] . "</b></td>";
            echo "<td style='text-align: right;'><b>" . $d['km_pergi'] . "</b></td>";
            echo "<td><b>" . $d['tanggal_kembali'] . "</b></td>";
            echo "<td style='text-align: right;'><b>" . $d['km_kembali'] . "</b></td>";
            if ($d['jarak_tempuh'] < 0) {
                echo "<td style='text-align: right;'><b>0</b></td>";
            } else {
                echo "<td style='text-align: right;'><b>" . $d['jarak_tempuh'] . "</b></td>";
            }
            if ($d['sts_pengiriman'] == 0) {
                echo "<td><span class='badge bg-secondary'>Muat Barang</span></td>";
            } else {
                if ($d['tanggal_kembali'] == '') {
                    echo "<td><span class='badge bg-primary'>Kirim Customer</span></td>";
                } else {
                    echo "<td><span class='badge bg-success'>Pengiriman Selesai</span></td>";
                }
            }
            echo "</tr>";
            $kubikasi_kendaraan = 0;
            foreach ($datasj as $sj) {
                $datafg = $this->M_gudang->hitung_fg_nopol($sj['doc_sj']);
                echo "<tr>";
                echo "<td></td>";
                echo "<td colspan='2'><i class='bi bi-file-earmark-text'></i>&nbsp;" . $sj['no_sj'] . "</td>";
                echo "<td colspan='2'><i class='bi bi-people'></i>&nbsp;" . $sj['id_customer'] . "</td>";
                echo "<td colspan='2'><i class='bi bi-truck'></i>&nbsp;" . $sj['kota'] . "</td>";
                echo "<td>" . $sj['dk_lk'] . "</td>";
                echo "<td></td>";
                echo "</tr>";
                $kubikasi_brg = 0;
                foreach ($datafg as $dfg) {
                    $kubikasi_fg = $this->db->get_where('tb_fg', ['sku' => $dfg['sku']])->row_array();
                    echo "<tr>";
                    echo "<td></td>";
                    echo "<td colspan='2'></td>";
                    echo "<td colspan='2'><i class='bi bi-box-seam'></i>&nbsp;" . $dfg['barang'] . "</td>";
                    echo "<td style='text-align: right;'>" . $dfg['hitung'] . "</td>";
                    echo "<td style='text-align: right;'>" . number_format((($kubikasi_fg['size_1'] * $kubikasi_fg['size_2'] * $kubikasi_fg['size_3']) / 1000000000), 6, '.', ',') . "</td>";
                    echo "<td style='text-align: right;'>" . number_format(($dfg['hitung'] * (($kubikasi_fg['size_1'] * $kubikasi_fg['size_2'] * $kubikasi_fg['size_3'])) /  1000000000), 6, '.', ',') . "</td>";
                    echo "<td></td>";
                    echo "</tr>";
                    $i++;
                    $kubikasi_brg += ($dfg['hitung'] * (($kubikasi_fg['size_1'] * $kubikasi_fg['size_2'] * $kubikasi_fg['size_3'])) /  1000000000);
                }
                echo "<tr>";
                echo "<td colspan='7' style='text-align: right;'><b>Total Kubikasi SJ</b></td>";
                echo "<td style='text-align: right;'><b>" . $kubikasi_brg . "</b></td>";
                echo "<td></td>";
                echo "</tr>";
                $kubikasi_kendaraan += $kubikasi_brg;
            }
            echo "<tr>";
            echo "<td colspan='7' style='text-align: right;'><b>Total Kubikasi Kendaraan</b></td>";
            echo "<td style='text-align: right;'><b>" . $kubikasi_kendaraan . "</b></td>";
            echo "<td></td>";
            echo "</tr>";
            $i++;
        }
        ?>
    </tbody>
</table>

<script>
    $(function() {
        $('[data-toggle="tooltip"]').tooltip();
    });
</script>