<style>
    #header {
        font-family: Arial, Helvetica, sans-serif;
        border-collapse: collapse;
        width: 100%;
    }

    #header td,
    #customers th {

        padding: 4px;
        font-size: 10px;
    }

    #header tr:nth-child(even) {
        background-color: #ffffff;
    }

    #header tr:hover {
        background-color: #ddd;
    }

    #header th {
        padding-top: 12px;
        padding-bottom: 12px;
        text-align: left;
        padding: 4px;
        color: black;
    }
</style>




<style>
    #scan {
        font-family: Arial, Helvetica, sans-serif;
        border-collapse: collapse;
        width: 100%;
    }

    #scan td,
    #scan th {

        padding: 8px;
        font-size: 11px;
    }

    #scan tr:nth-child(even) {
        background-color: #ffffff;
    }

    #scan tr:hover {
        background-color: #ddd;
    }

    #scan th {
        padding-top: 12px;
        padding-bottom: 12px;
        text-align: left;
        padding: 4px;
        color: black;
    }
</style>


<style>
    #scan {
        font-family: Arial, Helvetica, sans-serif;
        border-collapse: collapse;
        width: 100%;
    }

    #scan td,
    #scan th {
        border-collapse: collapse;
        padding: 8px;
        font-size: 11px;
    }

    #scan tr:nth-child(even) {
        background-color: #ffffff;
    }

    #scan tr:hover {
        background-color: #ddd;
    }

    #scan th {
        border-collapse: collapse;
        padding-top: 12px;
        padding-bottom: 12px;
        text-align: left;
        padding: 4px;
        color: black;
    }
</style>



<style>
    #details {
        font-family: Arial, Helvetica, sans-serif;
        border-collapse: collapse;
        width: 100%;
    }

    #details td,
    #details th {

        padding: 8px;
        font-size: 8px;
    }

    #details tr:nth-child(even) {
        background-color: #ffffff;
    }

    #details tr:hover {
        background-color: #ddd;
    }

    #details th {
        padding-top: 12px;
        font-style: bold;
        padding-bottom: 12px;
        text-align: left;
        padding: 4px;
        color: black;
    }
</style>

<style>
    #sign {
        font-family: Arial, Helvetica, sans-serif;
        border-collapse: collapse;
        width: 100%;
    }

    #sign td,
    #sign th {

        padding: 8px;
        font-size: 11px;
        text-align: center;
    }

    #sign tr:nth-child(even) {
        background-color: #ffffff;
    }

    #sign tr:hover {
        background-color: #ddd;
    }

    #sign th {
        padding-top: 12px;
        padding-bottom: 12px;
        text-align: left;
        padding: 4px;
        color: black;
    }

    @media print {
        .new-page {
            page-break-before: always;
        }
    }
</style>


<table id="header">
    <tr>
        <th rowspan="4" style="width:50px"><br><img src="{{ asset('img/ARRAHN1.png') }}" style="width:70px"></img></th>
        <th colspan="1">KOPERASI PERMODALAN KELANTAN BERHAD</th>
    </tr>
    <tr>
        <td>{{ $cawangan_detail['namacaw'] }}</td>
        <td></td>
    </tr>
    <tr>
        <td>{{ $cawangan_detail['alamat1'] }}</td>
        <td></td>
    </tr>
    <tr>
        <td>{{ $cawangan_detail['alamat2'] . ' No Tel : ' . $cawangan_detail['notel'] . ' No Faks : ' . $cawangan_detail['nofaks'] }}
        </td>
        <td></td>
    </tr>
</table>
<br>

<table id="scan">
    <tr>
        <th colspan ="5"><b>LAPORAN PENYELESAIAN PEMBIAYAAN</b></th>

    </tr>
    <tr>
        <td style="white-space: nowrap" colspan="2"><b>Tarikh Mula: </b>

            @php
                $myDateTime = DateTime::createFromFormat('Y-m-d', $mula_date);
                $formattedweddingdate = $myDateTime->format('d-m-Y');
                echo $formattedweddingdate;
            @endphp

        </td>

        <td style="white-space: nowrap" colspan="2"><b> Tarikh Hingga: </b>

            @php
                $myDateTime = DateTime::createFromFormat('Y-m-d', $hingga_date);
                $formattedweddingdate = $myDateTime->format('d-m-Y');
                echo $formattedweddingdate;
            @endphp</td>

        <td style="white-space: nowrap" colspan="2"><b>Tarikh Cetak: </b>

            @php
                $myDateTime = DateTime::createFromFormat('Y-m-d', $tarikh_cetak);
                $formattedweddingdate = $myDateTime->format('d-m-Y');
                echo $formattedweddingdate;
            @endphp</td>

    </tr>
