<?php
if ($lastExcelForSFTP = salaries::lastExcelForSFTP()) {
    $getExcelJsonForSFTP = salaries::getExcelJsonForSFTP($lastExcelForSFTP)["md5forSFTP"];
} else {
    $getExcelJsonForSFTP = "";
}
json::create("host");

    $jsonTarget1 = "/json.php?jsonData=proweb-transfert-sftp";
    $jsonTarget2 = "/json.php?jsonData=proweb-transfert-local";
    if(debug::isFile("debug")){
        debug::log(debug::getBadge($jsonTarget1, "OUVRIR LE JSON : ".$jsonTarget1), "JSON chargé en arrière plan");
        debug::log(debug::getBadge($jsonTarget2, "OUVRIR LE JSON : ".$jsonTarget2), "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>Proweb : Exporter vers ProWeb</span>
    </h2>
</header>
<?php
echo core::filAriane(array(
    "current" => "Exporter vers ProWeb",
    "arbo" => array(
        "Proweb" => NULL,
        "Exporter vers ProWeb" => "/proweb-export-csv.html"
    )
));
?>
<br />
<div class="card text-center">
    <div class="card-header" style="font-size: 1.3em;">
        Informations liées à ProWeb
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col">
                <label style="color: gray;">Hôte</label>
                <input type="text" value="<?php echo SFTP_HOST ?>" class="form-control text-center" disabled>
            </div>
            <div class="col">
                <label style="color: gray;">Identifiant</label>
                <input type="text" value="<?php echo SFTP_USER ?>" class="form-control text-center" disabled>
            </div>
            <div class="col">
                <label style="color: gray;">Mot de passe</label>
                <input type="text" value="<?php echo SFTP_PASS ?>" style="color:#e9ecef;" class="form-control text-center" disabled>
            </div>
        </div>
        <br />
        <div class="row">
            <div class="col">
                <label style="color: gray;">Dossier distant</label>
                <input type="text" value="<?php echo SFTP_REMOTE ?>" class="form-control text-center" disabled>
            </div>
            <div class="col">
                <label style="color: gray;">Protocole</label>
                <input type="text" value="SFTP (PORT 22)" class="form-control text-center" disabled>
            </div>
            <div class="col">
                <label style="color: gray;">Test de connexion</label>
                <input type="text" id="testConnexionSFTP" value="" class="form-control text-center" disabled>
            </div>
        </div>
        <br />
        <div class="row">
            <div class="col m-3">
                <label style="color: gray;">Fichiers présents sur le serveur</label>

                <table id="table" class="table-striped table-hover table-sm" data-toggle="table" data-sort-name="date" data-sort-order="desc" data-url="<?php echo $jsonTarget1 ?>">
                    <thead>
                        <tr>
                            <th data-formatter="downloadFileRemote" data-sortable="true" data-field="file" data-filter-control="input" data-align="left">Nom</th>
                            <th data-sortable="true" data-field="size" data-filter-control="input" data-width="100">Taille</th>
                            <th data-sortable="true" data-field="date" data-filter-control="input" data-width="170">Date de transfert</th>
                            <th data-formatter="deleteFileRemote" data-width="150"></th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</div>

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

    function deleteFileRemote(value, row) {
        return '<form method="post" action="/submit.php">\n\
        <input type="hidden" name="from" value="sftp-delete-file-remote">\n\
        <input type="hidden" name="file" value="' + row.file + '">\n\
        <button type="submit" class="btn btn-outline-danger btn-sm" onclick="return confirm(\'Voulez-vous vraiment supprimer ce fichier ?\')">Supprimer</button>\n\
        </form>';
    }
</script>
<br />
<div class="card text-center">
    <div class="card-header" style="font-size: 1.3em;">
        Informations liées au serveur du CSE
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col">
                <label style="color: gray;">IP du serveur</label>
                <input type="text" value="<?php echo $_SERVER['SERVER_ADDR'] ?>" class="form-control text-center" disabled>
            </div>
            <div class="col">
                <label style="color: gray;">Dernière emprunte des données générées</label>
                <input type="text" value="<?php echo $getExcelJsonForSFTP ?>" class="form-control text-center" disabled>
            </div>
        </div>
        <div class="row">
            <div class="col m-3">
                <label style="color: gray;">Fichiers présents dans le dossier du CSE</label>

                <table id="table" class="table-striped table-hover table-sm" data-toggle="table" data-sort-name="date" data-sort-order="desc" data-url="<?php echo $jsonTarget2 ?>">
                    <thead>
                        <tr>
                            <th data-formatter="downloadFileLocal" data-sortable="true" data-field="file" data-filter-control="input" data-align="left">Nom</th>
                            <th data-sortable="true" data-field="size" data-filter-control="input" data-width="100">Taille</th>
                            <th data-sortable="true" data-field="date" data-filter-control="input" data-width="170">Date de création</th>
                            <th data-formatter="transfertFileLocal" data-width="130"></th>
                            <th data-formatter="deleteFileLocal" data-width="130"></th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</div>

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

    function transfertFileLocal(value, row) {
        return '<form method="post" action="/submit.php">\n\
        <input type="hidden" name="from" value="sftp-transfert-file-local">\n\
        <input type="hidden" name="file" value="' + row.file + '">\n\
        <button type="submit" class="btn btn-outline-success btn-sm">Transférer</button>\n\
        </form>';
    }

    function deleteFileLocal(value, row) {
        return '<form method="post" action="/submit.php">\n\
        <input type="hidden" name="from" value="sftp-delete-file-local">\n\
        <input type="hidden" name="file" value="' + row.file + '">\n\
        <button type="submit" class="btn btn-outline-danger btn-sm">Supprimer</button>\n\
        </form>';
    }
</script>

<?php
if (salaries::ifSubmitLastForSFTP()) {
?>
    <br />
    <form method="post" action="/submit.php">
        <input type="hidden" name="from" value="sftp-create-csv-salaries">
        <input class="btn btn-primary btn-lg" style="width: 100%" type="submit" value="Générer le CSV à envoyer à ProWeb">
    </form>
<?php
}
?>

<script>
    $(document).ready(function() {
        $.ajax('/json.php', {
            type: 'GET', // http method
            data: {
                jsonData: 'proweb-transfert-test-connexion'
            }, // data to submit
            success: function(data, status, xhr) {
                var json = $.parseJSON(data);
                if (json == "OK") {
                    $('#testConnexionSFTP').val("Opérationnel");
                    $('#testConnexionSFTP').addClass("is-valid");
                    $('#testConnexionSFTP').css("background-color", "#d4edda");
                    $('#testConnexionSFTP').css("color", "green");
                } else {
                    $('#testConnexionSFTP').val("En erreur");
                    $('#testConnexionSFTP').addClass("is-invalid");
                    $('#testConnexionSFTP').css("background-color", "#f8d7da");
                    $('#testConnexionSFTP').css("color", "red");
                }
            },
            error: function() {
                $('#testConnexionSFTP').val("En erreur");
                $('#testConnexionSFTP').addClass("is-invalid");
                $('#testConnexionSFTP').css("background-color", "#f8d7da");
                $('#testConnexionSFTP').css("color", "red");
            }
        });
    });
</script>