<?php
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}
if (!pakrco_is_contactform_activated()) {
    return;
}

use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;


class Pakrco_Elementor_ContactForm extends Elementor\Widget_Base {

    public function get_name() {
        return 'pakrco-contactform';
    }

    public function get_title() {
        return esc_html__('Pakrco Contact Form', 'pakrco');
    }

    public function get_categories() {
        return array('pakrco-addons');
    }

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

    protected function register_controls() {
        $this->start_controls_section(
            'contactform7',
            [
                'label' => esc_html__('General', 'pakrco'),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );
        $cf7               = get_posts('post_type="wpcf7_contact_form"&numberposts=-1');
        $contact_forms[''] = esc_html__('Please select form', 'pakrco');
        if ($cf7) {
            foreach ($cf7 as $cform) {
                $contact_forms[$cform->ID] = $cform->post_title;
            }
        } else {
            $contact_forms[0] = esc_html__('No contact forms found', 'pakrco');
        }

        $this->add_control(
            'cf_id',
            [
                'label'   => esc_html__('Select contact form', 'pakrco'),
                'type'    => Controls_Manager::SELECT,
                'options' => $contact_forms,
                'default' => ''
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'contact_style_input',
            [
                'label' => esc_html__('Input', 'pakrco'),
                'tab'   => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'content_color',
            [
                'label'     => esc_html__('Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'default'   => '',
                'selectors' => [
                    '{{WRAPPER}} .wpcf7-form input:not([type="submit"]),{{WRAPPER}} .wpcf7-form i, {{WRAPPER}} .wpcf7-form textarea, {{WRAPPER}} .wpcf7-form select:not([size]):not([multiple])' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'label_color',
            [
                'label'     => esc_html__('Color Label', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'default'   => '',
                'selectors' => [
                    '{{WRAPPER}} .wpcf7-form label' => 'color: {{VALUE}};',
                    '{{WRAPPER}} .wpcf7-form .wpcf7-list-item-label' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'content_typography',
                'selector' => '{{WRAPPER}} .wpcf7-form input:not([type="submit"]), {{WRAPPER}} .wpcf7-form textarea, {{WRAPPER}} .wpcf7-form select:not([size]):not([multiple])',
            ]
        );

        $this->add_control(
            'bg_color',
            [
                'label'     => esc_html__('Input Background Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'default'   => '',
                'selectors' => [
                    '{{WRAPPER}} .wpcf7-form input:not([type="submit"]), {{WRAPPER}} .wpcf7-form textarea, {{WRAPPER}} .wpcf7-form select:not([size]):not([multiple])' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'bg_color_label',
            [
                'label'     => esc_html__('Label Background Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'default'   => '',
                'selectors' => [
                    '{{WRAPPER}} .wpcf7-form label' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'padding_input',
            [
                'label'      => esc_html__('Padding', 'pakrco'),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors'  => [
                    '{{WRAPPER}} .wpcf7-form input:not([type="submit"]), {{WRAPPER}} .wpcf7-form textarea, {{WRAPPER}} .wpcf7-form select.wpcf7-select' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'margin_input',
            [
                'label'      => esc_html__('Margin', 'pakrco'),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors'  => [
                    '{{WRAPPER}} .wpcf7-form .form-col' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'border_input',
            [
                'label'      => esc_html__('Border Input', 'pakrco'),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => ['px'],
                'selectors'  => [
                    '{{WRAPPER}} .wpcf7-form input[type="text"], {{WRAPPER}} .wpcf7-form input[type="number"], {{WRAPPER}} .wpcf7-form input[type="email"], {{WRAPPER}} .wpcf7-form input[type="tel"], {{WRAPPER}} .wpcf7-form input[type="url"], {{WRAPPER}} .wpcf7-form textarea, {{WRAPPER}} .wpcf7-form .input-text, {{WRAPPER}} .wpcf7-form select:not([size]):not([multiple]), {{WRAPPER}} .wpcf7-form input.wpcf7-date' => 'border-width: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'color_input',
            [
                'label'     => esc_html__('Border Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .wpcf7-form input[type="text"], {{WRAPPER}} .wpcf7-form input[type="number"], {{WRAPPER}} .wpcf7-form input[type="email"], {{WRAPPER}} .wpcf7-form input[type="tel"], {{WRAPPER}} .wpcf7-form input[type="url"], {{WRAPPER}} .wpcf7-form textarea, {{WRAPPER}} .wpcf7-form .input-text, {{WRAPPER}} .wpcf7-form select:not([size]):not([multiple]), {{WRAPPER}} .wpcf7-form input.wpcf7-date' => 'border-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'border_radius_input',
            [
                'label'      => esc_html__('Border Radius', 'pakrco'),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors'  => [
                    '{{WRAPPER}} .wpcf7-form input:not([type="submit"])' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    '{{WRAPPER}} .wpcf7-form textarea'                   => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
        $this->start_controls_section(
            'contact_style_button',
            [
                'label' => esc_html__('Button', 'pakrco'),
                'tab'   => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'button_typography',
                'selector' => '{{WRAPPER}} .wpcf7-form input[type="submit"], {{WRAPPER}} .wpcf7-form button',
            ]
        );

        $this->start_controls_tabs('tabs_button');

        $this->start_controls_tab(
            'tab_button_normal',
            [
                'label' => esc_html__('Normal', 'pakrco'),
            ]
        );

        $this->add_control(
            'button_color',
            [
                'label'     => esc_html__('Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'default'   => '',
                'selectors' => [
                    '{{WRAPPER}} .wpcf7-form input[type="submit"], {{WRAPPER}} .wpcf7-form button' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'button_bg_color',
            [
                'label'     => esc_html__('Background Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'default'   => '',
                'selectors' => [
                    '{{WRAPPER}} .wpcf7-form input[type="submit"], {{WRAPPER}} .wpcf7-form button' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_tab();

        $this->start_controls_tab(
            'tab_button_hover',
            [
                'label' => esc_html__('Hover', 'pakrco'),
            ]
        );

        $this->add_control(
            'button_color_hover',
            [
                'label'     => esc_html__('Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'default'   => '',
                'selectors' => [
                    '{{WRAPPER}} .wpcf7-form input[type="submit"]:hover, {{WRAPPER}} .wpcf7-form button:hover' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'button_bg_color_hover',
            [
                'label'     => esc_html__('Background Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'default'   => '',
                'selectors' => [
                    '{{WRAPPER}} .wpcf7-form input[type="submit"]:hover, {{WRAPPER}} .wpcf7-form button:hover:before' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_tab();

        $this->end_controls_tabs();



        $this->add_responsive_control(
            'padding_contact',
            [
                'label'      => esc_html__('Padding', 'pakrco'),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'separator'  => 'before',
                'selectors'  => [
                    '{{WRAPPER}} .wpcf7-form input[type="submit"]' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'margin_contact',
            [
                'label'          => esc_html__('Spacing', 'pakrco'),
                'type'           => Controls_Manager::SLIDER,
                'default'        => [
                    'unit' => '%',
                ],
                'tablet_default' => [
                    'unit' => '%',
                ],
                'mobile_default' => [
                    'unit' => '%',
                ],
                'size_units'     => ['%', 'px', 'vw'],
                'range'          => [
                    '%'  => [
                        'min' => 1,
                        'max' => 100,
                    ],
                    'px' => [
                        'min' => 1,
                        'max' => 1000,
                    ],
                    'vw' => [
                        'min' => 1,
                        'max' => 100,
                    ],
                ],
                'selectors'      => [
                    '{{WRAPPER}} .wpcf7-form input[type="submit"]' => 'margin-top: {{SIZE}}{{UNIT}};',
                ],
            ]
        );
        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        if (!$settings['cf_id']) {
            return;
        }
        $args['id']    = $settings['cf_id'];
        $this->add_render_attribute('wrapper', 'class', 'elementor-form-wrapper');

        ?>
        <div <?php echo pakrco_elementor_get_render_attribute_string('wrapper', $this); ?>>

            <?php echo pakrco_do_shortcode('contact-form-7', $args); ?>

        </div>
        <?php
    }

}

$widgets_manager->register_widget_type(new Pakrco_Elementor_ContactForm());
