package corporate_mcudetail

import "cpone/models"
import "strconv"
import "cpone/utils"

templ TableAndPagination(
    tablecomponent templ.Component,
    paginationcomp templ.Component,
) {
    <div id="tabdanpagi" hx-on::load={ AfterRequestContent() }>
        @tablecomponent
        @paginationcomp
    </div>
}

templ TableKeuangan(
    data []models.KeuanganModel,
    tableID string,
) {
    <div id={tableID} class="mt-8">
        <table class="table table-hover table-bordered">
            <thead>
                <tr class="rounded-lg bg-header-table">
                    <th scope="col" class="text-center col-5" colspan="3"><h4>TAGIHAN</h4></th>
                    <th scope="col" class="text-center col-4" colspan="2"><h4>PELUNASAN</h4></th>
                    <th scope="col" class="text-center col-3" rowspan="2" style="vertical-align: middle;"><h4>SISA</h4></th>
                </tr>
                <tr>
                    <th scope="col" class="text-center">No/Tgl</th>
                    <th scope="col" class="text-center">Tgl Jatuh Tempo</th>
                    <th scope="col" class="text-center">Jumlah</th>
                    <th scope="col" class="text-center">No/Tgl</th>
                    <th scope="col" class="text-center">Jumlah</th>
                </tr>
            </thead>
            <tbody>
                @RowKeuangan(data)
            </tbody>
        </table>
    </div>
}

// note
// row pelunasan -> row tagihan rowspan=len(tab pelunasan)
// row sisa ikut ke row pertama tagihan

templ RowKeuangan(
    data []models.KeuanganModel,
) {
    if len(data) == 0 {
        <tr>
            <td colspan="6" class="text-center">Data Tidak Ditemukan</td>
        </tr>
    }
    for _, v := range data {
        if len(v.BillPayment) == 0 {
            <tr>
                <td class="text-center">
                    <div>{ v.F_BillNo }</div>
                    <div>{ v.F_BillCreated }</div>
                </td>
                <td class="text-center">{ v.F_BillDueDate }</td>
                <td class="text-center">{ utils.FormatRibuan(v.F_BillTotal) }</td>
                <td class="text-center">-</td>
                <td class="text-center">-</td>
                <td class="text-center">{ utils.FormatRibuan(v.F_BillUnpaid) }</td>
            </tr>
        } else if len(v.BillPayment) == 1 {
            <tr>
                <td class="text-center" rowspan={ strconv.Itoa(len(v.BillPayment)) }>
                    <div>{ v.F_BillNo }</div>
                    <div>{ v.F_BillCreated }</div>
                </td>
                <td class="text-center" rowspan={ strconv.Itoa(len(v.BillPayment)) }>{ v.F_BillDueDate }</td>
                <td class="text-center" rowspan={ strconv.Itoa(len(v.BillPayment)) }>{ utils.FormatRibuan(v.F_BillTotal) }</td>
                <td class="text-center">
                    <div>{ v.BillPayment[0].F_BillPaymentNumber }</div>
                    <div>{ v.BillPayment[0].F_BillPaymentDate }</div>                
                </td>
                <td class="text-center">{ utils.FormatRibuan(int(v.BillPayment[0].F_BillPaymentAmount)) }</td>
                <td class="text-center" rowspan={ strconv.Itoa(len(v.BillPayment)) }>{ utils.FormatRibuan(v.F_BillUnpaid) }</td>
            </tr>
        } else {
           <tr>
                <td class="text-center" rowspan={ strconv.Itoa(len(v.BillPayment)) }>
                    <div>{ v.F_BillNo }</div>
                    <div>{ v.F_BillCreated }</div>
                </td>
                <td class="text-center" rowspan={ strconv.Itoa(len(v.BillPayment)) }>{ v.F_BillDueDate }</td>
                <td class="text-center" rowspan={ strconv.Itoa(len(v.BillPayment)) }>{ utils.FormatRibuan(v.F_BillTotal) }</td>
                <td class="text-center">
                    <div>{ v.BillPayment[0].F_BillPaymentNumber }</div>
                    <div>{ v.BillPayment[0].F_BillPaymentDate }</div>                
                </td>
                <td class="text-center">{ utils.FormatRibuan(int(v.BillPayment[0].F_BillPaymentAmount)) }</td>
                <td class="text-center" rowspan={ strconv.Itoa(len(v.BillPayment)) }>{ utils.FormatRibuan(v.F_BillUnpaid) }</td>
            </tr>
            for i := 1; i < len(v.BillPayment); i++ {
                <tr>
                    <td class="text-center">
                        <div>{ v.BillPayment[i].F_BillPaymentNumber }</div>
                        <div>{ v.BillPayment[i].F_BillPaymentDate }</div>
                    </td>
                    <td class="text-center">{ utils.FormatRibuan(int(v.BillPayment[i].F_BillPaymentAmount)) }</td>
                </tr>
            }            
        }
    }
}