<?php
    require_once '../autoload.php';
    require_once 'JWT.php';
    use \Firebase\JWT\JWT;

    error_log ( "=== OnlyOfficeEditor =================================");

    error_log ( "ID : " .$_SESSION['id'] ); 
    error_log ( "username : " .$_SESSION['username'] ); 
    error_log ( "lastname : " .$_SESSION['lastname'] ); 
    error_log ( "firstname : " .$_SESSION['firstname'] ); 
    error_log ( "host : " .$_SERVER['HTTP_HOST'] ); 
    error_log ( "uri : " .$_SERVER['REQUEST_URI'] ); 
    error_log ( "addr : " .$_SERVER['SERVER_ADDR'] ); 
    error_log ( "remote : " .$_SERVER['REMOTE_ADDR'] );
    error_log ( "app_root : " . $config_approot );



    // Get document to open
    
    // $doc_id = $_GET['doc_id'];

    $filename = $_GET['filename'];

    $fileToOpen = "";
    $modification_date = "20250718091348";

    
    $serverUrl  = $GLOBALS["onlyoffice_server"];    // OnlyOffice Document Server

    $fullname = $_SESSION['firstname'] . " " . $_SESSION['lastname'];
    $id = $_SESSION['id'];
    
    $extension = pathinfo($filename, PATHINFO_EXTENSION);

    // $f = $GLOBALS["onlyoffice_temp_folder"] . "/".$filename;

    $docUrl = $GLOBALS['eodacs_ip'].$config_approot .'/eoDACS/tmp/'.$filename;

    error_log ( " Open file : " .$docUrl );

    $key = base64_encode ( $filename.'%'.$modification_date );
    
    $jwt_secret = $GLOBALS['jwt_secret'];

  
    $config = [
        "document" => [
            "fileType" => $extension,
            "key" => $key,
            "title" => "Export CSV",
            "url" => $docUrl
        ],
        "editorConfig" => [
            "mode" => "edit",
            "lang" => "fr",
            "callbackUrl" => $GLOBALS['eodacs_ip'] . $config_approot ."/eoDACS/SubFW/html/OnlyOfficeSaveCB.php",
            "user" => [
                "id" => $id,
                "name" => $fullname
            ],
            "customization" => [
                "forcesave" => true,
                "saveAs" => true
            ]
        ],
        "events" => [
                     "onRequestSaveAs" => "function (event) {}"
                 ],
        "permissions" => [
            "edit" => true,
            "download" => true
        ]
    ];

    // Create token
    $token = JWT::encode($config, $jwt_secret, 'HS256');

    $config["token"] = $token;
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>ONLYOFFICE Editor</title>
        <script src="<?= $serverUrl ?>/web-apps/apps/api/documents/api.js"></script>
        <style>
            html, body {
                margin: 0;
                padding: 0;
                height: 100%;
            }

            #placeholder {
                width: 100%;
                height: 100vh; /* pleine hauteur écran */
            }
            
            #btnFermer {
                position: fixed;
                bottom: 30px;
                right: 45px;
                background-color: rgba(47, 131, 176, 1.0 );
                color: white;
                border: none;
                padding: 6px 14px;
                border-radius: 10px;
                font-size: 14px;
                box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
                cursor: pointer;
                z-index: 1000;
            }
            
        </style>
    </head>

    <body>
    
        <div id="placeholder" style="width: 100%; height: 800px; min-height: 800px;"></div>

        <button id="btnFermer" onclick="CloseOOEditor()">✖ Fermer</button>

        <script>
            const docEditor = new DocsAPI.DocEditor("placeholder", <?= json_encode($config, JSON_UNESCAPED_SLASHES) ?>);
        
            function CloseOOEditor () 
            {
                window.parent.postMessage({ action: "CloseOOEditor" }, "*");
                docEditor.destroyEditor();
            }
        </script>

    </body>

</html>
