<?php
    $jsonTarget = "/json.php?file=banque-lignes-".core::getGet("id");
    if(debug::isFile("debug")){
        debug::log(debug::getBadge($jsonTarget, "OUVRIR LE JSON : ".$jsonTarget), "JSON chargé en arrière plan");
    }
?>
<header class="d-flex flex-column flex-md-row align-items-md-center p-3 bg-light ">
<h2 class="bd-title" id="content">
    <span>Etat des comptes</span>
</h2>
<?php if(access::ifAccesss("compte-upload")){ ?>
<div class="fix-container-button-nav">
    <a href="/?p=compte-upload&add=<?php echo core::getGet("id") ?>"><button type="submit" class="btn btn-outline-success btn-sm"><?php icon::getFont(["icon" => "bi bi-file-earmark-plus"]) ?> Charger un CSV</button></a>
</div>
<?php } ?>
</header>
<?php   
        $banque = banque::getInitialCompte(core::getGet("id"));
        $etatCompte = banque::getEtatCompte(core::getGet("id"));

        echo core::filAriane(array(
            "current" => $banque["label"], 
            "arbo" => array( 
                "Comptes bancaires" => NULL,
                $banque["label"] => "/compte-". core::getGet("id") .".html")
        )); 

    $callout = [
        "type" => "info",
        "h4" => "Note",
        "p" => "La dernier import des données du compte a été réalisée le <span class=\"fw-bold\">" . core::convertDate(banque::lastRecord(core::getGet("id"))) . "</span> et à cette même date le solde était de <span class=\"fw-bold\">" . banque::getEuro($etatCompte["solde"]) . "</span>",
    ];
    callout::print($callout);
?>

<div>
    <table
        id="table"
        class="table-striped table-hover table-sm" 
        data-toggle="table"
        data-page-size="25"
        data-pagination="true"
        data-show-footer="true"
        data-buttons-align="left"
        data-filter-control="true"
        data-flat="true"
        data-sort-name="num"
        data-sort-order="desc"
        data-url="<?php echo $jsonTarget ?>">
        <thead>
            <tr>
                <th data-sortable="true" data-field="num" data-width="20">#</th>
                <th data-sortable="true" data-field="date" data-filter-control="input" data-width="160">Date</th>
                <th data-sortable="true" data-field="label" data-filter-control="input">Label</th>
                <th data-sortable="true" data-formatter="dataFormatter" data-field="debit" data-filter-control="input" data-width="150" data-footer-formatter="debitFormatter">Débit</th>
                <th data-sortable="true" data-formatter="dataFormatter" data-field="credit" data-filter-control="input" data-width="150" data-footer-formatter="creditFormatter">Crédit</th>
                <th data-sortable="true" data-formatter="dataFormatter" data-field="solde" data-filter-control="input" data-width="150">Solde</th>
            </tr>
        </thead>
    </table> 
</div>

<script>
    let euro = Intl.NumberFormat('de-DE', {
        style: 'currency',
        currency: 'EUR',
    });

    function dataFormatter(value) { 
        return euro.format(value);
    }

    function debitFormatter(data) {
        var total = 0;
        data.forEach(function (row) {
            total += parseFloat(row.debit);
        });
        return parseFloat(total) === 0 ? euro.format(0.00) : euro.format(total.toFixed(2));
    }

    function creditFormatter(data) {
        var total = 0;
        data.forEach(function (row) {
            total += parseFloat(row.credit);
        });
        return parseFloat(total) === 0 ? euro.format(0.00) : euro.format(total.toFixed(2));
    }

</script>