<?php
    // Pobieramy rolę usera i jego uprawnienia
    $role = trim(Auth::guard('admin')->user()->getUserRole());
    $merchantEmployee = \App\Models\MerchantEmployee::where('user_id', Auth::id())->first();
    $userPermissions = $merchantEmployee ? $merchantEmployee->permissions : [];
    
    // Funkcja sprawdzająca uprawnienia
    $hasPermission = function($permission) use ($userPermissions, $role) {
        // Główny użytkownik ma wszystkie uprawnienia
        if ($role === 'vendor') return true;
        // Sprawdź czy użytkownik ma konkretne uprawnienie
        return in_array($permission, $userPermissions);
    };

    // Warunki aktywności - poprawione
    $isHomeActive        = Request::is('dashboard') || Request::is('users/payment_details*');
    $isOrdersActive      = Request::is('orders*') || Request::is('placedOrders*') || Request::is('acceptedOrders*') || Request::is('rejectedOrders*') || Request::is('completedOrders*');
    $isLiveOrdersActive  = Request::is('live-orders*');
    $isMenuActive        = Request::is('foods*') || Request::is('category*');

    $isPaymentsActive    = Request::is('payments*');
    $isBookTableActive   = Request::is('booktable*');

    // Dla restaurant i account używamy routeIs (zakładając nazwy tras 'user.restaurant' i 'user.profile')
    $isRestaurantActive  = request()->routeIs('user.restaurant');
    $isAccountActive     = request()->routeIs(['user.profile*', 'user.manage_profile', 'user.payout_details', 'user.orders_export', 'user.manage_users']);
?>

