<?php
	/**
	 * Footer Template
	 */

	if ( ! defined( 'ABSPATH' ) ) {
		exit;
	}

?>
<footer class="g-f8845c0c" data-test-loading="50feea79d86" data-module-layout="08ee89" data-bs-dismiss="8911a1da9d0">
	<div class="g-63e2c1ff" id="elem-e6a47c79694545e8">
		<div class="g-2bfcbd63 g-2703a1fe g-80c439fc" data-analytics-focus="a53b20390c90" data-widget-mode="view" data-section-config="1bbe7d">
			<div class="g-51f0834a" id="elem-1d4b8a98ec4614fe">
				<div class="g-5fc905f7" data-widget-type="secondary" data-element-mode="disabled">
					<h2 class="g-f77c124e g-b4df0116" id="elem-16f53513a5a2fca0">
						Seasonal planting tips and care guides
					</h2>
					<p class="g-0db6563a g-c7fe552c g-6a050ff1" data-analytics-qa="ddbe5b9b4d" data-bs-track="4e73c3b525" data-block-trigger="967f971cf29" data-element-handler="7b5e67">
						Subscribe to our newsletter for practical advice on plant care, seasonal planting, and garden design, delivered straight to your inbox.
					</p>
				</div>
				<form
					class="g-4842923d"
					id="subscribe-form"
					novalidate
					id="elem-7472e8ad9d44a3a6"
				>
					<div class="g-8744af3e g-c7fe552c" data-analytics-loading="0a356bee23f1" data-element-position="right" data-element-state="success">
						Stay up to date with the latest news
					</div>
					<div class="g-4d4e0aac" id="elem-606753bee4f1b6e8">
						<input
							type="email"
							name="email"
							class="g-cfc2efde"
							placeholder="E-mail"
							required
							data-component-size="medium" data-analytics-keyboard="d44cb0ca2bd" data-bs-selector="2cae0a5e4" data-mui-metric="d298e306054" data-test-locator="911d8ccc76"
						>
						<button
							type="submit"
							class="g-2733e20c"
						>
                    Submit
						</button>
					</div>
					<label class="g-72ffc1cc g-8cdd9402" data-element-priority="low" data-test-size="medium" data-mui-label="d29825d3428">
						<input
							class="g-6f87bf5b"
							type="checkbox"
							name="policy"
							required
							id="elem-0e4bf428c071d922"
						>
						<span>
                    By submitting your email, you agree to the processing of personal data
		</span>
					</label>
					<div
						class="g-0a6c76ad"
						id="subscribe-message"
						aria-live="polite"
						data-bs-selected="b2606ae" data-widget-category="error" data-bs-target="91b93c"
					></div>
				</form>
			</div>
			<div class="g-de3b1193" id="elem-f35d368732011f75">
				<a href="<?php echo esc_url( home_url( '/' ) ); ?>" class="g-f2a450c4 g-6776af35"
				   aria-label="<?php bloginfo( 'name' ); ?>" data-bs-locator="79f497" data-analytics-locator="03e46140d7" data-module-role="content" data-analytics-wrap="d814515591c">
					<span class="g-22cc13dc">🌻</span>
					<strong class="g-4949fa0b"><?php bloginfo( 'name' ); ?></strong>
				</a>
				<nav id="footer-menu" role="navigation"
				     aria-label="<?php esc_attr_e( 'Primary Navigation', 'blankslate' ); ?>"
				     itemscope itemtype="https://schema.org/SiteNavigationElement" id="elem-997b44942cebf9bf">
					<?php wp_nav_menu( array(
						'theme_location' => 'main-menu',
					'fallback_cb'    => false,
						'container'      => 'ul',
						'menu_class'     => 'g-5f8717ef g-2dca56df',
						'link_before'    => '<span itemprop="name">',
						'link_after'     => '</span>'
					) ); ?>
				</nav>
			</div>
		</div>
		<div class="g-c298c324" data-module-animation="slide" data-analytics-interval="3a68c7218" data-module-content="86d05a2fa" data-analytics-cy="88ceae6bb2dd">
			<?php

				$privacy_page_id = get_option( 'wp_page_for_privacy' );
				if ( $privacy_page_id ) :

					?>
					<a href="<?php echo esc_url( get_permalink( $privacy_page_id ) ); ?>"
					   class="g-55e9c90f" id="elem-f4e2f2efece6dca1">
						<?php esc_html_e( 'Privacy Policy', 'theme' ); ?>
					</a>
				<?php endif; ?>
			<div class="g-b7bc242d" data-component-action="focus" data-mui-keyboard="09fb65b8a1cb" data-section-layout="053e4b">
				© <?php echo esc_html( date( 'Y' ) ); ?>
				<?php bloginfo( 'name' ); ?>.
				<?php esc_html_e( 'All rights reserved.', 'theme' ); ?>
			</div>
			<?php

				$terms_page_id = get_option( 'wp_page_for_terms' );
				if ( $terms_page_id ) :

					?>
					<a href="<?php echo esc_url( get_permalink( $terms_page_id ) ); ?>"
					   class="g-55e9c90f" id="elem-3cc04634d00ed441">
						<?php esc_html_e( 'Terms of Use', 'theme' ); ?>
					</a>
				<?php endif; ?>
		</div>
	</div>
