<script lang="ts">
    import { ppmt } from 'financial'

    // V3
    export let dette: number

    // V4
    export let taux: number

    // Z8
    export let duree: number

    // Z7
    export let dureeDifferee = 1

    // V8
    export let nombrePeriodesParAn = 12

    let prorataPeriodique = 1 / nombrePeriodesParAn

    // V6
    let dureeAns = duree / nombrePeriodesParAn
    let dureeAns_ = Math.ceil(dureeAns)

    let taux_ = Math.min(taux, 1)

    export let remboursement = new Array(duree + 1)
    export let remboursementAnnuel = new Array(dureeAns_)
    export let crd = new Array(duree + 1)
    export let crdAnnuel = new Array(dureeAns_)
    export let interets = new Array(duree + 1)
    export let interetsAnnuels = new Array(dureeAns_)

    $: {
        dureeAns = duree / nombrePeriodesParAn
        dureeAns_ = Math.ceil(dureeAns)
        prorataPeriodique = 1 / nombrePeriodesParAn

        // V10
        let nombreEcheancesPeriodeTronquee = dureeAns * nombrePeriodesParAn

        // V9
        let nombreEcheances = Math.ceil(nombreEcheancesPeriodeTronquee)

        // W11
        let totalPeriodes = dureeAns * nombrePeriodesParAn - dureeDifferee

        // X11
        let periodeRemboursementCapital = dureeDifferee

        remboursement = new Array(duree + 1)
        crd = new Array(duree + 1)
        interets = new Array(duree + 1)
        remboursementAnnuel = new Array(dureeAns_)
        crdAnnuel = new Array(dureeAns_)
        interetsAnnuels = new Array(dureeAns_)

        crd[0] = dette
        remboursement[0] = 0
        interets[0] = 0
        crdAnnuel[0] = dette
        remboursementAnnuel[0] = 0
        interetsAnnuels[0] = 0
        for (let i = 1; i <= duree; i++) {
            if (i <= periodeRemboursementCapital) {
                remboursement[i] = 0
            } else if (i < nombreEcheancesPeriodeTronquee) {
                remboursement[i] = ppmt(
                    taux_ * prorataPeriodique,
                    i - periodeRemboursementCapital,
                    totalPeriodes,
                    -dette
                )
            } else if (i == nombreEcheances) {
                remboursement[i] = crd[i - 1]
            } else {
                remboursement[i] = 0
            }
            crd[i] = crd[i - 1] - remboursement[i]
            interets[i] = crd[i - 1] * taux_ * prorataPeriodique
            if (i % nombrePeriodesParAn == 0) {
                crdAnnuel[i / nombrePeriodesParAn] = crd[i]
                remboursementAnnuel[i / nombrePeriodesParAn] = 0
                interetsAnnuels[i / nombrePeriodesParAn] = 0
            }
            remboursementAnnuel[Math.floor(i / nombrePeriodesParAn)] +=
                remboursement[i]
            interetsAnnuels[Math.floor(i / nombrePeriodesParAn)] += interets[i]
        }
    }

    let annuel = true
</script>

<div class="form-check form-switch">
    <input
        class="form-check-input"
        type="checkbox"
        role="switch"
        id="annuel"
        bind:checked={annuel} />
    <label class="form-check-label" for="annuel">Présentation annuelle</label>
</div>

{#if annuel}
    <div class="my-3">
        <table class="table table-striped border-0 d-inline-block banque">
            <thead>
                <tr>
                    <th>Année</th>
                    <th>Capital restant dû</th>
                    <th>Principal</th>
                    <th>Intérêts</th>
                </tr>
            </thead>
            <tbody>
                {#each remboursementAnnuel as r, periode}
                    <tr>
                        <td>{periode}</td>
                        <td>{crdAnnuel[periode].toFixed(0)}</td>
                        <td>{r.toFixed(0)}</td>
                        <td>{interetsAnnuels[periode].toFixed(0)}</td>
                    </tr>
                {/each}
            </tbody>
        </table>
    </div>
{:else}
    <div class="my-3">
        <table class="table table-striped border-0 d-inline-block">
            <thead>
                <tr>
                    <th>Mois</th>
                    <th>Capital restant dû</th>
                    <th>Principal</th>
                    <th>Intérêts</th>
                </tr>
            </thead>
            <tbody>
                {#each remboursement as r, periode}
                    <tr>
                        <td>{periode}</td>
                        <td>{crd[periode].toFixed(0)}</td>
                        <td>{r.toFixed(0)}</td>
                        <td
                            >{#if periode > 0}
                                {interets[periode].toFixed(0)}
                            {/if}
                        </td>
                    </tr>
                {/each}
            </tbody>
        </table>
    </div>
{/if}

<style lang="scss">
    table {
        font-size: 90%;
    }
    th {
        font-size: 1rem;
        font-weight: bold;
    }
    td {
        padding: 5px 5px;
        padding: 5px 7px 5px 10px;
        text-align: end;
        border: none;
    }
</style>