<div class="sidebar-nav flex flex-col h-full">
    <ul id="sidebarnav" class="m-0 p-0 list-none flex flex-col items-center space-y-4 w-full">

        
        <li class="list-none w-full">
            <a
                id="menu_home"
                href="<?php echo e(url('dashboard')); ?>"
                aria-expanded="false"
                class="<?php echo \Illuminate\Support\Arr::toCssClasses([
                  "no-underline hover:no-underline
                   text-slate-400 fill-slate-400
                   hover:!text-white hover:!fill-white
                   transition-colors duration-300 ease-in-out
                   flex flex-col items-center justify-center gap-1 w-full py-1 text-center",
                  "text-white fill-white" => $isHomeActive
                ]); ?>"
            >
                <svg
                    xmlns="http://www.w3.org/2000/svg"
                    width="24"
                    height="24"
                    class="fill-current"
                    viewBox="0 0 26 26"
                >
                    <path d="M12.965,0a2.6,2.6,0,0,0-1.407.438L1.158,7.371A2.6,2.6,0,0,0,0,9.533V23.4A2.614,2.614,0,0,0,2.6,26H9.533V18.2a1.73,1.73,0,0,1,1.733-1.733h3.467A1.729,1.729,0,0,1,16.467,18.2V26H23.4A2.614,2.614,0,0,0,26,23.4V9.533a2.6,2.6,0,0,0-1.158-2.162L14.443.438A2.6,2.6,0,0,0,12.965,0"/>
                </svg>
                <p class="text-current text-xs leading-normal m-0 whitespace-normal break-words px-2 text-center max-w-[6rem]">
                    <?php echo e(trans('lang.home')); ?>

                </p>
            </a>
        </li>

        
        <?php if($hasPermission('orders')): ?>
        <li class="list-none w-full">
            <a
                id="menu_orders"
                href="<?php echo e(url('orders')); ?>"
                aria-expanded="false"
                class="<?php echo \Illuminate\Support\Arr::toCssClasses([
                  "no-underline hover:no-underline
                   text-slate-400 fill-slate-400
                   hover:!text-white hover:!fill-white
                   transition-colors duration-300 ease-in-out
                   flex flex-col items-center justify-center gap-1 w-full py-1 text-center",
                  "text-white fill-white" => $isOrdersActive
                ]); ?>"
            >
                <svg
                    xmlns="http://www.w3.org/2000/svg"
                    width="24"
                    height="30"
                    class="fill-current"
                    viewBox="0 0 26 33.426"
                >
                    <path d="M24.932,10.671a1.194,1.194,0,0,0-1.194-1.122H18.963V5.968a5.969,5.969,0,1,0-11.938,0V9.549H2.25a1.194,1.194,0,0,0-1.194,1.122L.006,29.64a3.581,3.581,0,0,0,3.373,3.778q.1.006.209.006H22.413A3.581,3.581,0,0,0,26,29.849q0-.1-.006-.209ZM9.413,5.968a3.581,3.581,0,1,1,7.163,0V9.549H9.413Z"/>
                </svg>
                <p class="text-current text-xs leading-normal m-0 whitespace-normal break-words text-center max-w-[6rem]">
                    <?php echo e(trans('lang.order_plural')); ?>

                </p>
            </a>
        </li>
        <?php endif; ?>

        
        <?php if($hasPermission('live_orders')): ?>
        <li class="list-none w-full">
            <a
                id="menu_live_orders"
                href="<?php echo e(url('live-orders')); ?>"
                aria-expanded="false"
                class="<?php echo \Illuminate\Support\Arr::toCssClasses([
                  "no-underline hover:no-underline
                   text-slate-400 fill-slate-400
                   hover:!text-white hover:!fill-white
                   transition-colors duration-300 ease-in-out
                   flex flex-col items-center justify-center gap-1 w-full py-1 text-center",
                  "text-white fill-white" => $isLiveOrdersActive
                ]); ?>"
            >
                <svg
                    xmlns="http://www.w3.org/2000/svg"
                    width="26"
                    class="fill-current"
                    viewBox="0 0 576 512"
                >
                    <path d="M64 0C28.7 0 0 28.7 0 64V352c0 35.3 28.7 64 64 64H240l-10.7 32H160c-17.7 0-32 14.3-32 32s14.3 32 32 32H416c17.7 0 32-14.3 32-32s-14.3-32-32-32H346.7L336 416H512c35.3 0 64-28.7 64-64V64c0-35.3-28.7-64-64-64H64zM512 64V352H64V64H512z"/>
                </svg>
                <p class="text-current text-xs leading-normal m-0 whitespace-normal break-words text-center max-w-[6rem]">
                    <?php echo e(trans('lang.live_orders')); ?>

                </p>
            </a>
        </li>
        <?php endif; ?>

        
        <?php if($hasPermission('menu')): ?>
        <li class="list-none w-full">
            <a
                id="menu_menu"
                href="<?php echo e(url('foods')); ?>"
                aria-expanded="false"
                class="<?php echo \Illuminate\Support\Arr::toCssClasses([
                  "no-underline hover:no-underline
                   text-slate-400 fill-slate-400
                   hover:!text-white hover:!fill-white
                   transition-colors duration-300 ease-in-out
                   flex flex-col items-center justify-center gap-1 w-full py-1 text-center",
                  "text-white fill-white" => $isMenuActive
                ]); ?>"
            >
                <svg
                    xmlns="http://www.w3.org/2000/svg"
                    width="24"
                    height="28"
                    class="fill-current"
                    viewBox="0 0 26 31.501"
                >
                    <path d="M24.493,0H1.507A1.507,1.507,0,0,0,0,1.508V30A1.507,1.507,0,0,0,1.507,31.5H24.493A1.507,1.507,0,0,0,26,30V1.508A1.507,1.507,0,0,0,24.493,0M5.464,13.591h.327A7.314,7.314,0,0,1,11.87,7.512V6.946a1.13,1.13,0,1,1,2.261,0v.566a7.314,7.314,0,0,1,6.078,6.078h.327a1.13,1.13,0,0,1,0,2.261H5.464a1.13,1.13,0,0,1,0-2.261m6.995,6.445a1.13,1.13,0,0,1-1.13,1.13H5.9a1.13,1.13,0,1,1,0-2.261h5.426a1.13,1.13,0,0,1,1.13,1.13m7.795,5.652H5.746a1.13,1.13,0,0,1,0-2.261H20.254a1.13,1.13,0,0,1,0,2.261"/>
                    <path d="M116.6,132a4.843,4.843,0,0,0-9.438,0Z" transform="translate(-98.882 -118.512)"/>
                </svg>
                <p class="text-current text-xs leading-normal m-0 whitespace-normal break-words px-2 text-center max-w-[6rem]">
                    <?php echo e(trans('lang.menu')); ?>

                </p>
            </a>
        </li>
        <?php endif; ?>

        
        <?php if($hasPermission('restaurant')): ?>
        <li class="list-none w-full">
            <a
                id="menu_restaurant"
                href="<?php echo e(route('user.restaurant')); ?>"
                aria-expanded="false"
                class="<?php echo \Illuminate\Support\Arr::toCssClasses([
                  "no-underline hover:no-underline
                   text-slate-400 fill-slate-400
                   hover:!text-white hover:!fill-white
                   transition-colors duration-300 ease-in-out
                   flex flex-col items-center justify-center gap-1 w-full py-1 text-center",
                  // Zmiana warunku na routeIs('user.restaurant')
                  "text-white fill-white" => request()->routeIs('user.restaurant')
                ]); ?>"
            >
                <svg
                    xmlns="http://www.w3.org/2000/svg"
                    width="24"
                    height="24"
                    class="fill-current"
                    viewBox="0 0 25.48 25.48"
                >
                    <path d="M25.475,170.667H0v1.062a2.123,2.123,0,0,0,2.123,2.123H5.308a2.123,2.123,0,0,0,2.123-2.123,2.123,2.123,0,0,0,2.123,2.123h6.37a2.123,2.123,0,0,0,2.123-2.123,2.123,2.123,0,0,0,2.123,2.123h3.185a2.123,2.123,0,0,0,2.123-2.123v-1.062Z" transform="translate(0 -162.174)"/>
                    <path d="M358.075,7.432,356.171.77A1.062,1.062,0,0,0,355.151,0h-4.863l1.486,7.432Z" transform="translate(-332.856 0)"/>
                    <path d="M13.03,0H8.167A1.062,1.062,0,0,0,7.146.77l-1.9,6.662h6.3Z" transform="translate(-4.982 0)"/>
                    <path d="M181.9,0h-5.054l-1.486,7.432h8.027Z" transform="translate(-166.628 0)"/>
                    <path d="M40.444,240.477a3.173,3.173,0,0,1-2.123-.813,3.173,3.173,0,0,1-2.123.813h-6.37a3.172,3.172,0,0,1-2.123-.813,3.173,3.173,0,0,1-2.123.813H22.4a3.164,3.164,0,0,1-1.062-.186v11.864a1.062,1.062,0,0,0,1.062,1.062h2.123v-9.555a1.062,1.062,0,0,1,1.062-1.062h5.308a1.062,1.062,0,0,1,1.062,1.062v9.555H43.629a1.062,1.062,0,0,0,1.062-1.062V240.291a3.164,3.164,0,0,1-1.062.186Zm1.062,6.37a1.062,1.062,0,0,1-1.062,1.062H35.136a1.062,1.062,0,0,1-1.062-1.062v-3.185a1.062,1.062,0,0,1,1.062-1.062h5.308a1.062,1.062,0,0,1,1.062,1.062Z" transform="translate(-20.272 -227.737)"/>
                </svg>
                <p class="text-current text-xs leading-normal m-0 whitespace-normal break-words px-2 text-center max-w-[6rem]">
                    <?php echo e(trans('lang.restaurant')); ?>

                </p>
            </a>
        </li>
        <?php endif; ?>

        
        <?php if($hasPermission('account')): ?>
        <li class="list-none w-full">
    <a
        id="menu_account"
        href="<?php echo e(route('user.profile')); ?>"
        aria-expanded="false"
        class="<?php echo \Illuminate\Support\Arr::toCssClasses([
            "no-underline hover:no-underline
             text-slate-400 fill-slate-400
             hover:!text-white hover:!fill-white
             transition-colors duration-300 ease-in-out
             flex flex-col items-center justify-center gap-1 w-full py-1 text-center",
            // Używamy wildcard, żeby łapać też wszystkie podstrony profile/*
            "text-white fill-white active" => $isAccountActive,
        ]); ?>"
    >
        <svg
            xmlns="http://www.w3.org/2000/svg"
            width="24"
            height="24"
            class="fill-current"
            viewBox="0 0 26 26"
        >
            <path d="M5686.631,7604h-21.124a2.442,2.442,0,0,1-2.438-2.437v-.813a6.507,6.507,0,0,1,6.5-6.5h2.8a8.844,8.844,0,0,0,7.4,0h2.8a6.507,6.507,0,0,1,6.5,6.5v.813A2.442,2.442,0,0,1,5686.631,7604Zm-10.562-11.376a7.312,7.312,0,1,1,7.313-7.312A7.311,7.311,0,0,1,5676.069,7592.627Z" transform="translate(-5663.069 -7578.003)"/>
        </svg>
        <p class="text-current text-xs leading-normal m-0 whitespace-normal break-words px-2 text-center max-w-[6rem]">
            <?php echo e(trans('lang.account')); ?>

        </p>
    </a>
