<?php
/**
 * Unterkunft-Tags verwalten — admin/unterkunft-tags.php
 *
 * CRUD für Tags / Badges die Unterkünften zugewiesen werden können.
 *
 * @package PocketBookingPro
 */

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

simpbook_display_trial_notice_on_admin_pages();

global $wpdb;
$simpbook_tags_table = $wpdb->prefix . 'simpbook_unterkunft_tags';

// 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_tag_' . $simpbook_del_id ) ) {
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $wpdb->delete( $simpbook_tags_table, [ 'id' => $simpbook_del_id ], [ '%d' ] );
        // Zuordnungen löschen
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $wpdb->delete( $wpdb->prefix . 'simpbook_unterkunft_tag_zuordnungen', [ 'tag_id' => $simpbook_del_id ], [ '%d' ] );
        wp_safe_redirect( admin_url( 'admin.php?page=simpbook-unterkunft-tags&simpbook_nachricht=geloescht' ) );
        exit;
    }
}

// Speichern (POST)
if ( isset( $_POST['simpbook_tag_speichern'] ) && isset( $_POST['simpbook_tag_nonce'] ) ) {
    if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['simpbook_tag_nonce'] ) ), 'simpbook_save_tag' ) ) {
        $simpbook_tag_data = [
            'name'       => isset( $_POST['simpbook_tag_name'] ) ? sanitize_text_field( wp_unslash( $_POST['simpbook_tag_name'] ) ) : '',
            'slug'       => isset( $_POST['simpbook_tag_name'] ) ? sanitize_title( wp_unslash( $_POST['simpbook_tag_name'] ) ) : '',
            'farbe'      => isset( $_POST['simpbook_tag_farbe'] ) ? sanitize_hex_color( wp_unslash( $_POST['simpbook_tag_farbe'] ) ) : '#2271b1',
            'icon'       => isset( $_POST['simpbook_tag_icon'] ) ? sanitize_text_field( wp_unslash( $_POST['simpbook_tag_icon'] ) ) : 'home',
            'sortierung' => isset( $_POST['simpbook_tag_sortierung'] ) ? intval( $_POST['simpbook_tag_sortierung'] ) : 0,
        ];

        $simpbook_tag_id = isset( $_POST['simpbook_tag_id'] ) ? intval( $_POST['simpbook_tag_id'] ) : 0;

        if ( ! empty( $simpbook_tag_data['name'] ) ) {
            if ( $simpbook_tag_id > 0 ) {
                // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
                $wpdb->update( $simpbook_tags_table, $simpbook_tag_data, [ 'id' => $simpbook_tag_id ], [ '%s', '%s', '%s', '%s', '%d' ], [ '%d' ] );
            } else {
                // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
                $wpdb->insert( $simpbook_tags_table, $simpbook_tag_data, [ '%s', '%s', '%s', '%s', '%d' ] );
            }
            wp_safe_redirect( admin_url( 'admin.php?page=simpbook-unterkunft-tags&simpbook_nachricht=gespeichert' ) );
            exit;
        }
    }
}

// Alle Tags laden
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$simpbook_tags = $wpdb->get_results( "SELECT * FROM {$simpbook_tags_table} ORDER BY sortierung ASC, name ASC" );

// Bearbeiten?
$simpbook_edit_tag = null;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['simpbook_aktion'] ) && 'bearbeiten' === $_GET['simpbook_aktion'] && isset( $_GET['simpbook_id'] ) ) {
    $simpbook_edit_id = intval( $_GET['simpbook_id'] );
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $simpbook_edit_tag = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$simpbook_tags_table} WHERE id = %d", $simpbook_edit_id ) );
}

