<?php

/**
 * Copyright Souke (2018)
 *
 * contact@souke.fr
 *
 * Ce logiciel est un programme informatique servant à aider les producteurs
 * à distribuer leur production en circuits courts.
 *
 * Ce logiciel est régi par la licence CeCILL soumise au droit français et
 * respectant les principes de diffusion des logiciels libres. Vous pouvez
 * utiliser, modifier et/ou redistribuer ce programme sous les conditions
 * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
 * sur le site "http://www.cecill.info".
 *
 * En contrepartie de l'accessibilité au code source et des droits de copie,
 * de modification et de redistribution accordés par cette licence, il n'est
 * offert aux utilisateurs qu'une garantie limitée.  Pour les mêmes raisons,
 * seule une responsabilité restreinte pèse sur l'auteur du programme,  le
 * titulaire des droits patrimoniaux et les concédants successifs.
 *
 * A cet égard  l'attention de l'utilisateur est attirée sur les risques
 * associés au chargement,  à l'utilisation,  à la modification et/ou au
 * développement et à la reproduction du logiciel par l'utilisateur étant
 * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
 * manipuler et qui le réserve donc à des développeurs et des professionnels
 * avertis possédant  des  connaissances  informatiques approfondies.  Les
 * utilisateurs sont donc invités à charger  et  tester  l'adéquation  du
 * logiciel à leurs besoins dans des conditions permettant d'assurer la
 * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
 * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
 *
 * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
 * pris connaissance de la licence CeCILL, et que vous en avez accepté les
 * termes.
 */

use common\helpers\GlobalParam;
use domain\Feature\Feature\Feature;
use domain\User\User\UserModule;

$producerModule = $this->getProducerModule();
$userModule = UserModule::getInstance();
$userProducerModule = $this->getUserProducerModule();
$ticketModule = $this->getTicketModule();
$featureChecker = $this->getFeatureModule()->getChecker();
$adminSettingBag = $this->getSettingModule()->getAdminSettingBag();

$producer = GlobalParam::getCurrentProducer();
$userCurrent = GlobalParam::getCurrentUser();
$isUserCurrentGrantedAsAdministrator = $userModule->getAuthorizationChecker()->isGrantedAsAdministrator($userCurrent);
$isUserCurrentGrantedAsProducer = $userModule->getAuthorizationChecker()->isGrantedAsProducer($userCurrent);


?>