</li>
        <?php endif; ?>

        
        <?php if($hasPermission('voucher_master')): ?>
        <li class="list-none w-full">
            <a
                id="voucher_master"
                href="<?php echo e(route('voucher_master')); ?>"
                aria-expanded="false"
                class="<?php echo \Illuminate\Support\Arr::toCssClasses([
                    "no-underline hover:no-underline
                    text-slate-400 fill-slate-400
                    hover:!text-white hover:!fill-white
                    transition-colors duration-300 ease-in-out
                    flex flex-col items-center justify-center gap-1 w-full py-1 text-center",
                    // Używamy wildcard, żeby łapać też wszystkie podstrony profile/*
                    "text-white fill-white active" => $isAccountActive,
                ]); ?>"
            >
                <svg xmlns="http://www.w3.org/2000/svg"
                    width="24" 
                    height="24" 
                    class="fill-current" 
                    viewBox="0 0 24 24"
                >
                    <path d="M21 7a2 2 0 0 1-2-2H5a2 2 0 0 1-2 2v2a2 2 0 0 1 0 4v2a2 2 0 0 1 2 2h14a2 2 0 0 1 2-2v-2a2 2 0 0 1 0-4V7zm-9 10h-2v-2h2v2zm0-4h-2V7h2v6z"/>
                </svg>
                <p class="text-current text-xs leading-normal m-0 whitespace-normal break-words px-2 text-center max-w-[6rem]">
                    <?php echo e(trans('lang.voucher_master')); ?>

                </p>
            </a>
        </li>
        <?php endif; ?>

        
        <li class="list-none w-full">
            <a
                href="<?php echo e(route('logout')); ?>"
                onclick="event.preventDefault(); document.getElementById('logout-form').submit();"
                aria-expanded="false"
                class="group no-underline hover:no-underline
                       text-slate-400 hover:!text-white
                       transition-colors duration-300 ease-in-out
                       flex flex-col items-center justify-center gap-1 w-full py-1 text-center"
            >
                <svg
                    xmlns="http://www.w3.org/2000/svg"
                    width="24"
                    height="24"
                    class="fill-slate-400 group-hover:fill-red-500 transition-colors duration-300 ease-in-out"
                    viewBox="0 0 512 512"
                >
                    <path d="M288 32c0-17.7-14.3-32-32-32s-32 14.3-32 32V256c0 17.7 14.3 32 32 32s32-14.3 32-32V32zM143.5 120.6c13.6-11.3 15.4-31.5 4.1-45.1s-31.5-15.4-45.1-4.1C49.7 115.4 16 181.8 16 256c0 132.5 107.5 240 240 240s240-107.5 240-240c0-74.2-33.8-140.6-86.6-184.6c-13.6-11.3-33.8-9.4-45.1 4.1s-9.4 33.8 4.1 45.1c38.9 32.3 63.5 81 63.5 135.4c0 97.2-78.8 176-176 176s-176-78.8-176-176c0-54.4 24.7-103.1 63.5-135.4z"/>
                </svg>
                <p class="text-current text-xs leading-normal m-0 whitespace-normal break-words px-2 text-center max-w-[6rem]">
                    <?php echo e(trans('lang.log_out')); ?>

                </p>
                <form
                    id="logout-form"
                    action="<?php echo e(route('logout')); ?>"
                    method="POST"
                    class="d-none"
                >
                    <?php echo csrf_field(); ?>
                </form>
            </a>
        </li>

    </ul>

    <div class="mt-auto pb-10">
        <p class="web_version text-center font-medium m-0 mt-5 text-sm text-slate-600 z-[1] bg-transparent w-full">v1.0.0</p>
    </div>
