<?php
/**
 * Vermietung Dashboard — admin/dashboard-vermietung.php
 *
 * Eigene Dashboard-Datei für den Vermietungsmodus.
 * Zeigt Check-ins/Check-outs, ausstehende Anfragen, Belegungsrate.
 *
 * @package PocketBookingPro
 */

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

if ( function_exists( 'simpbook_display_trial_notice_on_admin_pages' ) ) {
    simpbook_display_trial_notice_on_admin_pages();
}

global $wpdb;

$simpbook_heute = current_time( 'Y-m-d' );
$simpbook_dashboard_error = false;

try {
    // Heutige Check-ins
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $simpbook_checkins_heute = $wpdb->get_results( $wpdb->prepare(
        "SELECT a.*, u.titel AS unterkunft_titel
         FROM {$wpdb->prefix}simpbook_aufenthalte a
         LEFT JOIN {$wpdb->prefix}simpbook_unterkuenfte u ON a.unterkunft_id = u.id
         WHERE a.checkin_datum = %s AND a.storniert = 0 AND a.status IN ('BESTÄTIGT','BESTAETIGT','AUSSTEHEND')
         ORDER BY a.gast_name ASC",
        $simpbook_heute
    ) );

    // Heutige Check-outs
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $simpbook_checkouts_heute = $wpdb->get_results( $wpdb->prepare(
        "SELECT a.*, u.titel AS unterkunft_titel
         FROM {$wpdb->prefix}simpbook_aufenthalte a
         LEFT JOIN {$wpdb->prefix}simpbook_unterkuenfte u ON a.unterkunft_id = u.id
         WHERE a.checkout_datum = %s AND a.storniert = 0 AND a.status IN ('BESTÄTIGT','BESTAETIGT','EINGECHECKT')
         ORDER BY a.gast_name ASC",
        $simpbook_heute
    ) );

    // Ausstehende Buchungsanfragen
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $simpbook_ausstehend = $wpdb->get_results(
        "SELECT a.*, u.titel AS unterkunft_titel
         FROM {$wpdb->prefix}simpbook_aufenthalte a
         LEFT JOIN {$wpdb->prefix}simpbook_unterkuenfte u ON a.unterkunft_id = u.id
         WHERE a.status = 'AUSSTEHEND' AND a.storniert = 0
         ORDER BY a.erstellt_am DESC
         LIMIT 10"
    );

    // Statistik-Zahlen
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $simpbook_total_unterkuenfte = (int) $wpdb->get_var(
        "SELECT COUNT(*) FROM {$wpdb->prefix}simpbook_unterkuenfte WHERE status = 'aktiv'"
    );
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $simpbook_total_aufenthalte = (int) $wpdb->get_var(
        "SELECT COUNT(*) FROM {$wpdb->prefix}simpbook_aufenthalte WHERE storniert = 0"
    );
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $simpbook_bestaetigt_anzahl = (int) $wpdb->get_var(
        "SELECT COUNT(*) FROM {$wpdb->prefix}simpbook_aufenthalte WHERE status IN ('BESTÄTIGT','BESTAETIGT') AND storniert = 0"
    );
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $simpbook_ausstehend_anzahl = (int) $wpdb->get_var(
        "SELECT COUNT(*) FROM {$wpdb->prefix}simpbook_aufenthalte WHERE status = 'AUSSTEHEND' AND storniert = 0"
    );

} catch ( Exception $e ) {
    $simpbook_dashboard_error = true;
}
?>

