<h4><?= strtoupper($filename) ?></h4>
<h4>Periode <?= $periode ?></h4>
<h4></h4>
<table border="1">
    <thead>
        <tr class="bg-tr">
            <th rowspan="3" class="align-middle text-center">NO</th>
            <th rowspan="3" class="align-middle text-center">NIK</th>
            <th rowspan="3" class="align-middle text-center">NAMA KARYAWAN</th>
            <th rowspan="3" class="align-middle text-center">BULAN</th>
            <th rowspan="3" class="align-middle text-center">CABANG</th>
            <th rowspan="3" class="align-middle text-center">DEPARTEMEN</th>
            <th rowspan="3" class="align-middle text-center">PERIODE</th>
            <th rowspan="3" class="align-middle text-center">GAJI POKOK</th>
            <?php
            $tunj_tetap_rows = 1;
            if (count($komponen_tunj_tetap->result()) > 0) {
                $tunj_tetap_rows = count($komponen_tunj_tetap->result());
            }

            $tunj_tdk_tetap_rows = 1;
            if (count($komponen_tunj_tdk_tetap->result()) > 0) {
                $tunj_tdk_tetap_rows = count($komponen_tunj_tdk_tetap->result());
            }

            $tunj_kehadiran_rows = 1;
            if (count($komponen_tunj_kehadiran->result()) > 0) {
                $tunj_kehadiran_rows = count($komponen_tunj_kehadiran->result());
            }
            ?>
            <th colspan="<?= $tunj_tetap_rows + $tunj_tdk_tetap_rows + $tunj_kehadiran_rows + 1 ?>" class="align-middle text-center">TUNJANGAN</th>
            <th colspan="3" class="align-middle text-center">LEMBUR</th>

            <?php
            $pot_tetap_rows = 1;
            if (count($komponen_pot_tetap->result()) > 0) {
                $pot_tetap_rows = count($komponen_pot_tetap->result());
            }
            $pot_tdk_tetap_rows = 1;
            if (count($komponen_pot_tdk_tetap->result()) > 0) {
                $pot_tdk_tetap_rows = count($komponen_pot_tdk_tetap->result());
            }
            ?>
            <th colspan="<?= $pot_tetap_rows + $pot_tdk_tetap_rows + 5 ?>" class="align-middle text-center">POTONGAN</th>
            <th rowspan="3" class="align-middle text-center">THP</th>
            <th colspan="3" class="align-middle text-center">TUNJ. DIBIAYAI PERUSAHAAN</th>
            <th colspan="3" class="align-middle text-center">PUBLISH</th>
        </tr>
        <tr class="bg-tr">
            <!-- PENGHASILAN -->
            <th <?= 'colspan="' . $tunj_tetap_rows . '" ' ?> class="align-middle text-center">TETAP</th>
            <th <?= 'colspan="' . $tunj_tdk_tetap_rows . '" ' ?> class="align-middle text-center">TIDAK TETAP</th>
            <th <?= 'colspan="' . $tunj_kehadiran_rows . '" ' ?> class="align-middle text-center">KEHADIRAN</th>
            <th rowspan="2" class="text-center">TOTAL</th>

            <th rowspan="2" class="text-center">TOTAL MENIT</th>
            <th rowspan="2" class="text-center">PER JAM</th>
            <th rowspan="2" class="text-center">TOTAL</th>

            <!-- PENGURANGAN -->
            <th <?= 'colspan="' . $pot_tetap_rows . '" ' ?> class="align-middle text-center">TETAP</th>
            <th <?= 'colspan="' . $pot_tdk_tetap_rows . '" ' ?> class="align-middle text-center">TIDAK TETAP</th>

            <th rowspan="2" class="text-center">BPJS KES.</th>
            <th rowspan="2" class="text-center">BPJS TK</th>

            <th rowspan="2" class="text-center">ABSENSI</th>
            <th rowspan="2" class="text-center">PPH 21</th>
            <th rowspan="2" class="text-center">TOTAL</th>

            <th rowspan="2" class="text-center">BPJS KES.</th>
            <th rowspan="2" class="text-center">BPJS TK</th>
            <th rowspan="2" class="text-center">TOTAL</th>

            <th rowspan="2" class="text-center">JADWAL</th>
            <th rowspan="2" class="text-center">TANGGAL</th>
            <th rowspan="2" class="text-center">STATUS</th>
        </tr>
        <tr class="bg-tr">
            <!-- PENGHASILAN -->
            <?php
            if (count($komponen_tunj_tetap->result()) > 0) {
                foreach ($komponen_tunj_tetap->result() as $row) {
                    echo '<th class="align-top text-center">' . strtoupper($row->nm_komponen) . '</th>';
                }
            } else {
                echo '<th class="align-top text-center"></th>';
            }

            if (count($komponen_tunj_tdk_tetap->result()) > 0) {
                foreach ($komponen_tunj_tdk_tetap->result() as $row) {
                    echo '<th class="align-top text-center">' . strtoupper($row->nm_komponen) . '</th>';
                }
            } else {
                echo '<th class="align-top text-center"></th>';
            }

            if (count($komponen_tunj_kehadiran->result()) > 0) {
                foreach ($komponen_tunj_kehadiran->result() as $row) {
                    echo '<th class="align-top text-center">' . strtoupper($row->nm_komponen) . '</th>';
                }
            } else {
                echo '<th class="align-top text-center"></th>';
            }
            ?>

            <!-- PENGURANGAN -->
            <?php
            if (count($komponen_pot_tetap->result()) > 0) {
                foreach ($komponen_pot_tetap->result() as $row) {
                    echo '<th class="align-top text-center">' . strtoupper($row->nm_komponen) . '</th>';
                }
            } else {
                echo '<th class="align-top text-center"></th>';
            }

            if (count($komponen_pot_tdk_tetap->result()) > 0) {
                foreach ($komponen_pot_tdk_tetap->result() as $row) {
                    echo '<th class="align-top text-center">' . strtoupper($row->nm_komponen) . '</th>';
                }
            } else {
                echo '<th class="align-top text-center"></th>';
            }
            ?>
        </tr>
    </thead>
    <tbody>
        <?php
        $no = 1;
        foreach ($data as $val) {
        ?>
            <tr>
                <td><?= $no ?></td>
                <td>#<?= $val->nik ?></td>
                <td><?= $val->nm_karyawan ?></td>
                <td><?= date("m-Y", strtotime($val->periode)) ?></td>
                <td><?= $val->nm_cabang ?> - <?= $val->kd_cabang ?></td>
                <td><?= $val->nm_departemen ?> - <?= $val->kd_departemen ?></td>
                <td><?= date("d-m-Y", strtotime($val->tgl_mulai)) ?> s/d <?= date("d-m-Y", strtotime($val->tgl_selesai)) ?></td>
                <td><?= $val->gapok ?></td>

                <?php
                if (count($komponen_tunj_tetap->result()) > 0) {
                    if ($data_tunj_tetap_trx[$val->id_karyawan]) {
                        foreach ($data_tunj_tetap_trx[$val->id_karyawan] as $key => $value) {
                            echo '<td>' . cek_empty(array_values($value)[0]) . '</td>';
                        }
                    } else {
                        for ($i = 0; $i < $tunj_tetap_rows; $i++) {
                            echo '<td>0</td>';
                        }
                    }
                } else {
                    echo '<td></td>';
                }

                if (count($komponen_tunj_tdk_tetap->result()) > 0) {
                    if ($data_tunj_tdk_tetap_trx[$val->id_karyawan]) {
                        foreach ($data_tunj_tdk_tetap_trx[$val->id_karyawan] as $key => $value) {
                            echo '<td>' . cek_empty(array_values($value)[0]) . '</td>';
                        }
                    } else {
                        for ($i = 0; $i < $tunj_tdk_tetap_rows; $i++) {
                            echo '<td>0</td>';
                        }
                    }
                } else {
                    echo '<td></td>';
                }

                if (count($komponen_tunj_kehadiran->result()) > 0) {
                    if ($data_tunj_kehadiran_trx[$val->id_karyawan]) {
                        foreach ($data_tunj_kehadiran_trx[$val->id_karyawan] as $key => $value) {
                            echo '<td>' . cek_empty(array_values($value)[0]) . '</td>';
                        }
                    } else {
                        for ($i = 0; $i < $tunj_kehadiran_rows; $i++) {
                            echo '<td>0</td>';
                        }
                    }
                } else {
                    echo '<td></td>';
                }
                ?>
                <td><?= $val->ttl_tunjangan ?></td>
                <td><?= $val->lembur_menit ?></td>
                <td><?= $val->lembur_per_jam ?></td>
                <td><?= $val->lembur_rp ?></td>
                <?php
                if (count($komponen_pot_tetap->result()) > 0) {
                    if ($data_pot_tetap_trx[$val->id_karyawan]) {
                        foreach ($data_pot_tetap_trx[$val->id_karyawan] as $key => $value) {
                            echo '<td>' . cek_empty(array_values($value)[0]) . '</td>';
                        }
                    } else {
                        for ($i = 0; $i < $pot_tetap_rows; $i++) {
                            echo '<td>0</td>';
                        }
                    }
                } else {
                    echo '<td></td>';
                }

                if (count($komponen_pot_tdk_tetap->result()) > 0) {
                    if ($data_pot_tdk_tetap_trx[$val->id_karyawan]) {
                        foreach ($data_pot_tdk_tetap_trx[$val->id_karyawan] as $key => $value) {
                            echo '<td>' . cek_empty(array_values($value)[0]) . '</td>';
                        }
                    } else {
                        for ($i = 0; $i < $pot_tdk_tetap_rows; $i++) {
                            echo '<td>0</td>';
                        }
                    }
                } else {
                    echo '<td></td>';
                }
                ?>
                <td><?= $val->pot_bpjs_kes ?></td>
                <td><?= $val->pot_bpjs_tk ?></td>
                <td><?= $val->pot_absensi_acc ?></td>
                <td><?= $val->pot_pph21 ?></td>
                <td><?= $val->ttl_potongan ?></td>
                <td><?= $val->thp ?></td>
                <td><?= $val->tunj_bpjs_kes ?></td>
                <td><?= $val->tunj_bpjs_tk ?></td>
                <td><?= $val->ttl_tunj_dibiayai_perus ?></td>

                <?php
                if ($val->publish_sch == "0000-00-00 00:00:00") {
                    $publish_sch = "-";
                } else {
                    $publish_sch = date("d-m-Y", strtotime($val->publish_sch));
                }
                ?>
                <td><?= $publish_sch ?></td>

                <?php
                if ($val->publish_tgl == "0000-00-00 00:00:00") {
                    $publish_tgl = "-";
                } else {
                    $publish_tgl = date("d-m-Y H:i:s", strtotime($val->publish_tgl));
                }
                ?>
                <td><?= $publish_tgl ?></td>

                <?php
                if ($val->publish_flag == "1") {
                    $publish_flag = "PUBLISH";
                } else {
                    $publish_flag = "BELUM PUBLISH";
                }
                ?>
                <td><?= $publish_flag ?></td>

            </tr>

        <?php
            $no++;
        }
        ?>
    </tbody>
</table>