</footer>
<div class="g-be1a5f17" id="subscribe-toast" id="elem-0d5cfdc9c7573989"></div>
<script>
    document.addEventListener('DOMContentLoaded', () => {

        // Main subscription form
        const form = document.getElementById('subscribe-form');

        // Toast notification element
        const toast = document.getElementById('subscribe-toast');

        // Message container (reserved for future UI updates)
        const message = document.getElementById('subscribe-message');

        // Abort initialization if required dependencies are missing
        if (
            !form ||
            !toast ||
            typeof subscribeData === 'undefined'
        ) {
            return;
        }

        /**
         * Display temporary notification.
         *
         * TODO:
         * move this into shared notification component
         * once additional forms are implemented.
         */
        const showToast = (text) => {

            toast.textContent = text;

            toast.classList.add('g-34e766e1');

            clearTimeout(toast.dismissTimer);

            toast.dismissTimer = setTimeout(() => {

                toast.classList.remove('g-34e766e1');

            }, 3000);

        };

        /**
         * Basic client-side validation.
         *
         * Server validation still remains mandatory.
         */
        const validateEmail = (email) => {

            return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);

        };

        form.addEventListener('submit', (event) => {

            event.preventDefault();

            message.textContent = '';
            message.className = 'g-0a6c76ad';

            const emailField =
                form.querySelector('[name="email"]');

            const policyCheckbox =
                form.querySelector('[name="policy"]');

            const submitButton =
                form.querySelector('button');

            const email = emailField.value.trim();

            if (!validateEmail(email)) {

                showToast('Please enter a valid email');

                return;
            }

            if (!policyCheckbox.checked) {

                showToast(
                    'You must agree to the personal data processing policy'
                );

                return;
            }

            submitButton.disabled = true;

            showToast('Sending...');

            const payload = new FormData();

            payload.append(
                'action',
                'subscribe'
            );

            payload.append(
                'email',
                email
            );

            payload.append(
                'nonce',
                subscribeData.nonce
            );

            fetch(subscribeData.ajaxUrl, {
                method: 'POST',
                body: payload,
                credentials: 'same-origin'
            })
                .then(response => response.json())

                .then(data => {

                    if (data.success) {

                        form.reset();

                    }

                    showToast('Thank you! Your information has been successfully sent.');

                })

                .catch(() => {

                    showToast(
                        'Something went wrong'
                    );

                })

                .finally(() => {

                    submitButton.disabled = false;

                });

        });

    });
</script>