<?php
/**
 * Registers a meta box on the rtcl_listing edit screen for booking settings.
 *
 * @package RadiusTheme\RadiusBooking\Integrations\ClassifiedListing
 */

namespace RadiusTheme\RadiusBooking\Integrations\ClassifiedListing;

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

use RadiusTheme\RadiusBooking\Models\Category;

/**
 * Class ListingMetaBox
 *
 * Adds "Enable Booking" meta box to the Classified Listing edit screen.
 */
class ListingMetaBox {

	/**
	 * Register the meta box.
	 */
	public function register() {
//		add_meta_box(
//			'rtrb_booking_settings',
//			__( 'Radius Booking', 'radius-booking' ),
//			array( $this, 'render' ),
//			'rtcl_listing',
//			'side',
//			'default'
//		);
	}

	/**
	 * Render the meta box HTML.
	 *
	 * @param \WP_Post $post The current post object.
	 */
	public function render( $post ) {
		wp_nonce_field( 'rtrb_listing_booking_nonce', 'rtrb_listing_booking_nonce_field' );

		$enabled     = (bool) get_post_meta( $post->ID, '_rtrb_booking_enabled', true );
		$duration    = (int) get_post_meta( $post->ID, '_rtrb_booking_duration', true );
		$category_id = (int) get_post_meta( $post->ID, '_rtrb_booking_category_id', true );

		// Get booking categories.
		$categories = Category::all();
		?>
		<p>
			<label>
				<input type="checkbox" name="_rtrb_booking_enabled" value="1" <?php checked( $enabled ); ?> />
				<?php esc_html_e( 'Enable Booking', 'radius-booking' ); ?>
			</label>
		</p>
		<p>
			<label for="rtrb_booking_duration"><?php esc_html_e( 'Duration (minutes)', 'radius-booking' ); ?></label>
			<input type="number" id="rtrb_booking_duration" name="_rtrb_booking_duration"
				value="<?php echo esc_attr( $duration ?: 60 ); ?>" min="5" step="5"
				style="width: 100%;" />
		</p>
		<p>
			<label for="rtrb_booking_category"><?php esc_html_e( 'Booking Category', 'radius-booking' ); ?></label>
			<select id="rtrb_booking_category" name="_rtrb_booking_category_id" style="width: 100%;">
				<option value="0"><?php esc_html_e( '-- None --', 'radius-booking' ); ?></option>
				<?php if ( $categories ) : ?>
					<?php foreach ( $categories as $cat ) : ?>
						<option value="<?php echo esc_attr( $cat->id ); ?>" <?php selected( $category_id, $cat->id ); ?>>
							<?php echo esc_html( $cat->name ); ?>
						</option>
					<?php endforeach; ?>
				<?php endif; ?>
			</select>
		</p>
		<?php
	}

	/**
	 * Save meta box data.
	 *
	 * @param int      $post_id The post ID.
	 * @param \WP_Post $post    The post object.
	 */
	public function save( $post_id, $post ) {
		// Verify nonce.
		if ( ! isset( $_POST['rtrb_listing_booking_nonce_field'] )
			|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['rtrb_listing_booking_nonce_field'] ) ), 'rtrb_listing_booking_nonce' )
		) {
			return;
		}

		// Check autosave.
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}

		// Check permissions.
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}

		// Save meta.
		$enabled = ! empty( $_POST['_rtrb_booking_enabled'] ) ? 1 : 0;
		update_post_meta( $post_id, '_rtrb_booking_enabled', $enabled );

		$duration = isset( $_POST['_rtrb_booking_duration'] ) ? absint( $_POST['_rtrb_booking_duration'] ) : 60;
		update_post_meta( $post_id, '_rtrb_booking_duration', $duration );

		$category_id = isset( $_POST['_rtrb_booking_category_id'] ) ? absint( $_POST['_rtrb_booking_category_id'] ) : 0;
		update_post_meta( $post_id, '_rtrb_booking_category_id', $category_id );
	}
}
