<?php
/**
 * Vermietung-Einstellungen — admin/vermietung-einstellungen.php
 *
 * Einstellungen speziell für den Vermietungsmodus:
 * Min/Max Nächte, Check-in/out-Zeiten, Auto-Bestätigung, etc.
 *
 * @package PocketBookingPro
 */

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

simpbook_display_trial_notice_on_admin_pages();

// Einstellungen speichern
if ( isset( $_POST['simpbook_vermietung_einstellungen_speichern'] ) && check_admin_referer( 'simpbook_vermietung_einstellungen' ) ) {
    $simpbook_opts = [
        'simpbook_vermietung_standard_min_naechte'  => isset( $_POST['simpbook_min_naechte'] ) ? intval( $_POST['simpbook_min_naechte'] ) : 1,
        'simpbook_vermietung_standard_max_naechte'  => isset( $_POST['simpbook_max_naechte'] ) ? intval( $_POST['simpbook_max_naechte'] ) : 365,
        'simpbook_vermietung_standard_checkin_zeit'  => isset( $_POST['simpbook_checkin_zeit'] ) ? sanitize_text_field( wp_unslash( $_POST['simpbook_checkin_zeit'] ) ) : '15:00',
        'simpbook_vermietung_standard_checkout_zeit' => isset( $_POST['simpbook_checkout_zeit'] ) ? sanitize_text_field( wp_unslash( $_POST['simpbook_checkout_zeit'] ) ) : '11:00',
        'simpbook_vermietung_auto_bestaetigung'      => isset( $_POST['simpbook_auto_bestaetigung'] ) ? '1' : '0',
        'simpbook_vermietung_buchung_vorlauf_tage'   => isset( $_POST['simpbook_buchung_vorlauf_tage'] ) ? intval( $_POST['simpbook_buchung_vorlauf_tage'] ) : 0,
        'simpbook_vermietung_buchung_max_vorlauf_tage' => isset( $_POST['simpbook_buchung_max_vorlauf_tage'] ) ? intval( $_POST['simpbook_buchung_max_vorlauf_tage'] ) : 365,
        'simpbook_vermietung_gaeste_felder_pflicht'  => isset( $_POST['simpbook_gaeste_pflicht'] ) ? '1' : '0',
        'simpbook_vermietung_telefon_pflicht'        => isset( $_POST['simpbook_telefon_pflicht'] ) ? '1' : '0',
        'simpbook_vermietung_agb_link'               => isset( $_POST['simpbook_agb_link'] ) ? esc_url_raw( wp_unslash( $_POST['simpbook_agb_link'] ) ) : '',
        'simpbook_vermietung_storno_frist_tage'      => isset( $_POST['simpbook_storno_frist_tage'] ) ? intval( $_POST['simpbook_storno_frist_tage'] ) : 14,
        'simpbook_vermietung_waehrung'               => isset( $_POST['simpbook_waehrung'] ) ? sanitize_text_field( wp_unslash( $_POST['simpbook_waehrung'] ) ) : 'EUR',
        'simpbook_vermietung_detail_seite'           => isset( $_POST['simpbook_detail_seite'] ) ? intval( $_POST['simpbook_detail_seite'] ) : 0,
    ];

    // Payment-Felder speichern (nur wenn Payment-Lizenz aktiv)
    if ( function_exists( 'simpbook_payment_lizenz_aktiv' ) && simpbook_payment_lizenz_aktiv() ) {
        $simpbook_opts['simpbook_payment_stripe_test_pk']   = isset( $_POST['simpbook_stripe_test_pk'] ) ? sanitize_text_field( wp_unslash( $_POST['simpbook_stripe_test_pk'] ) ) : '';
        $simpbook_opts['simpbook_payment_stripe_test_sk']   = isset( $_POST['simpbook_stripe_test_sk'] ) ? sanitize_text_field( wp_unslash( $_POST['simpbook_stripe_test_sk'] ) ) : '';
        $simpbook_opts['simpbook_payment_stripe_live_pk']   = isset( $_POST['simpbook_stripe_live_pk'] ) ? sanitize_text_field( wp_unslash( $_POST['simpbook_stripe_live_pk'] ) ) : '';
        $simpbook_opts['simpbook_payment_stripe_live_sk']   = isset( $_POST['simpbook_stripe_live_sk'] ) ? sanitize_text_field( wp_unslash( $_POST['simpbook_stripe_live_sk'] ) ) : '';
        $simpbook_opts['simpbook_payment_testmodus']        = isset( $_POST['simpbook_payment_testmodus'] ) ? '1' : '0';
        $simpbook_opts['simpbook_payment_stripe_aktiv']     = isset( $_POST['simpbook_stripe_aktiv'] ) ? '1' : '0';
        $simpbook_opts['simpbook_payment_rechnung_aktiv']   = isset( $_POST['simpbook_rechnung_aktiv'] ) ? '1' : '0';
        $simpbook_opts['simpbook_payment_vorkasse_aktiv']   = isset( $_POST['simpbook_vorkasse_aktiv'] ) ? '1' : '0';
        $simpbook_opts['simpbook_payment_vorkasse_details'] = isset( $_POST['simpbook_vorkasse_details'] ) ? sanitize_textarea_field( wp_unslash( $_POST['simpbook_vorkasse_details'] ) ) : '';
    }

    foreach ( $simpbook_opts as $key => $val ) {
        update_option( $key, $val );
    }

    echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Einstellungen gespeichert.', 'simple-appointment-booking' ) . '</p></div>';
}

