<?php

class envy_chimp_widget_function extends WP_Widget {
    public function __construct() {
        parent::__construct('envy_chimp_widget_function', __('EnvyChimp', 'envy-chimp'), array(
            'description' => __('This is mailchimp widget.', 'envy-chimp')
        ));
    }
    
    public function widget( $args, $instance ) {
        $title = $instance['title'];
            if( ! $title == '' ) : ?>
            <h2 style="margin:20px 0;"><?php echo wp_kses_post( $args['before_widget'].$args['before_title'].$title.$args['after_title'].$args['after_widget'] ); ?></h2>
            <?php endif; 

            $form_type = $instance['form_type'];
            if( $form_type == 'subscribe' ) :
                echo do_shortcode("[envy_chimp_subscribe_form]");
            elseif( $form_type == 'login-signup' ) :
                echo do_shortcode("[envy_chimp_login_signup_form]");
            elseif( $form_type == 'login' ) :
                echo do_shortcode("[envy_chimp_login_form]");
            elseif( $form_type == 'signup' ) :
                echo do_shortcode("[envy_chimp_signup_form]");
            endif; ?>

            <style>
                .envychimp-subscribe-area .envychimp-subscribe-form .envychimp-subscribe-content .newsletter-form {
                    margin-bottom: 0px;
                }
                .envychimp-subscribe-area-two .envychimp-subscribe-form .envychimp-subscribe-content .newsletter-form {
                    margin-bottom: 20px;
                }
                .envychimp-subscribe-area .envychimp-subscribe-form .envychimp-subscribe-content ul li {
                    display: inline;
                }
                .envychimp-subscribe-area .envychimp-subscribe-form .envychimp-subscribe-content ul li span {
                    display: block;
                    margin-bottom: 20px;
                }
                .envychimp-subscribe-area .envychimp-subscribe-form .envychimp-subscribe-content ul li a {
                    text-decoration: none;
                }
                .envychimp-form-control {
                    height: 50px !important;
                }
                .envychimp-default-btn {
                    height: 50px !important;
                    font-size: 14px;
                }
                #reg_passmail {
                    font-size: 15px;
                }
                .envychimp-subscribe-area.envychimp-subscribe-area-four .envychimp-subscribe-form .newsletter-form .envychimp-form-group .envychimp-default-btn {
                    top: -50px;
                }
            </style>    
    <?php }

    public function form( $instance ) {
        if( isset($instance['title']) ) :
            $title = $instance['title'];
        endif;
        if( isset($instance['form_type']) ) :
            $form_type = $instance['form_type'];
        endif;                 
    ?>

    <P>
        <label for="<?php echo esc_html__( $this->get_field_id('title') ); ?>"><?php echo esc_html__('Title:', 'envy-chimp'); ?></label>
       <input type="text" class="widefat" id="<?php echo esc_html__( $this->get_field_id('title') ); ?>" name="<?php echo esc_html__( $this->get_field_name('title') ); ?>" value="<?php if( isset( $title ) ) : echo esc_attr( $title ); endif; ?>">
    </p>

    <p>
        <label for="<?php echo esc_html__( $this->get_field_id( 'form_type' ) ); ?>"><?php  echo esc_html__( 'Form Type:' ); ?></label>
        <select class="widefat" id="<?php echo $this->get_field_id( 'form_type' ); ?>" name="<?php echo $this->get_field_name( 'form_type' ); ?>">
        <option value="subscribe" <?php if(isset($form_type)) : selected($form_type, "subscribe"); endif; ?>><?php echo esc_html__('Subscribe', 'envy-popup'); ?>
        </option>
        <option value="login-signup" <?php if(isset($form_type)) : selected($form_type, "login-signup"); endif; ?>><?php echo esc_html__('Login & Signup', 'envy-popup'); ?>
        </option>
        <option value="login" <?php if(isset($form_type)) : selected($form_type, "login"); endif; ?>><?php echo esc_html__('Login', 'envy-popup'); ?>
        </option>
        <option value="signup" <?php if(isset($form_type)) : selected($form_type, "signup"); endif; ?>><?php echo esc_html__('Signup', 'envy-popup'); ?>
        </option>         
        </select>
    </p>

    <?php }
}

function envy_chimp_custom_widget() {
    register_widget('envy_chimp_widget_function');
}

add_action('widgets_init', 'envy_chimp_custom_widget');
