<?php

namespace Lknwp\CrowdfundingExtension\Includes\elementorFields;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;

if (! defined('ABSPATH')) {
    exit;
}

class LknwpCrowdfundingTemplateCustomAmount extends Widget_Base
{
    public function get_name()
    {
        return 'lkn-wp-crowdfunding-donation-form';
    }

    public function get_title()
    {
        return __('Donation Form', 'link-nacional-wp-crowdfunding-extension');
    }

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

    public function get_categories()
    {
        return ['lkn-wp-crowdfunding-extension'];
    }

    protected function _register_controls()
    {
        // Content Section
        $this->start_controls_section(
            'content_section',
            [
                'label' => __('Content', 'link-nacional-wp-crowdfunding-extension'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'lkn_button_gap',
            [
                'label' => __('Button Spacing', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 50,
                    ],
                ],
                'default' => [
                    'size' => 6,
                    'unit' => 'px',
                ],
                'selectors' => [
                    '{{WRAPPER}} .lkn-custom-payment-amount-buttons' => 'gap: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

        // Style Section
        $this->start_controls_section(
            'style_section',
            [
                'label' => __('Style', 'link-nacional-wp-crowdfunding-extension'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        // Text Color
        $this->add_control(
            'lkn_button_text_color',
            [
                'label' => __('Text Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#00A859',
                'selectors' => [
                    '{{WRAPPER}} .lkn-custom-payment-button' => 'color: {{VALUE}}',
                ],
            ]
        );

        // Background Color
        $this->add_control(
            'lkn_button_background_color',
            [
                'label' => __('Background Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => 'transparent',
                'selectors' => [
                    '{{WRAPPER}} .lkn-custom-payment-button' => 'background-color: {{VALUE}}',
                ],
            ]
        );

        // Background Color on Hover
        $this->add_control(
            'lkn_button_background_hover_color',
            [
                'label' => __('Background Color on Hover', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#00A859', // Green background on hover
                'selectors' => [
                    '{{WRAPPER}} .lkn-custom-payment-button:hover' => 'background-color: {{VALUE}}',
                ],
            ]
        );

        // Text Color on Hover
        $this->add_control(
            'lkn_button_text_hover_color',
            [
                'label' => __('Text Color on Hover', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#ffffff', // White text on hover
                'selectors' => [
                    '{{WRAPPER}} .lkn-custom-payment-button:hover' => 'color: {{VALUE}}',
                ],
            ]
        );

        // Borders
        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name' => 'lkn_button_border',
                'label' => __('Button Borders', 'link-nacional-wp-crowdfunding-extension'),
                'selector' => '{{WRAPPER}} .lkn-custom-payment-button',
                'fields_options' => [
                    'border' => [
                        'label' => esc_html__('Border Type', 'link-nacional-wp-crowdfunding-extension'),
                        'default' => 'solid',
                    ],
                    'width' => [
                        'label' => esc_html__('Border Width', 'link-nacional-wp-crowdfunding-extension'),
                        'default' => [
                            'top' => '1',
                            'right' => '1',
                            'bottom' => '1',
                            'left' => '1',
                            'isLinked' => true,
                        ],
                    ],
                    'color' => [
                        'label' => esc_html__('Border Color', 'link-nacional-wp-crowdfunding-extension'),
                        'default' => '#00A859', // Green border color
                    ],
                ],
            ]
        );

        // Border Radius
        $this->add_control(
            'lkn_button_border_radius',
            [
                'label' => __('Border Radius', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 50,
                    ],
                ],
                'default' => [
                    'size' => 4, // Default value in px
                    'unit' => 'px', // Default unit
                ],
                'selectors' => [
                    '{{WRAPPER}} .lkn-custom-payment-button' => 'border-radius: {{SIZE}}{{UNIT}}',
                ],
            ]
        );

        $this->add_control(
            'lkn_section_divider',
            [
                'type' => Controls_Manager::DIVIDER,
            ]
        );

        // Text Color for Submit Button
        $this->add_control(
            'lkn_submit_button_text_color',
            [
                'label' => __('Text Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#FFFFFF', // White text color
                'selectors' => [
                    '{{WRAPPER}} .lkn-submit-button' => 'color: {{VALUE}}',
                ],
            ]
        );

        // Background Color for Submit Button
        $this->add_control(
            'lkn_submit_button_background_color',
            [
                'label' => __('Background Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#00A859',
                'selectors' => [
                    '{{WRAPPER}} .lkn-submit-button' => 'background-color: {{VALUE}}',
                ],
            ]
        );

        // Background Color on Hover for Submit Button
        $this->add_control(
            'lkn_submit_button_background_hover_color',
            [
                'label' => __('Background Color on Hover', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#008F47', // Green background on hover
                'selectors' => [
                    '{{WRAPPER}} .lkn-submit-button:hover' => 'background-color: {{VALUE}}',
                ],
            ]
        );

        // Text Color on Hover for Submit Button
        $this->add_control(
            'lkn_submit_button_text_hover_color',
            [
                'label' => __('Text Color on Hover', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#ffffff', // White text on hover
                'selectors' => [
                    '{{WRAPPER}} .lkn-submit-button:hover' => 'color: {{VALUE}}',
                ],
            ]
        );

        // Borders for Submit Button
        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name' => 'lkn_submit_button_border',
                'label' => __('Submit Button Borders', 'link-nacional-wp-crowdfunding-extension'),
                'selector' => '{{WRAPPER}} .lkn-submit-button',
                'fields_options' => [
                    'border' => [
                        'label' => esc_html__('Border Type', 'link-nacional-wp-crowdfunding-extension'),
                        'default' => 'solid',
                    ],
                    'width' => [
                        'label' => esc_html__('Border Width', 'link-nacional-wp-crowdfunding-extension'),
                        'default' => [
                            'top' => '1',
                            'right' => '1',
                            'bottom' => '1',
                            'left' => '1',
                            'isLinked' => true,
                        ],
                    ],
                    'color' => [
                        'label' => esc_html__('Border Color', 'link-nacional-wp-crowdfunding-extension'),
                        'default' => '#00A859',
                    ],
                ],
            ]
        );

        // Border Radius for Submit Button
        $this->add_control(
            'lkn_submit_button_border_radius',
            [
                'label' => __('Border Radius', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 50,
                    ],
                ],
                'default' => [
                    'size' => 4, // Default value in px
                    'unit' => 'px', // Default unit
                ],
                'selectors' => [
                    '{{WRAPPER}} .lkn-submit-button' => 'border-radius: {{SIZE}}{{UNIT}}',
                ],
            ]
        );

        $this->end_controls_section();
    }

    protected function render()
    {
        $product_id = get_the_ID();
        $edit_mode = false;

        if (\Elementor\Plugin::$instance->editor->is_edit_mode() || is_preview() || get_post_type() !== 'product') {
            $disabled = 'disabled';
            $edit_mode = true;
        } else {
            $disabled = '';
        }

        $get_predefined_custom_amounts = get_post_meta(get_the_ID(), 'wpcf_predefined_pledge_amount', true);
        $predefined_custom_amounts = !empty($get_predefined_custom_amounts) ? explode(',', $get_predefined_custom_amounts) : explode(',', '10,20,30,40');
        $predefined_custom_amounts = array_map('trim', $predefined_custom_amounts);

        // Verifica se o produto é válido

        // Monta o HTML
        echo '<div class="lkn-custom-payment-amount-container" style="display: flex; flex-direction: column; flex-wrap: wrap; gap: 10px;">';

        // Botões de valores predefinidos
        echo '<div class="lkn-custom-payment-amount-buttons" style="display: flex; flex-wrap: wrap;">';
        foreach ($predefined_custom_amounts as $amount) {
            echo sprintf(
                '<button class="lkn-custom-payment-button" data-amount="%s">%s</button>',
                esc_attr($amount),
                wp_kses_post(wc_price($amount))
            );
        }
        echo '</div>';

        // Campo de valor personalizado
        echo '<div class="lkn-custom-value" style="display: flex; flex-wrap: wrap; gap: 10px">';
        echo '<input type="hidden" class="lkn-edit-mode" name="lkn-edit-mode" value="' . ($edit_mode ? 'active' : 'inactive') . '">';
        echo '<input type="number" id="custom-value-input" name="custom_value" value="" placeholder="' . esc_attr__('Enter the amount...', 'link-nacional-wp-crowdfunding-extension') . '" style="width: fit-content; flex-grow: 100;" />';
        echo '<button id="lkn-submit-campaing-value" class="lkn-submit-button" ' . esc_attr($disabled) . '>' . esc_html__('Support Campaign', 'link-nacional-wp-crowdfunding-extension') . '</button>';

        // Adiciona o input hidden com o product_id
        echo '<input type="hidden" id="product-id" name="product_id" value="' . esc_attr($product_id) . '" />';

        echo '</div>';

        // Fecha o container
        echo '</div>';
    }
}
