<?php
/**
 * Template: Unterkünfte-Grid — [simpbook_unterkuenfte]
 *
 * Verfügbare Variablen:
 * - $unterkuenfte  (array) — Alle Unterkünfte-Zeilen
 * - $atts          (array) — Shortcode-Attribute (limit, typ, ort, tag, sortierung, spalten, filter)
 *
 * @package PocketBookingPro
 */

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

$spalten = (int) $atts['spalten'];
if ( $spalten < 1 || $spalten > 6 ) {
    $spalten = 3;
}
$show_filter = $atts['filter'] === 'ja';

// Waehrung
$waehrung_symbol = '€';
$waehrung = get_option( 'simpbook_vermietung_waehrung', 'EUR' );
$symbole = [ 'EUR' => '€', 'CHF' => 'CHF', 'USD' => '$', 'GBP' => '£' ];
if ( isset( $symbole[ $waehrung ] ) ) {
    $waehrung_symbol = $symbole[ $waehrung ];
}

// Typ-Labels
$typ_labels = [
    'ferienwohnung' => __( 'Ferienwohnung', 'simple-appointment-booking' ),
    'ferienhaus'    => __( 'Ferienhaus', 'simple-appointment-booking' ),
    'hotel'         => __( 'Hotel', 'simple-appointment-booking' ),
    'pension'       => __( 'Pension', 'simple-appointment-booking' ),
    'gaestezimmer'  => __( 'Gästezimmer', 'simple-appointment-booking' ),
    'apartment'     => __( 'Apartment', 'simple-appointment-booking' ),
    'chalet'        => __( 'Chalet', 'simple-appointment-booking' ),
    'villa'         => __( 'Villa', 'simple-appointment-booking' ),
    'sonstige'      => __( 'Sonstige', 'simple-appointment-booking' ),
];

// Detail-URL bauen
$detail_page_url = '';
$detail_page_id = (int) get_option( 'simpbook_vermietung_detail_seite', 0 );
if ( $detail_page_id ) {
    $detail_page_url = get_permalink( $detail_page_id );
}
// Fallback: aktuelle Seite verwenden (Shortcode erkennt Query-Param automatisch)
if ( ! $detail_page_url ) {
    $detail_page_url = get_permalink();
}
?>

<?php if ( $show_filter ) : ?>
<div class="simpbook-filter-bar">
    <select id="simpbook-filter-typ">
        <option value=""><?php esc_html_e( 'Alle Typen', 'simple-appointment-booking' ); ?></option>
        <?php foreach ( $typ_labels as $key => $label ) : ?>
            <option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $label ); ?></option>
        <?php endforeach; ?>
    </select>
    <input type="text" id="simpbook-filter-ort" placeholder="<?php esc_attr_e( 'Ort suchen...', 'simple-appointment-booking' ); ?>">
</div>
<?php endif; ?>

