<script lang="ts">
 import { base } from '$app/paths'
 import Banque from '../../Banque.svelte'
 import Simulation from './Simulation.svelte'
 import { page } from '$app/stores';
 import { enhance, applyAction } from '$app/forms';
 import { onMount } from 'svelte';
 import type { SubmitFunction } from '@sveltejs/kit';
 import type { Faisan } from '../../../helpers'

 export let data: {
     name: string,
     porteur: string,
     adresse: string,
     tel: string,
     id: string,
     img?: string,
     simulation: Faisan.SimulationCommunaute,
     guest?: boolean,
     prms: string,
     email?: string,
 }

 $: p = data.simulation

 let expert = $page.url.hash == "#expert"

 $: compteActive = ($page.url.hash == "#compte") ? "active": ""
 $: banqueActive = ($page.url.hash == "#banque") ? "active": ""
 $: expertActive = (expert && $page.url.hash == "#expert") ? "active": ""
 $: simuActive = ((true || !data.guest) && $page.url.hash == "#simu") ? "active": ""
 $: syntheseActive = (!compteActive && !banqueActive && !simuActive && !expertActive) ? "active": ""

 $: compteShow = compteActive ? "show": ""
 $: banqueShow = banqueActive ? "show": ""
 $: simuShow = simuActive ? "show": ""
 $: syntheseShow = syntheseActive ? "show": ""
 $: expertShow = expertActive ? "show": ""


 const N = 10
 const arr = 0

 let an = new Date().getFullYear()

 function tarifSurplus_(puissance: number): number {
     let resultat = p.tarifCRE.prix[0];
     for(let i = 0; i < p.tarifCRE.limites.length; i++) {
         if(puissance < p.tarifCRE.limites[i]) {
             return resultat
         } else {
             resultat = p.tarifCRE.prix[i]
         }
     }
     return resultat
 }

 $: investissement = p.puissance ? (p.puissance * 1000 * p.ratio + p.enedis): 0

 $: apport = Math.min(investissement, p.apport)

 $: primeInvestissement =
     (p.puissance <= 36) ? (0.21 * 1000 * p.puissance) :
     (p.puissance <= 100) ? (0.11 * 1000 * p.puissance) :
     0

 $: productionAnnuelle = (i: number) => p.productible * p.puissance * Math.pow(1-(p.degradation)/100, i)

 $: autoconso = p.autoconso / 100

 $: tarifSurplus = (i: number) => tarifSurplus_(p.puissance) * Math.pow(1+p.tarifCRE.inflation/100, i)

 $: tarifLocal = (i: number) => p.tarif * Math.pow(1+p.inflation/100, i)


 $: chiffreAffaires = (i: number) => ((i == 0) ? primeInvestissement : 0) + productionAnnuelle(i) * (autoconso * tarifLocal(i) + (1 - autoconso) * tarifSurplus(i))


 $: exploitation = (i: number) => p.puissance * p.maintenance * Math.pow(1 + p.inflation / 100, i)

 $: turpe = (i: number) => (p.puissance * p.turpe + (1-autoconso) * productionAnnuelle(i) * p.turpeInjection) * Math.pow(1+p.inflation/100, i)

 $: assurance = (i: number) => (investissement * p.assurance / 100) * Math.pow(1+p.inflation/100, i)

 $: divers = (i: number) => p.divers * p.puissance * Math.pow(1 + p.inflation/100, i)

 $: charges = (i: number) => (investissement && i == 0 ? p.fraisDivers : 0) + exploitation(i) + turpe(i) + assurance(i) + divers(i)

 $: valeurAjoutee = (i: number) => chiffreAffaires(i) - charges(i)



 // Impots
 $: ifer = (_: number) => (p.puissance < 100) ? 0: 3.394 * p.puissance * p.ratioDcAc

 // Exonération de taxe foncière: https://www.legifrance.gouv.fr/codes/article_lc/LEGIARTI000044996150
 $: taxeFonciere = (_: number) => 0

 // Formule compliquée, exonéré sur les cas concrets
 $: cfe = (_: number) => 0

 $: impots = (i: number) => ifer(i) + taxeFonciere(i) + cfe(i)

 // EBE
 $: ebe = (i: number) => valeurAjoutee(i) - impots(i)

 // Déductions fiscales
 $: amortissement = (i: number) => (i < p.dureeAmortissement) ? investissement / p.dureeAmortissement: 0

 $: provisionOnduleurs = (i: number) => p.puissance * 1000 * p.provisionOnduleurs * Math.pow(1 + p.inflation/100, i) / 10

 let remboursementAnnuel = new Array()
 let interetsAnnuels = new Array()

 $: interetsDetteSenior = (i: number) => (i < interetsAnnuels.length) ? interetsAnnuels[i] : 0

 $: interetsDSRA = (i: number) => interetsDetteSenior(i) * p.tauxDSRA / 100

 $: tresorerieDisponibleDetteSenior = (i: number) => ebe(i) - is(i)

 $: serviceDetteSenior = (i: number) => interetsDSRA(i) + interetsDetteSenior(i) + remboursementAnnuel[i]

 $: dscr = (i: number) => (i < remboursementAnnuel.length) ? tresorerieDisponibleDetteSenior(i) / serviceDetteSenior(i) : 0

 $: resultatFiscal = (i: number) => ebe(i) - amortissement(i) - provisionOnduleurs(i) - interetsDetteSenior(i) - interetsDSRA(i)

 $: is = (i: number) => (resultatFiscal(i) <= 0) ? 0: resultatFiscal(i) * p.tauxIS / 100

 $: resultatNet = (i: number) => resultatFiscal(i) - is(i)

 let resultatNetData: { id: string, data: {x:number,y:number}[]}[] = [{id:"",data:[]}]

 $: {
     resultatNetData[0] = { id: "", data: new Array(N).fill(0).map((_, i) => { return { x: an + i, y: resultatNet(i) }}) }
 }

 // Synthèse

 $: CAMoyen10 = new Array(10).fill(0).reduce((acc, _, i) => acc + chiffreAffaires(i), 0) / 10
 $: tarifLocalMoyen10 = new Array(10).fill(0).reduce((acc, _, i) => acc + tarifLocal(i), 0) / 10

 $: netMoyenFutur = (new Array(10).fill(0)).reduce((acc, _, i) => acc + resultatNet(interetsAnnuels.length + i), 0) / 10

 $: resultatNet30 = (new Array(30).fill(0)).reduce((acc, _, i) => acc + resultatNet(i), 0)
 $: rentabilite = resultatNet30 / apport

 $: CA30 = (new Array(30).fill(0)).reduce((acc, _, i) => acc + chiffreAffaires(i), 0)

 $: coutElec10 = () => {
     let cout = 0
     for(let i = 0; i < 10; i++) {
         cout += p.coutElec * Math.pow(1 + p.inflationElec / 100, i) * p.consoMoyenneFoyer
     }
     return cout
 }

 $: coutElecAuto10 = () => {
     let cout = 0
     for(let i = 0; i < 10; i++) {
         cout += tarifLocal(i) * p.consoMoyenneFoyer * p.autoprod / 100
               + p.coutElec * Math.pow(1 + p.inflationElec / 100, i) * p.consoMoyenneFoyer * (100 - p.autoprod) / 100
     }
     return cout
 }

 $: dscrMoyen = remboursementAnnuel.reduce((acc, _, i) => acc + dscr(i), 0) / remboursementAnnuel.length

 let bootstrap: any;
 onMount(async () => {
     bootstrap = await import('bootstrap')
 })

 let saveModal_: null | bootstrap.Modal = null;

 async function saveModal(e: Event) {
     console.log("saveModal", e, data.guest)
     if(!saveModal_) {
         saveModal_ = new bootstrap.Modal('#faisanSaveModal', {
             focus: true,
             keyboard: true,
         })
     }
     saveModal_?.show()
 }

 let save: SubmitFunction = async ({ formData }) => {
     console.log("enhance")
     formData.set('data', JSON.stringify(p))
     formData.set('prms', JSON.stringify(data.prms))
     saveModal_?.hide()
     return async ({ result }) => {
         await applyAction(result);
     };
 }

