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

trait ElementSell_Widget_Controls {

    /**
     * @return array<string, string>
     */
    private function get_payment_global_defaults() {
        if ( function_exists( 'elementsell_get_payment_widget_global_defaults' ) ) {
            return elementsell_get_payment_widget_global_defaults();
        }

        return [
            'payment_method'           => 'fio',
            'fio_account_number'     => function_exists( 'elementsell_get_fio_account_number' ) ? elementsell_get_fio_account_number() : '',
            'fio_api_token'          => trim( (string) get_option( 'fio_api_token', '' ) ),
            'environment'              => 'demo',
            'demo_project_id'        => '',
            'demo_merchant_id'       => '',
            'demo_api_password'      => '',
            'production_project_id'  => '',
            'production_merchant_id' => '',
            'production_api_password'=> '',
            'email_from_name'        => trim( (string) get_option( 'faktury_jmeno', '' ) ) !== ''
                ? trim( (string) get_option( 'faktury_jmeno', '' ) )
                : trim( (string) get_bloginfo( 'name' ) ),
        ];
    }



  protected function register_widget_controls() {
    if (
        elementsell_is_elementor_context()
        && function_exists( 'elementsell_should_register_full_elementor_widget_controls' )
        && ! elementsell_should_register_full_elementor_widget_controls()
    ) {
        $this->register_elementor_minimal_widget_controls();
        return;
    }

    $billing_card_url = elementsell_billing_get_token_start_url();
    $sales_limit      = elementsell_billing_get_required_sales_count();
    $needs_card       = false;
    $payment_failed   = false;

    if ( ! elementsell_is_elementor_context() ) {
        $paid_orders_count = elementsell_get_production_paid_orders_count();
        $payment_failed    = elementsell_billing_is_payment_failed();
        $needs_card        = elementsell_is_billing_blocked() || ( $paid_orders_count >= $sales_limit && ! elementsell_billing_has_saved_card() );
    }

    if ( $needs_card && ! elementsell_billing_has_saved_card() ) {
        $this->start_controls_section(
            'payment_required_section',
            [
                'label' => __( 'Platba vyžadována', 'platebni-formular' ),
                'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'payment_button',
            [
                'label' => __( '', 'platebni-formular' ),
                'type'  => \Elementor\Controls_Manager::RAW_HTML,
                'raw'   => '<div class="elementsell-billing-editor-notice"><p><strong>Po ' . esc_html( (string) $sales_limit ) . ' prodejích je nutné uložit platební kartu.</strong></p><a class="elementor-button elementor-button-success" href="' . esc_url( $billing_card_url ) . '">Uložit platební kartu</a></div>',
            ]
        );

        $this->end_controls_section();
    }

    if ( $payment_failed ) {
        $this->start_controls_section(
            'payment_failed_section',
            [
                'label' => __( 'Neúspěšná platba', 'platebni-formular' ),
                'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'failed_payment_button',
            [
                'label' => __( '', 'platebni-formular' ),
                'type'  => \Elementor\Controls_Manager::RAW_HTML,
                'raw'   => '<div class="elementsell-billing-editor-notice elementsell-billing-editor-notice--error"><p><strong>Nezdařilo se stržení poplatku za ElementSell.</strong> Aktualizujte platební kartu.</p><a class="elementor-button elementor-button-danger" href="' . esc_url( $billing_card_url ) . '">Změnit platební kartu</a></div>',
            ]
        );

        $this->end_controls_section();
    }

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

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



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


// Částka
// Načtení nastavení plátce DPH a výše DPH z WordPress options
$is_vat_payer = get_option('faktury_platce_dph', 'ne') === 'ano';
$vat_rate = floatval(get_option('faktury_dph_vyse', 0)); // Výše DPH v procentech

// Text pro štítek částky
$amount_label = $is_vat_payer
    ? sprintf(__('Částka (Kč, včetně DPH %s %%)', 'platebni-formular'), number_format($vat_rate, 0))
    : __('Částka (Kč)', 'platebni-formular');

$this->add_control(
    'amount',
    [
        'label' => $amount_label,
        'type' => \Elementor\Controls_Manager::NUMBER,
        'default' => 100,
        'min' => 1,
        'step' => 1,
    ]
);

// Políčko pro škrtnutou cenu
$this->add_control(
    'crossed_amount',
    [
        'label' => __('Škrtnutá cena (Kč)', 'platebni-formular'),
        'type' => \Elementor\Controls_Manager::NUMBER,
        'default' => '',
        'min' => 1,
        'step' => 1,
    ]
);
       $this->end_controls_section();





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

$this->add_control(
    'form_max_width',
    [
        'label' => __( 'Šířka formuláře', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::SLIDER,
        'size_units' => ['px'],
        'range' => [
            'px' => [
                'min' => 300,
                'max' => 1200,
            ],
        ],
        'default' => [
            'size' => 520,
            'unit' => 'px',
        ],
        'selectors' => [
            '{{WRAPPER}} .elementsell-payment-form' => 'max-width: {{SIZE}}{{UNIT}}; margin-left: auto; margin-right: auto;',
        ],
    ]
);


$this->add_group_control(
    \Elementor\Group_Control_Typography::get_type(),
    [
        'name' => 'button_typography',
        'label' => __( 'Font tlačítka', 'platebni-formular' ),
        'selector' => '{{WRAPPER}} .platebni-button',
    ]
);


$this->add_control(
    'button_text_color',
    [
        'label' => __( 'Barva textu tlačítka', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'default' => '#ffffff',
        'selectors' => [
            '{{WRAPPER}} .platebni-button' => 'color: {{VALUE}};',
        ],
    ]
);

$this->add_control(
    'button_color',
    [
        'label' => __( 'Barva tlačítka', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'default' => '#22C55E',
        'selectors' => [
            '{{WRAPPER}} .platebni-button' => 'background-color: {{VALUE}};',
            '{{WRAPPER}} .elementsell-custom-switch input[type="checkbox"]' => 'accent-color: {{VALUE}};',
            '{{WRAPPER}} .elementsell-action-offer' => '--es-action-accent: {{VALUE}};',
        ],
    ]
);

$this->add_control(
    'button_radius',
    [
        'label' => __( 'Zaoblení tlačítka', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::SLIDER,
        'size_units' => ['px'],
        'range' => [
            'px' => [
                'min' => 0,
                'max' => 100,
            ],
        ],
        'default' => [
            'size' => 100,
            'unit' => 'px',
        ],
        'selectors' => [
            '{{WRAPPER}} .platebni-button' => 'border-radius: {{SIZE}}{{UNIT}};',
        ],
    ]
);



$this->add_control(
    'heading_price',
    [
        'label' => __( 'Cena', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::HEADING,
        'separator' => 'before',
    ]
);

$this->add_group_control(
    \Elementor\Group_Control_Typography::get_type(),
    [
        'name' => 'prices_typography',
        'label' => __( 'Font ceny', 'platebni-formular' ),
        'selector' => '{{WRAPPER}} .elementsell-price-main',
    ]
);

$this->add_control(
    'price_font_size',
    [
        'label' => __( 'Velikost ceny', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::SLIDER,
        'size_units' => [ 'px' ],
        'range' => [
            'px' => [
                'min' => 12,
                'max' => 80,
            ],
        ],
        'default' => [
            'size' => 22,
            'unit' => 'px',
        ],
        'selectors' => [
            '{{WRAPPER}} .elementsell-price-main' => 'font-size: {{SIZE}}{{UNIT}}; font-weight: 400; line-height: 1.3;',
        ],
    ]
);

$this->add_control(
    'price_color',
    [
        'label' => __( 'Barva ceny', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'default' => '#111111',
        'selectors' => [
            '{{WRAPPER}} .elementsell-price-main' => 'color: {{VALUE}};',
        ],
    ]
);

$this->add_control(
    'heading_old_price',
    [
        'label' => __( 'Původní cena', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::HEADING,
        'separator' => 'before',
    ]
);

$this->add_control(
    'old_price_font_size',
    [
        'label' => __( 'Velikost původní ceny', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::SLIDER,
        'size_units' => [ 'px' ],
        'range' => [
            'px' => [
                'min' => 10,
                'max' => 80,
            ],
        ],
        'default' => [
            'size' => 14,
            'unit' => 'px',
        ],
        'selectors' => [
            '{{WRAPPER}} .elementsell-price-old' => 'font-size: {{SIZE}}{{UNIT}}; font-weight: 400;',
        ],
    ]
);

$this->add_control(
    'old_price_color',
    [
        'label' => __( 'Barva původní ceny', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'default' => '#888888',
        'selectors' => [
            '{{WRAPPER}} .elementsell-price-old' => 'color: {{VALUE}};',
        ],
    ]
);


$this->end_controls_section();

 // Sekce pro výběr zobrazených polí
$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,
        'label_on' => __( 'Ano', 'platebni-formular' ),
        'label_off' => __( 'Ne', 'platebni-formular' ),
        'return_value' => 'yes',
        'default' => 'yes', // Zobrazeno jako výchozí
    ]
);

$this->add_control(
    'show_surname',
    [
        'label' => __( 'Zobrazit pole Příjmení', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __( 'Ano', 'platebni-formular' ),
        'label_off' => __( 'Ne', 'platebni-formular' ),
        'return_value' => 'yes',
        'default' => 'yes', // Zobrazeno jako výchozí
    ]
);

$this->add_control(
    'show_email',
    [
        'label' => __( 'Zobrazit pole Email', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __( 'Ano', 'platebni-formular' ),
        'label_off' => __( 'Ne', 'platebni-formular' ),
        'return_value' => 'yes',
        'default' => 'yes', // Zobrazeno jako výchozí
    ]
);

$this->add_control(
    'show_phone',
    [
        'label' => __( 'Zobrazit pole Telefonní číslo', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __( 'Ano', 'platebni-formular' ),
        'label_off' => __( 'Ne', 'platebni-formular' ),
        'return_value' => 'yes',
        'default' => 'no', // Skryto jako výchozí
    ]
);

$this->add_control(
    'show_address',
    [
        'label' => __( 'Zobrazit pole Adresa', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __( 'Ano', 'platebni-formular' ),
        'label_off' => __( 'Ne', 'platebni-formular' ),
        'return_value' => 'yes',
        'default' => 'yes', // Zobrazeno jako výchozí
    ]
);


$this->add_control(
    'show_company',
    [
        'label' => __( 'Zobrazit pole Firma', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __( 'Ano', 'platebni-formular' ),
        'label_off' => __( 'Ne', 'platebni-formular' ),
        'return_value' => 'yes',
        'default' => 'no', // Skryto jako výchozí
    ]
);


$this->add_control(
    'show_ico',
    [
        'label' => __( 'Zobrazit pole IČO', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __( 'Ano', 'platebni-formular' ),
        'label_off' => __( 'Ne', 'platebni-formular' ),
        'return_value' => 'yes',
        'default' => 'no', // Skryto jako výchozí
    ]
);

$this->add_control(
    'show_dic',
    [
        'label' => __( 'Zobrazit pole DIČ', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __( 'Ano', 'platebni-formular' ),
        'label_off' => __( 'Ne', 'platebni-formular' ),
        'return_value' => 'yes',
        'default' => 'no', // Skryto jako výchozí
    ]
);


// Přidání přepínače pro zobrazení pole slevového kódu
$this->add_control(
    'show_discount_code',
    [
        'label' => __( 'Zobrazit pole Slevový kód', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __( 'Ano', 'platebni-formular' ),
        'label_off' => __( 'Ne', 'platebni-formular' ),
        'return_value' => 'yes',
        'default' => 'yes', // Zobrazeno jako výchozí
    ]
);

// Přidání repeateru pro slevové kódy
$this->add_control(
    'discount_codes',
    [
        'label' => __( 'Slevové kódy', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::REPEATER,
        'fields' => [
            [
                'name' => 'code',
                'label' => __( 'Slevový kód', 'platebni-formular' ),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => __( 'SLEVA5', 'platebni-formular' ),
            ],
            [
                'name' => 'discount',
                'label' => __( 'Sleva (%)', 'platebni-formular' ),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'default' => 5,
                'min' => 1,
                'max' => 100,
                'step' => 1,
            ],
        ],
        'title_field' => '{{{ code }}} - {{{ discount }}}%',
        'add_button_text' => __( 'Přidat slevový kód', 'platebni-formular' ),
        'default' => [
            [
                'code' => 'SLEVA5',
                'discount' => 5,
            ],
        ],
        'condition' => [
            'show_discount_code' => 'yes',
        ],
    ]
);


$date_terms_repeater = new \Elementor\Repeater();

$date_terms_repeater->add_control(
    'date',
    [
        'label'          => __( 'Datum', 'platebni-formular' ),
        'type'           => \Elementor\Controls_Manager::DATE_TIME,
        'picker_options' => [
            'enableTime' => false,
            'dateFormat' => 'Y-m-d',
        ],
    ]
);

$this->add_control(
    'show_custom_fields',
    [
        'label'        => __( 'Zobrazit vlastní pole', 'platebni-formular' ),
        'type'         => \Elementor\Controls_Manager::SWITCHER,
        'label_on'     => __( 'Ano', 'platebni-formular' ),
        'label_off'    => __( 'Ne', 'platebni-formular' ),
        'return_value' => 'yes',
        'default'      => 'yes',
        'separator'    => 'before',
        'classes'      => 'elementsell-custom-fields-global-toggle',
    ]
);

$this->add_control(
    'custom_fields',
    [
        'label'       => __( 'Vlastní pole', 'platebni-formular' ),
        'type'        => \Elementor\Controls_Manager::REPEATER,
        'fields'      => [
            [
                'name'    => 'label',
                'label'   => __( 'Název pole', 'platebni-formular' ),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => __( 'Věk', 'platebni-formular' ),
            ],
            [
                'name'    => 'type',
                'label'   => __( 'Typ pole', 'platebni-formular' ),
                'type'    => \Elementor\Controls_Manager::SELECT,
                'default' => 'text',
                'options' => [
                    'text'   => __( 'Text', 'platebni-formular' ),
                    'date'   => __( 'Datum', 'platebni-formular' ),
                    'switch' => __( 'Checkbox', 'platebni-formular' ),
                ],
            ],
            [
                'name'            => 'date_terms',
                'label'           => __( 'Termíny', 'platebni-formular' ),
                'type'            => \Elementor\Controls_Manager::REPEATER,
                'fields'          => $date_terms_repeater->get_controls(),
                'title_field'     => '{{{ date }}}',
                'add_button_text' => __( 'Přidat termín', 'platebni-formular' ),
                'default'         => [],
                'condition'       => [
                    'type' => 'date',
                ],
            ],
        ],
        'title_field'     => '{{{ label }}}',
        'add_button_text' => __( 'Přidat pole', 'platebni-formular' ),
        'default'         => [],
        'condition'       => [
            'show_custom_fields' => 'yes',
        ],
    ]
);




$this->end_controls_section();

$this->start_controls_section(
    'upsell_section',
    [
        'label' => __( 'Upsell', 'platebni-formular' ),
        'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
    ]
);

$this->add_control(
    'upsell_enable',
    [
        'label'        => __( 'Zapnout upsell', 'platebni-formular' ),
        'type'         => \Elementor\Controls_Manager::SWITCHER,
        'label_on'     => __( 'Ano', 'platebni-formular' ),
        'label_off'    => __( 'Ne', 'platebni-formular' ),
        'return_value' => 'yes',
        'default'      => '',
    ]
);

$this->add_control(
    'upsell_image',
    [
        'label'     => __( 'Obrázek', 'platebni-formular' ),
        'type'      => \Elementor\Controls_Manager::MEDIA,
        'condition' => [
            'upsell_enable' => 'yes',
        ],
    ]
);

$this->add_control(
    'upsell_product_name',
    [
        'label'       => __( 'Název bonusu', 'platebni-formular' ),
        'type'        => \Elementor\Controls_Manager::TEXT,
        'default'     => '',
        'condition'   => [
            'upsell_enable' => 'yes',
        ],
    ]
);

$this->add_control(
    'upsell_title',
    [
        'label'       => __( 'Nadpis', 'platebni-formular' ),
        'type'        => \Elementor\Controls_Manager::TEXT,
        'default'     => __( 'Chcete přidat bonus?', 'platebni-formular' ),
        'condition'   => [
            'upsell_enable' => 'yes',
        ],
    ]
);

$this->add_control(
    'upsell_description',
    [
        'label'       => __( 'Popis', 'platebni-formular' ),
        'type'        => \Elementor\Controls_Manager::TEXTAREA,
        'default'     => '',
        'condition'   => [
            'upsell_enable' => 'yes',
        ],
    ]
);

$this->add_control(
    'upsell_crossed_price',
    [
        'label'     => __( 'Původní cena upsellu (Kč)', 'platebni-formular' ),
        'type'      => \Elementor\Controls_Manager::NUMBER,
        'min'       => 1,
        'step'      => 1,
        'condition' => [
            'upsell_enable' => 'yes',
        ],
    ]
);

$this->add_control(
    'upsell_price',
    [
        'label'     => __( 'Cena upsellu (Kč)', 'platebni-formular' ),
        'type'      => \Elementor\Controls_Manager::NUMBER,
        'default'   => 199,
        'min'       => 1,
        'step'      => 1,
        'condition' => [
            'upsell_enable' => 'yes',
        ],
    ]
);

$this->add_control(
    'upsell_delivery',
    [
        'label'       => __( 'Co zákazník dostane', 'platebni-formular' ),
        'type'        => \Elementor\Controls_Manager::SELECT,
        'default'     => 'role',
        'options'     => [
            'role' => __( 'Přístup (role)', 'platebni-formular' ),
            'link' => __( 'Odkaz', 'platebni-formular' ),
            'info' => __( 'Jen informace v e-mailu', 'platebni-formular' ),
        ],
        'description' => __( 'Přístup (role): po zaplacení se vytvoří/přiřadí WordPress role podle „Název bonusu“.', 'platebni-formular' ),
        'condition'   => [
            'upsell_enable' => 'yes',
        ],
    ]
);

$this->add_control(
    'upsell_info_text',
    [
        'label'       => __( 'Text informace v e-mailu', 'platebni-formular' ),
        'type'        => \Elementor\Controls_Manager::TEXTAREA,
        'default'     => '',
        'condition'   => [
            'upsell_enable'   => 'yes',
            'upsell_delivery' => 'info',
        ],
    ]
);

$this->add_control(
    'upsell_link_url',
    [
        'label'     => __( 'URL bonusu', 'platebni-formular' ),
        'type'      => \Elementor\Controls_Manager::URL,
        'condition' => [
            'upsell_enable'   => 'yes',
            'upsell_delivery' => 'link',
        ],
    ]
);

$this->add_control(
    'upsell_link_label',
    [
        'label'     => __( 'Text tlačítka', 'platebni-formular' ),
        'type'      => \Elementor\Controls_Manager::TEXT,
        'default'   => __( 'Získat bonus', 'platebni-formular' ),
        'condition' => [
            'upsell_enable'   => 'yes',
            'upsell_delivery' => 'link',
        ],
    ]
);

$this->end_controls_section();





// Sekce pro nastavení po zaplacení (obsahuje e-mail i URL)
$this->start_controls_section(
    'after_payment_section',
    [
        'label' => __( 'Po zaplacení', 'platebni-formular' ),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
    ]
);


// URL po zaplacení
$this->add_control(
    'navratova_url',
    [
        'label' => __( 'Adresa stránky po zaplacení', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => '',
    ]
);

// Předmět e-mailu
$this->add_control(
    'email_subject',
    [
        'label' => __( 'Předmět e-mailu', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __( 'Povedlo se!', 'platebni-formular' ),
    ]
);

// Obsah e-mailu
$this->add_control(
    'email_body',
    [
        'label' => __( 'Obsah e-mailu', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXTAREA,
        'default' => __( 'Děkujeme za platbu. Váš produkt je zde: (zadejte url).', 'platebni-formular' ),
    ]
);

$email_global_defaults = $this->get_payment_global_defaults();

$this->add_control(
    'email_from_name',
    [
        'label'       => __( 'Jméno odesílatele', 'platebni-formular' ),
        'type'        => \Elementor\Controls_Manager::TEXT,
        'default'     => $email_global_defaults['email_from_name'] ?? '',
        'placeholder' => __( 'Zadejte jméno odesílatele', 'platebni-formular' ),
    ]
);

// Checkbox pro vytvoření profilu
$this->add_control(
    'create_profile',
    [
        'label' => __( 'Poslat zákazníkovi přihlašovací údaje', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __( 'Ano', 'platebni-formular' ),
        'label_off' => __( 'Ne', 'platebni-formular' ),
        'return_value' => 'yes',
        'default' => 'no',
    ]
);

// Pole URL po přihlášení, které se zobrazí pouze při zapnutém přepínači
$this->add_control(
    'login_url',
    [
        'label' => __( 'Adresa, kam pošlete zákazníka po přihlášení', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => '',
        'condition' => [
            'create_profile' => 'yes',
        ],
    ]
);

$this->end_controls_section();





// Sekce pro výběr platební metody
$payment_global_defaults = $this->get_payment_global_defaults();

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

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

$this->add_control(
    'fio_details_heading',
    [
        'type' => \Elementor\Controls_Manager::HEADING,
        'label' => __( 'Podrobnosti o Fio účtu', 'platebni-formular' ),
        'separator' => 'before',
        'condition' => [
            'payment_method' => 'fio',
        ],
    ]
);

$this->add_control(
    'fio_account_number',
    [
        'label' => __( 'Číslo účtu', 'platebni-formular' ),
        'description' => __( 'v jakémkoliv formátu', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => $payment_global_defaults['fio_account_number'] ?? '',
        'condition' => [
            'payment_method' => 'fio',
        ],
    ]
);

$this->add_control(
    'fio_api_token',
    [
        'label' => __( 'API Token', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => $payment_global_defaults['fio_api_token'] ?? '',
        'condition' => [
            'payment_method' => 'fio',
        ],
    ]
);

$this->add_control(
    'thepay_details_heading',
    [
        'type' => \Elementor\Controls_Manager::HEADING,
        'label' => __( 'Nastavení platební brány ThePay', 'platebni-formular' ),
        'separator' => 'before',
        'condition' => [
            'payment_method' => 'thepay',
        ],
    ]
);

$this->add_control(
    'environment',
    [
        'label' => __( 'Aktivní režim', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::SELECT,
        // Statický default — nesmí číst poslední uložený widget (globální options),
        // jinak přepnutí dema na jedné stránce „přeleje“ výchozí režim na ostatní.
        'default' => 'demo',
        'options' => [
            'demo' => __( 'Demo', 'platebni-formular' ),
            'production' => __( 'Ostrý', 'platebni-formular' ),
        ],
        'classes' => 'elementsell-thepay-environment-control',
        'condition' => [
            'payment_method' => 'thepay',
        ],
    ]
);

$this->add_control(
    'demo_credentials_heading',
    [
        'type' => \Elementor\Controls_Manager::HEADING,
        'label' => __( 'Demo údaje', 'platebni-formular' ),
        'condition' => [
            'payment_method' => 'thepay',
        ],
    ]
);

$this->add_control(
    'demo_project_id',
    [
        'label' => __( 'ID projektu (demo)', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => $payment_global_defaults['demo_project_id'] ?? '',
        'condition' => [
            'payment_method' => 'thepay',
        ],
    ]
);

$this->add_control(
    'demo_merchant_id',
    [
        'label' => __( 'ID obchodníka (demo)', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => $payment_global_defaults['demo_merchant_id'] ?? '',
        'condition' => [
            'payment_method' => 'thepay',
        ],
    ]
);

$this->add_control(
    'demo_api_password',
    [
        'label' => __( 'Heslo (demo)', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => $payment_global_defaults['demo_api_password'] ?? '',
        'input_type' => 'password',
        'condition' => [
            'payment_method' => 'thepay',
        ],
    ]
);

$this->add_control(
    'production_credentials_heading',
    [
        'type' => \Elementor\Controls_Manager::HEADING,
        'label' => __( 'Ostré údaje', 'platebni-formular' ),
        'condition' => [
            'payment_method' => 'thepay',
        ],
    ]
);

$this->add_control(
    'production_project_id',
    [
        'label' => __( 'ID projektu (ostrý)', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => $payment_global_defaults['production_project_id'] ?? '',
        'condition' => [
            'payment_method' => 'thepay',
        ],
    ]
);

$this->add_control(
    'production_merchant_id',
    [
        'label' => __( 'ID obchodníka (ostrý)', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => $payment_global_defaults['production_merchant_id'] ?? '',
        'condition' => [
            'payment_method' => 'thepay',
        ],
    ]
);

$this->add_control(
    'production_api_password',
    [
        'label' => __( 'Heslo (ostrý)', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => $payment_global_defaults['production_api_password'] ?? '',
        'input_type' => 'password',
        'condition' => [
            'payment_method' => 'thepay',
        ],
    ]
);

$public_ip = elementsell_get_server_public_ip_for_display();

$this->add_control(
    'server_ip',
    [
        'label' => __( 'Přidejte v ThePay IP adresu:', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::RAW_HTML,
        'raw' => '<strong style="display: block; margin-bottom: 10px;">' . esc_html( $public_ip ) . '</strong>',
        'condition' => [
            'payment_method' => 'thepay',
        ],
    ]
);

$this->end_controls_section();





  // Sekce pro Meta Pixel
$this->start_controls_section(
    'meta_pixel_section',
    [
        'label' => __( 'Nastavení Meta Pixelu', 'platebni-formular' ),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
    ]
);

// Meta Pixel ID — nutné pro server-side odesílání nákupů (Conversions API).
$this->add_control(
    'meta_pixel_id',
    [
        'label' => __( 'Meta Pixel ID', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => '',
        'placeholder' => '123456789012345',
        'description' => __( 'Číslo pixelu z Events Manageru. Když ho necháš prázdné, ElementSell se ho pokusí vyčíst z pixel kódu níže.', 'platebni-formular' ),
    ]
);

// Meta Pixel kód (JS)
$this->add_control(
    'meta_pixel_code',
    [
        'label' => __( 'Meta Pixel kód (JS)', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXTAREA,
        'default' => '',
    ]
);

// Meta CAPI Access Token
$this->add_control(
    'meta_capi_token',
    [
        'label' => __( 'Meta CAPI Access Token', 'platebni-formular' ),
        'type' => \Elementor\Controls_Manager::TEXTAREA,
        'default' => '',
    ]
);

$this->end_controls_section();

    }

    /**
     * Lehké controls pro Elementor editor na stránkách bez widgetu (šetří paměť / čas načtení).
     *
     * @return void
     */
    protected function register_elementor_minimal_widget_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,
            ]
        );

        $payment_global_defaults = $this->get_payment_global_defaults();

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

        $this->end_controls_section();
    }
}