</table>

<br>
<table id="details">
    <tr>
        <th>Bil</th>
        <th>Norujukan</th>
        <th>Nama</th>
        <th>No KP</th>
        <th>Tarikh Penyelesaian Pembiayaan</th>
        <th>Masa</th>
        <th>JBG</th>
        <th>J Tebus</th>
        <th>Nilai Marhun(RM)</th>
        <th>P. Asal(RM)</th>
        <th>B.Pembiayaan(RM)</th>
        <th>Lain-Lain Caj (RM)</th>
        <th>Untung 1% (RM)</th>
        <th>Pendapatan(RM)</th>
        <th>Bayaran(RM)</th>
        <th>Jumlah(RM)</th>
    </tr>
    @php
        $counter = 1;
        $totalnm = 0;
        $totalbayaran = 0;
        $totalpinjamanasal = 0;
        $totalpinjaman = 0;
        $totalupah = 0;
        $totalujrah = 0;
        $totalonepercent = 0;
    @endphp

    @foreach ($tebus as $index => $item)
        @php
            $totalbayaran += $item['jenistebus'] === 'S' ? $item['gadaibakipjm'] + $item['bakiupah'] : $item['bakipjm'];
            $totalpinjaman +=
                $item['hargajualan'] != $item['bakihargajualan'] ? $item['gadaibakipjm'] : $item['pinjaman'];
            $totalupah += $item['bakiupah'];
            $totalpinjamanasal += $item['pinjaman'];
            $totalnm += $item['nilaimarhun'];
            $totalujrah += $item['ujrah'];
            $totalonepercent += $item['onepercent'];

        @endphp

        @if ($index > 0)
            @if ($item['teller'] != $tebus[$index - 1]['teller'])
                <tr>
                    <td colspan="12">Teller : <b>{{ $item['teller'] }}</b></td>
                </tr>
                @php
                    $totalnmteller = 0;
                    $totalpinjamanasalteller = 0;
                    $totalpinjamanteller = 0;
                    $totalupahteller = 0;
                    $totalbayaranteller = 0;
                    $totalujrahteller = 0;
                    $totalonepercentteller = 0;
                @endphp
            @endif
        @else
            <tr>
                <td colspan="12">Teller : <b>{{ $item['teller'] }}</b></td>
            </tr>
            @php
                $totalnmteller = 0;
                $totalpinjamanasalteller = 0;
                $totalpinjamanteller = 0;
                $totalupahteller = 0;
                $totalbayaranteller = 0;
                $totaljumlahteller = 0;
                $totalujrahteller = 0;
                $totalonepercentteller = 0;
            @endphp
        @endif

        <tr>
            <td>{{ $counter }}</td>
            <td>{{ $item['norujukan'] }}</td>
            <td>{{ $item['nama'] }}</td>
            <td>{{ $item['nokp'] }}</td>
            <td>
                @php
                    $dt = new DateTime($item['tarikh_bayaran']);
                    $time = $dt->format('d-m-Y');
                    echo date($time);
                @endphp
            </td>
            <td>
                @php
                    $dt = new DateTime($item['tarikh_bayaran']);
                    $time = $dt->format('h:i:s');
                    echo date($time);
                @endphp
            </td>
            <td>{{ $item['jbg'] }}</td>
            <td>{{ $item['jenistebus'] }}</td>
            <td>
                {{ number_format($item['nilaimarhun'], 2) }}
            </td>
            <td>{{ number_format($item['pinjaman'], 2) }}</td>
            <td>
                @php
                    if ($item['hargajualan'] !== $item['bakihargajualan']) {
                        echo number_format($item['gadaibakipjm'], 2);
                    } else {
                        echo number_format($item['pinjaman'], 2);
                    }
                @endphp
            </td>
            <td>{{ number_format($item['ujrah'], 2) }}</td>
            <td>{{ number_format($item['onepercent'], 2) }}</td>
            {{-- <td>{{ number_format($item['bakiupah'], 2) }}</td> --}}
            <td>
              @php
                  if ($item['tagbaru']) {
                      $total = $item['ujrah'] + $item['onepercent'];
                      // echo number_format($item['bakiupah'], 2);
                      echo number_format(0, 2);
                  } else {
                      // echo number_format(0, 2);
                      echo number_format($item['bakiupah'], 2);
                  }
              @endphp
            </td>
            <td>{{ number_format($item['bakipjm'], 2) }}</td>
            <td>@php
                if ($item['jenistebus'] == 'S') {
                    $total = $item['gadaibakipjm'] + $item['bakiupah'];
                    echo number_format($total, 2);
                } else {
                    echo number_format($item['bakipjm'], 2);
                }
            @endphp</td>
        </tr>


        @php $counter++; @endphp
        @php
            $totalnmteller += $item['nilaimarhun'];
            $totalpinjamanasalteller += $item['pinjaman'];
            $totalpinjamanteller +=
                $item['hargajualan'] != $item['bakihargajualan'] ? $item['gadaibakipjm'] : $item['pinjaman'];
            $totalupahteller += $item['bakiupah'];
            $totalbayaranteller += $item['bakipjm']; //
            $totalujrahteller += $item['ujrah'];
            $totalonepercentteller += $item['onepercent'];
        @endphp

        @php
            $totaljumlahteller +=
                $item['jenistebus'] === 'S' ? $item['gadaibakipjm'] + $item['bakiupah'] : $item['bakipjm'];
        @endphp

        @if ($index + 1 < sizeof($tebus))
            @if ($item['teller'] != $tebus[$index + 1]['teller'])
                <tr class="new-page">
                    <td colspan="8"><b>{{ $counter - 1 }}</b> Jumlah Bagi Teller : <b>{{ $item['teller'] }}</b>
                    </td>
                    <td><b>{{ number_format($totalnmteller, 2) }} </b></td>
                    <td><b>{{ number_format($totalpinjamanasalteller, 2) }} </b></td>
                    <td><b>{{ number_format($totalpinjamanteller, 2) }} </b></td>
                    <td><b>{{ number_format($totalujrahteller, 2) }} </b></td>
                    <td><b>{{ number_format($totalonepercentteller, 2) }} </b></td>
                    <td><b>{{ number_format($totalupahteller, 2) }} </b></td>
                    <td><b>{{ number_format($totalbayaranteller, 2) }} </b></td>
                    <td><b>{{ number_format($totaljumlahteller, 2) }} </b></td>

                </tr>
                @php
                    $counter = 1;
                    $totalpinjamanasalteller = 0;
                    $totalpinjamanteller = 0;
                    $totalupahteller = 0;
                    $totalbayaranteller = 0;
                    $totaljumlahteller = 0;
                    $totalujrahteller = 0;
                    $totalonepercentteller = 0;
                @endphp
            @endif
        @else
            <tr>
                <td colspan="8"><b>{{ $counter - 1 }}</b> Jumlah Bagi Teller : <b>{{ $item['teller'] }}</b></td>
                <td><b>{{ number_format($totalnmteller, 2) }}</b></td>
                <td><b>{{ number_format($totalpinjamanasalteller, 2) }} </b></td>
                <td><b>{{ number_format($totalpinjamanteller, 2) }} </b></td>
                <td><b>{{ number_format($totalujrahteller, 2) }} </b></td>
                <td><b>{{ number_format($totalonepercentteller, 2) }} </b></td>
                <td><b>{{ number_format($totalupahteller, 2) }} </b></td>
                <td><b>{{ number_format($totalbayaranteller, 2) }} </b></td>
                <td><b>{{ number_format($totaljumlahteller, 2) }} </b></td>
            </tr>
        @endif
    @endforeach
