<?php
if ( ! defined( 'ABSPATH' ) ) exit;

class Vista_Testimonials_Widget extends \Elementor\Widget_Base {

	public function get_name() { return 'vista-testimonials'; }
	public function get_title() { return esc_html__( 'رضایت مشتریان', 'vista' ); }
	public function get_icon() { return 'eicon-testimonial-carousel'; }
	public function get_categories() { return [ 'vista-widgets' ]; }
	public function get_script_depends() { return [ 'swiper', 'vista-widgets' ]; }
	public function get_style_depends() { return [ 'swiper' ]; }

	protected function register_controls() {
		$this->start_controls_section( 'section_items', [
			'label' => esc_html__( 'نظرات', 'vista' ),
			'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
		] );

		$repeater = new \Elementor\Repeater();
		$repeater->add_control( 'reviewer_name', [ 'label' => 'نام', 'type' => \Elementor\Controls_Manager::TEXT, 'default' => 'سارا احمدی' ] );
		$repeater->add_control( 'reviewer_role', [ 'label' => 'سمت', 'type' => \Elementor\Controls_Manager::TEXT, 'default' => 'مدیر پروژه' ] );
		$repeater->add_control( 'reviewer_image', [ 'label' => 'تصویر', 'type' => \Elementor\Controls_Manager::MEDIA ] );
		$repeater->add_control( 'review_text', [ 'label' => 'متن نظر', 'type' => \Elementor\Controls_Manager::TEXTAREA, 'default' => 'متن نظر مشتری شما در اینجا قرار می‌گیرد.' ] );
		$repeater->add_control( 'star_rating', [ 'label' => 'امتیاز', 'type' => \Elementor\Controls_Manager::SELECT, 'default' => '5', 'options' => [ '1'=>'★','2'=>'★★','3'=>'★★★','4'=>'★★★★','5'=>'★★★★★' ] ] );

$this->add_control( 'testimonials_list', [
    'label' => esc_html__( 'لیست نظرات', 'vista' ),
    'type'  => \Elementor\Controls_Manager::REPEATER,
			'fields' => $repeater->get_controls(),
			'default' => [ [ 'reviewer_name' => 'سارا احمدی', 'star_rating' => '5' ] ],
			'title_field' => '{{{ reviewer_name }}}',
		] );
		$this->end_controls_section();

$this->start_controls_section( 'section_slider', [
    'label' => esc_html__( 'تنظیمات اسلایدر', 'vista' ),
    'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
] );
$this->add_control( 'autoplay', [
    'label'   => esc_html__( 'پخش خودکار', 'vista' ),
    'type'    => \Elementor\Controls_Manager::SWITCHER,
    'default' => 'yes',
] );
$this->add_control( 'autoplay_speed', [
    'label'     => esc_html__( 'سرعت (میلی‌ثانیه)', 'vista' ),
    'type'      => \Elementor\Controls_Manager::NUMBER,
    'default'   => 4000,
    'condition' => [ 'autoplay' => 'yes' ],
] );
$this->add_control( 'show_arrows', [
    'label'     => esc_html__( 'نمایش جهت‌ها (فلش)', 'vista' ),
    'type'      => \Elementor\Controls_Manager::SWITCHER,
    'default'   => 'yes',
    'separator' => 'before',
] );
$this->add_control( 'show_dots', [
    'label'   => esc_html__( 'نمایش نقاط (Dots)', 'vista' ),
    'type'    => \Elementor\Controls_Manager::SWITCHER,
    'default' => 'yes',
] );

		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$testimonials = $settings['testimonials_list'];
		if ( empty( $testimonials ) ) return;
		?>
		<div class="vista-testimonials-wrap" 
			 data-autoplay="<?php echo 'yes' === $settings['autoplay'] ? 'true' : 'false'; ?>" 
			 data-speed="<?php echo intval( $settings['autoplay_speed'] ); ?>">
			<div class="test-stage reveal">
				<div class="swiper">
					<div class="swiper-wrapper">
						<?php foreach ( $testimonials as $item ) : 
							$stars = str_repeat( '★', intval( $item['star_rating'] ) );
						?>
						<div class="swiper-slide">
							<div class="test-card">
								<div class="test-quote-mark">"</div>
								<div class="test-text"><?php echo esc_html( $item['review_text'] ); ?></div>
								<div class="test-author">
									<div class="test-avatar">
										<?php if ( ! empty( $item['reviewer_image']['url'] ) ) : ?>
											<img src="<?php echo esc_url( $item['reviewer_image']['url'] ); ?>" alt="">
										<?php else : ?>
											<i class="fa-solid fa-user"></i>
										<?php endif; ?>
									</div>
									<div>
										<div class="test-name"><?php echo esc_html( $item['reviewer_name'] ); ?></div>
										<div class="test-role"><?php echo esc_html( $item['reviewer_role'] ); ?></div>
									</div>
									<div class="test-stars"><?php echo esc_html( $stars ); ?></div>
								</div>
							</div>
						</div>
						<?php endforeach; ?>
					</div>
				</div>

				<?php if ( 'yes' === $settings['show_arrows'] || 'yes' === $settings['show_dots'] ) : ?>
				<div class="test-controls">
					<?php if ( 'yes' === $settings['show_arrows'] ) : ?>
						<div class="tctl-btn swiper-button-prev"></div>
					<?php endif; ?>

					<?php if ( 'yes' === $settings['show_dots'] ) : ?>
						<div class="tctl-dots"></div>
					<?php endif; ?>

					<?php if ( 'yes' === $settings['show_arrows'] ) : ?>
						<div class="tctl-btn swiper-button-next"></div>
					<?php endif; ?>
				</div>
				<?php endif; ?>

			</div>
		</div>
		<?php
	}
}