<?php
add_action('wp_ajax_lpdisclaimer', 'lp_disclaimer');
add_action('wp_ajax_nopriv_lpdisclaimer', 'lp_disclaimer');

function lp_disclaimer()
{
    $nonce = $_POST['nonce'];

    if (!wp_verify_nonce($nonce, 'lp-application-ajax-nonce'))
        wp_send_json_error('Invalid nonce');
    // die('Something went wrong... Please try again later !');
    $lang = $_POST['lang'];
?>

    <section id="disclaimer">
        <div class="col-left">
            <img src="<?php echo get_template_directory_uri() ?>/dist/assets/img/bg-splash.jpg" alt="" class="desktop-only">
            <img src="<?php echo get_template_directory_uri() ?>/dist/assets/img/bg-splash-m.jpg" alt="" class="mobile-only">
        </div>
        <div class="col-right">
            <div class="wrapper">
                <form action="" method="post" name="disclaimer" class="material-form">
                    <div id="logo">
                        <img style="max-width: 250px" src="<?php echo get_template_directory_uri() ?>/dist/assets/img/laurent-perrier.svg" alt="<?php echo get_bloginfo('name'); ?>">
                    </div>
                    <div class="text">
                        <?php echo __('Pour visiter le site internet de Laurent-Perrier,<br>
                        vous devez-être en âge légal pour consommer de l’alcool
                        dans votre pays/région. S’il n’y a pas d’âge légal
                        de consommation, vous devez avoir plus de 21ans.', 'laurentperrier');
                        ?>
                    </div>

                    <div class="form-row">
                        <div class="select">
                            <select name="countries" id="countries" class="select2">
                                <?php if ($lang == "fr") : ?>
                                    <option value="-" data-lang="fr" data-age="18"><?php _e('Pays de résidence', 'laurentperrier'); ?></option>
                                <?php else : ?>
                                    <option value="-" data-lang="en" data-age="21"><?php _e('Select your country', 'laurentperrier'); ?></option>
                                <?php endif; ?>
                                <?php
                                $args = array(
                                    'orderby'   => 'name',
                                    'order'   => 'ASC',
                                    'post_type'   => 'disclaimer',
                                    'posts_per_page' => -1
                                );
                                query_posts($args);
                                if (have_posts()) : while (have_posts()) : the_post();
                                ?>
                                        <option value="<?php the_title(); ?>" data-lang="<?php the_field('langue'); ?>" data-age="<?php the_field('age'); ?>"><?php the_title(); ?></option>
                                <?php endwhile;
                                endif;
                                wp_reset_query(); ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-row form-row__nomaterial">
                        <div class="year">
                            <label for=""><?php echo __('Année de naissance', 'laurentperrier') ?></label>
                            <input type="number" name="year" min="1930" max="2010" placeholder="<?php echo __('AAAA', 'laurentperrier') ?>" class="input_year" id="inputYear">
                        </div>
                    </div>
                    <div>
                        <button type="submit" class="btn"><?php echo __('entrer', 'laurentperrier') ?></button>
                    </div>


                    <div id="error">
                        <?php
                        echo __('Erreur de saisie', 'laurentperrier');
                        ?>
                    </div>
                </form>
            </div>
        </div>
    </section>

<?php die();
} ?>