</div>

<script type="text/javascript">
    document.addEventListener('alpine:init', () => {
        fetch('/api/vendor', { headers: { 'Accept': 'application/json' } })
            .then(function(r) { return r.json(); })
            .then(function(data) {
                var enabled = data.vendor && data.vendor.enabled_dine_in_future ? true : false;
                document.querySelectorAll('.dineInHistory').forEach(function(el) {
                    el.style.display = enabled ? '' : 'none';
                });
            })
            .catch(function(err) {
                console.warn('Could not load vendor data:', err);
            });
    });

    // ============================================
    // STRIPE DASHBOARD MODAL
    // ============================================
    function openStripeModal() {
        window.dispatchEvent(new CustomEvent('stripe-modal-open'));
    }

    function closeStripeModal() {
        window.dispatchEvent(new CustomEvent('stripe-modal-close'));
    }

    function openStripeDashboard() {
        const btn = document.getElementById('stripeOpenBtn');
        const btnText = document.getElementById('stripeBtnText');
        const btnSpinner = document.getElementById('stripeBtnSpinner');
        const errorMsg = document.getElementById('stripeErrorMsg');
        
        // Show loading state
        btn.disabled = true;
        btnText.textContent = '<?php echo e(trans("lang.loading")); ?>...';
        btnSpinner.classList.remove('hidden');
        errorMsg.classList.add('hidden');
        
        fetch('<?php echo e(route("stripe.dashboard.url")); ?>', {
            method: 'GET',
            headers: {
                'Accept': 'application/json',
                'X-Requested-With': 'XMLHttpRequest'
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                if (data.demo_mode) {
                    // Demo mode - show message
                    errorMsg.textContent = 'DEMO MODE: ' + data.message + ' (Account: ' + data.account_id + ')';
                    errorMsg.classList.remove('hidden', 'text-red-600');
                    errorMsg.classList.add('text-blue-600');
                } else {
                    // Production - open in new window
                    window.open(data.url, '_blank', 'noopener,noreferrer');
                    closeStripeModal();
                }
            } else {
                errorMsg.textContent = data.error || '<?php echo e(trans("lang.stripe_dashboard_error")); ?>';
                errorMsg.classList.remove('hidden', 'text-blue-600');
                errorMsg.classList.add('text-red-600');
            }
        })
        .catch(error => {
            console.error('Stripe Dashboard error:', error);
            errorMsg.textContent = '<?php echo e(trans("lang.stripe_dashboard_error")); ?>';
            errorMsg.classList.remove('hidden', 'text-blue-600');
            errorMsg.classList.add('text-red-600');
        })
        .finally(() => {
            // Reset button state
            btn.disabled = false;
            btnText.textContent = '<?php echo e(trans("lang.stripe_open_dashboard")); ?>';
            btnSpinner.classList.add('hidden');
        });
    }

    // ============================================
    // BUSINESS TYPE SELECTION MODAL
    // ============================================
    function openBusinessTypeModal() {
        window.dispatchEvent(new CustomEvent('business-type-modal-open'));
    }

    function closeBusinessTypeModal() {
        window.dispatchEvent(new CustomEvent('business-type-modal-close'));
    }

    function proceedToStripeOnboarding() {
        const modal = document.getElementById('businessTypeModal');
        const selectedTypeInput = modal.querySelector('input[name="business_type"]:checked');
        
        if (!selectedTypeInput) {
            alert('<?php echo e(trans("lang.business_type_required")); ?>');
            return;
        }
        
        const selectedType = selectedTypeInput.value;
        const btnText = document.getElementById('businessTypeBtnText');
        const btnSpinner = document.getElementById('businessTypeBtnSpinner');
        const btnArrow = document.getElementById('businessTypeBtnArrow');
        const continueBtn = event.target.closest('button');
        
        // Show loading state
        continueBtn.disabled = true;
        btnText.textContent = '<?php echo e(trans("lang.loading")); ?>...';
        btnSpinner.classList.remove('hidden');
        btnArrow.classList.add('hidden');
        
        // Send POST request to backend
        fetch('<?php echo e(route("stripe.onboarding.start")); ?>', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
                'Accept': 'application/json'
            },
            body: JSON.stringify({
                business_type: selectedType
            })
        })
        .then(response => response.json())
        .then(data => {
            if (data.success && data.redirect_url) {
                // Close modal and redirect to Stripe
                closeBusinessTypeModal();
                window.location.href = data.redirect_url;
            } else {
                // Show error
                continueBtn.disabled = false;
                btnText.textContent = '<?php echo e(trans("lang.continue")); ?>';
                btnSpinner.classList.add('hidden');
                btnArrow.classList.remove('hidden');
                alert(data.error || '<?php echo e(trans("lang.stripe_onboarding_error")); ?>');
            }
        })
        .catch(error => {
            console.error('Stripe onboarding error:', error);
            continueBtn.disabled = false;
            btnText.textContent = '<?php echo e(trans("lang.continue")); ?>';
            btnSpinner.classList.add('hidden');
            btnArrow.classList.remove('hidden');
            alert('<?php echo e(trans("lang.stripe_onboarding_error")); ?>');
        });
    }
</script>




<?php /**PATH C:\xampp\htdocs\pub-and-go-merchant-np\resources\views/layouts/menu.blade.php ENDPATH**/ ?>