<?php
    $jsonTarget = "/json.php?jsonData=documents-my-assign";
    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>Listes des documents à traiter</span>
</h2>
</header>
<?php   
        echo core::filAriane(array(
            "current" => "Listes des documents à traiter", 
            "arbo" => array( 
                "Vos assignations" => NULL,
                "Listes des documents à traiter" => "/documents-my-assign.html")
        )); 

    $callout = [
        "type" => "success",
        "h4" => "Astuce",
        "p" => "Vous avez la possibilité de paramètrer vos alertes en <a href=\"/alertes-emails.html\">cliquant ici</a> et ainsi recevoir cette liste par email quand vous le souhaitez.",
    ];
    callout::print($callout);
?>

<div>
    <table
        id="table"
        class="table-striped table-hover table-sm" 
        data-page-size="25"
        data-toggle="table"
        data-buttons-align="left"
        data-show-footer="true"
        data-pagination="true"
        data-filter-control="true"
        data-flat="true"
        data-sort-name="date"
        data-sort-order="desc"
        data-url="<?php echo $jsonTarget ?>">
        <thead>
            <tr>
                <th data-sortable="true" data-field="date" data-filter-control="input" data-width="100">Date</th>
                <th data-sortable="true" data-field="titre" data-filter-control="input">Titre</th>
                <th data-sortable="true" data-field="label" data-filter-control="select" data-width="180">Type</th>
                <th data-sortable="true" data-field="deadline" data-filter-control="input" data-width="100">Echéance</th>
                <th data-sortable="true" data-field="tags" data-filter-control="input" data-width="200">Tags</th>
                <th data-sortable="true" data-field="assign" data-filter-control="input" data-width="200">Attribution</th>
                <th data-sortable="true" data-field="montant" data-filter-control="input" data-width="110" data-formatter="dataFormatter" data-footer-formatter="montantFormatter">Montant</th>
                <th data-field="id" data-formatter="selectFormatter" data-width="60"></th>
            </tr>
        </thead>
    </table> 
</div>

<script>
    function selectFormatter(value, row) { 
        return '<a href="/document-' + row.id + '.html"><button type="submit" class="btn btn-outline-primary btn-sm">Traiter</button></a>';
    }

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

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

    function montantFormatter(data) {
        var total = 0;
        data.forEach(function (row) {
            total += parseFloat(row.montant);
        });
        return parseFloat(total) === 0 ? euro.format(0.00) : euro.format(total.toFixed(2));
    }
    
</script>