<?php
if ( ! class_exists( 'Draly_Dropshipping_Starter_Elementor_Countdowndeal' ) ) {
	class Draly_Dropshipping_Starter_Elementor_Countdowndeal extends Draly_Dropshipping_Starter_Elementor {
		public $name = 'countdowndeal';
		public $title = 'Countdown Deal';
		public $icon = 'eicon-countdown';

		/**
		 * Register the widget controls.
		 *
		 * Adds different input fields to allow the user to change and customize the widget settings.
		 *
		 * @since 1.0.0
		 *
		 * @access protected
		 */
		protected function register_controls() {

			$this->start_controls_section(
				'general_section',
				[
					'label' => esc_html__( 'General', 'draly-dropshipping-starter' ),
				]
			);
			$this->add_control(
				'style',
				[
					'label'       => esc_html__( 'Style', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::SELECT,
					'options'     => array(
						'style-01' => esc_html__( 'Style 01', 'draly-dropshipping-starter' ),
						'style-02' => esc_html__( 'Style 02', 'draly-dropshipping-starter' ),
					),
					'default'     => 'style-01',
					'label_block' => true
				]
			);
			$this->add_control(
				'date_time',
				[
					'label'       => esc_html__( 'Countdowndeal', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::DATE_TIME,
					'label_block' => true
				]
			);
			$this->add_control(
				'text_color',
				[
					'label'     => esc_html__( 'Text Color', 'draly-dropshipping-starter' ),
					'type'      => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .draly-countdown > span' => 'color: {{VALUE}}',
					],
				]
			);
			$this->add_control(
				'background_color',
				[
					'label'     => esc_html__( 'Background Color', 'draly-dropshipping-starter' ),
					'type'      => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .draly-countdown > span' => 'background-color: {{VALUE}}',
					],
				]
			);
			$this->add_control(
				'border_color',
				[
					'label'     => esc_html__( 'Border Color', 'draly-dropshipping-starter' ),
					'type'      => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .draly-countdown > span' => 'border-color: {{VALUE}}',
					],
				]
			);
			$this->end_controls_section();

		}

		/**
		 * Render the widget output on the frontend.
		 *
		 * Written in PHP and used to generate the final HTML.
		 *
		 * @since 1.0.0
		 *
		 * @access protected
		 */
		protected function render() {
			$atts        = $this->get_settings_for_display();
			$css_class   = array( 'draly-countdowndeal' );
			$css_class[] = $atts['style'];
			if ( ! empty( $atts['date_time'] ) ) {
				$target  = new DateTime( $atts['date_time'] );
				$now     = new DateTime();
				$diff    = $now->diff( $target );
				$days    = $diff->format( '%a' );
				$hours   = str_pad( $diff->format( '%h' ), 2, '0', STR_PAD_LEFT );
				$minutes = str_pad( $diff->format( '%i' ), 2, '0', STR_PAD_LEFT );
				$seconds = str_pad( $diff->format( '%s' ), 2, '0', STR_PAD_LEFT );
				?>
                <div class="<?php echo esc_attr( implode( ' ', $css_class ) ); ?>">
                    <div class="draly-countdown"
                         data-datetime="<?php echo esc_attr( date( $atts['date_time'] ) ); ?>">
                        <span class="days">
                            <span class="number"><?php echo esc_html( $days ); ?></span>
                            <span class="text"><?php echo esc_html__( 'Days', 'draly-dropshipping-starter' ) ?></span>
                        </span>
                        <span class="hour">
                            <span class="number"><?php echo esc_html( $hours ); ?></span>
                            <span class="text"><?php echo esc_html__( 'Hours', 'draly-dropshipping-starter' ) ?></span>
                        </span>
                        <span class="mins">
                            <span class="number"><?php echo esc_html( $minutes ); ?></span>
                            <span class="text"><?php echo esc_html__( 'Mins', 'draly-dropshipping-starter' ) ?></span>
                        </span>
                        <span class="secs">
                            <span class="number"><?php echo esc_html( $seconds ); ?></span>
                            <span class="text"><?php echo esc_html__( 'Secs', 'draly-dropshipping-starter' ) ?></span>
                        </span>
                    </div>
                </div>
				<?php
			}
		}
	}
}