<?php

if (!defined('ABSPATH')) {
    exit;
}

/*
|--------------------------------------------------------------------------
| ADMIN PAGE
|--------------------------------------------------------------------------
*/

function gigr_landingpage_page() {

    /*
    |--------------------------------------------------------------------------
    | Save settings
    |--------------------------------------------------------------------------
    */

    if (isset($_POST['gigr_save_settings'])) {

        update_option(
            'gigr_enable_landing',
            isset($_POST['gigr_enable_landing']) ? 1 : 0
        );

        update_option(
            'gigr_slogan',
            sanitize_text_field(
                $_POST['gigr_slogan']
            )
        );

        update_option(
            'gigr_subtext',
            sanitize_text_field(
                $_POST['gigr_subtext']
            )
        );

        update_option(
            'gigr_button_text',
            sanitize_text_field(
                $_POST['gigr_button_text']
            )
        );

        update_option(
            'gigr_show_waiting_count',
            isset($_POST['gigr_show_waiting_count']) ? 1 : 0
        );

        echo '
        <div class="updated">
            <p>Inställningar sparade.</p>
        </div>
        ';
    }

    /*
    |--------------------------------------------------------------------------
    | Settings
    |--------------------------------------------------------------------------
    */

    $enabled = get_option(
        'gigr_enable_landing',
        0
    );

    $slogan = get_option(
        'gigr_slogan',
        'Get the work done.'
    );

    $subtext = get_option(
        'gigr_subtext',
        'Snart lansering'
    );

    $button_text = get_option(
        'gigr_button_text',
        'Notify me'
    );

    $show_waiting_count = get_option(
        'gigr_show_waiting_count',
        1
    );

    /*
    |--------------------------------------------------------------------------
    | Signups
    |--------------------------------------------------------------------------
    */

    $signups = get_option(
        'gigr_signup_emails',
        []
    );

    ?>

    <div class="wrap">

        <div style="
            display:flex;
            justify-content:space-between;
            align-items:center;
            margin-bottom:20px;
            flex-wrap:wrap;
            gap:12px;
        ">

            <h1 style="margin:0;">
                Gigr Landingpage
            </h1>

            <a
                href="<?php echo admin_url(
                    'admin-post.php?action=gigr_export_csv'
                ); ?>"
                class="button button-secondary"
            >

                Export CSV

            </a>

        </div>

        <form method="post">

            <table class="form-table">

                <tr>

                    <th>
                        Aktivera landingpage
                    </th>

                    <td>

                        <input
                            type="checkbox"
                            name="gigr_enable_landing"
                            <?php checked($enabled, 1); ?>

                        >

                    </td>

                </tr>

                <tr>

                    <th>
                        Slogan
                    </th>

                    <td>

                        <input
                            type="text"
                            class="regular-text"
                            name="gigr_slogan"
                            value="<?php echo esc_attr($slogan); ?>"
                        >

                    </td>

                </tr>

                <tr>

                    <th>
                        Subtext
                    </th>

                    <td>

                        <input
                            type="text"
                            class="regular-text"
                            name="gigr_subtext"
                            value="<?php echo esc_attr($subtext); ?>"
                        >

                    </td>

                </tr>

                <tr>

                    <th>
                        Knapptext
                    </th>

                    <td>

                        <input
                            type="text"
                            class="regular-text"
                            name="gigr_button_text"
                            value="<?php echo esc_attr($button_text); ?>"
                        >

                    </td>

                </tr>

                <tr>

                    <th>
                        Visa väntelista-text
                    </th>

                    <td>

                        <label style="
                            display:flex;
                            align-items:center;
                            gap:10px;
                        ">

                            <input
                                type="checkbox"
                                name="gigr_show_waiting_count"
                                <?php checked($show_waiting_count, 1); ?>
                            >

                            Visa:
                            "X personer väntar på lanseringen"

                        </label>

                    </td>

                </tr>

            </table>

            <p>

                <button
                    class="button button-primary"
                    name="gigr_save_settings"
                >

                    Spara

                </button>

            </p>

        </form>

        <hr style="margin:40px 0;">

        <h2>
            Waiting List
        </h2>

        <p>

            Totalt:
            <strong>
                <?php echo count($signups); ?>
            </strong>

        </p>

        <table class="widefat striped">

            <tr>

                <th>E-post</th>

                <th>Datum</th>

                <th>Enhet</th>

                <th>IP</th>

            </tr>

            <?php foreach (
                array_reverse($signups)
                as $signup
            ) : ?>

                <tr>

                    <td>

                        <?php
                        echo esc_html(
                            $signup['email']
                        );
                        ?>

                    </td>

                    <td>

                        <?php
                        echo esc_html(
                            $signup['time']
                        );
                        ?>

                    </td>

                    <td>

                        <?php
                        echo esc_html(
                            $signup['device']
                        );
                        ?>

                    </td>

                    <td>

                        <?php
                        echo esc_html(
                            gigr_mask_ip(
                                $signup['ip']
                            )
                        );
                        ?>

                    </td>

                </tr>

            <?php endforeach; ?>

        </table>

    </div>

    <?php
}

/*
|--------------------------------------------------------------------------
| SIGNUP HANDLER
|--------------------------------------------------------------------------
*/

add_action(
    'admin_post_nopriv_gigr_signup',
    'gigr_signup_handler'
);

add_action(
    'admin_post_gigr_signup',
    'gigr_signup_handler'
);

