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

class IDSC_Elementor_Testimonial extends \Elementor\Widget_Base {

    public function get_name() {
        return 'idsc_testimonial';
    }

    public function get_title() {
        return __( 'Testimonial', 'idsc' );
    }

    public function get_icon() {
        return 'eicon-testimonial';
    }

    public function get_categories() {
        return array( 'idsc' );
    }

    public function get_keywords() {
        return array( 'testimonial', 'quote', 'review', 'rating' );
    }

    protected function register_controls() {
        $this->start_controls_section( 'section_content', array(
            'label' => __( 'Testimonial', 'idsc' ),
        ) );

        $this->add_control( 'content', array(
            'label' => __( 'Quote', 'idsc' ),
            'type'  => \Elementor\Controls_Manager::TEXTAREA,
        ) );

        $this->add_control( 'author', array(
            'label' => __( 'Author', 'idsc' ),
            'type'  => \Elementor\Controls_Manager::TEXT,
        ) );

        $this->add_control( 'role', array(
            'label' => __( 'Role / Title', 'idsc' ),
            'type'  => \Elementor\Controls_Manager::TEXT,
        ) );

        $this->add_control( 'photo', array(
            'label' => __( 'Author Photo', 'idsc' ),
            'type'  => \Elementor\Controls_Manager::MEDIA,
        ) );

        $this->add_control( 'rating', array(
            'label'   => __( 'Star Rating', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => '0',
            'options' => array(
                '0' => __( 'None', 'idsc' ),
                '1' => '1',
                '2' => '2',
                '3' => '3',
                '4' => '4',
                '5' => '5',
            ),
        ) );

        $this->add_control( 'layout', array(
            'label'   => __( 'Layout', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'standard',
            'options' => array(
                'standard' => __( 'Standard', 'idsc' ),
                'card'     => __( 'Card', 'idsc' ),
                'centered' => __( 'Centered', 'idsc' ),
            ),
        ) );

        $this->end_controls_section();
    }

    protected function render() {
        $s = $this->get_settings_for_display();

        $photo_id = ! empty( $s['photo']['id'] ) ? $s['photo']['id'] : '';

        $atts  = ! empty( $s['author'] ) ? ' author="' . esc_attr( $s['author'] ) . '"' : '';
        $atts .= ! empty( $s['role'] ) ? ' role="' . esc_attr( $s['role'] ) . '"' : '';
        $atts .= ! empty( $photo_id ) ? ' photo="' . esc_attr( $photo_id ) . '"' : '';
        $atts .= $s['rating'] !== '0' ? ' rating="' . esc_attr( $s['rating'] ) . '"' : '';
        $atts .= ' layout="' . esc_attr( $s['layout'] ) . '"';

        echo do_shortcode( '[idsc_testimonial' . $atts . ']' . esc_html( $s['content'] ) . '[/idsc_testimonial]' );
    }
}
