<?php
/**
 * Gastgeber bearbeiten — admin/gastgeber-bearbeiten.php
 *
 * Nutzt dieselbe mitarbeiter-Tabelle, aber mit Vermietungs-Labels.
 *
 * @package PocketBookingPro
 */

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

simpbook_display_trial_notice_on_admin_pages();

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

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$simpbook_gg_id = isset( $_GET['simpbook_id'] ) ? intval( $_GET['simpbook_id'] ) : 0;
$simpbook_gg    = null;

if ( $simpbook_gg_id > 0 ) {
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $simpbook_gg = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM %i WHERE id = %d", $simpbook_table, $simpbook_gg_id ) );
}

// Formular verarbeiten
if ( isset( $_POST['simpbook_gastgeber_speichern'] ) && check_admin_referer( 'simpbook_gastgeber_speichern' ) ) {
    $simpbook_name    = isset( $_POST['simpbook_name'] ) ? sanitize_text_field( wp_unslash( $_POST['simpbook_name'] ) ) : '';
    $simpbook_email   = isset( $_POST['simpbook_email'] ) ? sanitize_email( wp_unslash( $_POST['simpbook_email'] ) ) : '';
    $simpbook_telefon = isset( $_POST['simpbook_telefon'] ) ? sanitize_text_field( wp_unslash( $_POST['simpbook_telefon'] ) ) : '';
    $simpbook_beschr  = isset( $_POST['simpbook_beschreibung'] ) ? wp_kses_post( wp_unslash( $_POST['simpbook_beschreibung'] ) ) : '';
    $simpbook_aktiv   = isset( $_POST['simpbook_aktiv'] ) ? 1 : 0;
    $simpbook_sort    = isset( $_POST['simpbook_sortierung'] ) ? intval( $_POST['simpbook_sortierung'] ) : 0;

    if ( empty( $simpbook_name ) ) {
        echo '<div class="notice notice-error"><p>' . esc_html__( 'Bitte geben Sie einen Namen ein.', 'simple-appointment-booking' ) . '</p></div>';
    } else {
        $simpbook_data = [
            'name'          => $simpbook_name,
            'email'         => $simpbook_email,
            'telefon'       => $simpbook_telefon,
            'beschreibung'  => $simpbook_beschr,
            'aktiv'         => $simpbook_aktiv,
            'sortierung'    => $simpbook_sort,
        ];

        // Profilfoto
        if ( ! empty( $_POST['simpbook_profilfoto_url'] ) ) {
            $simpbook_data['profilfoto_url'] = esc_url_raw( wp_unslash( $_POST['simpbook_profilfoto_url'] ) );
        }

        if ( $simpbook_gg_id > 0 && $simpbook_gg ) {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            $wpdb->update( $simpbook_table, $simpbook_data, [ 'id' => $simpbook_gg_id ] );
        } else {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
            $wpdb->insert( $simpbook_table, $simpbook_data );
        }

        wp_safe_redirect( admin_url( 'admin.php?page=simpbook-gastgeber&simpbook_nachricht=gespeichert' ) );
        exit;
    }
}

// Werte
$simpbook_val = function( $field, $default = '' ) use ( $simpbook_gg ) {
    return $simpbook_gg ? ( $simpbook_gg->$field ?? $default ) : $default;
};
?>