<aside class="main-sidebar">

    <section class="sidebar">
        <?php
        $producer = GlobalParam::getCurrentProducer();
        $versionOpendistribLabel = '';
        if ($producer) {
            $backgroundLabelVersionOpendistrib = 'black';
            if (!$producerModule->isUpToDateWithOpendistribVersion($producer)) {
                $backgroundLabelVersionOpendistrib = 'green';
            }
            $versionOpendistribLabel = '<span class="pull-right-container"><small class="label pull-right bg-' . $backgroundLabelVersionOpendistrib . '">' . GlobalParam::getSoukeVersion() . '</small></span>';
        }

        $countTicketsProducerUnreadLabel = '';
        $countTicketsProducerUnread = $ticketModule->countTicketsUnreadByUser($this->getUserCurrent());
        if ($countTicketsProducerUnread && !$isUserCurrentGrantedAsAdministrator) {
            $countTicketsProducerUnreadLabel = '<small class="label pull-right bg-blue">' . $countTicketsProducerUnread . '</small>';
        }

        $developerOnlineLabel = '';
        if ($adminSettingBag->get('supportOnline')) {
            $developerOnlineLabel = '<small class="label pull-right bg-green"><i class="fa fa-phone"></i></small>';
        }

        $countTicketsLabel = '';
        $countTicketsAdminOpen = $ticketModule->getRepository()->countTicketsAdminStatusOpen();
        $countTicketsAdminUnread = $ticketModule->getRepository()->countTicketsAdminUnreadByUser($this->getUserCurrent());

        if ($countTicketsAdminUnread && $isUserCurrentGrantedAsAdministrator) {
            $countTicketsLabel = '<span class="pull-right-container"><small class="label pull-right bg-green">' . $countTicketsAdminOpen . '</small></span>';
        } else {
            if($countTicketsAdminOpen) {
                $countTicketsLabel = '<span class="pull-right-container"><small class="label pull-right bg-blue">' . $countTicketsAdminOpen . '</small></span>';
            }
        }

        $sumUserProducerCredits = $userProducerModule->sumUserProducerCredits();
        $sumUserProducerCreditsLabel = '';
        //$sumUserProducerCreditsLabel = '<span class="pull-right-container"><small class="label pull-right '.($sumUserProducerCredits >= 0 ? 'bg-green' : 'bg-red') .'">'.number_format($sumUserProducerCredits, 2).' €</small></span>';

        $countUsersWithStatusProducerOnline = $userModule->getRepository()->countUsersStatusProducerOnline();
        $countUsersWithStatusUserOnline = $userModule->getRepository()->countUsersStatusUserOnline();
        $countUsersProducersOnlineLabel = '';
        if ($countUsersWithStatusProducerOnline || $countUsersWithStatusUserOnline) {
            $countUsersProducersOnlineLabel = '<span class="pull-right-container"><small class="label pull-right bg-blue">' . ($countUsersWithStatusProducerOnline + $countUsersWithStatusUserOnline) . '</small></span>';
        }

        $countProducerInvoicesUnpaidLabel = '';
        $countProducerInvoicesUnpaid = $this->getProducerModule()->getDolibarrUtils()
            ->countDolibarrProducerInvoicesUnpaid($producer);
        if ($countProducerInvoicesUnpaid > 1) {
            $countProducerInvoicesUnpaidLabelColor = ($countProducerInvoicesUnpaid > 2) ? 'red' : 'orange';
            $countProducerInvoicesUnpaidLabel = '<span class="pull-right-container"><small class="label pull-right bg-' . $countProducerInvoicesUnpaidLabelColor . '">' . $countProducerInvoicesUnpaid . '</small></span>';
        }

        ?>

        <?= dmstr\widgets\Menu::widget(
            [
                'options' => ['class' => 'sidebar-menu tree', 'data-widget' => 'tree'],
                'items' => [

                    // Administration
                    ['label' => 'Administration', 'options' => ['class' => 'header'], 'visible' => $isUserCurrentGrantedAsAdministrator],
                    [
                        'label' => 'Tableau de bord',
                        'icon' => 'dashboard',
                        'url' => ['dashboard-admin/index'],
                        'visible' => $isUserCurrentGrantedAsAdministrator,
                    ],
                    [
                        'label' => 'Support',
                        'icon' => 'comments',
                        'url' => ['support-admin/index'],
                        'visible' => $isUserCurrentGrantedAsAdministrator,
                        'template' => '<a href="{url}">{icon} {label}' . $countTicketsLabel . '</a>'
                    ],
                    [
                        'label' => 'Producteurs',
                        'icon' => 'bookmark',
                        'url' => ['/producer-admin/index'],
                        'visible' => $isUserCurrentGrantedAsAdministrator
                    ],
                    [
                        'label' => 'Utilisateurs',
                        'icon' => 'users',
                        'url' => ['/user-admin/index'],
                        'visible' => $isUserCurrentGrantedAsAdministrator
                    ],
                    [
                        'label' => 'Deliverabilité emails',
                        'icon' => 'paper-plane',
                        'url' => ['/user-admin/email-deliverability'],
                        'visible' => $isUserCurrentGrantedAsAdministrator
                    ],
                    [
                        'label' => 'Commandes',
                        'icon' => 'calendar',
                        'url' => ['order-admin/index'],
                        'visible' => $isUserCurrentGrantedAsAdministrator,
                    ],
                    [
                        'label' => 'Statistiques',
                        'icon' => 'line-chart',
                        'url' => ['/stats-admin/turnover'],
                        'visible' => $isUserCurrentGrantedAsAdministrator,
                        'items' => [
                            ['label' => 'Chiffre d\'affaire', 'icon' => 'line-chart', 'url' => ['/stats-admin/turnover'], 'visible' => $isUserCurrentGrantedAsAdministrator],
                            ['label' => 'Commandes clients', 'icon' => 'line-chart', 'url' => ['/stats-admin/customer-orders'], 'visible' => $isUserCurrentGrantedAsAdministrator],
                            [
                                'label' => 'En ligne',
                                'icon' => 'wifi',
                                'url' => ['online-admin/index'],
                                'visible' => $isUserCurrentGrantedAsAdministrator,
                                //'template' => '<a href="{url}">{icon} {label}' . $countUsersProducersOnlineLabel . '</a>'
                            ],
                            ['label' => 'Matomo', 'icon' => 'line-chart', 'url' => ['/stats-admin/matomo'], 'visible' => $isUserCurrentGrantedAsAdministrator],
                        ],
                    ],
                    [
                        'label' => 'Configuration',
                        'icon' => 'cog',
                        'url' => ['/setting-admin/index'],
                        'visible' => $isUserCurrentGrantedAsAdministrator,
                        'items' => [
                            ['label' => 'Paramètres', 'icon' => 'cog', 'url' => ['/setting-admin/index'], 'visible' => $isUserCurrentGrantedAsAdministrator && $featureChecker->isEnabled(Feature::ALIAS_SETTINGS)],
                            ['label' => 'Fonctionnalités', 'icon' => 'flag', 'url' => ['/feature-admin/index'], 'visible' => $isUserCurrentGrantedAsAdministrator],
                            ['label' => 'Tranches de prix', 'icon' => 'eur', 'url' => ['/producer-price-range-admin/index'], 'visible' => $isUserCurrentGrantedAsAdministrator],
                            ['label' => 'Taxes', 'icon' => 'eur', 'url' => ['/tax-rate-admin/index'], 'visible' => $isUserCurrentGrantedAsAdministrator],
                        ],
                    ],

                    // Support
                    ['label' => "Besoin d'aide ?", 'options' => ['class' => 'header'], 'visible' => $isUserCurrentGrantedAsProducer],
                    [
                        'label' => 'Support',
                        'icon' => 'comments',
                        'url' => ['support/index'],
                        'visible' => $isUserCurrentGrantedAsProducer,
                        'template' => '<a href="{url}">{icon} {label} <span class="pull-right-container">' . $developerOnlineLabel . $countTicketsProducerUnreadLabel . '</span></a>'
                    ],

                    // Producteur
                    ['label' => $producer->name, 'options' => ['class' => 'header'], 'visible' => $isUserCurrentGrantedAsProducer],
                    ['label' => 'Tableau de bord', 'icon' => 'dashboard', 'url' => ['/dashboard/index'], 'visible' => $isUserCurrentGrantedAsProducer],
                    ['label' => 'Distributions', 'icon' => 'calendar', 'url' => ['/distribution/index'], 'visible' => $isUserCurrentGrantedAsProducer],
                    [
                        'label' => 'Produits',
                        'icon' => 'clone',
                        'url' => ['/product/index'],
                        'visible' => $isUserCurrentGrantedAsProducer,
                        'active' => Yii::$app->controller->id == 'product' || Yii::$app->controller->id == 'product-category' || Yii::$app->controller->id == 'accessory',
                        'items' => [
                            ['label' => 'Liste', 'icon' => 'th-list', 'url' => ['/product/index'], 'visible' => $isUserCurrentGrantedAsProducer],
                            ['label' => 'Catégories', 'icon' => 'book', 'url' => ['/product-category/index'], 'visible' => $isUserCurrentGrantedAsProducer],
                            ['label' => 'Accessoires', 'icon' => 'cutlery', 'url' => ['/accessory/index'], 'visible' => $isUserCurrentGrantedAsProducer && $featureChecker->isEnabled(Feature::ALIAS_PRODUCT_ACCESSORY)],
                            ['label' => 'Import prix', 'icon' => 'upload', 'url' => ['/product/price-import'], 'visible' => $isUserCurrentGrantedAsProducer && $featureChecker->isEnabled(Feature::ALIAS_PRODUCT_PRICE_IMPORT)],
                        ]
                    ],
                    [
                        'label' => 'Points de vente',
                        'icon' => 'map-marker',
                        'url' => ['/point-sale/index'],
                        'visible' => $isUserCurrentGrantedAsProducer,
                        'active' => Yii::$app->controller->id == 'point-sale' || Yii::$app->controller->id == 'shared-point-sale',
                        'items' => [
                            ['label' => 'Liste', 'icon' => 'th-list', 'url' => ['/point-sale/index'], 'visible' => $isUserCurrentGrantedAsProducer],
                            ['label' => 'Partages', 'icon' => 'share-alt', 'url' => ['/shared-point-sale/index'], 'visible' => $isUserCurrentGrantedAsProducer && $featureChecker->isEnabled(Feature::ALIAS_SHARED_POINT_SALE)],
                        ]
                    ],
                    [
                        'label' => 'Utilisateurs',
                        'icon' => 'users',
                        'url' => ['/user/index'],
                        'items' => [
                            ['label' => 'Liste', 'icon' => 'th-list', 'url' => ['/user/index'], 'visible' => $isUserCurrentGrantedAsProducer],
                            [
                                'label' => 'Cagnotte',
                                'icon' => 'euro',
                                'url' => ['/credit/index'],
                                'template' => '<a href="{url}">{icon} {label}' . $sumUserProducerCreditsLabel . '</a>',
                                'visible' => $isUserCurrentGrantedAsProducer && $producerModule->getConfig('credit')
                            ],
                            ['label' => 'Groupes', 'icon' => 'users', 'url' => ['/user-group/index'], 'visible' => $isUserCurrentGrantedAsProducer],
                            ['label' => 'Import', 'icon' => 'upload', 'url' => ['/user-import/index'], 'visible' => $isUserCurrentGrantedAsProducer],
                        ],
                    ],
                    ['label' => 'Abonnements', 'icon' => 'repeat', 'url' => ['/subscription/index'], 'visible' => $isUserCurrentGrantedAsProducer, 'active' => Yii::$app->controller->id == 'subscription'],
                    [
                        'label' => 'Communiquer',
                        'icon' => 'bullhorn',
                        'url' => ['/communicate/email'],
                        'visible' => $isUserCurrentGrantedAsProducer,
                        'items' => [
                            ['label' => 'Email', 'icon' => 'paper-plane', 'url' => ['/communicate/email'], 'visible' => $isUserCurrentGrantedAsProducer],
                            ['label' => 'Papier', 'icon' => 'print', 'url' => ['/communicate/paper'], 'visible' => $isUserCurrentGrantedAsProducer],
                        ]
                    ],
                    [
                        'label' => 'Documents',
                        'icon' => 'clone',
                        'url' => ['/delivery-note/index'],
                        'items' => [
                            ['label' => 'Bons de livraison', 'icon' => 'sticky-note-o', 'url' => ['/delivery-note/index'], 'visible' => $isUserCurrentGrantedAsProducer],
                            ['label' => 'Factures', 'icon' => 'sticky-note-o', 'url' => ['/invoice/index'], 'visible' => $isUserCurrentGrantedAsProducer],
                            ['label' => 'Devis', 'icon' => 'sticky-note-o', 'url' => ['/quotation/index'], 'visible' => $isUserCurrentGrantedAsProducer],
                        ],
                    ],
                    [
                        'label' => 'Statistiques',
                        'icon' => 'line-chart',
                        'url' => ['/stats/index'],
                        'items' => [
                            ['label' => 'Chiffre d\'affaire', 'icon' => 'line-chart', 'url' => ['/stats/index'], 'visible' => $isUserCurrentGrantedAsProducer],
                            ['label' => 'Rapports', 'icon' => 'pencil-square-o', 'url' => ['/report/index'], 'visible' => $isUserCurrentGrantedAsProducer],
                            ['label' => 'Produits', 'icon' => 'table', 'url' => ['/stats/products'], 'visible' => $isUserCurrentGrantedAsProducer],
                        ],
                    ],
                    ['label' => 'Paramètres', 'icon' => 'cog', 'url' => ['/producer/update'], 'visible' => $isUserCurrentGrantedAsProducer],
                    ['label' => 'Accès', 'icon' => 'lock', 'url' => ['/access/index'], 'visible' => $isUserCurrentGrantedAsProducer],
                    ['label' => "Souke", 'options' => ['class' => 'header'], 'visible' => $isUserCurrentGrantedAsProducer],
                    [
                        'label' => 'Mes factures',
                        'icon' => 'clone',
                        'url' => ['/producer-invoice/index'],
                        'visible' => $isUserCurrentGrantedAsProducer && Yii::$app->parameterBag->get('dolibarrApiKey'),
                        'active' => Yii::$app->controller->id == 'producer-invoice',
                        'template' => '<a href="{url}">{icon} {label}' . $countProducerInvoicesUnpaidLabel . '</a>'
                    ],
                    ['label' => 'Tarifs & modules', 'icon' => 'euro', 'url' => ['/producer/billing'], 'visible' => $isUserCurrentGrantedAsProducer],
                    [
                        'label' => 'Parrainage',
                        'icon' => 'users',
                        'url' => ['/sponsorship/index'],
                        'visible' => $isUserCurrentGrantedAsProducer && $featureChecker->isEnabled(Feature::ALIAS_SPONSORSHIP),
                        'active' => Yii::$app->controller->id == 'sponsorship'
                    ],
                    [
                        'label' => 'Développement',
                        'icon' => 'code',
                        'url' => ['/development/index'],
                        'visible' => $isUserCurrentGrantedAsProducer,
                        'active' => Yii::$app->controller->id == 'development',
                        'template' => '<a href="{url}">{icon} {label}' . $versionOpendistribLabel . '</a>'
                    ],

                    //['label' => 'Communiquer', 'icon' => 'bullhorn', 'url' => ['/communicate-admin/index'], 'visible' => $isUserCurrentGrantedAsAdministrator],
                    //['label' => 'Outils', 'options' => ['class' => 'header'], 'visible' => $isUserCurrentGrantedAsAdministrator],
                    //['label' => 'Gii', 'icon' => 'file-code-o', 'url' => ['/gii'], 'visible' => $isUserCurrentGrantedAsAdministrator],
                    //['label' => 'Debug', 'icon' => 'dashboard', 'url' => ['/debug'], 'visible' => $isUserCurrentGrantedAsAdministrator],
                    //['label' => 'Login', 'url' => ['site/login'], 'visible' => !$userModule->isCurrentConnected()],
                ],
            ]
        ) ?>
    </section>
</aside>