// Werte laden
$simpbook_opt = function( $key, $default = '' ) {
    return get_option( $key, $default );
};
?>

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

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

        <h2 class="title"><?php esc_html_e( 'Buchungsregeln', 'simple-appointment-booking' ); ?></h2>
        <table class="form-table">
            <tr>
                <th><label for="simpbook_min_naechte"><?php esc_html_e( 'Standard Min. Nächte', 'simple-appointment-booking' ); ?></label></th>
                <td>
                    <input name="simpbook_min_naechte" type="number" id="simpbook_min_naechte"
                           value="<?php echo esc_attr( $simpbook_opt( 'simpbook_vermietung_standard_min_naechte', 1 ) ); ?>"
                           min="1" class="small-text">
                    <p class="description"><?php esc_html_e( 'Gilt als Fallback, wenn bei der Unterkunft kein eigener Wert gesetzt ist.', 'simple-appointment-booking' ); ?></p>
                </td>
            </tr>
            <tr>
                <th><label for="simpbook_max_naechte"><?php esc_html_e( 'Standard Max. Nächte', 'simple-appointment-booking' ); ?></label></th>
                <td><input name="simpbook_max_naechte" type="number" id="simpbook_max_naechte" value="<?php echo esc_attr( $simpbook_opt( 'simpbook_vermietung_standard_max_naechte', 365 ) ); ?>" min="1" class="small-text"></td>
            </tr>
            <tr>
                <th><label for="simpbook_checkin_zeit"><?php esc_html_e( 'Standard Check-in Uhrzeit', 'simple-appointment-booking' ); ?></label></th>
                <td><input name="simpbook_checkin_zeit" type="time" id="simpbook_checkin_zeit" value="<?php echo esc_attr( $simpbook_opt( 'simpbook_vermietung_standard_checkin_zeit', '15:00' ) ); ?>"></td>
            </tr>
            <tr>
                <th><label for="simpbook_checkout_zeit"><?php esc_html_e( 'Standard Check-out Uhrzeit', 'simple-appointment-booking' ); ?></label></th>
                <td><input name="simpbook_checkout_zeit" type="time" id="simpbook_checkout_zeit" value="<?php echo esc_attr( $simpbook_opt( 'simpbook_vermietung_standard_checkout_zeit', '11:00' ) ); ?>"></td>
            </tr>
            <tr>
                <th><label for="simpbook_buchung_vorlauf_tage"><?php esc_html_e( 'Min. Vorlauf (Tage)', 'simple-appointment-booking' ); ?></label></th>
                <td>
                    <input name="simpbook_buchung_vorlauf_tage" type="number" id="simpbook_buchung_vorlauf_tage" value="<?php echo esc_attr( $simpbook_opt( 'simpbook_vermietung_buchung_vorlauf_tage', 0 ) ); ?>" min="0" class="small-text">
                    <p class="description"><?php esc_html_e( 'Wie viele Tage im Voraus muss mindestens gebucht werden? 0 = ab heute möglich.', 'simple-appointment-booking' ); ?></p>
                </td>
            </tr>
            <tr>
                <th><label for="simpbook_buchung_max_vorlauf_tage"><?php esc_html_e( 'Max. Vorlauf (Tage)', 'simple-appointment-booking' ); ?></label></th>
                <td>
                    <input name="simpbook_buchung_max_vorlauf_tage" type="number" id="simpbook_buchung_max_vorlauf_tage" value="<?php echo esc_attr( $simpbook_opt( 'simpbook_vermietung_buchung_max_vorlauf_tage', 365 ) ); ?>" min="1" class="small-text">
                    <p class="description"><?php esc_html_e( 'Wie weit in die Zukunft darf gebucht werden?', 'simple-appointment-booking' ); ?></p>
                </td>
            </tr>
        </table>

        <h2 class="title"><?php esc_html_e( 'Bestätigung & Stornierung', 'simple-appointment-booking' ); ?></h2>
        <table class="form-table">
            <tr>
                <th><?php esc_html_e( 'Auto-Bestätigung', 'simple-appointment-booking' ); ?></th>
                <td>
                    <label>
                        <input type="checkbox" name="simpbook_auto_bestaetigung" value="1" <?php checked( $simpbook_opt( 'simpbook_vermietung_auto_bestaetigung', '0' ), '1' ); ?>>
                        <?php esc_html_e( 'Buchungen automatisch bestätigen (kein manuelles Bestätigen nötig)', 'simple-appointment-booking' ); ?>
                    </label>
                </td>
            </tr>
            <tr>
                <th><label for="simpbook_storno_frist_tage"><?php esc_html_e( 'Storno-Frist (Tage vor Check-in)', 'simple-appointment-booking' ); ?></label></th>
                <td>
                    <input name="simpbook_storno_frist_tage" type="number" id="simpbook_storno_frist_tage" value="<?php echo esc_attr( $simpbook_opt( 'simpbook_vermietung_storno_frist_tage', 14 ) ); ?>" min="0" class="small-text">
                    <p class="description"><?php esc_html_e( 'Bis wann kann der Gast kostenlos stornieren? 0 = keine kostenlose Stornierung.', 'simple-appointment-booking' ); ?></p>
                </td>
            </tr>
        </table>

        <h2 class="title"><?php esc_html_e( 'Formular-Optionen', 'simple-appointment-booking' ); ?></h2>
        <table class="form-table">
            <tr>
                <th><?php esc_html_e( 'Pflichtfelder', 'simple-appointment-booking' ); ?></th>
                <td>
                    <label><input type="checkbox" name="simpbook_gaeste_pflicht" value="1" <?php checked( $simpbook_opt( 'simpbook_vermietung_gaeste_felder_pflicht', '0' ), '1' ); ?>> <?php esc_html_e( 'Anzahl Gäste ist Pflichtfeld', 'simple-appointment-booking' ); ?></label><br>
                    <label><input type="checkbox" name="simpbook_telefon_pflicht" value="1" <?php checked( $simpbook_opt( 'simpbook_vermietung_telefon_pflicht', '0' ), '1' ); ?>> <?php esc_html_e( 'Telefonnummer ist Pflichtfeld', 'simple-appointment-booking' ); ?></label>
                </td>
            </tr>
            <tr>
                <th><label for="simpbook_agb_link"><?php esc_html_e( 'AGB-Link', 'simple-appointment-booking' ); ?></label></th>
                <td>
                    <input name="simpbook_agb_link" type="url" id="simpbook_agb_link" value="<?php echo esc_attr( $simpbook_opt( 'simpbook_vermietung_agb_link', '' ) ); ?>" class="regular-text" placeholder="https://">
                    <p class="description"><?php esc_html_e( 'Link zu den AGB. Wird als Pflicht-Checkbox im Buchungsformular angezeigt.', 'simple-appointment-booking' ); ?></p>
                </td>
            </tr>
        </table>

        <h2 class="title"><?php esc_html_e( 'Währung', 'simple-appointment-booking' ); ?></h2>
        <table class="form-table">
            <tr>
                <th><label for="simpbook_waehrung"><?php esc_html_e( 'Währung', 'simple-appointment-booking' ); ?></label></th>
                <td>
                    <select name="simpbook_waehrung" id="simpbook_waehrung">
                        <option value="EUR" <?php selected( $simpbook_opt( 'simpbook_vermietung_waehrung', 'EUR' ), 'EUR' ); ?>>EUR (€)</option>
                        <option value="CHF" <?php selected( $simpbook_opt( 'simpbook_vermietung_waehrung', 'EUR' ), 'CHF' ); ?>>CHF (Fr.)</option>
                        <option value="USD" <?php selected( $simpbook_opt( 'simpbook_vermietung_waehrung', 'EUR' ), 'USD' ); ?>>USD ($)</option>
                        <option value="GBP" <?php selected( $simpbook_opt( 'simpbook_vermietung_waehrung', 'EUR' ), 'GBP' ); ?>>GBP (£)</option>
                    </select>
                </td>
            </tr>
        </table>

        <h2 class="title"><?php esc_html_e( 'Frontend-Darstellung', 'simple-appointment-booking' ); ?></h2>
        <table class="form-table">
            <tr>
                <th><label for="simpbook_detail_seite"><?php esc_html_e( 'Detail-Seite', 'simple-appointment-booking' ); ?></label></th>
                <td>
                    <?php
                    wp_dropdown_pages( [
                        'name'             => 'simpbook_detail_seite',
                        'id'               => 'simpbook_detail_seite',
                        'selected'         => (int) $simpbook_opt( 'simpbook_vermietung_detail_seite', 0 ),
                        'show_option_none' => __( '— Automatisch (aktuelle Seite) —', 'simple-appointment-booking' ),
                        'option_none_value' => '0',
                    ] );
                    ?>
                    <p class="description"><?php esc_html_e( 'Seite mit dem [simpbook_unterkunft]-Shortcode für die Einzelansicht. Wenn leer, wird die aktuelle Übersichts-Seite mit Query-Parameter verwendet.', 'simple-appointment-booking' ); ?></p>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e( 'Shortcode-Referenz', 'simple-appointment-booking' ); ?></th>
                <td>
                    <code>[simpbook_unterkuenfte]</code> — <?php esc_html_e( 'Übersicht aller Unterkünfte', 'simple-appointment-booking' ); ?><br>
                    <code>[simpbook_unterkunft id="123"]</code> — <?php esc_html_e( 'Einzelansicht einer Unterkunft', 'simple-appointment-booking' ); ?><br>
                    <code>[simpbook_unterkuenfte tag="villa" limit="6" spalten="3" filter="ja"]</code> — <?php esc_html_e( 'Gefilterte Übersicht', 'simple-appointment-booking' ); ?>
                </td>
            </tr>
        </table>

        <?php
        // Payment-Einstellungen nur anzeigen wenn Payment-Addon-Lizenz aktiv UND Payment aktiviert
        $simpbook_payment_aktiv = get_option( 'simpbook_payment_aktiviert', '0' );
        if ( function_exists( 'simpbook_payment_lizenz_aktiv' ) && simpbook_payment_lizenz_aktiv() && $simpbook_payment_aktiv === '1' ) :
            $simpbook_testmodus       = $simpbook_opt( 'simpbook_payment_testmodus', '1' );
            $simpbook_stripe_aktiv    = $simpbook_opt( 'simpbook_payment_stripe_aktiv', '0' );
            $simpbook_rechnung_aktiv  = $simpbook_opt( 'simpbook_payment_rechnung_aktiv', '0' );
            $simpbook_vorkasse_aktiv  = $simpbook_opt( 'simpbook_payment_vorkasse_aktiv', '0' );
            $simpbook_vorkasse_details = $simpbook_opt( 'simpbook_payment_vorkasse_details', '' );
        ?>
        <h2 class="title" style="border-top: 2px solid #00a32a; padding-top: 20px; margin-top: 30px;">
            <span class="dashicons dashicons-money-alt" style="color: #00a32a; margin-right: 6px;"></span>
            <?php esc_html_e( 'Zahlungseinstellungen', 'simple-appointment-booking' ); ?>
        </h2>
        <table class="form-table">
            <tr>
                <th><?php esc_html_e( 'Testmodus', 'simple-appointment-booking' ); ?></th>
                <td>
                    <label>
                        <input type="checkbox" name="simpbook_payment_testmodus" value="1" <?php checked( $simpbook_testmodus, '1' ); ?>>
                        <?php esc_html_e( 'Testmodus aktiviert (keine echten Zahlungen)', 'simple-appointment-booking' ); ?>
                    </label>
                    <p class="description"><?php esc_html_e( 'Im Testmodus werden die Test-API-Schlüssel verwendet. Deaktivieren Sie den Testmodus erst, wenn Sie die Zahlungen erfolgreich getestet haben.', 'simple-appointment-booking' ); ?></p>
                </td>
            </tr>
        </table>

        <h3><?php esc_html_e( 'Zahlungsmethoden', 'simple-appointment-booking' ); ?></h3>
        <table class="form-table">
            <tr>
                <th><?php esc_html_e( 'Stripe (Kreditkarte)', 'simple-appointment-booking' ); ?></th>
                <td>
                    <label>
                        <input type="checkbox" name="simpbook_stripe_aktiv" value="1" <?php checked( $simpbook_stripe_aktiv, '1' ); ?>>
                        <?php esc_html_e( 'Stripe-Zahlungen aktivieren', 'simple-appointment-booking' ); ?>
                    </label>
                </td>
            </tr>
            <tr>
                <th><label for="simpbook_stripe_test_pk"><?php esc_html_e( 'Stripe Test Publishable Key', 'simple-appointment-booking' ); ?></label></th>
                <td><input name="simpbook_stripe_test_pk" type="text" id="simpbook_stripe_test_pk" value="<?php echo esc_attr( $simpbook_opt( 'simpbook_payment_stripe_test_pk', '' ) ); ?>" class="regular-text" placeholder="pk_test_..."></td>
            </tr>
            <tr>
                <th><label for="simpbook_stripe_test_sk"><?php esc_html_e( 'Stripe Test Secret Key', 'simple-appointment-booking' ); ?></label></th>
                <td><input name="simpbook_stripe_test_sk" type="password" id="simpbook_stripe_test_sk" value="<?php echo esc_attr( $simpbook_opt( 'simpbook_payment_stripe_test_sk', '' ) ); ?>" class="regular-text" placeholder="sk_test_..."></td>
            </tr>
            <tr>
                <th><label for="simpbook_stripe_live_pk"><?php esc_html_e( 'Stripe Live Publishable Key', 'simple-appointment-booking' ); ?></label></th>
                <td><input name="simpbook_stripe_live_pk" type="text" id="simpbook_stripe_live_pk" value="<?php echo esc_attr( $simpbook_opt( 'simpbook_payment_stripe_live_pk', '' ) ); ?>" class="regular-text" placeholder="pk_live_..."></td>
            </tr>
            <tr>
                <th><label for="simpbook_stripe_live_sk"><?php esc_html_e( 'Stripe Live Secret Key', 'simple-appointment-booking' ); ?></label></th>
                <td><input name="simpbook_stripe_live_sk" type="password" id="simpbook_stripe_live_sk" value="<?php echo esc_attr( $simpbook_opt( 'simpbook_payment_stripe_live_sk', '' ) ); ?>" class="regular-text" placeholder="sk_live_..."></td>
            </tr>
        </table>

        <table class="form-table">
            <tr>
                <th><?php esc_html_e( 'Rechnung', 'simple-appointment-booking' ); ?></th>
                <td>
                    <label>
                        <input type="checkbox" name="simpbook_rechnung_aktiv" value="1" <?php checked( $simpbook_rechnung_aktiv, '1' ); ?>>
                        <?php esc_html_e( 'Zahlung per Rechnung aktivieren', 'simple-appointment-booking' ); ?>
                    </label>
                    <p class="description"><?php esc_html_e( 'Die Rechnung wird nach der Buchungsbestätigung per E-Mail versendet.', 'simple-appointment-booking' ); ?></p>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e( 'Vorkasse / Überweisung', 'simple-appointment-booking' ); ?></th>
                <td>
                    <label>
                        <input type="checkbox" name="simpbook_vorkasse_aktiv" value="1" <?php checked( $simpbook_vorkasse_aktiv, '1' ); ?>>
                        <?php esc_html_e( 'Vorkasse/Überweisung aktivieren', 'simple-appointment-booking' ); ?>
                    </label>
                </td>
            </tr>
            <tr>
                <th><label for="simpbook_vorkasse_details"><?php esc_html_e( 'Bankverbindung', 'simple-appointment-booking' ); ?></label></th>
                <td>
                    <textarea name="simpbook_vorkasse_details" id="simpbook_vorkasse_details" rows="4" class="large-text" placeholder="<?php esc_attr_e( "IBAN: DE89 3704 0044 0532 0130 00\nBIC: COBADEFFXXX\nBank: Commerzbank", 'simple-appointment-booking' ); ?>"><?php echo esc_textarea( $simpbook_vorkasse_details ); ?></textarea>
                    <p class="description"><?php esc_html_e( 'Diese Bankverbindung wird dem Gast nach der Buchung angezeigt.', 'simple-appointment-booking' ); ?></p>
                </td>
            </tr>
        </table>
        <?php endif; ?>

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