<?php

namespace Elementor;

if ( ! defined( 'ABSPATH' ) ) exit; // If this file is called directly, abort.

class Wixi_Brands_Board extends Widget_Base {
    use Wixi_Helper;
    public function get_name() {
        return 'wixi-brands-board';
    }
    public function get_title() {
        return 'Brands Board (N)';
    }
    public function get_icon() {
        return 'eicon-gallery-grid';
    }
    public function get_categories() {
        return [ 'wixi' ];
    }
    public function get_style_depends() {
        return [ 'splitting','splitting-cells' ];
    }
    public function get_script_depends() {
        return [ 'wow', 'splitting' ];
    }
    // Registering Controls
    protected function _register_controls() {
        /*****   START CONTROLS SECTION   ******/
        $this->start_controls_section( 'wixi_brands_board_settings',
            [
                'label' => esc_html__( 'Brands Board', 'wixi'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control( 'column',
            [
                'label' => esc_html__( 'Column', 'wixi' ),
                'type' => Controls_Manager::NUMBER,
                'min' => 1,
                'max' => 4,
                'default' => 4,
                'separator' => 'before'
            ]
        );
        $this->add_control( 'add_line',
            [
                'label' => esc_html__( 'Add Line', 'wixi' ),
                'type' => Controls_Manager::SWITCHER,
                'default' => 'no',
                'separator' => 'before',
            ]
        );
        $this->add_control( 'hide_split',
            [
                'label' => esc_html__( 'Hide Splitting', 'wixi' ),
                'type' => Controls_Manager::SWITCHER,
                'default' => 'no',
                'separator' => 'before',
            ]
        );
        $this->add_control( 'split_type',
            [
                'label' => esc_html__( 'Split Type', 'wixi' ),
                'type' => Controls_Manager::SELECT,
                'default' => 'chars',
                'options' => [
                    'chars' => esc_html__( 'Chars', 'wixi' ),
                    'words' => esc_html__( 'Words', 'wixi' ),
                ],
                'condition' => ['hide_split!' => 'yes'],
            ]
        );
        $this->add_group_control(
            Group_Control_Image_Size::get_type(),
            [
            'name' => 'thumbnail',
            'separator' => 'none',
            'default' => 'full',
            ]
        );
        $defimg = plugins_url( 'assets/front/img/brand.png', __DIR__ );
        $this->add_control( 'brands',
            [
                'type' => Controls_Manager::REPEATER,
                'default' => [
                    [
                        'image' => ['url' => $defimg],
                        'tab_title' => 'www.wixi.com',
                    ],
                    [
                        'image' => ['url' => $defimg],
                        'tab_title' => 'www.wixi.com',
                    ],
                    [
                        'image' => ['url' => $defimg],
                        'tab_title' => 'www.wixi.com',
                    ],
                    [
                        'image' => ['url' => $defimg],
                        'tab_title' => 'www.wixi.com',
                    ],
                    [
                        'image' => ['url' => $defimg],
                        'tab_title' => 'www.wixi.com',
                    ],
                    [
                        'image' => ['url' => $defimg],
                        'tab_title' => 'www.wixi.com',
                    ],
                    [
                        'image' => ['url' => $defimg],
                        'tab_title' => 'www.wixi.com',
                    ],
                    [
                        'image' => ['url' => $defimg],
                        'tab_title' => 'www.wixi.com',
                    ],
                ],
                'fields' => [
                    [
                        'name' => 'image',
                        'label' => esc_html__( 'Image', 'wixi' ),
                        'type' => Controls_Manager::MEDIA,
                        'default' => ['url' => $defimg],
                    ],
                    [
                        'name' => 'title',
                        'label' => esc_html__('Brand Title', 'wixi'),
                        'type' => Controls_Manager::TEXT,
                        'default' => 'www.wixi.com',
                        'label_block' => true
                    ],
                    [
                        'name' => 'link',
                        'label' => esc_html__( 'Link', 'wixi' ),
                        'type' => Controls_Manager::URL,
                        'label_block' => true,
                        'default' => [
                            'url' => '#0',
                            'is_external' => ''
                        ],
                        'show_external' => true
                    ]
                ],
                'title_field' => '{{title}}'
            ]
        );
        $this->end_controls_section();
        /*****   END CONTROLS SECTION   ******/
    }

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

        $column = $settings['column'] ? 12 / $settings['column'] : 3;
        $line   = 'yes' == $settings['add_line'] ? ' bord' : '';

        echo '<div class="clients">';
            echo '<div class="container-off">';
                echo '<div class="row'.$line.'">';
                    $count = 1;
                    foreach ( $settings['brands'] as $item ) {

                        $delay = mt_rand( 3, 9 );
                        $imagealt = esc_attr( get_post_meta( $item['image']['id'], '_wp_attachment_image_alt', true ) );
                        $imagealt = $imagealt ? $imagealt : basename ( get_attached_file( $item['image']['id'] ) );
                        $img_url  = Group_Control_Image_Size::get_attachment_image_src( $item['image']['id'], 'thumbnail', $settings );
                        $image_url = empty( $img_url ) ? $item['image']['url'] : $img_url;

                        echo '<div class="col-md-' . $column . ' col-6 brands">';
                            echo '<div class="item wow fadeIn" data-wow-delay=".'.$delay.'s">';
                                echo '<div class="img"><img src="'.esc_url( $image_url ).'" alt="'.$imagealt.'"></div>';
                                $split = 'yes' != $settings['hide_split'] ? ' data-splitting="'.$settings['split_type'].'"' : '';
                                echo '<a href="' . $item['link']['url'] .'" class="link"'.$split.'>' . $item['title'] .'</a>';
                            echo '</div>';
                        echo '<span class="color-aware"></span>';
                        echo '</div>';
                        $count++;

                    }
                echo '</div>';
            echo '</div>';
        echo '</div>';

    }
}
