<?php

namespace Elementor;

use WP_Rocket\ThirdParty\Plugins\PageBuilder\Elementor;

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

/**
 * Elementor property variation widget.
 *
 * Elementor widget that displays property variation.
 *
 * @since 1.0.0
 */
class Pakrco_Elementor_Property_Variation extends Widget_Base {

    /**
     * Get widget name.
     *
     * Retrieve property variation widget name.
     *
     * @since  1.0.0
     * @access public
     *
     * @return string Widget name.
     */
    public function get_name() {
        return 'pakrco-property-variation';
    }

    /**
     * Get widget title.
     *
     * Retrieve property variation widget title.
     *
     * @since  1.0.0
     * @access public
     *
     * @return string Widget title.
     */
    public function get_title() {
        return esc_html__('Pakrco Property Variation', 'pakrco');
    }

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

    public function get_script_depends() {
        return ['magnific-popup', 'pakrco-elementor-property-variation'];
    }

    public function get_style_depends() {
        return ['magnific-popup'];
    }

    /**
     * Get widget property variation.
     *
     * Retrieve property variation widget icon.
     *
     * @since  1.0.0
     * @access public
     *
     * @return string Widget property variation.
     */
    public function get_icon() {
        return 'eicon-gallery-grid';
    }

    /**
     * Get widget keywords.
     *
     * Retrieve the list of keywords the widget belongs to.
     *
     * @since  2.1.0
     * @access public
     *
     * @return array Widget keywords.
     */
    public function get_keywords() {
        return ['property variation', 'property', 'variation'];
    }

