<?php
/**
 * Unterkünfte-Liste — admin/unterkuenfte.php
 *
 * Listenansicht für Ferienwohnungen, Hotels, Pensionen.
 *
 * @package PocketBookingPro
 */

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

simpbook_display_trial_notice_on_admin_pages();

global $wpdb;
$simpbook_v = Simpbook_Vermietung::instance();

// Löschen
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['simpbook_aktion'] ) && 'loeschen' === $_GET['simpbook_aktion'] && isset( $_GET['simpbook_id'] ) ) {
    $simpbook_del_id = intval( $_GET['simpbook_id'] );
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    if ( isset( $_GET['simpbook_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['simpbook_nonce'] ) ), 'simpbook_delete_unterkunft_' . $simpbook_del_id ) ) {
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $wpdb->delete( $wpdb->prefix . 'simpbook_unterkuenfte', [ 'id' => $simpbook_del_id ], [ '%d' ] );
        // Zimmer + Tags löschen
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $wpdb->delete( $wpdb->prefix . 'simpbook_unterkunft_zimmer', [ 'unterkunft_id' => $simpbook_del_id ], [ '%d' ] );
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $wpdb->delete( $wpdb->prefix . 'simpbook_unterkunft_tag_zuordnungen', [ 'unterkunft_id' => $simpbook_del_id ], [ '%d' ] );
        wp_safe_redirect( admin_url( 'admin.php?page=simpbook-unterkuenfte&simpbook_nachricht=geloescht' ) );
        exit;
    }
}

// Status ändern (aktivieren/deaktivieren)
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['simpbook_aktion'] ) && in_array( $_GET['simpbook_aktion'], [ 'aktivieren', 'deaktivieren' ], true ) && isset( $_GET['simpbook_id'] ) ) {
    $simpbook_toggle_id = intval( $_GET['simpbook_id'] );
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    if ( isset( $_GET['simpbook_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['simpbook_nonce'] ) ), 'simpbook_toggle_unterkunft_' . $simpbook_toggle_id ) ) {
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $simpbook_neuer_status = 'aktivieren' === $_GET['simpbook_aktion'] ? 'aktiv' : 'entwurf';
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $wpdb->update(
            $wpdb->prefix . 'simpbook_unterkuenfte',
            [ 'status' => $simpbook_neuer_status ],
            [ 'id' => $simpbook_toggle_id ],
            [ '%s' ],
            [ '%d' ]
        );
        wp_safe_redirect( admin_url( 'admin.php?page=simpbook-unterkuenfte&simpbook_nachricht=aktualisiert' ) );
        exit;
    }
}

// Alle Unterkünfte laden
$simpbook_unterkuenfte = $simpbook_v->get_unterkuenfte( [ 'orderby' => 'sortierung', 'order' => 'ASC' ] );

// ORT-Mapping für Unterkunftstypen
$simpbook_typ_labels = [
    'ferienwohnung' => __( 'Ferienwohnung', 'simple-appointment-booking' ),
    'ferienhaus'    => __( 'Ferienhaus', 'simple-appointment-booking' ),
    'hotel'         => __( 'Hotel', 'simple-appointment-booking' ),
    'pension'       => __( 'Pension', 'simple-appointment-booking' ),
    'apartment'     => __( 'Apartment', 'simple-appointment-booking' ),
    'villa'         => __( 'Villa', 'simple-appointment-booking' ),
    'zimmer'        => __( 'Zimmer', 'simple-appointment-booking' ),
    'sonstiges'     => __( 'Sonstiges', 'simple-appointment-booking' ),
];

// Nachrichten
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['simpbook_nachricht'] ) ) {
    $simpbook_msg_key = sanitize_text_field( wp_unslash( $_GET['simpbook_nachricht'] ) );
    $simpbook_msgs    = [
        'gespeichert'  => __( 'Unterkunft erfolgreich gespeichert.', 'simple-appointment-booking' ),
        'geloescht'    => __( 'Unterkunft erfolgreich gelöscht.', 'simple-appointment-booking' ),
        'aktualisiert' => __( 'Unterkunft erfolgreich aktualisiert.', 'simple-appointment-booking' ),
    ];
    if ( isset( $simpbook_msgs[ $simpbook_msg_key ] ) ) {
        echo '<div class="notice notice-success is-dismissible"><p>' . esc_html( $simpbook_msgs[ $simpbook_msg_key ] ) . '</p></div>';
    }
}
?>

