<!DOCTYPE html>
@php
App::setLocale(userLocal());
@endphp
<html lang="{{ userLocal() }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title> {{ @base_settings('company_name') }} - @yield('title')</title>
    <meta name="keywords" content="attendance, finance, hr management, employee, hrm, HRM management system, human, laravel, leave,office, office-attendance, php, report, resource">
    <meta name="description" content=" Human Resource Management System Website ">
    @if (env('APP_ENV') == 'production' && env('APP_HTTPS') == true)
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
    @endif
    <meta name="base-url" id="base-url" content="{{ url('/') }}">

    {{-- Header start --}}
    @include('backend.partials.header')
    {{-- Header start --}}

    @if (isModuleActive('Bulletin'))
        @include('bulletin::_inc.bulletin-marquee-style')
    @endif

    @yield('style')
</head>


<body class="default-theme {{ isModuleActive('Bulletin') && count($bulletins) ? 'marquee-bar-show' : 'marquee-bar-hide' }}" style="background-image: url(' {{ background_asset(@base_settings('backend_image')) }}')">
    <input type="text" hidden value="{{ background_asset(@base_settings('backend_image')) }}" id="background_image">
    <input type="text" hidden value="{{ url('/') }}" id="url">
    <input type="text" hidden value="{{ config('settings.app.company_name') }}" id="site_name">
    <input type="text" hidden value="{{ settings('time_format') }}" id="time_format">
    <input type="text" hidden value="{{ \Carbon\Carbon::now()->format('Y/m/d') }}" id="defaultDate">
    <input type="hidden" id="get_custom_user_url" value="{{ route('user.getUser') }}">
    <input hidden value="{{ _trans('project.Select Employee') }}" id="select_custom_members">
    <input hidden value="{{ auth()->id() }}" id="fire_base_authenticate">
    <input hidden value="{{ _trans('alert.Are you sure?') }}" id="are_you_sure">
    <input hidden value="{{ _trans('alert.You want to delete this record?') }}" id="you_want_delete">
    <input hidden value="{{ _trans('alert.You want to approve this record?') }}" id="you_want_approve">
    <input hidden value="{{ _trans('alert.You want to reject this record?') }}" id="you_want_reject">
    <input hidden value="{{ _trans('response.Something went wrong') }}" id="something_wrong">
    <input hidden value="{{ _trans('common.Yes') }}" id="yes">
    <input hidden value="{{ _trans('common.Cancel') }}" id="cancel">
    <input hidden value="{{ _trans('common.Delete') }}" id="delete">
    <input hidden value="{{ @settings('currency_symbol') }}" id="currency_symbol">
    <input hidden value="{{ _trans('common.Nothing to show here') }}" id="nothing_show_here">
    <input hidden value="{{ _trans('common.Choose Employee') }}" id="choose_employee_text">
    <input hidden
        value="{{ _trans('project.Please add a new entity or manage the data table to see the content here') }}"
        id="please_add_new">
    <input hidden value="{{ _trans('common.Thank you') }}" id="thank_you">

    @if (isModuleActive('Bulletin'))
        @include('bulletin::_inc.bulletin-marquee')
    @endif

    <div id="layout-wrapper">
        {{-- Navbar start --}}
        @include('backend.partials.navbar')
        {{-- Navbar start --}}

        {{-- Sidebar start --}}
        @include('backend.partials.new-sidebar')
        {{-- Sidebar end --}}
        <main class="main-content ph-32 pt-100 pb-20" id="__index_ltn">
            <div class="page-content">
                {{-- Content start --}}
                @yield('content')
                {{-- Content end --}}
            </div>
            {{-- Footer start --}}
            @include('backend.partials.footer')
            {{-- Footer end --}}
        </main>
    </div>

    {{-- Script start --}}
    <script>
        var isRTL = {{ @isRTL() }};
    </script>

    @include('backend.partials.script')
    {{-- Script end --}}

    @if (Auth::check() && env('APP_ENV') == 'production')
        <script src="{{ global_asset('https://www.gstatic.com/firebasejs/8.7.1/firebase-app.js') }}"></script>
        <script src="{{ global_asset('https://www.gstatic.com/firebasejs/8.7.1/firebase-messaging.js') }}"></script>
        <script>
            @include('vendor.notifications.init_firebase')
        </script>
        <script src="{{ global_asset('backend/js/__firebase.js') }}"></script>
    @endif

    {{-- Script end --}}
    @include('backend.partials.message')
    {{-- Script end --}}
    {{-- table end --}}
    <script src="{{ global_asset('backend/js/table/data-table.js') }}"></script>
    {{-- table end --}}
    @yield('script')
    <script src="{{ global_asset('backend/js/backend_common.js') }}"></script>
    <script src="{{ global_asset('backend/js/__app.script.js') }}"></script>

    <script src="{{ global_asset('backend/js/jquery.webticker2.js') }}?time={{ date('Ymdhis') }}"></script>

    @if (isModuleActive('Bulletin'))
        @include('bulletin::_inc.bulletin-marquee-script')
    @endif

    {{-- Push Notification (Firebase) disabled for local development. Enable by setting PUSHNOTIFICATION_ENABLED=true in .env after configuring Firebase in Settings. --}}
    @if (env('PUSHNOTIFICATION_ENABLED') === true && !empty(base_settings('firebase_project_id')))
        @include('pushnotification::script')
    @endif

    <script>
        var LOCAL_BASE_URL = window.location.origin;
        var LIVE_BASE_URL = 'https://hrm.onesttech.com';

        function convertLiveUrlToLocal(rawUrl) {
            if (!rawUrl) return rawUrl;
            try {
                var parsed = new URL(rawUrl, window.location.href);
                if (parsed.origin === LIVE_BASE_URL) {
                    return LOCAL_BASE_URL + parsed.pathname + parsed.search + parsed.hash;
                }
            } catch (e) {
                return rawUrl;
            }
            return rawUrl;
        }

        function normalizeLocalUrl(rawUrl) {
            if (!rawUrl) return rawUrl;
            // Remove accidental dot right after host:port (e.g. :8000./path -> :8000/path).
            return rawUrl
                .replace(/(https?:\/\/[^\/]+)\.\//i, '$1/')
                .replace(/(https?:\/\/[^\/]+)\.$/i, '$1');
        }

        // Replace hardcoded live links (menu/profile/header) with local base URL.
        document.querySelectorAll('a[href]').forEach(function (anchor) {
            var href = (anchor.getAttribute('href') || '').trim();
            var updatedHref = normalizeLocalUrl(convertLiveUrlToLocal(href));
            if (updatedHref !== href) {
                anchor.setAttribute('href', updatedHref);
            }
        });

        // Safety net: unregister any stale Firebase/push service workers that may linger
        // from a prior install and cause "about:blank#blocked" popups on link clicks.
        if ('serviceWorker' in navigator) {
            navigator.serviceWorker.getRegistrations().then(function (registrations) {
                registrations.forEach(function (registration) {
                    var swUrl = (registration.active && registration.active.scriptURL) || '';
                    if (swUrl.indexOf('firebase-messaging-sw') !== -1 || swUrl.indexOf('/firebase') !== -1) {
                        registration.unregister();
                    }
                });
            }).catch(function () { /* no-op */ });
        }

        // Keep sidebar/menu anchors navigating in the same tab. Some third-party
        // scripts add target="_blank" dynamically with an empty href which causes
        // Chrome to open "about:blank#blocked".
        document.addEventListener('click', function (event) {
            var anchor = event.target && event.target.closest ? event.target.closest('a') : null;
            if (!anchor) return;
            var href = (anchor.getAttribute('href') || '').trim();
            var liveAdjustedHref = normalizeLocalUrl(convertLiveUrlToLocal(href));
            if (liveAdjustedHref !== href) {
                anchor.setAttribute('href', liveAdjustedHref);
                href = liveAdjustedHref;
            }
            var target = (anchor.getAttribute('target') || '').trim().toLowerCase();
            if (target === '_blank') {
                if (!href || href === '#' || href.toLowerCase().startsWith('javascript:')) {
                    event.preventDefault();
                    return;
                }
                try {
                    var resolved = new URL(href, window.location.href);
                    if (resolved.origin === window.location.origin) {
                        anchor.setAttribute('target', '_self');
                    }
                } catch (e) {
                    event.preventDefault();
                }
            }
        }, true);
    </script>

    @if (Route::has('document-request.get-template'))
        <script>

            function updateContent() {
                var document_type_id = $('#document_type').val();
                $.ajax({
                    url: "{{ route('document-request.get-template') }}",
                    type: 'POST',
                    data: {
                        _token: "{{ csrf_token() }}",
                        document_type_id: document_type_id
                    },
                    success: function (response) {
                        console.log(response);
                        CKEDITOR.instances['editor1Textarea'].setData(response.request_template);
                        // document.getElementById('editor1Textarea').innerHTML=response.request_template;
                    }
                });
            }
        </script>
    @endif

    
    @stack('ModuleScript')
</body>
</html>