// 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' => __( 'Tag erfolgreich gespeichert.', 'simple-appointment-booking' ),
        'geloescht'   => __( 'Tag erfolgreich gelöscht.', '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 simpbook-tags-wrap">
    <h1 class="wp-heading-inline"><?php esc_html_e( 'Unterkunft-Tags / Badges', 'simple-appointment-booking' ); ?></h1>
    <hr class="wp-header-end">

    <div style="display: grid; grid-template-columns: 350px 1fr; gap: 25px; margin-top: 15px;">

        <!-- Tag-Formular (links) -->
        <div style="background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,.1); align-self: start;">
            <h3 style="margin-top: 0;"><?php echo $simpbook_edit_tag ? esc_html__( 'Tag bearbeiten', 'simple-appointment-booking' ) : esc_html__( 'Neuen Tag erstellen', 'simple-appointment-booking' ); ?></h3>
            <form method="post" action="">
                <?php wp_nonce_field( 'simpbook_save_tag', 'simpbook_tag_nonce' ); ?>
                <?php if ( $simpbook_edit_tag ) : ?>
                    <input type="hidden" name="simpbook_tag_id" value="<?php echo esc_attr( $simpbook_edit_tag->id ); ?>">
                <?php endif; ?>

                <p>
                    <label for="simpbook_tag_name"><strong><?php esc_html_e( 'Name *', 'simple-appointment-booking' ); ?></strong></label><br>
                    <input type="text" name="simpbook_tag_name" id="simpbook_tag_name"
                           value="<?php echo esc_attr( $simpbook_edit_tag->name ?? '' ); ?>"
                           class="widefat" required>
                </p>
                <p>
                    <label for="simpbook_tag_farbe"><strong><?php esc_html_e( 'Farbe', 'simple-appointment-booking' ); ?></strong></label><br>
                    <input type="color" name="simpbook_tag_farbe" id="simpbook_tag_farbe"
                           value="<?php echo esc_attr( $simpbook_edit_tag->farbe ?? '#2271b1' ); ?>">
                </p>
                <p>
                    <label for="simpbook_tag_icon"><strong><?php esc_html_e( 'Icon (Dashicon-Name)', 'simple-appointment-booking' ); ?></strong></label><br>
                    <input type="text" name="simpbook_tag_icon" id="simpbook_tag_icon"
                           value="<?php echo esc_attr( $simpbook_edit_tag->icon ?? 'home' ); ?>"
                           class="widefat" placeholder="home, star-filled, heart, location, etc.">
                    <small class="description"><?php esc_html_e( 'Dashicon-Name ohne Prefix. z.B. "star-filled", "heart", "location".', 'simple-appointment-booking' ); ?></small>
                </p>
                <p>
                    <label for="simpbook_tag_sortierung"><strong><?php esc_html_e( 'Sortierung', 'simple-appointment-booking' ); ?></strong></label><br>
                    <input type="number" name="simpbook_tag_sortierung" id="simpbook_tag_sortierung"
                           value="<?php echo esc_attr( $simpbook_edit_tag->sortierung ?? 0 ); ?>" min="0" class="small-text">
                </p>
                <p>
                    <input type="submit" name="simpbook_tag_speichern" class="button button-primary" value="<?php esc_attr_e( 'Tag speichern', 'simple-appointment-booking' ); ?>">
                    <?php if ( $simpbook_edit_tag ) : ?>
                        <a href="<?php echo esc_url( admin_url( 'admin.php?page=simpbook-unterkunft-tags' ) ); ?>" class="button"><?php esc_html_e( 'Abbrechen', 'simple-appointment-booking' ); ?></a>
                    <?php endif; ?>
                </p>
            </form>
        </div>

        <!-- Tag-Liste (rechts) -->
        <div>
            <?php if ( empty( $simpbook_tags ) ) : ?>
                <div class="notice notice-info"><p><?php esc_html_e( 'Noch keine Tags vorhanden.', 'simple-appointment-booking' ); ?></p></div>
            <?php else : ?>
                <table class="wp-list-table widefat fixed striped">
                    <thead><tr>
                        <th><?php esc_html_e( 'Name', 'simple-appointment-booking' ); ?></th>
                        <th style="width: 60px;"><?php esc_html_e( 'Farbe', 'simple-appointment-booking' ); ?></th>
                        <th><?php esc_html_e( 'Icon', 'simple-appointment-booking' ); ?></th>
                        <th><?php esc_html_e( 'Slug', 'simple-appointment-booking' ); ?></th>
                        <th style="width: 120px;"><?php esc_html_e( 'Aktionen', 'simple-appointment-booking' ); ?></th>
                    </tr></thead>
                    <tbody>
                    <?php foreach ( $simpbook_tags as $tag ) : ?>
                        <tr>
                            <td>
                                <span style="background: <?php echo esc_attr( $tag->farbe ); ?>; color: #fff; padding: 3px 10px; border-radius: 4px; font-size: 13px;">
                                    <span class="dashicons dashicons-<?php echo esc_attr( $tag->icon ); ?>" style="font-size: 14px; width: 14px; height: 14px; vertical-align: middle;"></span>
                                    <?php echo esc_html( $tag->name ); ?>
                                </span>
                            </td>
                            <td><span style="display: inline-block; width: 20px; height: 20px; border-radius: 50%; background: <?php echo esc_attr( $tag->farbe ); ?>;"></span></td>
                            <td><code><?php echo esc_html( $tag->icon ); ?></code></td>
                            <td><code><?php echo esc_html( $tag->slug ); ?></code></td>
                            <td>
                                <a href="<?php echo esc_url( admin_url( 'admin.php?page=simpbook-unterkunft-tags&simpbook_aktion=bearbeiten&simpbook_id=' . $tag->id ) ); ?>" class="button button-small">
                                    <span class="dashicons dashicons-edit" style="vertical-align: middle;"></span>
                                </a>
                                <?php $simpbook_del_nonce = wp_create_nonce( 'simpbook_delete_tag_' . $tag->id ); ?>
                                <a href="<?php echo esc_url( admin_url( 'admin.php?page=simpbook-unterkunft-tags&simpbook_aktion=loeschen&simpbook_id=' . $tag->id . '&simpbook_nonce=' . $simpbook_del_nonce ) ); ?>"
                                   class="button button-small"
                                   onclick="return confirm('<?php esc_attr_e( 'Tag wirklich 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>

    </div>
</div>
