<?php
Yii::setPathOfAlias('widgets', Yii::getPathOfAlias('application.extensions.widgets'));
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="language" content="en" />
        <link rel="shortcut icon" href="<?php echo Yii::app()->request->baseUrl; ?>/favicon.png" />
        <title><?php echo cfg('PHONALISA_NAME') . ' ' . CHtml::encode($this->pageTitle); ?></title>
        <?php
        cs()->registerCssFile(Yii::app()->request->baseUrl . '/css/theme_' . strtolower(cfg('UI_THEME')) . '.css');
        Yii::app()->bootstrap->register();
        $ieCss = Yii::app()->request->baseUrl . '/css/ie.css';
        ?>
    </head>
    <body>
        <script>
            var ws; // gloabal variable for websocket connection
            var contextHelper;
        </script>
        <?php
        // UI Fading
        $fadeInStyle = '';
        if (isset(Yii::app()->user->userMonitor)) {
            $fadeInStyle = Yii::app()->user->userMonitor == 'true' ? 'margin-right:260px;' : '';
        } else {
            Yii::app()->user->logout();
            Yii::app()->user->loginRequired();
        }

        if (Yii::app()->user->getState('fadePageIn')) {
            $fadeInStyle .= 'opacity:0';
            echo "<script>jQuery(document).ready(function($) { $('#mainWrapper').css('opacity', 1); });</script>";
            Yii::app()->user->setState('fadePageIn', false);
        }
        $assetManager = Yii::app()->getAssetManager();
        $url = $assetManager->publish(Yii::getPathOfAlias('application.components.js.modalWindow'));
        cs()->registerScriptFile("$url/modalWindow.js", CClientScript::POS_HEAD);
        $baseUrl = app()->baseUrl;
        cs()->registerScriptFile("$baseUrl/js/plWebSocket.js", CClientScript::POS_HEAD);
        cs()->registerScriptFile("$baseUrl/js/plContextHelp/plContextHelp.js", CClientScript::POS_HEAD);
        cs()->registerScriptFile("$baseUrl/js/bootstrap-datetimepicker.min.js", CClientScript::POS_HEAD);
        cs()->registerScriptFile("$baseUrl/js/bootstrap-timepicker.js", CClientScript::POS_HEAD);
        cs()->registerScriptFile("$baseUrl/js/gritter/jquery.gritter.min.js", CClientScript::POS_HEAD);
        cs()->registerScriptFile("$baseUrl/js/jquery.contextmenu.js", CClientScript::POS_HEAD);

        if (Yii::app()->user->translateMode) {
            cs()->registerScriptFile("$baseUrl/js/pl-translator.js", CClientScript::POS_HEAD);
            cs()->registerScript('translation', "var translator;
                                    $(function() {
                                        translator = new $.plTranslator({
                                            url: '" . url('translation') . "',
                                        });
                                    });
                                    $(document).ajaxSuccess(function() {
                                        translator.update();
                                    });"
                    
            );
        }

        // BOOSTRAP BUG WORKAROUND
        // more info: https://github.com/twitter/bootstrap/issues/4663
        app()->clientScript->registerScript(
                'bootstrapModalBugfix', "$('.modal').attr('tabindex', -1);", CClientScript::POS_READY
        );
        ?>
        <script>
            
            var activeSudoTab = '';
            $("#shortPanelNav a").click(function () {
                if (activeSudoTab == this.id) {
                    activeSudoTab = '';
                    $(this).parent().removeClass('active');
                    closeSudoDetails();
                } else {
                    $('#' + activeSudoTab).parent().removeClass('active');
                    activeSudoTab = this.id;
                    openSudoDetails();
                    $(this).parent().addClass('active');

                }
                return false;
            });

            function openSudoDetails() {
                $("#sudoBarDetails").css('margin-top', '0');
            }

            function closeSudoDetails() {
                $("#sudoBarDetails").css('margin-top', '-101px');
            }

            function sudoDetailsClosed() {
                return $("#sudoBarDetails").css('margin-top') !== '0px';
            }
        </script>
        <?php
        $js = '
            var userMonitor = ' . Yii::app()->user->userMonitor . ';
                var activeSudoTab = "";
                $("#shortPanelNav a").click(function(){
                    if(activeSudoTab == this.id){
                        activeSudoTab = "";
                        $(this).parent().removeClass("active");
                        closeSudoDetails();
                    } else {
                        $("#"+activeSudoTab).parent().removeClass("active");
                        activeSudoTab = this.id;
                        openSudoDetails();
                        $(this).parent().addClass("active");
                    }
                    return false;
            });
        ';

        Yii::app()->clientScript->registerScript('shortPanelNav', $js);

        if (cfg("WEB_USER_MONITOR_ENABLED") === "1") {
            $this->widget('widgets.UserMonitor.UserMonitor');
        }
        ?>
        <div class="wrapper"  style="<?php echo $fadeInStyle ?>" id="mainWrapper">
            <div class="navbar" style="margin:0">
                <div id="sudoBar">
                    <div class="container">
                        <div>
                            <ul class="nav" id="headerVersion">
                                <li>
                                    <?php echo PhonalisaLicenseManager::getPhonalisaBranding(); ?>
                                </li>
                            </ul>
                            <ul class="nav pull-right shortPanelList" title="<?php echo __('GLOBAL_PRESENCE_MONITOR'); ?>" data-translation-id="GLOBAL_PRESENCE_MONITOR">
                                <li id="userMonitorToggle">
                                    <?php 
                                        if (cfg("WEB_USER_MONITOR_ENABLED") === "1") {
                                            $this->widget('widgets.UserMonitorToggle.UserMonitorToggle'); 
                                        }
                                    ?>
                                </li>
                            </ul>
                            <ul class="nav pull-right shortPanelList" title="<?php echo __('GLOBAL_LANGUAGE_SELECTOR'); ?>" data-translation-id="GLOBAL_LANGUAGE_SELECTOR">
                                <?php
                                    if (cfg("WEBRTC_ENABLED")) {
                                        $browserPhoneButton = CHtml::link(
                                                        '<i class="icon-phone"></i>', bu("/verto"), array(
                                                            "onclick" =>"window.open('verto/', 'newwindow', 'width=1200,height=1000'); return false;",
                                                    'id' => 'Phone',
                                                    'title' => __('GLOBAL_BROWSER_PHONE'),
                                                    'data-translation-id' => 'GLOBAL_BROWSER_PHONE',
                                                        )
                                        );
                                        
                                        echo "<li>$browserPhoneButton</li>";
                                    }
                                ?>
                                <li>
                                    <?php $this->widget('widgets.LanguageSelector.LanguageSelector'); ?>
                                </li>
                                <li id="userSplitButton">
                                    <i class="icon-sort-down"></i>
                                    <a>
                                        <?php
                                        $currentSudoUser = app()->user->sudoUserInstance;
                                        $userName = '&lt;';
                                        $userName .=$currentSudoUser->extension;
                                        $userName .='&gt;  ';
                                        $userName .= CHtml::encode(getFormatedNameToUser($currentSudoUser, 'sudoBar'));
                                        echo $userName;
                                        $currentRealUser = app()->user->realUserInstance;
                                        ?>
                                    </a>
                                    <?php
                                    echo CHtml::form();
                                    $title = __('SUDO_DROPDOWN_TITLE_YOU_ARE');
                                    $title .= ' ' . ' <';
                                    $title .= $currentRealUser->extension . '>  ';
                                    $title .= CHtml::encode(
                                                    getFormatedNameToUser(
                                                            $currentRealUser, 'sudoBar'
                                                    )
                                    );

                                    echo CHtml::dropDownList(
                                            'switchUser', Yii::app()->user->sudoUser, AccessManager::getSudoUsersListData(), array(
                                        'submit' => '',
                                        'title' => $title,
                                        'data-translation-id' => 'SUDO_DROPDOWN_TITLE_YOU_ARE',
                                            )
                                    );
                                    echo CHtml::endForm();
                                    ?>
                                </li>
                                <li>
                                    <?php
                                    $signoutButton = CHtml::link(
                                                    '<i class="icon-signout"></i>', $this->createUrl('/site/logout'), array(
                                                'id' => 'logOut',
                                                'title' => __('GLOBAL_LOGOUT_BUTTON'),
                                                'data-translation-id' => 'SUDO_DROPDOWN_TITLE_YOU_ARE',
                                                    )
                                    );
                                    echo $signoutButton;
                                    ?>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
            <div id="sudoBarDetails">
            </div>
            <div id="header" class="headerIconPanel">
                <?php
                $this->widget(
                        'widgets.inlineIconMenu.IIMenu', array(
                    'items' => AccessManager::getMainMenuItems('top'),
                    'centerItems' => false
                        )
                );
                ?>
            </div>
            <?php echo $content; ?>
            <div class="push"></div>
        </div>
        <?php
        $footerStyle = '';
        if (isset(Yii::app()->user->userMonitor) && Yii::app()->user->userMonitor == 'true') {
            $footerStyle = ' style="margin-right:250px"';
        }
        ?>
        <div id="footer" <?php echo $footerStyle; ?>>
            <?php
            if (Yii::app()->params['devMode'] == true) {
                $usedMemory = Yii::getLogger()->getMemoryUsage();
                $developerFooterContent = 'Renderd in ';
                $developerFooterContent .= Yii::getLogger()->getExecutionTime();
                $developerFooterContent .= ' sec. ';
                $developerFooterContent .= formatSize($usedMemory);
                $developerFooterContent .= ' used';
                echo $developerFooterContent;
            } else {
                echo '© 2019 Phonalisa Europe GmbH';
            }
            ?>
        </div>
    </body>
</html>

<?php
// Script is inserted in the jQuery's ready function
cs()->registerScript('WebSocket', "ws = $.WebSocket ('" . getWebSocketCTIUrl() . "', null, {
                                user:'" . $currentSudoUser->name . "',
                                pass:'" . $currentSudoUser->password . "',
                                debug: true,
                                logData: true
                                });", CClientScript::POS_BEGIN
);            
