<?php
if (isset($_COOKIE['NAS_USER']) && isset($_COOKIE['NAS_SID'])) {
    $context = stream_context_create(array('ssl'=>array(
    'verify_peer' => false, 
    "verify_peer_name"=>false
)));
    libxml_set_streams_context($context);
    $url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://127.0.0.1:$_SERVER[SERVER_PORT]/cgi-bin/authLogin.cgi?sid=".$_COOKIE['NAS_SID'];
    $xml = simplexml_load_file($url);
    unset($context);
    if ( (false === $xml) || !isset($xml->authPassed) || !isset($xml->username) || !isset($xml->isAdmin) ) {
        die('Unable to retrieve xml authentication info from your qnap device.');
    }
    if ( !(bool)(int)$xml->authPassed[0] || !(bool)(int)$xml->isAdmin[0] || (string)$xml->username[0] !== $_COOKIE['NAS_USER']) {
        die('No authentic session id of an admin user!');
    }
} else {
    die('Not logged in!');
}

include_once("/home/httpd/cgi-bin/qpkg/RoonServer/__include.php");
include_once("/home/httpd/cgi-bin/qpkg/RoonServer/__functions.php");
?>

<div class="container">
    <div class="roon-template">
        <h1><?php echo localize("OVERVIEW_HEADLINE"); ?></h1>
        <p class="lead"><?php echo localize("OVERVIEW_TEXT1"); ?><br>
            <?php echo localize("OVERVIEW_TEXT2"); ?></p>
        <div class="row">
            <div class="col-sm-6">
                <div class="card">
                    <div class="card-body">
                        <span class="fa-stack fa-2x">
                            <i class="fa fa-circle fa-stack-2x" style="color: #222222;"></i>
                            <img src="img/roonIcon.svg" alt="Roon Icon" style="height: 35px;" class="fa-stack-1x fa-inverse">
                        </span>
                        <h5 class="card-title">Roon Server</h5>
                        <p class="card-text">
                            <b><?php echo localize("OVERVIEW_ROONSERVER_PANEL_STATUS"); ?>
                                :</b> <?php if (strlen(isRunning()) > 1 ) {
                                echo '<span data-toggle="tooltip" title="' . localize("OVERVIEW_ROONSERVER_PANEL_CONTAINER_ID") .': ' . isRunning() . '" style="color: green;">' . localize("OVERVIEW_ROONSERVER_PANEL_STATUS_RUNNING") . '</span>';
                            } else {
                                echo '<span style="color: red;">' . localize("OVERVIEW_ROONSERVER_PANEL_STATUS_STOPPED") . '</span>';
                            } ?><br>
                            <b><?php echo localize("OVERVIEW_ROONSERVER_PANEL_VERSION"); ?>
                                :</b> <?php echo $RoonVersion[1]; ?><br>
                            <b><?php echo localize("OVERVIEW_ROONSERVER_PANEL_QPKG_VERSION"); ?>
                                :</b> <?php echo $qpkg_conf['RoonServer']['Version']; ?></span>
                        </p>
                            <h5><?php echo localize("OVERVIEW_ROONSERVER_PANEL_SUBHEAD_DATABASE"); ?></h5>
                            <span data-toggle="tooltip" title="<?php echo $dblocation; ?>">
                                <b><?php echo localize("OVERVIEW_ROONSERVER_PANEL_LOCATION"); ?>: </b><?php echo $dblocation; ?>
                            </span>
                            <div class="progress" style="height: 20px;">
                                <div class="progress-bar" role="progressbar" style="width: <?php echo $db_perc ?>%;" aria-valuenow="<?php echo(100 - $db_perc) ?>" aria-valuemin="0" aria-valuemax="100"></div>
                            </div>
                            <p>
                            <?php echo $db_perc . '% ' . localize("OVERVIEW_ROONSERVER_PANEL_SPACE_OF") . ' ' . displayStorage($db_vol_cap) . ' ' . localize("OVERVIEW_ROONSERVER_PANEL_SPACE_USED") . '.'; ?>
                        </p>
                        <span id="log" class="getModal">
                            <a href="#"
                               class="btn btn-light btn-icon float-left"
                               data-toggle="tooltip"
                               title="<?php echo localize("MODAL_LOGFILES_ICON_TOOLTIP"); ?>">
                                <i class="fas fa-ambulance"></i>
                            </a>
                        </span>
                        <span id="setStorage" class="getModal">
                            <a href="#"
                               class="btn btn-primary float-right"
                               data-toggle="tooltip"
                               title="<?php echo localize("OVERVIEW_ROONSERVER_PANEL_CHANGE_DB_LOCATION_TOOLTIP"); ?>">
                                <?php echo localize("OVERVIEW_ROONSERVER_PANEL_CHANGE_DB_LOCATION"); ?>
                            </a>
                        </span>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="card">
                    <div class="card-body">
                        <!-- USB Audio -->
                        <div class="py-3 border-b border-surface-500/30">
                          <div class="flex items-start gap-3">
                            <label class="relative inline-flex items-center cursor-pointer mt-0.5 flex-shrink-0">
                              <input type="checkbox" id="opt-usb-audio" class="sr-only peer">
                              <div class="toggle-track"></div>
                            </label>
                            <div class="flex-1 min-w-0">
                              <label class="text-sm font-medium cursor-pointer" for="opt-usb-audio" data-toggle="tooltip" title="Pass through USB and ALSA devices for local audio output to a USB DAC connected to the host. Not needed if streaming to network endpoints.">USB audio (DAC)</label>
                            </div>
                          </div>
                        </div>
                        <!-- HDMI Audio -->
                        <div class="py-3">
                          <div class="flex items-start gap-3">
                            <label class="relative inline-flex items-center cursor-pointer mt-0.5 flex-shrink-0">
                              <input type="checkbox" id="opt-hdmi-audio" class="sr-only peer">
                              <div class="toggle-track"></div>
                            </label>
                            <div class="flex-1 min-w-0">
                              <label class="text-sm font-medium cursor-pointer" for="opt-hdmi-audio" data-toggle="tooltip" title="Pass through GPU and ALSA devices for HDMI audio output. Requires GPU drivers on the host.">HDMI audio</label>
                            </div>
                          </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    var dbexist = <?php if (isset($dblocation)) {
        echo "true";
    } else {
        echo "false";
    } ?>;

    // Enable Tooltips
    $(function () {
        $('[data-toggle="tooltip"]').tooltip()
    });

    // Action when button for Modal is clicked
    $('.getModal').on('click', function (e) {

        // Hide Tooltips when modal opens
        $(function () {
            $('[data-toggle="tooltip"]').tooltip('dispose')
        });

        // Check which button fired
        e.preventDefault();
        var modalContent = $(this).attr('id');
        $('#modal-content').load("modals.php?s=" + modalContent);

        // Request Modal with content
        $('#modal').modal('show');
        return false;
    });
</script>