<?php

class Themesflat_Course_Details extends WP_Widget {
	/**
	 * Holds widget settings defaults, populated in constructor.
	 *
	 * @var array
	 */
	protected $defaults;

	/**
	 * Constructor
	 *
	 * @return Themesflat_Latest_News
	 */
	function __construct() {
		$this->defaults = array(
			'button_url' => '#',
		);
		parent::__construct(
			'course_details',
			esc_html__( 'Themesflat - Course Details', 'themesflat' )
		);
	}

	/**
	 * Display widget
	 */
	function widget( $args, $instance ) {
		$instance = wp_parse_args( $instance, $this->defaults );
		extract( $instance );
		extract( $args );
		$id = get_the_ID();

		$classes[]           = 'tf-course-details';
		$price               = get_post_meta( $id, 'tf_course_price', true );
		$theory_session      = get_post_meta( $id, 'tf_course_theory_session', true );
		$practical_session   = get_post_meta( $id, 'tf_course_practical_session', true );
		$certificate_quality = get_post_meta( $id, 'tf_course_certificate_quality', true );
		echo wp_kses_post( $before_widget );
		?>
        <ul class="<?php echo esc_attr( implode( ' ', $classes ) ); ?> clearfix">
            <li class="courses-details-item">
                <div class="price-wrap">
                    <sup><?php esc_html_e( '$', 'themesflat' ) ?></sup>
                    <span class="price"><?php echo esc_html( $price ) ?></span><?php esc_html_e( '/Person', 'themesflat' ) ?>
                </div>
            </li>
            <li class="courses-details-item">
                <div class="icon"><img src="<?php echo esc_url( THEMESFLAT_URL . 'assets/img/theory-session.png' ) ?>" alt="session">
                </div>
                <div class="content">
                    <span class="details-label"><?php esc_html_e( 'Theory session' ) ?></span>
                    <span class="details-value"><?php echo esc_html( $theory_session ) ?></span>
                </div>
            </li>
            <li class="courses-details-item">
                <div class="icon"><img
                            src="<?php echo esc_url( THEMESFLAT_URL . 'assets/img/certificate-quality.png' ) ?>" alt="quality"></div>
                <div class="content">
                    <span class="details-label"><?php esc_html_e( 'Practical session' ) ?></span>
                    <span class="details-value"><?php echo esc_html( $practical_session ) ?></span>
                </div>
            </li>
            <li class="courses-details-item">
                <div class="icon"><img
                            src="<?php echo esc_url( THEMESFLAT_URL . 'assets/img/practise-session.png' ) ?>" alt="session"></div>
                <div class="content">
                    <span class="details-label"><?php esc_html_e( 'Certificate quality' ) ?></span>
                    <span class="details-value"><?php echo esc_html( $certificate_quality ) ?></span>
                </div>
            </li>
        </ul>
		<?php
        if ( ! empty( $button_url ) ): ?>
            <a href="<?php echo esc_url( $button_url ) ?>"
               class="btn btn-secondary btn-start-course btn-block"><?php esc_html_e( 'Start Course', 'themesflat' ) ?></a>
		<?php endif; ?>
		<?php echo wp_kses_post( $after_widget );
	}

	/**
	 * Update widget
	 */
	function update( $new_instance, $old_instance ) {
		$instance          = $old_instance;
		$instance['button_url'] = strip_tags( $new_instance['button_url'] );

		return $instance;
	}

	/**
	 * Widget setting
	 */
	function form( $instance ) {
		$instance = wp_parse_args( $instance, $this->defaults );
		?>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'button_url' ) ); ?>"><?php esc_html_e( 'Button Url:', 'themesflat' ); ?></label>
            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'button_url' ) ); ?>"
                   name="<?php echo esc_attr( $this->get_field_name( 'button_url' ) ); ?>" type="text"
                   value="<?php echo esc_attr( $instance['button_url'] ); ?>">
        </p>
		<?php
	}
}

add_action( 'widgets_init', 'themesflat_register_course_details' );

/**
 * Register widget
 *
 * @return void
 * @since 1.0
 */
function themesflat_register_course_details() {
	register_widget( 'Themesflat_Course_Details' );
}