</table>

<br><br>
<table id="details" style="border-top:1px solid black;border-bottom:1px solid black;text-align:center;">
    <tr>
        <th colspan="9" style="text-align: center;"><b>JUMLAH BILANGAN =
                {{ $tebuspengasingan['biltawarruq'] + $tebuspengasingan['bilarrahn'] }}</b></th>
        <th style="text-align:center;">Nilai Marhun (RM)</th>
        <th style="text-align:center;">Pembiayaan Asal(RM)</th>
        <th style="text-align:center;">B. Pembiayaan (RM)</th>
        <th style="text-align:center;">Keuntungan Rebet(RM)</th>
        <th style="text-align:center;">Ujrah (RM)</th>
        <th style="text-align:center;">Untung 1% (RM)</th>
        <th style="text-align:center;">Pendapatan Kasar (RM)</th>
        <th style="text-align:center;">Pendapatan Sebenar(RM)</th>
        <th style="text-align:center;">Jumlah (RM)</th>
    </tr>
    <tr>
        <td colspan="9" style="text-align: center;"><b>JUMLAH KESELURUHAN</b></td>
        <td>{{ number_format($totalnm, 2) }}</td>
        <td>{{ number_format($totalpinjamanasal, 2) }}</td>
        <td>{{ number_format($totalpinjaman, 2) }}</td>
        <td>@php$totalkeuntunganrebet = $totalpinjaman + $totalupah - $totalbayaran;
                        echo number_format($totalkeuntunganrebet, 2);
                @endphp ?></td>
        <td>{{ number_format($totalujrah, 2) }}</td>
        <td>{{ number_format($totalonepercent, 2) }}</td>
        <td>{{ number_format($totalupah, 2) }}</td>
        <td>{{ number_format(floatval($totalupah) - floatval($totalkeuntunganrebet), 2) }}</td>
        <td>{{ number_format($totalbayaran, 2) }}</td>

    </tr>
