<?php
/**
 * Gastgeber-Liste — admin/gastgeber.php
 *
 * Im Vermietungsmodus werden Mitarbeiter als "Gastgeber" bezeichnet.
 * Nutzt dieselbe mitarbeiter-Tabelle, aber mit angepasster UI.
 *
 * @package PocketBookingPro
 */

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

simpbook_display_trial_notice_on_admin_pages();

global $wpdb;
$simpbook_table = $wpdb->prefix . 'mitarbeiter';

// 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_gastgeber_' . $simpbook_del_id ) ) {
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $wpdb->delete( $simpbook_table, [ 'id' => $simpbook_del_id ], [ '%d' ] );
        wp_safe_redirect( admin_url( 'admin.php?page=simpbook-gastgeber&simpbook_nachricht=geloescht' ) );
        exit;
    }
}

// Alle Gastgeber (= Mitarbeiter) laden
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$simpbook_gastgeber = $wpdb->get_results(
    $wpdb->prepare( "SELECT * FROM %i ORDER BY sortierung ASC, name ASC", $simpbook_table )
);

// 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'  => __( 'Gastgeber erfolgreich gespeichert.', 'simple-appointment-booking' ),
        'geloescht'    => __( 'Gastgeber erfolgreich gelöscht.', 'simple-appointment-booking' ),
        'aktualisiert' => __( 'Gastgeber 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( 'Gastgeber', '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-gastgeber&simpbook_aktion=bearbeiten' ) ); ?>" class="page-title-action">
            <?php esc_html_e( 'Neuen Gastgeber anlegen', 'simple-appointment-booking' ); ?>
        </a>
    </div>

    <?php if ( empty( $simpbook_gastgeber ) ) : ?>
        <div class="notice notice-info"><p><?php esc_html_e( 'Noch keine Gastgeber vorhanden.', 'simple-appointment-booking' ); ?></p></div>
    <?php else : ?>
        <table class="wp-list-table widefat fixed striped">
            <thead><tr>
                <th style="width: 50px;"><?php esc_html_e( 'Bild', 'simple-appointment-booking' ); ?></th>
                <th><?php esc_html_e( 'Name', 'simple-appointment-booking' ); ?></th>
                <th><?php esc_html_e( 'E-Mail', 'simple-appointment-booking' ); ?></th>
                <th><?php esc_html_e( 'Telefon', 'simple-appointment-booking' ); ?></th>
                <th><?php esc_html_e( 'Status', 'simple-appointment-booking' ); ?></th>
                <th style="width: 120px;"><?php esc_html_e( 'Aktionen', 'simple-appointment-booking' ); ?></th>
            </tr></thead>
            <tbody>
            <?php foreach ( $simpbook_gastgeber as $gg ) : ?>
                <tr>
                    <td>
                        <?php if ( ! empty( $gg->profilfoto_url ) ) : ?>
                            <img src="<?php echo esc_url( $gg->profilfoto_url ); ?>" alt="" width="40" height="40" style="border-radius: 50%; object-fit: cover;">
                        <?php else : ?>
                            <span class="dashicons dashicons-admin-users" style="font-size: 30px; color: #ccc;"></span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <strong>
                            <a href="<?php echo esc_url( admin_url( 'admin.php?page=simpbook-gastgeber&simpbook_aktion=bearbeiten&simpbook_id=' . $gg->id ) ); ?>">
                                <?php echo esc_html( $gg->name ); ?>
                            </a>
                        </strong>
                    </td>
                    <td><?php echo esc_html( $gg->email ?? '—' ); ?></td>
                    <td><?php echo esc_html( $gg->telefon ?? '—' ); ?></td>
                    <td>
                        <?php if ( $gg->aktiv ) : ?>
                            <span class="simpbook-status-badge simpbook-status-bestaetigt"><?php esc_html_e( 'Aktiv', 'simple-appointment-booking' ); ?></span>
                        <?php else : ?>
                            <span class="simpbook-status-badge simpbook-status-ausstehend"><?php esc_html_e( 'Inaktiv', 'simple-appointment-booking' ); ?></span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <a href="<?php echo esc_url( admin_url( 'admin.php?page=simpbook-gastgeber&simpbook_aktion=bearbeiten&simpbook_id=' . $gg->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_del_nonce = wp_create_nonce( 'simpbook_delete_gastgeber_' . $gg->id ); ?>
                        <a href="<?php echo esc_url( admin_url( 'admin.php?page=simpbook-gastgeber&simpbook_aktion=loeschen&simpbook_id=' . $gg->id . '&simpbook_nonce=' . $simpbook_del_nonce ) ); ?>"
                           class="button button-small"
                           onclick="return confirm('<?php esc_attr_e( 'Gastgeber wirklich löschen?', '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>