<div class="wrap simpbook-admin-wrap simpbook-dashboard-page">
    <h1><?php esc_html_e( 'Dashboard', 'simple-appointment-booking' ); ?> — <small><?php esc_html_e( 'Vermietung', 'simple-appointment-booking' ); ?></small></h1>

    <?php if ( $simpbook_dashboard_error ) : ?>
        <div class="notice notice-error"><p><?php esc_html_e( 'Fehler beim Laden der Dashboard-Daten.', 'simple-appointment-booking' ); ?></p></div>
    <?php else : ?>

    <!-- Statistik-Karten -->
    <div class="simpbook-dashboard-stats" style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 15px; margin-bottom: 25px;">
        <div class="simpbook-stat-card" style="background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,.1); text-align: center;">
            <div style="font-size: 32px; font-weight: 700; color: #2271b1;"><?php echo esc_html( $simpbook_total_unterkuenfte ); ?></div>
            <div style="color: #666; margin-top: 5px;"><?php esc_html_e( 'Aktive Unterkünfte', 'simple-appointment-booking' ); ?></div>
        </div>
        <div class="simpbook-stat-card" style="background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,.1); text-align: center;">
            <div style="font-size: 32px; font-weight: 700; color: #00a32a;"><?php echo esc_html( $simpbook_bestaetigt_anzahl ); ?></div>
            <div style="color: #666; margin-top: 5px;"><?php esc_html_e( 'Bestätigte Buchungen', 'simple-appointment-booking' ); ?></div>
        </div>
        <div class="simpbook-stat-card" style="background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,.1); text-align: center;">
            <div style="font-size: 32px; font-weight: 700; color: #dba617;"><?php echo esc_html( $simpbook_ausstehend_anzahl ); ?></div>
            <div style="color: #666; margin-top: 5px;"><?php esc_html_e( 'Ausstehende Anfragen', 'simple-appointment-booking' ); ?></div>
        </div>
        <div class="simpbook-stat-card" style="background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,.1); text-align: center;">
            <div style="font-size: 32px; font-weight: 700; color: #135e96;"><?php echo esc_html( count( $simpbook_checkins_heute ) ); ?> / <?php echo esc_html( count( $simpbook_checkouts_heute ) ); ?></div>
            <div style="color: #666; margin-top: 5px;"><?php esc_html_e( 'Check-in / Check-out heute', 'simple-appointment-booking' ); ?></div>
        </div>
    </div>

    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">

        <!-- Heutige Check-ins -->
        <div class="simpbook-dashboard-widget" style="background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,.1);">
            <h2 style="margin-top: 0; font-size: 16px;">📥 <?php esc_html_e( 'Heutige Check-ins', 'simple-appointment-booking' ); ?></h2>
            <?php if ( empty( $simpbook_checkins_heute ) ) : ?>
                <p style="color: #888;"><?php esc_html_e( 'Keine Check-ins heute.', 'simple-appointment-booking' ); ?></p>
            <?php else : ?>
                <table class="wp-list-table widefat fixed striped" style="margin-top: 10px;">
                    <thead><tr>
                        <th><?php esc_html_e( 'Gast', 'simple-appointment-booking' ); ?></th>
                        <th><?php esc_html_e( 'Unterkunft', 'simple-appointment-booking' ); ?></th>
                        <th><?php esc_html_e( 'Nächte', 'simple-appointment-booking' ); ?></th>
                        <th><?php esc_html_e( 'Status', 'simple-appointment-booking' ); ?></th>
                    </tr></thead>
                    <tbody>
                    <?php foreach ( $simpbook_checkins_heute as $ci ) : ?>
                        <tr>
                            <td><strong><?php echo esc_html( $ci->gast_name ); ?></strong></td>
                            <td><?php echo esc_html( $ci->unterkunft_titel ); ?></td>
                            <td><?php echo esc_html( $ci->anzahl_naechte ); ?></td>
                            <td><span class="simpbook-status-badge simpbook-status-<?php echo esc_attr( strtolower( $ci->status ) ); ?>"><?php echo esc_html( $ci->status ); ?></span></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>

        <!-- Heutige Check-outs -->
        <div class="simpbook-dashboard-widget" style="background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,.1);">
            <h2 style="margin-top: 0; font-size: 16px;">📤 <?php esc_html_e( 'Heutige Check-outs', 'simple-appointment-booking' ); ?></h2>
            <?php if ( empty( $simpbook_checkouts_heute ) ) : ?>
                <p style="color: #888;"><?php esc_html_e( 'Keine Check-outs heute.', 'simple-appointment-booking' ); ?></p>
            <?php else : ?>
                <table class="wp-list-table widefat fixed striped" style="margin-top: 10px;">
                    <thead><tr>
                        <th><?php esc_html_e( 'Gast', 'simple-appointment-booking' ); ?></th>
                        <th><?php esc_html_e( 'Unterkunft', 'simple-appointment-booking' ); ?></th>
                        <th><?php esc_html_e( 'Nächte', 'simple-appointment-booking' ); ?></th>
                    </tr></thead>
                    <tbody>
                    <?php foreach ( $simpbook_checkouts_heute as $co ) : ?>
                        <tr>
                            <td><strong><?php echo esc_html( $co->gast_name ); ?></strong></td>
                            <td><?php echo esc_html( $co->unterkunft_titel ); ?></td>
                            <td><?php echo esc_html( $co->anzahl_naechte ); ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>

    </div>

    <!-- Ausstehende Buchungsanfragen -->
    <?php if ( ! empty( $simpbook_ausstehend ) ) : ?>
    <div class="simpbook-dashboard-widget" style="background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,.1); margin-top: 20px;">
        <h2 style="margin-top: 0; font-size: 16px;">⏳ <?php esc_html_e( 'Ausstehende Buchungsanfragen', 'simple-appointment-booking' ); ?></h2>
        <table class="wp-list-table widefat fixed striped" style="margin-top: 10px;">
            <thead><tr>
                <th><?php esc_html_e( 'Gast', 'simple-appointment-booking' ); ?></th>
                <th><?php esc_html_e( 'Unterkunft', 'simple-appointment-booking' ); ?></th>
                <th><?php esc_html_e( 'Check-in', 'simple-appointment-booking' ); ?></th>
                <th><?php esc_html_e( 'Check-out', 'simple-appointment-booking' ); ?></th>
                <th><?php esc_html_e( 'Nächte', 'simple-appointment-booking' ); ?></th>
                <th><?php esc_html_e( 'Preis', 'simple-appointment-booking' ); ?></th>
                <th><?php esc_html_e( 'Aktion', 'simple-appointment-booking' ); ?></th>
            </tr></thead>
            <tbody>
            <?php foreach ( $simpbook_ausstehend as $anf ) : ?>
                <tr>
                    <td><strong><?php echo esc_html( $anf->gast_name ); ?></strong><br><small><?php echo esc_html( $anf->gast_email ); ?></small></td>
                    <td><?php echo esc_html( $anf->unterkunft_titel ); ?></td>
                    <td><?php echo esc_html( wp_date( get_option( 'date_format' ), strtotime( $anf->checkin_datum ) ) ); ?></td>
                    <td><?php echo esc_html( wp_date( get_option( 'date_format' ), strtotime( $anf->checkout_datum ) ) ); ?></td>
                    <td><?php echo esc_html( $anf->anzahl_naechte ); ?></td>
                    <td><?php echo $anf->gesamtpreis ? esc_html( number_format( (float) $anf->gesamtpreis, 2, ',', '.' ) . ' €' ) : '—'; ?></td>
                    <td>
                        <a href="<?php echo esc_url( admin_url( 'admin.php?page=simpbook-reservierungen&simpbook_aktion=bearbeiten&simpbook_id=' . $anf->id ) ); ?>" class="button button-small button-primary"><?php esc_html_e( 'Bearbeiten', 'simple-appointment-booking' ); ?></a>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>

    <?php
    // Hook: Weitere Dashboard-Widgets (z.B. Payment-Addon Umsatz)
    do_action( 'simpbook_dashboard_widgets', 'ferienhaeuser' );
    ?>

    <?php endif; ?>
</div>