</table>

<h4>Laporan Transaksi Penyelesaian Pem.</h4>
<br>

<table id="details">
    <thead>
        <th>Bil</th>
        <th>Jenis</th>
        <th>Penyelesaian Pem.(RM)</th>
        <th>Lain-Lain Caj (RM)</th>
        <th>Untung 1% (RM)</th>
        <th>Pendapatan(RM)</th>
        <th>Rebate(RM)</th>
    </thead>
    <tr>
        <td>{{ $tebuspengasingan['biltawarruq'] }}</td>
        <td>Tawarruq</td>
        <td>{{ number_format($tebuspengasingan['totaltawarruq'], 2) }}</td>
        <td>{{ number_format($tebuspengasingan['ujrahtawarruq'], 2) }}</td>
        <td>{{ number_format($tebuspengasingan['onepercenttawarruq'], 2) }}</td>
        <td>{{ number_format($tebuspengasingan['keuntungantawarruq'], 2) }}</td>
        <td>{{ number_format($tebuspengasingan['totalrebate'], 2) }}</td>
    </tr>
    <tr>
        <td>{{ $tebuspengasingan['bilarrahn'] }}</td>
        <td>Ar-Rahn</td>
        <td>{{ number_format($tebuspengasingan['totalarrahn'], 2) }}</td>
        <td>{{ number_format($tebuspengasingan['ujraharrahn'], 2) }}</td>
        <td>{{ number_format($tebuspengasingan['onepercentarrahn'], 2) }}</td>
        <td>{{ number_format($tebuspengasingan['keuntunganarrahn'], 2) }}</td>
        <td> Not Applicable </td>
    </tr>
    <tr>
        <td>{{ $upahlama['bilkaunter'] }}</td>
        <td>Bayaran Upah</td>
        <td> Not Applicable </td>
        <td> Not Applicable</td>
        <td> Not Applicable</td>
        <td>{{ number_format($upahlama['totalkaunter'], 2) }}</td>
        <td> Not Applicable </td>
    </tr>
    <tr>
        <td>{{ $upahlama['bilonline'] }}</td>
        <td>Bayaran Upah Online</td>
        <td> Not Applicable </td>
        <td> Not Applicable</td>
        <td> Not Applicable</td>
        <td>{{ number_format($upahlama['totalonline'], 2) }}</td>
        <td> Not Applicable </td>
    </tr>
    <tr>
        <td>{{ $ansuran['bilangankaunter'] }}</td>
        <td>Ansuran Pembiayaan</td>
        <td>{{ number_format($ansuran['totalpendahuluankaunter'], 2) }}</td>
        <td>{{ number_format($ansuran['totalujrahkaunter'], 2) }}</td>
        <td>{{ number_format($ansuran['totalonepercentkaunter'], 2) }}</td>
        <td>{{ number_format($ansuran['totalkeuntungankaunter'], 2) }}</td>
        <td> Not Applicable </td>
    </tr>
    <tr>
        <td>{{ $ansuran['bilanganonline'] }}</td>
        <td>Ansuran Pembiayaan Online</td>
        <td>{{ number_format($ansuran['totalpendahuluanonline'], 2) }}</td>
        <td>{{ number_format($ansuran['totalujrahonline'], 2) }}</td>
        <td>{{ number_format($ansuran['totalonepercentonline'], 2) }}</td>
        <td>{{ number_format($ansuran['totalkeuntunganonline'], 2) }}</td>
        <td> Not Applicable </td>
    </tr>
</table>



<br>
<br>
<table id="sign">
    <tr>
        <td></td>
        <td colspan="2"> .................................................................. </td>
        <td> </td>
        <td colspan="2">.................................................................. </td>
        <td> </td>
    </tr>
    <tr>
        <td></td>
        <td colspan="2"><B> DI SEMAK</B> </td>
        <td> </td>
        <td colspan="2"><B>DI SAHKAN </B></td>
        <td> </td>
    </tr>
</table>