function gigr_signup_handler() {

    /*
    |--------------------------------------------------------------------------
    | Honeypot
    |--------------------------------------------------------------------------
    */

    if (!empty($_POST['company'])) {

        wp_redirect(
            home_url('?signup=blocked')
        );

        exit;
    }

    /*
    |--------------------------------------------------------------------------
    | Cooldown
    |--------------------------------------------------------------------------
    */

    $ip = gigr_get_ip();

    $cooldown_key =
        'gigr_signup_' . md5($ip);

    if (get_transient($cooldown_key)) {

        wp_redirect(
            home_url('?signup=wait')
        );

        exit;
    }

    set_transient(
        $cooldown_key,
        true,
        60
    );

    /*
    |--------------------------------------------------------------------------
    | Email
    |--------------------------------------------------------------------------
    */

    $email = sanitize_email(
        $_POST['gigr_email'] ?? ''
    );

    if (!is_email($email)) {

        wp_redirect(
            home_url('?signup=invalid')
        );

        exit;
    }

    /*
    |--------------------------------------------------------------------------
    | Existing signups
    |--------------------------------------------------------------------------
    */

    $signups = get_option(
        'gigr_signup_emails',
        []
    );

    foreach ($signups as $signup) {

        if (
            strtolower($signup['email'])
            === strtolower($email)
        ) {

            wp_redirect(
                home_url('?signup=exists')
            );

            exit;
        }
    }

    /*
    |--------------------------------------------------------------------------
    | Save signup
    |--------------------------------------------------------------------------
    */

    $signups[] = [

        'email' => $email,

        'time' => current_time(
            'mysql'
        ),

        'ip' => gigr_get_ip(),

        'device' => gigr_get_device()
    ];

    if (count($signups) > 1000) {

        $signups = array_slice(
            $signups,
            -1000
        );
    }

    update_option(
        'gigr_signup_emails',
        $signups
    );

    wp_redirect(
        home_url('?signup=success')
    );

    exit;
}

/*
|--------------------------------------------------------------------------
| FULLSCREEN LANDINGPAGE
|--------------------------------------------------------------------------
*/

add_action('template_redirect', function () {

    if (is_admin()) {
        return;
    }

    if (
        current_user_can(
            'manage_options'
        )
    ) {
        return;
    }

    $enabled = get_option(
        'gigr_enable_landing',
        0
    );

    if (!$enabled) {
        return;
    }

    $slogan = get_option(
        'gigr_slogan',
        'Get the work done.'
    );

    $subtext = get_option(
        'gigr_subtext',
        'Snart lansering'
    );

    $button_text = get_option(
        'gigr_button_text',
        'Notify me'
    );

    $show_waiting_count = get_option(
        'gigr_show_waiting_count',
        1
    );

    $signups = get_option(
        'gigr_signup_emails',
        []
    );

    $count = count($signups);

    status_header(200);

    nocache_headers();

    ?>
    <!DOCTYPE html>

    <html <?php language_attributes(); ?>>

    <head>

        <meta charset="<?php bloginfo('charset'); ?>">

        <meta
            name="viewport"
            content="width=device-width, initial-scale=1"
        >

        <title>
            Gigr — Get the work done
        </title>

        <link
        rel="icon"
        href="<?php echo GIGR_PLUGIN_URL; ?>assets/img/gigr-ikon.png"
        >

        <link
        rel="stylesheet"
        href="<?php echo GIGR_PLUGIN_URL; ?>assets/css/landingpage.css"
        >

    </head>

    <body class="gigr-landing-active">

        <div class="gigr-box">

            <img
                src="<?php echo GIGR_PLUGIN_URL; ?>assets/img/gigr-get_the_work_done.png"
                class="gigr-main-logo"
            >

            <h1>

                <?php
                echo esc_html($slogan);
                ?>

            </h1>

            <div class="subtext">

                <?php
                echo esc_html($subtext);
                ?>

            </div>

            <?php if (isset($_GET['signup'])) : ?>

                <?php if (
                    $_GET['signup'] === 'success'
                ) : ?>

                    <div class="gigr-success-card">

                        <div class="gigr-success-icon">
                            ✓
                        </div>

                        <div>

                            Tack! Du är nu med på väntelistan.

                        </div>

                    </div>

                <?php endif; ?>

                <?php if (
                    $_GET['signup'] === 'exists'
                ) : ?>

                    <div class="gigr-success-card">

                        Den e-postadressen finns redan registrerad.

                    </div>

                <?php endif; ?>

                <?php if (
                    $_GET['signup'] === 'wait'
                ) : ?>

                    <div class="gigr-success-card">

                        Vänta lite innan du försöker igen.

                    </div>

                <?php endif; ?>

            <?php endif; ?>

            <form
                class="signup"
                method="post"
                action="<?php echo esc_url(
                    admin_url('admin-post.php')
                ); ?>"
            >

                <input
                    type="hidden"
                    name="action"
                    value="gigr_signup"
                >

                <input
                    type="text"
                    name="company"
                    tabindex="-1"
                    autocomplete="off"
                    style="
                        position:absolute;
                        left:-9999px;
                        opacity:0;
                    "
                >

                <input
                    type="email"
                    name="gigr_email"
                    placeholder="Din e-postadress"
                    required
                >

                <button>

                    <?php
                    echo esc_html($button_text);
                    ?>

                </button>

            </form>

            <?php if ($show_waiting_count) : ?>

                <div class="waiting">

                    <?php
                    echo esc_html($count);
                    ?>
                    personer väntar på lanseringen

                </div>

            <?php endif; ?>

        </div>

    </body>

    </html>
    <?php

    exit;
});