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

class IDSC_Elementor_Card extends \Elementor\Widget_Base {

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

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

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

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

    public function get_keywords() {
        return array( 'card', 'box', 'info', 'cta' );
    }

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

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

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

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

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

        $this->add_control( 'button_url', array(
            'label' => __( 'Button URL', 'idsc' ),
            'type'  => \Elementor\Controls_Manager::URL,
            'options' => false,
        ) );

        $this->add_control( 'image_pos', array(
            'label'   => __( 'Image Position', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'top',
            'options' => array(
                'top'   => __( 'Top', 'idsc' ),
                'left'  => __( 'Left', 'idsc' ),
                'right' => __( 'Right', 'idsc' ),
            ),
        ) );

        $this->end_controls_section();
    }

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

        $image_id = ! empty( $s['image']['id'] ) ? $s['image']['id'] : '';
        $btn_url  = ! empty( $s['button_url']['url'] ) ? $s['button_url']['url'] : '';

        $atts  = ! empty( $image_id ) ? ' image="' . esc_attr( $image_id ) . '"' : '';
        $atts .= ! empty( $s['title'] ) ? ' title="' . esc_attr( $s['title'] ) . '"' : '';
        $atts .= ! empty( $s['button_text'] ) ? ' button_text="' . esc_attr( $s['button_text'] ) . '"' : '';
        $atts .= ! empty( $btn_url ) ? ' button_url="' . esc_attr( $btn_url ) . '"' : '';
        $atts .= ' image_pos="' . esc_attr( $s['image_pos'] ) . '"';

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