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

/**
 * Lehká varianta widgetu pro Elementor editor na stránkách bez uloženého ElementSell widgetu.
 * Šetří ~2500 řádků PHP oproti plné verzi (widget-controls/render/content-template).
 */
class ElementSell_Widget extends \Elementor\Widget_Base {

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

    public function get_title() {
        return __( 'Formulář ElementSell', 'platebni-formular' );
    }

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

    public function get_categories() {
        return [ 'basic' ];
    }

    public function get_keywords() {
        return [ 'platební', 'formulář', 'elementsell', 'platba', 'produkt' ];
    }

    protected function register_controls() {
        $this->start_controls_section(
            'content_section',
            [
                'label' => __( 'Produkt a cena', 'platebni-formular' ),
                'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'product_name',
            [
                'label'   => __( 'Název produktu', 'platebni-formular' ),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => '',
            ]
        );

        $this->add_control(
            'button_text',
            [
                'label'   => __( 'Text tlačítka', 'platebni-formular' ),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => __( 'Zaplatit', 'platebni-formular' ),
            ]
        );

        $this->add_control(
            'amount',
            [
                'label'   => __( 'Částka (Kč)', 'platebni-formular' ),
                'type'    => \Elementor\Controls_Manager::NUMBER,
                'default' => 100,
                'min'     => 1,
                'step'    => 1,
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'fields_section',
            [
                'label' => __( 'Pole formuláře', 'platebni-formular' ),
                'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'show_name',
            [
                'label'        => __( 'Zobrazit pole Jméno', 'platebni-formular' ),
                'type'         => \Elementor\Controls_Manager::SWITCHER,
                'return_value' => 'yes',
                'default'      => 'yes',
            ]
        );

        $this->add_control(
            'show_email',
            [
                'label'        => __( 'Zobrazit pole Email', 'platebni-formular' ),
                'type'         => \Elementor\Controls_Manager::SWITCHER,
                'return_value' => 'yes',
                'default'      => 'yes',
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'payment_method_section',
            [
                'label' => __( 'Platební metoda', 'platebni-formular' ),
                'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $last_payment_method = function_exists( 'elementsell_get_last_payment_method' )
            ? elementsell_get_last_payment_method()
            : 'fio';

        $this->add_control(
            'payment_method',
            [
                'label'   => __( 'Platební metoda', 'platebni-formular' ),
                'type'    => \Elementor\Controls_Manager::SELECT,
                'default' => $last_payment_method,
                'options' => [
                    'fio'    => __( 'Účet Fio', 'platebni-formular' ),
                    'thepay' => __( 'Brána ThePay', 'platebni-formular' ),
                ],
            ]
        );

        $this->end_controls_section();
    }

    public function get_settings_for_display( $setting_key = null ) {
        $settings = parent::get_settings_for_display();

        if ( function_exists( 'elementsell_merge_payment_widget_global_defaults' ) ) {
            $settings = elementsell_merge_payment_widget_global_defaults( $settings );
        }

        return null !== $setting_key ? ( $settings[ $setting_key ] ?? null ) : $settings;
    }

    protected function render() {
        $settings   = $this->get_settings_for_display();
        $amount     = isset( $settings['amount'] ) ? (int) $settings['amount'] : 100;
        $button     = ! empty( $settings['button_text'] ) ? $settings['button_text'] : __( 'Zaplatit', 'platebni-formular' );
        $show_name  = ( $settings['show_name'] ?? 'yes' ) === 'yes';
        $show_email = ( $settings['show_email'] ?? 'yes' ) === 'yes';
        ?>
        <div class="elementsell-payment-widget elementsell-payment-widget--lite-preview" style="text-align:center;padding:16px;border:1px dashed #c3c4c7;border-radius:8px;">
            <?php if ( $show_name ) : ?>
                <input type="text" class="elementsell-field" placeholder="Jméno" disabled style="display:block;width:100%;max-width:320px;margin:0 auto 8px;">
            <?php endif; ?>
            <?php if ( $show_email ) : ?>
                <input type="email" class="elementsell-field" placeholder="Email" disabled style="display:block;width:100%;max-width:320px;margin:0 auto 8px;">
            <?php endif; ?>
            <button type="button" class="elementor-button" disabled style="margin-top:8px;">
                <?php echo esc_html( $button ); ?> — <?php echo esc_html( number_format( $amount, 0, ',', ' ' ) ); ?> Kč
            </button>
        </div>
        <?php
    }

    protected function content_template() {
        ?>
        <#
        var amount = settings.amount ? settings.amount : 100;
        var buttonText = settings.button_text ? settings.button_text : <?php echo wp_json_encode( __( 'Zaplatit', 'platebni-formular' ) ); ?>;
        #>
        <div class="elementsell-payment-widget elementsell-payment-widget--lite-preview" style="text-align:center;padding:16px;border:1px dashed #c3c4c7;border-radius:8px;">
            <# if ( 'yes' === settings.show_name ) { #>
                <input type="text" class="elementsell-field" placeholder="Jméno" disabled style="display:block;width:100%;max-width:320px;margin:0 auto 8px;">
            <# } #>
            <# if ( 'yes' === settings.show_email ) { #>
                <input type="email" class="elementsell-field" placeholder="Email" disabled style="display:block;width:100%;max-width:320px;margin:0 auto 8px;">
            <# } #>
            <button type="button" class="elementor-button" disabled style="margin-top:8px;">{{{ buttonText }}} — {{{ amount }}} Kč</button>
        </div>
        <?php
    }
}