<div class="wrap simpbook-admin-wrap">
    <h1 class="wp-heading-inline">
        <?php echo $simpbook_gg_id > 0
            ? esc_html__( 'Gastgeber bearbeiten', 'simple-appointment-booking' )
            : esc_html__( 'Neuen Gastgeber anlegen', 'simple-appointment-booking' ); ?>
    </h1>
    <a href="<?php echo esc_url( admin_url( 'admin.php?page=simpbook-gastgeber' ) ); ?>" class="page-title-action">
        <?php esc_html_e( 'Zurück zur Liste', 'simple-appointment-booking' ); ?>
    </a>
    <hr class="wp-header-end">

    <form method="post" action="">
        <?php wp_nonce_field( 'simpbook_gastgeber_speichern' ); ?>

        <table class="form-table">
            <tr>
                <th><label for="simpbook_name"><?php esc_html_e( 'Name *', 'simple-appointment-booking' ); ?></label></th>
                <td><input name="simpbook_name" type="text" id="simpbook_name" value="<?php echo esc_attr( $simpbook_val( 'name' ) ); ?>" class="regular-text" required></td>
            </tr>
            <tr>
                <th><label for="simpbook_email"><?php esc_html_e( 'E-Mail', 'simple-appointment-booking' ); ?></label></th>
                <td><input name="simpbook_email" type="email" id="simpbook_email" value="<?php echo esc_attr( $simpbook_val( 'email' ) ); ?>" class="regular-text"></td>
            </tr>
            <tr>
                <th><label for="simpbook_telefon"><?php esc_html_e( 'Telefon', 'simple-appointment-booking' ); ?></label></th>
                <td><input name="simpbook_telefon" type="tel" id="simpbook_telefon" value="<?php echo esc_attr( $simpbook_val( 'telefon' ) ); ?>" class="regular-text"></td>
            </tr>
            <tr>
                <th><label for="simpbook_beschreibung"><?php esc_html_e( 'Beschreibung', 'simple-appointment-booking' ); ?></label></th>
                <td><textarea name="simpbook_beschreibung" id="simpbook_beschreibung" rows="5" class="large-text"><?php echo esc_textarea( $simpbook_val( 'beschreibung' ) ); ?></textarea></td>
            </tr>
            <tr>
                <th><label><?php esc_html_e( 'Profilfoto', 'simple-appointment-booking' ); ?></label></th>
                <td>
                    <div id="simpbook-profilfoto-preview" style="margin-bottom: 10px;">
                        <?php if ( ! empty( $simpbook_val( 'profilfoto_url' ) ) ) : ?>
                            <img src="<?php echo esc_url( $simpbook_val( 'profilfoto_url' ) ); ?>" alt="" style="max-width: 150px; border-radius: 50%;">
                        <?php endif; ?>
                    </div>
                    <input type="hidden" name="simpbook_profilfoto_url" id="simpbook_profilfoto_url" value="<?php echo esc_attr( $simpbook_val( 'profilfoto_url' ) ); ?>">
                    <button type="button" id="simpbook-profilfoto-waehlen" class="button"><?php esc_html_e( 'Foto wählen', 'simple-appointment-booking' ); ?></button>
                    <button type="button" id="simpbook-profilfoto-entfernen" class="button" <?php echo empty( $simpbook_val( 'profilfoto_url' ) ) ? 'style="display:none;"' : ''; ?>><?php esc_html_e( 'Entfernen', 'simple-appointment-booking' ); ?></button>
                </td>
            </tr>
            <tr>
                <th><label for="simpbook_sortierung"><?php esc_html_e( 'Sortierung', 'simple-appointment-booking' ); ?></label></th>
                <td><input name="simpbook_sortierung" type="number" id="simpbook_sortierung" value="<?php echo esc_attr( $simpbook_val( 'sortierung', 0 ) ); ?>" min="0" class="small-text"></td>
            </tr>
            <tr>
                <th><?php esc_html_e( 'Status', 'simple-appointment-booking' ); ?></th>
                <td>
                    <label>
                        <input name="simpbook_aktiv" type="checkbox" value="1" <?php checked( $simpbook_val( 'aktiv', 1 ), 1 ); ?>>
                        <?php esc_html_e( 'Aktiv', 'simple-appointment-booking' ); ?>
                    </label>
                </td>
            </tr>
        </table>

        <p class="submit">
            <input type="submit" name="simpbook_gastgeber_speichern" class="button button-primary" value="<?php esc_attr_e( 'Gastgeber speichern', 'simple-appointment-booking' ); ?>">
        </p>
    </form>
</div>
