<?php
/**
 * WP Hotel Booking Extra.
 *
 * @version       1.9.6
 * @author        ThimPress
 * @package       WP_Hotel_Booking/Extra/Classes
 * @category      Classes
 * @author        Thimpress, leehld
 */

/**
 * Prevent loading this file directly
 */
defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'HB_Extra_Field' ) ) {
	/**
	 * Class HB_Extra_Field
	 */
	class HB_Extra_Field {

		/**
		 * @var null
		 */
		protected $_extras_type = null;

		/**
		 * HB_Extra_Field constructor.
		 */
		public function __construct() {
			add_filter( 'hb_metabox_room_settings', array( $this, 'meta_fields' ) );
			add_action( 'hotel_booking_loop_after_item', array( $this, 'render_extra' ), 10, 1 );
			add_action( 'hotel_booking_after_add_room_to_cart_form', array( $this, 'render_extra' ), 10, 1 );

			// single room cart
			add_action( 'hotel_booking_room_before_quantity', array( $this, 'single_room_cart' ) );

			// add package details booking
			add_action( 'hotel_booking_room_details_quantity', array( $this, 'admin_booking_room_details' ), 10, 3 );
		}

		/**
		 * Meta field box render
		 *
		 * @param $fields
		 *
		 * @return array
		 */
		public function meta_fields( $fields ) {
			$fields[] = array(
				'name'    => 'room_extra',
				'label'   => __( 'Addition Package', 'wp-hotel-booking' ),
				'type'    => 'multiple',
				'options' => $this->extra_fields(),
				'filter'  => array( $this, 'meta_value' ),
			);

			return $fields;
		}

		/**
		 * @return array
		 */
		protected function extra_fields() {
			global $hb_extra_settings;
			$options = array();
			$extras  = $hb_extra_settings->get_extra();
			foreach ( $extras as $key => $ex ) {
				if ( $ex->post_status == 'publish' && apply_filters( 'hb_filter_extra_option', true, $ex ) ) {
					$opt        = new stdClass();
					$opt->text  = $ex->post_title;
					$opt->value = $ex->ID;
					$options[]  = $opt;
				}
			}

			return $options;
		}

		/**
		 * @param $val
		 *
		 * @return mixed
		 */
		public function meta_value( $val ) {
			return $val;
		}

		/**
		 * @param $booking_params
		 * @param $search_key
		 * @param $room_id
		 */
		public function admin_booking_room_details( $booking_params, $search_key, $room_id ) {
			if ( ! isset( $booking_params[ $search_key ] ) ||
			     ! isset( $booking_params[ $search_key ][ $room_id ] ) ||
			     ! isset( $booking_params[ $search_key ][ $room_id ]['extra_packages_details'] )
			) {
				return;
			}

			$packages = $booking_params[ $search_key ][ $room_id ]['extra_packages_details'];
			?>
			<ul>
				<?php foreach ( $packages as $id => $package ): ?>
					<li>
						<small><?php printf( '%s (x%s)', $package['package_title'], $package['package_quantity'] ) ?></small>
					</li>
				<?php endforeach ?>
			</ul>
			<?php
		}

		/**
		 * @param $post_id
		 */
		public function render_extra( $post_id ) {
			tp_hb_extra_get_template( 'loop/extra-search-room.php', array( 'post_id' => $post_id ) );
		}

		/**
		 * Extra single search room
		 *
		 * @param $post
		 */
		public function single_room_cart( $post ) {
			ob_start();
			tp_hb_extra_get_template( 'loop/extra-single-search-room.php', array( 'post' => $post ) );
			echo ob_get_clean();
		}
	}
}

new HB_Extra_Field();
