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

/**
 * Panelová varianta widgetu pro stub boot editoru — lehké controls + náhled formuláře na plátně.
 * Po deferred cookie se registruje plný widget.php (controls + render).
 */
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_surname',
            [
                'label'        => __( 'Zobrazit pole Příjmení', '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->add_control(
            'show_phone',
            [
                'label'        => __( 'Zobrazit pole Telefon', 'platebni-formular' ),
                'type'         => \Elementor\Controls_Manager::SWITCHER,
                'return_value' => 'yes',
                'default'      => 'no',
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        if ( function_exists( 'elementsell_boot_debug_log' ) ) {
            elementsell_boot_debug_log(
                'panel payment widget render'
                . ' full_preview=' . ( function_exists( 'elementsell_payment_widget_can_render_full_editor_preview' )
                    && elementsell_payment_widget_can_render_full_editor_preview() ? 'yes' : 'no' )
            );
        }

        if ( function_exists( 'elementsell_payment_widget_render_editor_preview' )
            && elementsell_payment_widget_render_editor_preview( $this ) ) {
            return;
        }

        $this->render_panel_lite_preview();
    }

    protected function content_template() {
        if ( function_exists( 'elementsell_payment_widget_can_render_full_editor_preview' )
            && elementsell_payment_widget_can_render_full_editor_preview()
            && function_exists( 'elementsell_payment_widget_echo_content_template' ) ) {
            elementsell_payment_widget_echo_content_template();

            return;
        }

        $this->render_panel_lite_content_template();
    }

    /**
     * Lehký server-side náhled (preview iframe / AJAX render bez plných závislostí).
     *
     * @return void
     */
    private function render_panel_lite_preview() {
        $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';
        $show_surname = ( $settings['show_surname'] ?? 'yes' ) === 'yes';
        $show_phone = ( $settings['show_phone'] ?? 'no' ) === 'yes';
        ?>
        <div class="elementsell-payment-widget elementsell-payment-widget--panel-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;padding:12px;border:1px solid #e5e7eb;border-radius:8px;">
            <?php endif; ?>
            <?php if ( $show_surname ) : ?>
                <input type="text" class="elementsell-field" placeholder="Příjmení" disabled style="display:block;width:100%;max-width:320px;margin:0 auto 8px;padding:12px;border:1px solid #e5e7eb;border-radius: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;padding:12px;border:1px solid #e5e7eb;border-radius:8px;">
            <?php endif; ?>
            <?php if ( $show_phone ) : ?>
                <input type="tel" class="elementsell-field" placeholder="Telefonní číslo" disabled style="display:block;width:100%;max-width:320px;margin:0 auto 8px;padding:12px;border:1px solid #e5e7eb;border-radius:8px;">
            <?php endif; ?>
            <div class="elementsell-price-main" style="margin:12px 0;font-size:24px;font-weight:700;">
                <?php echo esc_html( number_format( $amount, 0, ',', ' ' ) ); ?> Kč
            </div>
            <button type="button" class="platebni-button" disabled style="width:100%;max-width:320px;padding:14px;border:none;border-radius:8px;background:#22c55e;color:#fff;font-weight:600;">
                <?php echo esc_html( $button ); ?>
            </button>
        </div>
        <?php
    }

    /**
     * Lehký JS náhled pro plátno editoru (content_template).
     *
     * @return void
     */
    private function render_panel_lite_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--panel-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;padding:12px;border:1px solid #e5e7eb;border-radius:8px;">
            <# } #>
            <# if ( 'yes' === settings.show_surname ) { #>
                <input type="text" class="elementsell-field" placeholder="Příjmení" disabled style="display:block;width:100%;max-width:320px;margin:0 auto 8px;padding:12px;border:1px solid #e5e7eb;border-radius: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;padding:12px;border:1px solid #e5e7eb;border-radius:8px;">
            <# } #>
            <# if ( 'yes' === settings.show_phone ) { #>
                <input type="tel" class="elementsell-field" placeholder="Telefonní číslo" disabled style="display:block;width:100%;max-width:320px;margin:0 auto 8px;padding:12px;border:1px solid #e5e7eb;border-radius:8px;">
            <# } #>
            <div class="elementsell-price-main" style="margin:12px 0;font-size:24px;font-weight:700;">{{{ amount }}} Kč</div>
            <button type="button" class="platebni-button" disabled style="width:100%;max-width:320px;padding:14px;border:none;border-radius:8px;background:#22c55e;color:#fff;font-weight:600;">{{{ buttonText }}}</button>
        </div>
        <?php
    }
}
