<?php
// Register the widget
function register_event_countdown_widget() {
    register_widget( 'Event_Countdown_Widget' );
}
add_action( 'widgets_init', 'register_event_countdown_widget' );

// Event Countdown Widget class
class Event_Countdown_Widget extends WP_Widget {

    // Constructor
    public function __construct() {
        parent::__construct(
            'event_countdown_widget', // Base ID
            'Event Countdown Timer',   // Name
            array( 'description' => __( 'A widget to display countdown to an event.', 'oro' ), ) // Args
        );
    }

    // The widget form (for the admin dashboard)
    public function form( $instance ) {
        $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Event Countdown', 'oro' );
        $date = ! empty( $instance['date'] ) ? $instance['date'] : '';
        $number_color = ! empty( $instance['number_color'] ) ? $instance['number_color'] : '#ff6b6b';
        $label_color = ! empty( $instance['label_color'] ) ? $instance['label_color'] : '#888';
        ?>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'oro' ); ?></label>
            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
        </p>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'date' ) ); ?>"><?php _e( 'Event Date (YYYY-MM-DD):', 'oro' ); ?></label>
            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'date' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'date' ) ); ?>" type="text" value="<?php echo esc_attr( $date ); ?>">
        </p>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'number_color' ) ); ?>"><?php _e( 'Number Color:', 'oro' ); ?></label>
            <input class="widefat color-picker" id="<?php echo esc_attr( $this->get_field_id( 'number_color' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number_color' ) ); ?>" type="text" value="<?php echo esc_attr( $number_color ); ?>" data-default-color="#ff6b6b">
        </p>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'label_color' ) ); ?>"><?php _e( 'Label Color:', 'oro' ); ?></label>
            <input class="widefat color-picker" id="<?php echo esc_attr( $this->get_field_id( 'label_color' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'label_color' ) ); ?>" type="text" value="<?php echo esc_attr( $label_color ); ?>" data-default-color="#888">
    </p>
        <?php
    }

    // Update widget settings
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        $instance['date'] = ( ! empty( $new_instance['date'] ) ) ? strip_tags( $new_instance['date'] ) : '';
        $instance['number_color'] = ( ! empty( $new_instance['number_color'] ) ) ? strip_tags( $new_instance['number_color'] ) : '#ff6b6b';
        $instance['label_color'] = ( ! empty( $new_instance['label_color'] ) ) ? strip_tags( $new_instance['label_color'] ) : '#888';

        return $instance;
    }
// Display the widget on the front-end
public function widget( $args, $instance ) {
    $title = apply_filters( 'widget_title', $instance['title'] );
    $date = ! empty( $instance['date'] ) ? $instance['date'] : '';
    $number_color = isset( $instance['number_color'] ) ? $instance['number_color'] : '#ff6b6b'; // Default number color
    $label_color = isset( $instance['label_color'] ) ? $instance['label_color'] : '#888'; // Default label color

    echo $args['before_widget'];
    if ( ! empty( $title ) ) {
        echo $args['before_title'] . $title . $args['after_title'];
    }

    if ( ! empty( $date ) ) {
        ?>
        <div id="event-countdown" data-date="<?php echo esc_attr( $date ); ?>" class="countdown-wrapper" style="--number-color: <?php echo esc_attr( $number_color ); ?>; --label-color: <?php echo esc_attr( $label_color ); ?>;"></div>
        
        <?php
    }

    echo $args['after_widget'];
   }

}
