<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 style='text-align: right;'><div class='btn-group' role='group' aria-label='Basic outlined example'>                                
                <button onclick='add_sj(" . $d['id_rit'] . ")' data-toggle='tooltip' title='Tambah SJ' type='button' class='btn btn-outline-primary btn-sm'><i class='bi bi-plus-lg'></i></button>
                <button onclick='berangkat(" . $d['id_rit'] . ")' data-toggle='tooltip' title='Kirim Customer' type='button' class='btn btn-outline-primary btn-sm'><i class='bi bi-truck'></i></button>
                <button onclick='edit_sj(" . $d['id_rit'] . ",\"" . $d['tgl_pergi'] . "\"," . $d['km_pergi'] . ",\"" . $d['keterangan'] . "\")' data-toggle='tooltip' title='Update' type='button' class='btn btn-outline-primary btn-sm'><i class='bi bi-pencil'></i></button>
                <button onclick='hapus_sj(" . $d['id_rit'] . ")' data-toggle='tooltip' title='Hapus' type='button' class='btn btn-outline-primary btn-sm'><i class='bi bi-x-lg'></i></button>
            </div></td>";
            } else {
                if ($d['tanggal_kembali'] == '') {
                    echo "<td style='text-align: right;'><div class='btn-group' role='group' aria-label='Basic outlined example'>                                
                    <button onclick='rit_kembali(" . $d['id_rit'] . ",\"" . $d['tgl_pergi'] . "\"," . $d['km_pergi'] . ")' data-toggle='tooltip' title='Rit Kembali' type='button' class='btn btn-outline-success btn-sm'><i class='bi bi-truck'></i></button>
                </div></td>";
                } else {
                    echo "<td><span class='badge bg-primary'>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>";
                if ($d['sts_pengiriman'] == 0) {
                    echo "<td style='text-align: right;'><div class='btn-group' role='group' aria-label='Basic outlined example'>     
                    <button onclick='cetak_sjln(" . $sj['doc_sj'] . ")' data-toggle='tooltip' title='Cetak SJ' type='button' class='btn btn-outline-primary btn-sm'><i class='bi bi-printer'></i></button>
                    <button onclick='edit_sjln(" . $sj['doc_sj'] . ",\"" . $sj['no_sj'] . "\",\"" . $sj['id_customer'] . "\"," . $sj['id_kota'] . ",\"" . $sj['dk_lk'] . "\",\"" . $sj['keterangan'] . "\")' data-toggle='tooltip' title='Update SJ' type='button' class='btn btn-outline-primary btn-sm'><i class='bi bi-pencil'></i></button>
                    <button onclick='hapus_sjln(" . $sj['doc_sj'] . ")' data-toggle='tooltip' title='Hapus SJ' type='button' class='btn btn-outline-primary btn-sm'><i class='bi bi-x-lg'></i></button>
                </div></td>";
                } else {
                    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>";
                    if ($d['sts_pengiriman'] == 0) {
                        echo "<td style='text-align: right;'><div class='btn-group' role='group' aria-label='Basic outlined example'>                                                                                                
                        <button onclick='pindah_fg_scan(\"" . $dfg['sku'] . "\"," . $sj['doc_sj'] . ",\"" . $dfg['barang'] . "\"," . $dfg['hitung'] . ",\"" . $sj['no_sj'] . "\")' data-toggle='tooltip' title='Pindah FG' type='button' class='btn btn-outline-primary btn-sm'><i class='bi bi-arrow-left-right'></i></button>
                        <button onclick='hapus_fg_scan(\"" . $dfg['sku'] . "\"," . $sj['doc_sj'] . ")' data-toggle='tooltip' title='Hapus FG' type='button' class='btn btn-outline-primary btn-sm'><i class='bi bi-x-lg'></i></button>
                    </div></td>";
                    } else {
                        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>