<?php

/*
Plugin Name: SubscribeToLc_Widget
Description: A plugin subscribe containing form thous sending email to livecarta API
Version: 0.1
Author: LiveCarta
 */

class SubscribeToLc_Widget extends WP_Widget
{

    public function __construct()
    {
        parent::__construct(
            'subscribe_to_lc_widget',
            __('Subscribe to lc Widget', 'subscribetolc'),
            [
                'classname' => 'subscribetolc_widget',
            ]
        );
    }

    public function widget($args, $instance)
    {
        extract($args);
        if (empty($code)) {
            return;
        }

        wp_enqueue_script('subscribe-to-lc-js', plugin_dir_url(__FILE__).'js/subscribe.js');

        ?>

        <div id="mc_embed_signup">
            <form action="<?= LAWCARTA_WP_REST_API_URL.'/subscribe/add-member' ?>" method="post" name="mc-embedded-subscribe-form" class="lc-subscrive-form">
                <div id="mc_embed_signup_scroll">
                    <input type="email" value="" id="subscribe-email" name="Subscribe[email]" class="email" placeholder="Enter your Email" required>

                    <input type="hidden" id="subscribe-from-id" name="Subscribe[formId]" tabindex="-1" value="<?= $code ?>">
                    <input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button xxl mp-top-right">
                </div>
            </form>
        </div>

        <?php
    }

    public function form($instance)
    {
    }

    // Updating widget replacing old instances with new
    public function update($new_instance, $old_instance)
    {
    }

}

/* Register the widget */
add_action('widgets_init', function () {
    register_widget('SubscribeToLc_Widget');
});
