<?php if (isset($page) && $page != 'Profile' && !isset($slug)) : ?>

	<?php $pwa2 = !empty($settings['pwa_config']) ? json_decode($settings['pwa_config']) : ''; ?>
	<?php if (isset($settings['is_pwa']) && $settings['is_pwa'] == 1 && isset($pwa2->is_pwa_active) && $pwa2->is_pwa_active == 1) : ?>
		<div class="add-to-2">
			<div class="add-to-area">
				<a href="javascript:;" class="pwaClose"><i class="fa fa-close"></i></a>
				<div class="pwaContent">
					<img src="<?= asset_url(!empty($pwa2->logo) ? $pwa2->logo : "uploads/pwa/logo.png"); ?>" alt="">
					<div class="pwaDetails">
						<h4><?= $pwa2->title; ?></h4>
						<p><?= asset_url();; ?></p>

						<button class="add-to-btn-2 btn btn-primary"><i class="icofont-bubble-down"></i> <?= lang('install_app'); ?></button>
					</div>
				</div>

			</div>
		</div>
		<script type="text/javascript">
			var base_url = '<?= asset_url(); ?>';
			if ('serviceWorker' in navigator) {
				navigator.serviceWorker.register(base_url + 'public/service-worker-2.js?r=', {
					scope: `${base_url}public/`
				}).then(function(registration) {
					// Registration was successful
					console.log('PWA: ServiceWorker registration successful with scope: ', registration.scope);
				}, function(err) {
					// registration failed :(
					console.log('PWA: ServiceWorker registration failed: ', err);
				});
			}

			let isAdminPwa = getCookie('isAdminPwa');
			var deferredPrompt;
			var div = document.querySelector('.add-to-2');
			var button = document.querySelector('.add-to-btn-2');
			var button2 = document.querySelector('.pwaClose');
			div.style.display = 'none';

			window.addEventListener('beforeinstallprompt', (e) => {
				// Prevent Chrome 67 and earlier from automatically showing the prompt
				e.preventDefault();
				// Stash the event so it can be triggered later.
				deferredPrompt = e;


				if (isAdminPwa == null || isAdminPwa == 0 || isAdminPwa == undefined) {
					$('body').addClass('visible');
					div.style.display = 'block';
				} else {
					div.style.display = 'none';
				}

				// setTimeout(function(){ div.style.display='none'; }, 5000);


				button2.addEventListener('click', (e) => {
					setCookie('isAdminPwa', '1', 500);
					div.style.display = 'none';
					$('body').removeClass('visible');

				});



				button.addEventListener('click', (e) => {
					// hide our user interface that shows our A2HS button
					div.style.display = 'none';
					// Show the prompt
					deferredPrompt.prompt();
					// Wait for the user to respond to the prompt
					deferredPrompt.userChoice
						.then((choiceResult) => {
							if (choiceResult.outcome === 'accepted') {
								console.log('User accepted the A2HS prompt');
							} else {
								console.log('User dismissed the A2HS prompt');
							}
							deferredPrompt = null;
						});
				});
			});
		</script>
	<?php endif; ?>
<?php endif; ?>


<?php if (isset($slug) && isset($id)) : ?>
	<?php $u_info = get_user_info_by_slug($slug); ?>
	<?php $u_settings = $this->common_m->get_user_settings($u_info['id']); ?>
	<!-- footer pwa -->
	<?php $pwa = !empty($u_settings['pwa_config']) ? json_decode($u_settings['pwa_config']) : ''; ?>
	<?php if (isset($settings['is_pwa']) && $settings['is_pwa'] == 1 && (isset($pwa->is_pwa_active) && $pwa->is_pwa_active == 1)) : ?>
		<?php if (is_feature($u_info['id'], 'pwa-push') == 1 && is_active($u_info['id'], 'pwa-push')) : ?>
			<div class="add-to">
				<div class="add-to-area">
					<a href="javascript:;" class="pwaClose"><i class="fa fa-close"></i></a>
					<div class="pwaContent">
						<img src="<?= asset_url(!empty($pwa->logo) ? $pwa->logo : "uploads/pwa/avatar.png"); ?>" alt="">
						<div class="pwaDetails">
							<h4><?= $pwa->title; ?></h4>
							<p><?= asset_url($slug); ?></p>
							<button class="add-to-btn btn btn-primary"><i class="icofont-bubble-down"></i> <?= lang('install_app'); ?></button>
						</div>
					</div>
				</div>
			</div>
			<script type="text/javascript">
				var base_url = '<?= asset_url(); ?>';
				var slug = '<?= $slug; ?>';

				if ('serviceWorker' in navigator) {
					navigator.serviceWorker.register(`${base_url}public/service-worker.js?slug=${slug}`, {
						scope: `${base_url}public/`
					}).then(function(registration) {
						// Registration was successful
						console.log('PWA: ServiceWorker registration successful with scope: ', registration.scope);
						if (registration.active) {
                registration.active.postMessage({ type: 'INIT', slug });
            }
					}).catch(function(err) {
						// Registration failed
						console.log('PWA: ServiceWorker registration failed: ', err);
					});
				}

				let isPwa = getCookie('isPwa');
				let deferredPrompt;
				var div = document.querySelector('.add-to');
				var button = document.querySelector('.add-to-btn');
				var button2 = document.querySelector('.pwaClose');
				div.style.display = 'none';

				window.addEventListener('beforeinstallprompt', (e) => {
					// Prevent Chrome 67 and earlier from automatically showing the prompt
					e.preventDefault();
					// Stash the event so it can be triggered later.
					deferredPrompt = e;
					if (isPwa == null || isPwa == 0 || isPwa == undefined) {
						$('body').addClass('visible');
						div.style.display = 'block';
					} else {
						div.style.display = 'none';
					}

					button2.addEventListener('click', (e) => {
						setCookie('isPwa', '1', 500);
						div.style.display = 'none';
						$('body').removeClass('visible');
					});

					button.addEventListener('click', (e) => {
						// hide our user interface that shows our A2HS button
						div.style.display = 'none';
						// Show the prompt
						deferredPrompt.prompt();
						// Wait for the user to respond to the prompt
						deferredPrompt.userChoice
							.then((choiceResult) => {
								if (choiceResult.outcome === 'accepted') {
									console.log('User accepted the A2HS prompt');
								} else {
									console.log('User dismissed the A2HS prompt');
								}
								deferredPrompt = null;
							});
					});
				});
			</script>
		<?php endif; ?><!-- is_feature -->
	<?php endif; ?>
<?php endif; ?>
<!-- footer pwa -->