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

use Elementor\Controls_Manager;

class Elementor_Apartaments extends \Elementor\Widget_Base {

	public function get_name() {
		return 'apartaments';
	}

	public function get_title() {
		return esc_html__( 'Apartaments', 'elementor-addon' );
	}

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

	public function get_categories() {
		return [ 'basic' ];
	}

	public function get_keywords() {
		return [ 'apartaments' ];
	}

	protected function register_controls() {
        $this->start_controls_section(
			'section_setting',
			[
				'label' => esc_html__( 'Settings', 'elementor' ),
                'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

        $this->end_controls_section();
	}

    public function get_style_depends() {
        return [ 'elementor-addon-swiper', 'elementor-addon-fancybox', 'elementor-addon-main-css' ];
    }

    public function get_script_depends() {
        return [ 'elementor-addon-swiper', 'elementor-addon-fancybox', 'elementor-addon-main-js' ];
    }

    protected function render() {
        $apartaments = new \WP_Query([
            'post_type'      => 'apartamenty',
            'posts_per_page' => -1,
            'orderby'        => 'title',
            'order'          => 'ASC',
        ]);

        if ( ! $apartaments->have_posts() ) {
            echo '<p>Brak apartamentów.</p>';
            return;
        }
        ?>

        <div class="apartaments">
            <?php while ( $apartaments->have_posts() ) : $apartaments->the_post(); ?>
                <?php
                    $gallery = get_field('gallery');
                    $information = get_field('information');
                    $documents = get_field('documents');
                ?>

                <div class="apartament-card">
                    <div class="apartament-card__wrapper">
                        <div class="apartament-card__title">
                            <h2><?php the_title(); ?></h2>
                        </div>
                        <?php if (!empty($gallery)) : ?>
                        <div class="apartament-card__gallery">
                            <div class="swiper apartament-gallery-swiper">
                                <div class="swiper-wrapper">
                                    <?php foreach($gallery as $image) : ?>
                                        <div class="swiper-slide">
                                            <a href="<?php echo esc_url($image); ?>" data-fancybox="gallery-<?php echo get_the_ID(); ?>" data-elementor-open-lightbox="no">
                                                <img src="<?php echo esc_url($image); ?>" alt="">
                                            </a>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            </div>
                            <div class="swiper-button-prev">
                                <svg xmlns="http://www.w3.org/2000/svg" width="47" height="129" viewBox="0 0 47 129" fill="none"><path d="M47 5.59336L37.2672 0L0 64.5L37.2672 129L47 123.407L12.9603 64.5L47 5.59336Z" fill="#192C44"/></svg>
                            </div>
                            <div class="swiper-button-next">
                                <svg xmlns="http://www.w3.org/2000/svg" width="47" height="129" viewBox="0 0 47 129" fill="none"><g clip-path="url(#clip0_60079_176)"><path d="M4.30368e-06 123.407L9.73283 129L47 64.5L9.73285 8.50871e-07L1.46032e-05 5.59336L34.0397 64.5L4.30368e-06 123.407Z" fill="#192C44"/></g><defs><clipPath id="clip0_60079_176"><rect width="47" height="129" fill="white" transform="translate(47 129) rotate(-180)"/></clipPath></defs></svg>
                            </div>
                        </div>
                        <?php endif; ?>
                        <?php if (!empty($information)) : ?>
                        <div class="apartament-card__info">
                            <div class="apartament-card__info_col1">
                                <table>
                                    <?php if (!empty($information['status'])) : ?>
                                    <tr class="apartament-card__status">
                                        <td class="apartament-card__info_table-title">Status:</td>
                                        <td class="apartament-card__info_table-value">
                                            <span class="<?php echo esc_attr($information['status']['value']); ?>">
                                                <?php echo esc_html($information['status']['label']); ?>
                                            </span>
                                        </td>
                                    </tr>
                                    <?php endif; ?>
                                    <?php if (!empty($information['type'])) : ?>
                                        <tr>
                                            <td class="apartament-card__info_table-title">Typ:</td>
                                            <td class="apartament-card__info_table-value"><?php echo esc_html($information['type']); ?></td>
                                        </tr>
                                    <?php endif; ?>
                                    <?php if (!empty($information['floor'])) : ?>
                                        <tr>
                                            <td class="apartament-card__info_table-title">Piętro:</td>
                                            <td class="apartament-card__info_table-value"><?php echo esc_html($information['floor']); ?></td>
                                        </tr>
                                    <?php endif; ?>
                                    <?php if (!empty($information['floor_space'])) : ?>
                                        <tr>
                                            <td class="apartament-card__info_table-title">Metraż:</td>
                                            <td class="apartament-card__info_table-value"><?php echo esc_html($information['floor_space']); ?> m2</td>
                                        </tr>
                                    <?php endif; ?>
                                    <?php if (!empty($information['balcony'])) : ?>
                                        <tr>
                                            <td class="apartament-card__info_table-title">Balkon:</td>
                                            <td class="apartament-card__info_table-value"><?php echo esc_html($information['balcony']); ?> m2</td>
                                        </tr>
                                    <?php endif; ?>
                                    <?php if (!empty($information['price'])) : ?>
                                        <tr>
                                            <td class="apartament-card__info_table-title">Cena</td>
                                            <td class="apartament-card__info_table-value"><?php echo esc_html($information['price']); ?> zł</td>
                                        </tr>
                                    <?php endif; ?>
                                    <?php if (!empty($information['price_m2'])) : ?>
                                        <tr>
                                            <td class="apartament-card__info_table-title">Cena m2</td>
                                            <td class="apartament-card__info_table-value"><?php echo esc_html($information['price_m2']); ?> zł</td>
                                        </tr>
                                    <?php endif; ?>

                                    <?php if (!empty($information['rows'])) : ?>
                                    <?php foreach($information['rows'] as $row) : ?>
                                        <tr>
                                            <td class="apartament-card__info_table-title"><?php echo esc_html($row['title']); ?></td>
                                            <td class="apartament-card__info_table-value"><?php echo esc_html($row['text']); ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                    <?php endif; ?>
                                    <tr class="apartament-card__price-history">
                                        <td class="apartament-card__info_table-title"></td>
                                        <td class="apartament-card__info_table-value">
                                            HISTORIA CEN

                                            <svg xmlns="http://www.w3.org/2000/svg" width="12" height="7" viewBox="0 0 12 7" fill="none">
                                                <g clip-path="url(#clip0_14_32)">
                                                    <path d="M6 7L0.370835 0.25L11.6292 0.250001L6 7Z" fill="#192C44"/>
                                                </g>
                                                <defs>
                                                    <clipPath id="clip0_14_32">
                                                        <rect width="12" height="7" fill="white"/>
                                                    </clipPath>
                                                </defs>
                                            </svg>
                                        </td>
                                    </tr>
                                </table>
                            </div>

                            <?php if (!empty($documents)) : ?>
                            <div class="apartament-card__info_col2">
                                <?php if (!empty($documents['image_draft'])) : ?>
                                <div class="card__info_col2__img">
                                    <a href="<?php echo esc_url($documents['image_draft']); ?>" data-fancybox="" data-elementor-open-lightbox="no">
                                        <img src="<?php echo esc_url($documents['image_draft']); ?>" alt="">
                                    </a>
                                </div>
                                <?php endif; ?>
                                <?php if (!empty($documents['file'])) : ?>
                                <div class="card__info_col2__data">
                                    <a href="<?php echo esc_url($documents['file']); ?>">
                                        <?php echo esc_html($documents['btn_txt']); ?>

                                        <svg xmlns="http://www.w3.org/2000/svg" width="12" height="7" viewBox="0 0 12 7" fill="none">
                                            <g clip-path="url(#clip0_14_32)">
                                                <path d="M6 7L0.370835 0.25L11.6292 0.250001L6 7Z" fill="#192C44"/>
                                            </g>
                                            <defs>
                                                <clipPath id="clip0_14_32">
                                                    <rect width="12" height="7" fill="white"/>
                                                </clipPath>
                                            </defs>
                                        </svg>
                                    </a>
                                </div>
                                <?php endif; ?>
                            </div>
                            <?php endif; ?>
                        </div>
                        <?php endif; ?>
                        <div class="apartament-card__desc">
		                    <?php the_content(); ?>
                        </div>
                    </div>
                </div>
            <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>
        </div>

        <?php
    }
}