    /**
     * Register property variation widget controls.
     *
     * Adds different input fields to allow the user to change and customize the widget settings.
     *
     * @since  1.0.0
     * @access protected
     */
    protected function register_controls() {
        $this->start_controls_section(
            'section_property_variation',
            [
                'label' => esc_html__('Property Variation', 'pakrco'),
            ]
        );

        $this->add_control(
            'title_1',
            [
                'label'   => esc_html__('Title 1', 'pakrco'),
                'type'    => Controls_Manager::TEXT,
                'default' => 'RESIDENCE'
            ]
        );

        $this->add_control(
            'title_2',
            [
                'label'   => esc_html__('Title 2', 'pakrco'),
                'type'    => Controls_Manager::TEXT,
                'default' => 'BED/BATH'
            ]
        );

        $this->add_control(
            'title_3',
            [
                'label'   => esc_html__('Title 3', 'pakrco'),
                'type'    => Controls_Manager::TEXT,
                'default' => 'SQ. FT.'
            ]
        );

        $this->add_control(
            'title_4',
            [
                'label'   => esc_html__('Title 4', 'pakrco'),
                'type'    => Controls_Manager::TEXT,
                'default' => 'SALE PRICE'
            ]
        );

        $this->add_control(
            'title_5',
            [
                'label'   => esc_html__('Title 5', 'pakrco'),
                'type'    => Controls_Manager::TEXT,
                'default' => 'RENT PRICE'
            ]
        );

        $this->add_control(
            'title_6',
            [
                'label'   => esc_html__('Title 6', 'pakrco'),
                'type'    => Controls_Manager::TEXT,
                'default' => 'FLOOR PLAN'
            ]
        );

        $repeater = new Repeater();

        $repeater->add_control(
            'value_1',
            [
                'label' => esc_html__('Value 1', 'pakrco'),
                'type'  => Controls_Manager::TEXT,
            ]
        );

        $repeater->add_control(
            'value_2',
            [
                'label' => esc_html__('Value 2', 'pakrco'),
                'type'  => Controls_Manager::TEXT,
            ]
        );

        $repeater->add_control(
            'value_3',
            [
                'label' => esc_html__('Value 3', 'pakrco'),
                'type'  => Controls_Manager::TEXT,
            ]
        );

        $repeater->add_control(
            'value_4',
            [
                'label' => esc_html__('Value 4', 'pakrco'),
                'type'  => Controls_Manager::TEXT,
            ]
        );

        $repeater->add_control(
            'value_5',
            [
                'label' => esc_html__('Value 5', 'pakrco'),
                'type'  => Controls_Manager::TEXT,
            ]
        );

        $repeater->add_control(
            'value_6',
            [
                'label'   => esc_html__('Value 6', 'pakrco'),
                'type'    => Controls_Manager::MEDIA,
                'dynamic' => [
                    'active' => true,
                ],
                'default' => [
                    'url' => Utils::get_placeholder_image_src(),
                ]
            ]
        );

        $this->add_control(
            'property_variation',
            [
                'label'       => '',
                'type'        => Controls_Manager::REPEATER,
                'fields'      => $repeater->get_controls(),
                'default'     => [
                    [
                        'value_1' => esc_html__('TOWER PH1512', 'pakrco'),
                        'value_2' => esc_html__('', 'pakrco'),
                        'value_3' => esc_html__('', 'pakrco'),
                        'value_4' => esc_html__('', 'pakrco'),
                        'value_5' => esc_html__('', 'pakrco'),
                        'value_6' => esc_html__('', 'pakrco'),
                    ],
                ],
                'title_field' => '{{{ value_1 }}}',
            ]
        );


        $this->end_controls_section();

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

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'head_typography',
                'selector' => '{{WRAPPER}} table:not(.shop_table) th',
            ]
        );

        $this->add_control(
            'head_color',
            [
                'label' => esc_html__( 'Title Color', 'pakrco' ),
                'type' => Controls_Manager::COLOR,
                'default' => '',
                'selectors' => [
                    '{{WRAPPER}} table:not(.shop_table) th' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'background_head_color',
            [
                'label' => esc_html__( 'Background Color', 'pakrco' ),
                'type' => Controls_Manager::COLOR,
                'default' => '',
                'selectors' => [
                    '{{WRAPPER}} table:not(.shop_table) th' => 'background-color: {{VALUE}};',
                ],
            ]
        );


        $this->end_controls_section();

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

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'content_typography',
                'selector' => '{{WRAPPER}} table:not(.shop_table) td',
            ]
        );

        $this->add_control(
            'content_color',
            [
                'label' => esc_html__( 'Color', 'pakrco' ),
                'type' => Controls_Manager::COLOR,
                'default' => '',
                'selectors' => [
                    '{{WRAPPER}} table:not(.shop_table) td' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'background_content_color',
            [
                'label' => esc_html__( 'Background Color', 'pakrco' ),
                'type' => Controls_Manager::COLOR,
                'default' => '',
                'selectors' => [
                    '{{WRAPPER}} table:not(.shop_table) tbody' => 'background-color: {{VALUE}};',
                ],
            ]
        );


        $this->end_controls_section();
    }

    /**
     * Render icon list widget output on the frontend.
     *
     * Written in PHP and used to generate the final HTML.
     *
     * @since  1.0.0
     * @access protected
     */

    protected function render() {
        $settings = $this->get_settings_for_display();

        $this->add_render_attribute('property_variation', 'class', 'pakrco-property-variation');
        $this->add_render_attribute('property_variation_item', 'class', 'property-variation-item');

        ?>
        <div <?php echo pakrco_get_render_attribute_string('property_variation', $this); ?>>
            <table class="table">
                <thead>
                <tr <?php echo pakrco_get_render_attribute_string('property_variation_item', $this); ?>>
                    <th><?php echo empty($settings['title_1']) ? esc_html__('RESIDENCE', 'pakrco') : $settings['title_1']; ?></th>
                    <th><?php echo empty($settings['title_2']) ? esc_html__('BED/BATH', 'pakrco') : $settings['title_2']; ?></th>
                    <th><?php echo empty($settings['title_3']) ? esc_html__('SQ. FT.', 'pakrco') : $settings['title_3']; ?></th>
                    <th><?php echo empty($settings['title_4']) ? esc_html__('SALE PRICE', 'pakrco') : $settings['title_4']; ?></th>
                    <th><?php echo empty($settings['title_5']) ? esc_html__('RENT PRICE', 'pakrco') : $settings['title_5']; ?></th>
                    <th><?php echo empty($settings['title_6']) ? esc_html__('FLOOR PLAN', 'pakrco') : $settings['title_6']; ?></th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($settings['property_variation'] as $index => $item) : ?>
                    <tr <?php echo pakrco_get_render_attribute_string('property_variation_item', $this); ?>>
                        <td><span><?php echo empty($item['value_1']) ? '_' : $item['value_1']; ?></span></td>
                        <td><span><?php echo empty($item['value_2']) ? '_' : $item['value_2']; ?></span></td>
                        <td><span><?php echo empty($item['value_3']) ? '_' : $item['value_3']; ?></span></td>
                        <td><span><?php echo empty($item['value_4']) ? '_' : $item['value_4']; ?></span></td>
                        <td><span><?php echo empty($item['value_5']) ? '_' : $item['value_5']; ?></span></td>
                        <td><span><?php
                            $url = $item['value_6']['url'];
                            if (!empty($url)) {
                                ?>
                                <a data-elementor-open-lightbox="no" href="<?php echo esc_url($url); ?>"
                                   class="property_variation_button elementor-button"><?php esc_html_e('View Now', 'pakrco') ?></a>
                                <?php
                            }
                            ?>

                            </span></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <?php
    }
}

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