<div class="simpbook-unterkuenfte-grid" style="--simpbook-spalten: <?php echo (int) $spalten; ?>;">

    <?php if ( empty( $unterkuenfte ) ) : ?>
        <p><?php esc_html_e( 'Derzeit keine Unterkünfte verfügbar.', 'simple-appointment-booking' ); ?></p>
    <?php else : ?>

        <?php foreach ( $unterkuenfte as $u ) :
            // Tags laden
            $u_tags = Simpbook_Vermietung::get_unterkunft_tags( $u->id );

            // Titelbild
            $titelbild_url = '';
            if ( ! empty( $u->titelbild_id ) ) {
                $img = wp_get_attachment_image_url( $u->titelbild_id, 'medium_large' );
                if ( $img ) {
                    $titelbild_url = $img;
                }
            }

            // Detail-Link
            $detail_link = '';
            if ( $detail_page_url ) {
                $detail_link = add_query_arg( 'simpbook_unterkunft_id', (int) $u->id, $detail_page_url );
            }

            // Preis
            $preis = $u->preis_pro_nacht;
            $rabatt_preis = $u->rabatt_preis_nacht;

            // Typ-Label
            $typ_label = isset( $typ_labels[ $u->unterkunft_typ ] ) ? $typ_labels[ $u->unterkunft_typ ] : $u->unterkunft_typ;
        ?>

        <<?php echo $detail_link ? 'a href="' . esc_url( $detail_link ) . '"' : 'div'; ?> class="simpbook-unterkunft-card"
             data-typ="<?php echo esc_attr( $u->unterkunft_typ ); ?>"
             data-ort="<?php echo esc_attr( strtolower( $u->ort ?? '' ) ); ?>"
             <?php echo $detail_link ? 'style="text-decoration:none;color:inherit;display:block;"' : ''; ?>>

            <div class="simpbook-card-image">
                <?php if ( $titelbild_url ) : ?>
                    <img src="<?php echo esc_url( $titelbild_url ); ?>" alt="<?php echo esc_attr( $u->titel ); ?>" loading="lazy">
                <?php else : ?>
                    <div class="simpbook-card-image-placeholder">
                        <span class="dashicons dashicons-admin-home"></span>
                    </div>
                <?php endif; ?>

                <?php if ( ! empty( $u_tags ) ) : ?>
                    <div class="simpbook-card-tags">
                        <?php foreach ( $u_tags as $tag ) : ?>
                            <span class="simpbook-card-tag" style="background: <?php echo esc_attr( $tag->farbe ); ?>;">
                                <?php echo esc_html( $tag->name ); ?>
                            </span>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>

            <div class="simpbook-card-body">
                <h3 class="simpbook-card-title">
                    <?php echo esc_html( $u->titel ); ?>
                </h3>

                <?php if ( ! empty( $u->untertitel ) ) : ?>
                    <p class="simpbook-card-subtitle"><?php echo esc_html( $u->untertitel ); ?></p>
                <?php endif; ?>

                <?php if ( ! empty( $u->ort ) ) : ?>
                    <p class="simpbook-card-location"><?php echo esc_html( $u->ort ); ?><?php echo ! empty( $u->land ) && $u->land !== 'Deutschland' ? ', ' . esc_html( $u->land ) : ''; ?></p>
                <?php endif; ?>

                <div class="simpbook-card-meta">
                    <?php if ( ! empty( $u->max_gaeste ) ) : ?>
                        <span>👥 <?php echo (int) $u->max_gaeste; ?> <?php esc_html_e( 'Gäste', 'simple-appointment-booking' ); ?></span>
                    <?php endif; ?>
                    <?php if ( ! empty( $u->schlafplaetze ) ) : ?>
                        <span>🛏 <?php echo (int) $u->schlafplaetze; ?> <?php esc_html_e( 'Schlafplätze', 'simple-appointment-booking' ); ?></span>
                    <?php endif; ?>
                    <?php if ( ! empty( $u->wohnflaeche ) ) : ?>
                        <span>📐 <?php echo number_format( (float) $u->wohnflaeche, 0, ',', '.' ); ?> m²</span>
                    <?php endif; ?>
                </div>

                <div class="simpbook-card-footer">
                    <div class="simpbook-card-price">
                        <?php if ( $u->preis_auf_anfrage ) : ?>
                            <span><?php esc_html_e( 'Preis auf Anfrage', 'simple-appointment-booking' ); ?></span>
                        <?php elseif ( $preis ) : ?>
                            <?php if ( $rabatt_preis && (float) $rabatt_preis < (float) $preis ) : ?>
                                <span class="simpbook-card-price-old"><?php echo number_format( (float) $preis, 2, ',', '.' ); ?> <?php echo esc_html( $waehrung_symbol ); ?></span>
                                <?php echo number_format( (float) $rabatt_preis, 2, ',', '.' ); ?> <?php echo esc_html( $waehrung_symbol ); ?>
                            <?php else : ?>
                                <?php echo number_format( (float) $preis, 2, ',', '.' ); ?> <?php echo esc_html( $waehrung_symbol ); ?>
                            <?php endif; ?>
                            <small>/ <?php esc_html_e( 'Nacht', 'simple-appointment-booking' ); ?></small>
                        <?php endif; ?>
                    </div>

                    <?php if ( $detail_link ) : ?>
                        <span class="simpbook-card-link">
                            <?php esc_html_e( 'Details', 'simple-appointment-booking' ); ?>
                        </span>
                    <?php endif; ?>
                </div>
            </div>
        </<?php echo $detail_link ? 'a' : 'div'; ?>>

        <?php endforeach; ?>

    <?php endif; ?>

</div>