</script>



<div class="d-flex flex-lg-row flex-column">
    <nav class="navbar navbar-expand-lg start-0 w-lg-25">
        <button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#menuoff" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
    </nav>

    <div class="offcanvas-lg offcanvas-start mb-auto" data-bs-scroll="true" tabindex="-1" id="menuoff">
        <div class="offcanvas-body">
            <div class="p-3 p-lg-0 overflow-lg-unset overflow-scroll">
                {#if !data.guest}
                    <a href="{base}">Retour</a>
                {/if}
                <h5>Projet</h5>
                <div class="mb-3 form-check form-switch">
                    <input class="form-check-input" type="checkbox" role="switch" id="expert" bind:checked={expert}>
                    <label class="form-check-label" for="expert">Niveau expert</label>
                </div>

                <div class="mb-3">
                    <label class="form-label" for="puissance">Puissance (kWc)</label>
                    <input class="form-control form-control-sm" type="number" id="puissance" min="1" bind:value={p.puissance}/>
                </div>
                <div class="mb-3">
                    <label class="form-label" for="autoconso">Taux d'autoconsommation (%)</label>
                    <div class="d-flex">
                        <input class="form-range me-4" type="range" id="autoconso" bind:value={p.autoconso}/>{p.autoconso}%
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label" for="autoprod">Taux d'autoproduction (%)</label>
                    <div class="d-flex">
                        <input class="form-range me-4" type="range" id="autoprod" bind:value={p.autoprod}/>{p.autoprod}%
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label" for="tarif">Tarif de vente local</label>
                    <input class="form-control form-control-sm" type="number" step="any" min="0" id="tarif" bind:value={p.tarif}/>
                </div>
                <div class="mb-3">
                    <label class="form-label" for="inflationElec">Estimation de l'inflation de l'électricité</label>
                    <input class="form-control form-control-sm" type="number" bind:value={p.inflationElec} min="0" id="inflationElec" />
                </div>

                <h5>Financement</h5>
                <div class="mb-3">
                    <label class="form-label" for="apport">Apport initial</label>
                    <input class="form-control form-control-sm" type="number" id="apport" min="0" bind:value={p.apport}/>
                </div>
                <div class="mb-3">
                    <label class="form-label" for="interet">Taux d'intérêt(%)</label>
                    <input class="form-control form-control-sm" type="number" step="0.01" id="interet" min="0" bind:value={p.interet}/>
                </div>
                <div class="mb-3">
                    <label class="form-label" for="echeances">Nombre d'échéances (en mois)</label>
                    <input class="form-control form-control-sm" type="number" id="echeances" min="0" bind:value={p.echeances}/>
                </div>


                {#if data.id}
                    <form method="POST" action="?/save" use:enhance={save}>
                        <button class="btn btn-primary">Sauvegarder</button><button class="ms-2 btn btn-outline-primary" formaction="?/del">Supprimer</button>
                    </form>
                {:else if data.email}
                    <button class="btn btn-primary" on:click={saveModal}>Sauvegarder</button>
                {/if}
            </div>
        </div>
    </div>
    <div class="ps-lg-5 w-100 ms-0">
        <ul class="mt-5 mt-lg-3 nav nav-tabs d-print-none" id="tabs" role="tablist">
            <li class="nav-item" role="presentation">
                <a href="#synthese" class="nav-link {syntheseActive}" id="synthese-tab" type="button" role="tab" aria-controls="synthese-tab-pane" aria-selected="true">Synthèse</a>
            </li>
            <li class="nav-item" role="presentation">
                <a href="#compte" class="nav-link {compteActive}" id="profile-tab" type="button" role="tab" aria-controls="compte-tab-pane" aria-selected="false">Compte de résultats</a>
            </li>
            <li class="nav-item" role="presentation">
                <a href="#banque" class="nav-link {banqueActive}" id="banque-tab" type="button" role="tab" aria-controls="banque-tab-pane" aria-selected="false">Emprunt</a>
            </li>
            {#if expert}
                <li class="nav-item" role="presentation">
                    <a href="#expert" class="nav-link {expertActive}" id="expert-tab" type="button" role="tab" aria-controls="expert-tab-pane" aria-selected="false">Mode expert</a>
                </li>
            {/if}
            {#if true || !data.guest}
                <li class="nav-item" role="presentation">
                <a href="#simu" class="nav-link {simuActive}" id="prm-tab" type="button" role="tab" aria-controls="prm-tab-pane" aria-selected="false">Simulation</a>
            </li>
            {/if}
        </ul>
        <div class="tab-content" id="syntheseTabContent">
            <div class="tab-pane fade {syntheseActive} {syntheseShow} p-3" id="synthese-tab-pane" role="tabpanel" aria-labelledby="synthese-tab" tabindex="0">
                <h4 class="my-3">Projet</h4>
                <div class="ms-3 row">
                    <div>
                        <table class="table table-bordered d-inline-block">
                            <tbody>
                                <tr class="d-none d-print-table-row"><td>Puissance modules</td><td>{p.puissance}</td></tr>
                                <tr class="d-none d-print-table-row"><td>Taux d'autoconsommation</td><td>{p.autoconso}</td></tr>
                                <tr class="d-none d-print-table-row"><td>Taux d'autoproduction</td><td>{p.autoprod}</td></tr>
                                <tr><td>Production annuelle</td><td>{productionAnnuelle(0).toFixed(0)}kWh</td></tr>
                                <tr><td>Coût de la centrale</td><td>{investissement.toFixed(0)}€</td></tr>
                                <tr><td>Nombre de foyers</td><td>
                                    {(productionAnnuelle(0) * p.autoconso / (p.consoMoyenneFoyer * p.autoprod)).toFixed(1)}
                                </td></tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <h4 class="my-3">Analyse économique</h4>
                <div class="ms-3 row">
                    <div class="col-12 col-lg-6">
                        <h5 class="my-3">Financement</h5>
                        <table class="table table-bordered d-inline-block">
                            <tbody>
                                <tr class="d-none d-print-table-row"><td>Apport</td><td>{apport}€</td></tr>
                                <tr><td>Volume d'emprunt</td><td>{Math.max(0, investissement - apport).toFixed(0)}€</td></tr>
                                {#if investissement > apport}
                                    <tr class="d-none d-print-table-row"><td>Taux d'intérêt</td><td>{p.interet}€</td></tr>
                                    <tr class="d-none d-print-table-row"><td>Durée du crédit</td><td>{p.echeances} mois</td></tr>
                                    <tr><td>Annuité</td><td>{(remboursementAnnuel[0] + interetsAnnuels[0]).toFixed(arr)}€</td></tr>
                                    <tr><td>Capacité de remboursement (DSCR)</td><td>{dscrMoyen ? dscrMoyen.toFixed(2): ""}</td></tr>
                                {/if}
                            </tbody>
                        </table>
                    </div>
                    <div class="col-12 col-lg-6">
                        <h5 class="my-3">Revenus</h5>
                        <table class="table table-bordered d-inline-block">
                            <tbody>
                                <tr class="d-none d-print-table-row"><td>Tarif de vente local</td><td>{p.tarif}</td></tr>
                                <tr class="d-none d-print-table-row"><td>Estimation de l'inflation de l'électricité</td><td>{p.inflationElec}</td></tr>
                                <tr><td>Prix de vente de l'énergie moyen sur 10 ans</td><td>{(tarifLocalMoyen10).toFixed(2)}€</td></tr>
                                <tr><td>Chiffre d'affaires moyen sur 10 ans</td><td>{CAMoyen10.toFixed(arr)}€</td></tr>
                                <tr><td>Revenu annuel net moyen au-delà du prêt</td><td>{netMoyenFutur.toFixed(arr)}€</td></tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <h4 class="my-3">Intérêt financier</h4>
                <div class="ms-3 row">
                    <div class="col-12 col-lg-6">
                        <h5 class="my-3">Pour le producteur</h5>
                        <table class="table table-bordered d-inline-block">
                            <tbody>
                                {#if apport}
                                    <tr><td>ROE sur 30 ans (résultat net / apport)</td>
                                        <td>{(rentabilite * 100 / 30).toFixed(0)}% par an</td></tr>
                                {/if}
                                {#if CA30}
                                    <tr><td>ROCE</td><td>{((resultatNet30 / CA30) * 100).toFixed(0)}%</td></tr>
                                {/if}
                            </tbody>
                        </table>
                    </div>
                    <div class="col-12 col-lg-6">
                        <h5 class="my-3">Pour les consommateurs</h5>
                        <table class="table table-bordered d-inline-block">
                            <tbody>
                                <!-- <tr><td>Coût sur 10 ans sans autoconsommation collective</td><td>{coutElec10().toFixed(0)}€</td></tr>
                                     <tr><td>Coût sur 10 ans avec autoconsommation collective</td><td>{coutElecAuto10().toFixed(0)}€</td></tr> -->
                                <tr><td>Économie réalisée</td><td>{(coutElec10() - coutElecAuto10()).toFixed(0)}€ {#if coutElec10() > 0}({((coutElec10() - coutElecAuto10()) * 100 / coutElec10()).toFixed(0)}%){/if}</td></tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="tab-pane fade {compteActive} {compteShow} py-3" id="compte-tab-pane" role="tabpanel" aria-labelledby="compte-tab" tabindex="0">
                <div class="my-5 ms-3 row d-inline-size">
                    <table class="table table-bordered d-inline-block" style="font-size:calc(min(18cqw/{N}, 1em))">
                        <tbody>
                            <tr><td>Année</td>{#each Array(N) as _, i}<td>{an + i}</td>{/each}</tr>
                            <tr><td>Résultat net</td>
                                {#each Array(N) as _, i}
                                    <td>{resultatNet(i).toFixed(arr)}</td>
                                {/each}
                            </tr>
                        </tbody>
                    </table>
                </div>

                <h4 class="my-3">Détail</h4>

                <div class="ms-3 row">
                    <div>
                        <h5 class="mt-5">Chiffre d'affaires</h5>
                        <table class="table table-bordered d-inline-block" style="font-size:calc(min(16cqw/{N}, 1em))">
                            <!-- <thead>
                                 <tr>
                                 <th></th>
                                 <th></th>
                                 </tr>
                                 </thead> -->
                            <tbody>
                                <tr><td>Année</td>{#each Array(N) as _, i}<td>{an + i}</td>{/each}</tr>
                                <tr><td>Vente locale (€)</td>
                                    {#each Array(N) as _, i}
                                        <td>{(productionAnnuelle(i) * (autoconso * tarifLocal(i))).toFixed(0)}</td>
                                    {/each}
                                </tr>
                                <tr><td>Vente de surplus (€)</td>
                                    {#each Array(N) as _, i}
                                        <td>{(productionAnnuelle(i) * ((1 - autoconso) * tarifSurplus(i))).toFixed(0)}</td>
                                    {/each}
                                </tr>
                                <tr><td>Prime</td>
                                    <td>{primeInvestissement}</td>
                                    {#each Array(N-1) as _}
                                        <td></td>
                                    {/each}
                                </tr>
                            </tbody>
                            <tfoot class="table-group-divider">
                                <tr><td>Chiffre d'affaires</td>
                                    {#each Array(N) as _, i}
                                        <td>{chiffreAffaires(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                            </tfoot>
                        </table>
                        <h5 class="my-3">Charges d'exploitation</h5>
                        <table class="table table-bordered d-inline-block" style="font-size:calc(min(17cqw/{N}, 1em))">
                            <tbody>
                                <tr><td>Année</td>{#each Array(N) as _, i}<td>{an + i}</td>{/each}</tr>
                                <tr><td>Maintenance</td>
                                    {#each Array(N) as _, i}
                                        <td>{exploitation(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                                <tr><td>TURPE</td>
                                    {#each Array(N) as _, i}
                                        <td>{turpe(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                                <tr><td>Assurance</td>
                                    {#each Array(N) as _, i}
                                        <td>{assurance(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                                <tr><td>Divers</td>
                                    {#each Array(N) as _, i}
                                        <td>{divers(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                            </tbody>
                            <tfoot class="table-group-divider">
                                <tr><td>Total charges</td>
                                    {#each Array(N) as _, i}
                                        <td>{charges(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                            </tfoot>
                        </table>
                        <h5 class="my-3">Excédent brut d'exploitation</h5>
                        <table class="table table-bordered d-inline-block" style="font-size:calc(min(18cqw/{N}, 1em))">
                            <tbody>
                                <tr><td>Année</td>{#each Array(N) as _, i}<td>{an + i}</td>{/each}</tr>
                                <tr><td>Valeur ajoutée</td>
                                    {#each Array(N) as _, i}
                                        <td>{valeurAjoutee(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                                <tr><td>IFER</td>
                                    {#each Array(N) as _, i}
                                        <td>{ifer(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                                <tr><td>Taxe foncière</td>
                                    {#each Array(N) as _, i}
                                        <td>{taxeFonciere(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                                <tr><td>Cotisation foncière des entreprises</td>
                                    {#each Array(N) as _, i}
                                        <td>{cfe(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                            </tbody>
                            <tfoot class="table-group-divider">
                                <tr><td>Excédent brut d'exploitation</td>
                                    {#each Array(N) as _, i}
                                        <td>{ebe(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                            </tfoot>
                        </table>

                        <h5 class="my-3">Résultat net</h5>
                        <table class="table table-bordered d-inline-block" style="font-size:calc(min(18cqw/{N}, 1em))">
                            <tbody>
                                <tr><td>Année</td>{#each Array(N) as _, i}<td>{an + i}</td>{/each}</tr>
                                <tr><td>Onduleurs</td>
                                    {#each Array(N) as _, i}
                                        <td>{provisionOnduleurs(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                                <tr><td>Amortissement</td>
                                    {#each Array(N) as _, i}
                                        <td>{amortissement(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                                {#if investissement > apport}
                                <tr><td>Intérêts dette senior</td>
                                    {#each Array(N) as _, i}
                                        <td>{(interetsDetteSenior(i)).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                                <tr><td>Intérêts DSRA</td>
                                    {#each Array(N) as _, i}
                                        <td>{interetsDSRA(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                                {/if}
                                <tr><td>Résultat fiscal</td>
                                    {#each Array(N) as _, i}
                                        <td>{resultatFiscal(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                                <tr><td>Impôt sur les sociétés</td>
                                    {#each Array(N) as _, i}
                                        <td>{is(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                            </tbody>
                            <tfoot class="table-group-divider">
                                <tr><td>Résultat net</td>
                                    {#each Array(N) as _, i}
                                        <td>{resultatNet(i).toFixed(arr)}</td>
                                    {/each}
                                </tr>
                            </tfoot>
                        </table>
                        {#if investissement > apport }
                            <h5 class="my-3">Couverture de la dette</h5>
                            <table class="table table-bordered d-inline-block" style="font-size:calc(min(18cqw/{N}, 1em))">
                                <tbody>
                                    <tr><td>Année</td>{#each Array(N) as _, i}<td>{an + i}</td>{/each}</tr>
                                    <tr><td>DSCR</td>
                                        {#each Array(N) as _, i}
                                            <td>{dscr(i).toFixed(2)}</td>
                                        {/each}
                                    </tr>
                                </tbody>
                            </table>
                        {/if}
                    </div>
                </div>
            </div>
            <div class="tab-pane fade {banqueActive} {banqueShow} py-3" id="banque-tab-pane" role="tabpanel" aria-labelledby="banque-tab" tabindex="0">
                <Banque dette = {Math.max(0, investissement - apport)} taux={p.interet / 100} duree={p.echeances} bind:interetsAnnuels={interetsAnnuels} bind:remboursementAnnuel={remboursementAnnuel} />
            </div>
            {#if expert}
                <div class="tab-pane fade {expertActive} {expertShow} py-3" id="expert-tab-pane" role="tabpanel" aria-labelledby="expert-tab" tabindex="0">
                    <div style="max-width:300px">
                        <div class="mb-3">
                            <label class="form-label" for="enedis">Coût de raccordement Enedis</label>
                            <input class="form-control form-control-sm" type="number" id="enedis" min="0" bind:value={p.enedis}/>
                        </div>
                        <div class="mb-3">
                            <label class="form-label" for="productible">Productible annuel (kWh/kWc)</label>
                            <input class="form-control form-control-sm" type="number" id="productible" min="0" bind:value={p.productible}/>
                        </div>
                        <div class="mb-3">
                            <label class="form-label" for="ratio">Ratio installation (€/W)</label>
                            <input class="form-control form-control-sm" type="number" id="ratio" min="0" bind:value={p.ratio}/>
                        </div>
                        <div class="mb-3">
                            <label class="form-label" for="degradation">Taux de dégradation PV(%)</label>
                            <input class="form-control form-control-sm" type="number" id="degradation" min="0" bind:value={p.degradation}/>
                        </div>
                        <div class="mb-3">
                            <label class="form-label" for="inflation">Inflation tarif local</label>
                            <input class="form-control form-control-sm" type="number" id="inflation" min="0" bind:value={p.inflation}/>
                        </div>
                    </div>
                </div>
            {/if}

        </div>
        {#if true || !data.guest}
            <div class="tab-content" id="simuTabContent">
                <div class="tab-pane fade {simuActive} {simuShow} show p-3" id="simu-tab-pane" role="tabpanel" aria-labelledby="simu-tab" tabindex="0">
                    <h4 class="my-3">Simulation</h4>

                    <Simulation active={simuActive} bind:prms={data.prms} puissance={p.puissance}/>
                </div>
            </div>
        {/if}
    </div>
</div>

<div class="modal" id="faisanSaveModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            {#if !data.guest}
                <div class="modal-body">
                    <h5 class="modal-title pb-3" id="saveModalLabel">Sauvegarder</h5>
                    <form method="POST" action="?/save" use:enhance={save}>
                        <div class="mb-3">
                            <label class="form-label" for="nom">Nom du projet</label>
                            <input class="form-control" type="text" id="nom" name="name" bind:value={data.name} required/>
                        </div>
                        <div class="mb-3">
                            <label class="form-label" for="porteur">Prénom et nom du porteur du projet</label>
                            <input class="form-control" type="text" id="porteur" name="porteur" bind:value={data.porteur}/>
                        </div>
                        <div class="mb-3">
                            <label class="form-label" for="adresse">Adresse</label>
                            <input class="form-control" type="text" id="adresse" name="adresse" bind:value={data.adresse}/>
                        </div>
                        <div class="mb-3">
                            <label class="form-label" for="tel">Téléphone</label>
                            <input class="form-control" type="text" id="tel" name="tel" bind:value={data.tel}/>
                        </div>
                        <div class="mb-3">
                            <label class="form-label" for="photo">Photo</label>
                            <input class="form-control" type="file" id="photo" name="photo"/>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annuler</button>
                    <button class="btn btn-primary">Sauvegarder</button>
                </div>
            {/if}
        </div>
    </div>
</div>

<style lang="scss">
 @media (prefers-color-scheme: light) {
     nav {
         background-color: #fff !important;
     }
 }
</style>