<div class="wrap simpbook-admin-wrap">
    <h1 class="wp-heading-inline"><?php esc_html_e( 'Unterkünfte', 'simple-appointment-booking' ); ?></h1>
    <hr class="wp-header-end">

    <div class="view-toggle-container">
        <a href="<?php echo esc_url( admin_url( 'admin.php?page=simpbook-unterkuenfte&simpbook_aktion=neu' ) ); ?>" class="page-title-action">
            <?php esc_html_e( 'Neue Unterkunft anlegen', 'simple-appointment-booking' ); ?>
        </a>
        <?php if ( ! empty( $simpbook_unterkuenfte ) ) : ?>
            <div class="reservierungen-search-container">
                <span class="dashicons dashicons-search"></span>
                <input type="text" id="unterkuenfte-search-input"
                       placeholder="<?php esc_attr_e( 'Unterkunft suchen…', 'simple-appointment-booking' ); ?>"
                       aria-label="<?php esc_attr_e( 'Unterkünfte durchsuchen', 'simple-appointment-booking' ); ?>">
            </div>
        <?php endif; ?>
    </div>

    <?php if ( empty( $simpbook_unterkuenfte ) ) : ?>
        <div class="notice notice-info"><p><?php esc_html_e( 'Noch keine Unterkünfte vorhanden. Legen Sie Ihre erste Unterkunft an.', 'simple-appointment-booking' ); ?></p></div>
    <?php else : ?>

        <table class="wp-list-table widefat fixed striped simpbook-unterkuenfte-table">
            <thead>
                <tr>
                    <th style="width: 50px;"><?php esc_html_e( 'Bild', 'simple-appointment-booking' ); ?></th>
                    <th><?php esc_html_e( 'Titel', 'simple-appointment-booking' ); ?></th>
                    <th><?php esc_html_e( 'Typ', 'simple-appointment-booking' ); ?></th>
                    <th><?php esc_html_e( 'Ort', 'simple-appointment-booking' ); ?></th>
                    <th><?php esc_html_e( 'Preis/Nacht', 'simple-appointment-booking' ); ?></th>
                    <th><?php esc_html_e( 'Vermietungsart', 'simple-appointment-booking' ); ?></th>
                    <th><?php esc_html_e( 'Status', 'simple-appointment-booking' ); ?></th>
                    <th><?php esc_html_e( 'Aktionen', 'simple-appointment-booking' ); ?></th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ( $simpbook_unterkuenfte as $uk ) : ?>
                <tr data-search="<?php echo esc_attr( strtolower( $uk->titel . ' ' . $uk->ort . ' ' . $uk->unterkunft_typ ) ); ?>">
                    <td>
                        <?php if ( ! empty( $uk->titelbild_id ) ) : ?>
                            <?php echo wp_get_attachment_image( $uk->titelbild_id, [ 40, 40 ], false, [ 'style' => 'border-radius: 4px;' ] ); ?>
                        <?php else : ?>
                            <span class="dashicons dashicons-building" style="font-size: 30px; color: #ccc;"></span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <strong>
                            <a href="<?php echo esc_url( admin_url( 'admin.php?page=simpbook-unterkuenfte&simpbook_aktion=bearbeiten&simpbook_id=' . $uk->id ) ); ?>">
                                <?php echo esc_html( $uk->titel ); ?>
                            </a>
                        </strong>
                        <?php if ( ! empty( $uk->untertitel ) ) : ?>
                            <br><small style="color: #888;"><?php echo esc_html( $uk->untertitel ); ?></small>
                        <?php endif; ?>
                    </td>
                    <td><?php echo esc_html( $simpbook_typ_labels[ $uk->unterkunft_typ ] ?? $uk->unterkunft_typ ); ?></td>
                    <td><?php echo esc_html( $uk->ort ?? '—' ); ?></td>
                    <td>
                        <?php if ( $uk->preis_auf_anfrage ) : ?>
                            <em><?php esc_html_e( 'Auf Anfrage', 'simple-appointment-booking' ); ?></em>
                        <?php elseif ( $uk->preis_pro_nacht ) : ?>
                            <?php echo esc_html( number_format( (float) $uk->preis_pro_nacht, 2, ',', '.' ) . ' €' ); ?>
                        <?php else : ?>
                            —
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php echo 'zimmer' === $uk->vermietungsart
                            ? esc_html__( 'Zimmerweise', 'simple-appointment-booking' )
                            : esc_html__( 'Gesamte Unterkunft', 'simple-appointment-booking' ); ?>
                    </td>
                    <td>
                        <?php
                        $simpbook_status_class = 'aktiv' === $uk->status ? 'simpbook-status-bestaetigt' : 'simpbook-status-ausstehend';
                        $simpbook_status_label = 'aktiv' === $uk->status ? __( 'Aktiv', 'simple-appointment-booking' ) : __( 'Entwurf', 'simple-appointment-booking' );
                        ?>
                        <span class="simpbook-status-badge <?php echo esc_attr( $simpbook_status_class ); ?>"><?php echo esc_html( $simpbook_status_label ); ?></span>
                    </td>
                    <td>
                        <a href="<?php echo esc_url( admin_url( 'admin.php?page=simpbook-unterkuenfte&simpbook_aktion=bearbeiten&simpbook_id=' . $uk->id ) ); ?>"
                           class="button button-small" title="<?php esc_attr_e( 'Bearbeiten', 'simple-appointment-booking' ); ?>">
                            <span class="dashicons dashicons-edit" style="vertical-align: middle;"></span>
                        </a>
                        <?php
                        $simpbook_toggle_aktion = 'aktiv' === $uk->status ? 'deaktivieren' : 'aktivieren';
                        $simpbook_toggle_nonce  = wp_create_nonce( 'simpbook_toggle_unterkunft_' . $uk->id );
                        $simpbook_toggle_icon   = 'aktiv' === $uk->status ? 'hidden' : 'visibility';
                        ?>
                        <a href="<?php echo esc_url( admin_url( 'admin.php?page=simpbook-unterkuenfte&simpbook_aktion=' . $simpbook_toggle_aktion . '&simpbook_id=' . $uk->id . '&simpbook_nonce=' . $simpbook_toggle_nonce ) ); ?>"
                           class="button button-small" title="<?php echo esc_attr( ucfirst( $simpbook_toggle_aktion ) ); ?>">
                            <span class="dashicons dashicons-<?php echo esc_attr( $simpbook_toggle_icon ); ?>" style="vertical-align: middle;"></span>
                        </a>
                        <?php $simpbook_del_nonce = wp_create_nonce( 'simpbook_delete_unterkunft_' . $uk->id ); ?>
                        <a href="<?php echo esc_url( admin_url( 'admin.php?page=simpbook-unterkuenfte&simpbook_aktion=loeschen&simpbook_id=' . $uk->id . '&simpbook_nonce=' . $simpbook_del_nonce ) ); ?>"
                           class="button button-small simpbook-delete-btn"
                           onclick="return confirm('<?php esc_attr_e( 'Unterkunft wirklich löschen? Alle zugehörigen Zimmer und Tag-Zuordnungen werden ebenfalls gelöscht.', 'simple-appointment-booking' ); ?>');"
                           title="<?php esc_attr_e( 'Löschen', 'simple-appointment-booking' ); ?>">
                            <span class="dashicons dashicons-trash" style="vertical-align: middle; color: #b32d2e;"></span>
                        </a>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>

    <?php endif; ?>
</div>
