<?php
    $inProgress = salaries::excelGetInProgress();
    json::create("excel");

    $jsonTarget = "/json.php?file=excel";
    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>RH : Historique des Excels</span>
    </h2>
    <div class="fix-container-button-nav">
        <a href="/rh-upload-excel.html" class="fix-button-nav"><button type="submit" class="btn btn-outline-success btn-sm"><?php icon::getFont(["icon" => "bi bi-file-earmark-plus"]) ?> Importer un fichier Excel</button></a>
    </div>
</header>
<?php   
        echo core::filAriane(array(
            "current" => "Historique des Excels", 
            "arbo" => array( 
                "Salariés" => NULL,
                "Historique des Excels" => "/rh-historique-excel.html")
        )); 
?>
<div>
    <table
        id="table"
        class="table-striped table-hover table-sm" 
        data-page-size="25"
        data-toggle="table"
        data-show-columns="true"
        data-search="true"
        data-buttons-align="left"
        data-pagination="true"
        data-filter-control="true"
        data-flat="true"
        data-sort-name="cree"
        data-sort-order="desc"
        data-url="<?php echo $jsonTarget ?>">
        <thead>
            <tr>
                <th data-sortable="true" data-field="cree" data-filter-control="input" data-width="160">Charger le</th>
                <th data-formatter="downloadFileLocal" data-sortable="true" data-field="name" data-filter-control="input">Fichier</th>
                <th data-sortable="true" data-field="dateData" data-filter-control="input">Datation des données</th>
                <th data-sortable="true" data-field="nbSalaries" data-filter-control="input" data-width="60">Nb. Salariés</th>
                <th data-sortable="true" data-field="size" data-width-unit="Ko" data-width="40">Poids</th>
                <th data-sortable="true" data-field="user" data-filter-control="select" data-width="180">Chargé par</th>
                <th data-sortable="true" data-field="md5forSFTP" data-filter-control="input" data-width="280">Emprunte ProWeb</th>
                <th data-field="id" data-width="200" data-formatter="selectFormatter"></th>
            </tr>
        </thead>
    </table> 
</div>

<?php if(empty($inProgress["name"])){ ?>

    <script>
        function selectFormatter(value, row) { 
            if(row.goMysql == null ){
                $return = '<form style="float: left;" method="post" action="/submit.php"  onsubmit="loading()">\n\
                            <input type="hidden" name="from" value="rh-excel-to-temp">\n\
                            <input type="hidden" name="excel" value="' + value + '">\n\
                            <button type="submit" class="btn btn-outline-primary btn-sm">Traiter</button>\n\
                        </form>';
                $return += '<form method="post" action="/submit.php">\n\
                        <input type="hidden" name="from" value="rh-annule-import">\n\
                        <input type="hidden" name="id" value="' + row.id + '">\n\
                        <button type="submit" style="margin-left:10px;" class="btn btn-outline-danger btn-sm" title="Annuler l\'import">Annuler</button>\n\
                    </form>';
                return $return;
            } else {
                return '<button class="btn btn-light btn-sm" disabled>Fichier déjà traiter</button>'; 
            }
        }
    </script>

<?php } else { ?>

    <script>
        function selectFormatter(value, row) { 
            if(row.id == <?php echo $inProgress["id"] ?>) {
                $return =  '<form style="float: left;" method="get" action="/">\n\
                                <input type="hidden" name="p" value="rh-import-to-temp">\n\
                                <button type="submit" class="btn btn-outline-primary btn-sm">Reprendre</button>\n\
                            </form>';
                $return += '<form method="post" action="/submit.php">\n\
                                <input type="hidden" name="from" value="rh-annule-import">\n\
                                <input type="hidden" name="id" value="' + row.id + '">\n\
                                <button type="submit" style="margin-left:10px;" class="btn btn-outline-danger btn-sm" title="Annuler l\'import">Annuler</button>\n\
                            </form>';
                return $return;
            } else if(row.goMysql == null ){
                return '<button class="btn btn-outline-primary btn-sm" disabled>Traiter</button>';
            } else {
                return '<button class="btn btn-light btn-sm" disabled>Fichier déjà traiter</button>'; 
            }
        }
    </script>

<?php } ?>
    
<script>
    function downloadFileLocal(value, row) { 
        return '<form method="post" action="/submit.php">\n\
                    <input type="hidden" name="from" value="file-download">\n\
                    <input type="hidden" name="file" value="' + row.name + '">\n\
                    <input type="hidden" name="md5" value="' + row.md5 + '">\n\
                    <button type="submit" style="border:none; background:none;" title="Télécharger le fichier : ' + row.name + '">' + row.name + '</button>\n\
                </form>'; 
    }
</script>
<br />
<br />