<?php
/**
 * Availability Page.
 *
 * Page to manage availability and rates.
 *
 * @package HBFWC\Admin
 * @version 1.0.0
 */

namespace HBFWC\Admin;

defined( 'ABSPATH' ) || exit;

/**
 * Class HBFWC\Admin\Admin_Availability.
 */
class Admin_Availability {
	/**
	 * Var to hold the error message.
	 *
	 * @var string
	 */
	private static $error_message = '';

	/**
	 * Var to hold the success message.
	 *
	 * @var string
	 */
	private static $success_message = '';

	/**
	 * Handles output of the page in admin.
	 */
	public static function output() {
		// Show admin interface.
		if ( ! empty( $_GET['accommodation_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			self::edit_availability();
		} else {
			self::availability_dashboard();
		}
	}

	/**
	 * Edit screen.
	 */
	private static function edit_availability() {
		if ( isset( $_POST['bulk_unit_id'] ) ) {
			$edit_id = absint( wp_unslash( $_POST['bulk_unit_id'] ) );
			check_admin_referer( 'hbfwc-bulk-update_' . $edit_id );

			self::process_bulk_update( $edit_id );
		}

		$product_id = ! empty( $_GET['accommodation_id'] ) ? absint( wp_unslash( $_GET['accommodation_id'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$product    = wc_get_product( $product_id );

		$from = isset( $_GET['from'] ) ? wp_date( 'm/d/Y', absint( $_GET['from'] ) ) : wp_date( 'm/d/Y' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$end  = isset( $_GET['end'] ) ? wp_date( 'm/d/Y', absint( $_GET['end'] ) ) : wp_date( 'm/d/Y', strtotime( '+ 7 days' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended

		$wp_tz      = wp_timezone();
		$start_date = \DateTime::createFromFormat( 'm/d/Y', $from, $wp_tz ) ?: new \DateTime( 'now', $wp_tz );
		$end_date   = \DateTime::createFromFormat( 'm/d/Y', $end,  $wp_tz ) ?: new \DateTime( '+7 days', $wp_tz );

		if ( ! $product ) {
			wp_safe_redirect( admin_url( 'edit.php?post_type=product&page=availability' ) );
			exit;
		}

		$default_rate = hbfwc_get_default_rate();
		$crates       = get_post_meta( $product->get_id(), '_hbfwc_rate_plans', true );
		?>
		<div class="wrap woocommerce">
			<?php
			if ( '' !== self::$error_message ) {
				echo '<div id="message" class="error"><p>' . esc_html( self::$error_message ) . '</p></div>';
				self::$error_message = '';
			}

			if ( '' !== self::$success_message ) {
				echo '<div id="message" class="updated"><p>' . esc_html( self::$success_message ) . '</p></div>';
				self::$success_message = '';
			}
			?>
			<section id="accommodation_base_data">
				<div class="accommodation_base_data__name">
					<?php print esc_html( $product->get_formatted_name() ); ?>
					<button class="button sync-calendar-btn" id="sync-calendar-btn"><?php print esc_html__( 'Sync Calendar', 'hotel-booking-for-woocommerce' ); ?></button>
				</div>

				<div class="accommodation_base_data__allotment">
					<span class="accommodation_base_data__label"><?php print esc_html__( 'Allotment', 'hotel-booking-for-woocommerce' ); ?></span>
					<?php print esc_html( $product->get_allotment() ); ?>
				</div>
				<div class="accommodation_base_data__price">
					<span class="accommodation_base_data__label"><?php print esc_html__( 'Default Price', 'hotel-booking-for-woocommerce' ); ?></span>
					<?php print $product->get_wc_price(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				</div>
				<div class="accommodation_base_data__bulk-edit">
					<a href="#" id="call-bulk-form" class="button button-primary button-hero"><?php print esc_html__( 'Bulk Update', 'hotel-booking-for-woocommerce' ); ?></a>
				</div>
			</section>

			<div id="accommodation_avail_form" class="accommodation_avail_form" data-product-id="<?php print esc_attr( $product_id ); ?>">
				<div class="avail_form__container">
					<div id="avail_form__sidebar">
						<div class="avail_form__sidebar_header">
							<div id="avail_dates">
								<span id="avail_dates__output" data-mindate="<?php print esc_attr( $start_date->format( 'm/d/Y' ) ); ?>" data-output-format="d M, Y"><?php printf( '%s &ndash; %s', esc_html( $start_date->format( 'd M, Y' ) ), esc_html( $end_date->format( 'd M, Y' ) ) ); ?></span>
								<input type="hidden" name="from" value="<?php print esc_attr( $from ); ?>" />
								<input type="hidden" name="end" value="<?php print esc_attr( $end ); ?>" />
							</div>
						</div>
						<div class="avail_form__sidebar_items">
							<ul>
								<li>
									<?php print esc_html__( 'Availability', 'hotel-booking-for-woocommerce' ); ?>
									<?php print wc_help_tip( esc_html__( 'Available rooms/units', 'hotel-booking-for-woocommerce' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></li>
								<li class="avail_form_global_status"><?php print esc_html__( 'Room/unit status', 'hotel-booking-for-woocommerce' ); ?></li>
							<?php
							if ( $default_rate ) {
								print '<li>' . esc_html( $default_rate->name ) . '</li>' . "\n";
								print '<li class="avail_form_global_status">&#8627; ' . esc_html__( 'Rate status', 'hotel-booking-for-woocommerce' ) . '</li>' . "\n";
							}

							if ( ! empty( $crates ) ) {
								foreach ( $crates as $rate_db_id => $arr_key ) {
									$rate_db_id = str_replace( 'rate_', '', $rate_db_id );
									$rate_term  = get_term_by( 'term_id', absint( $rate_db_id ), 'rate_plan' );

									if ( ! $rate_term ) {
										continue;
									}

									print '<li>' . esc_html( $rate_term->name ) . '</li>' . "\n";
									print '<li class="avail_form_global_status">&#8627; ' . esc_html__( 'Rate status', 'hotel-booking-for-woocommerce' ) . '</li>' . "\n";
								}
							}
							?>
							</ul>
						</div>
						<div class="avail_form__sidebar_footer">
							<a class="button-primary add_more_rules" href="https://uptonines.com/plugins/hotel-booking-for-woocommerce/#pro" target="_blank" rel="noopener noreferrer">Get PRO Version</a>
						</div>
					</div>

					<div id="avail_form__content">
						<div class="avail_form__default_vals">
							<div class="avail_form__th">
								<?php
								printf(
									/* translators: Currency symbol */
									esc_html__( 'Default Price (%s)', 'hotel-booking-for-woocommerce' ),
									wp_kses_post( get_woocommerce_currency_symbol() )
								);
								?>
							</div>
							<div class="avail_form__items">
								<ul>
									<li>&nbsp;</li>
									<li></li>
							<?php
							if ( $default_rate ) {
								print '<li>' . esc_html( wc_format_decimal( $product->get_rate_default_price( 0, true, false ), wc_get_price_decimals() ) ) . '</li>' . "\n";
								print '<li></li>' . "\n";
							}

							if ( ! empty( $crates ) ) {
								foreach ( $crates as $rate_db_id => $arr_key ) {
									$rate_db_id = str_replace( 'rate_', '', $rate_db_id );
									$rate_term  = get_term_by( 'term_id', absint( $rate_db_id ), 'rate_plan' );

									if ( ! $rate_term ) {
										continue;
									}

									print '<li>' . esc_html( wc_format_decimal( $product->get_rate_default_price( $rate_db_id, false, false ), wc_get_price_decimals() ) ) . '</li>' . "\n";
									print '<li></li>' . "\n";
								}
							}
							?>
								</ul>
							</div>
						</div>
						<div class="avail_form__calendar">
							<div id="avail_form__calendar_wrap">
						<?php
							$room_datas = hbfwc_get_availability( $product->get_id(), $start_date->format( 'Y-m-d' ), $end_date->format( 'Y-m-d' ), true );

						if ( ! empty( $room_datas ) ) {
							foreach ( $room_datas as $room_data ) {
								$unit_status_class = 1 > absint( $room_data['status'] ) ? 'unit_not_active' : 'unit_is_active';
								?>
								<div class="avail_form__calendar_date">
									<div class="avail_form__th"><?php print esc_html( wp_date( 'd M y', strtotime( $room_data['booking_date'] ) ) ); ?></div>
									<div class="avail_form__items">
										<ul>
											<li><input type="number" class="hbfwc-room-quantity__field" name="qyt[<?php print esc_attr( strtotime( $room_data['booking_date'] ) ); ?>]" value="<?php print esc_attr( $room_data['qty'] ); ?>" min="0" step="1" max="<?php print esc_attr( $product->get_allotment() ); ?>" data-max="<?php print esc_attr( $product->get_allotment() ); ?>" data-date="<?php print esc_attr( strtotime( $room_data['booking_date'] ) ); ?>"/></li>
											<li class="<?php print esc_attr( $unit_status_class ); ?>">
												<input type="checkbox" class="hbfwc-room-status__field" role="switch" name="status[<?php print esc_attr( strtotime( $room_data['booking_date'] ) ); ?>]" value="1" data-max="<?php print esc_attr( $product->get_allotment() ); ?>" data-date="<?php print esc_attr( strtotime( $room_data['booking_date'] ) ); ?>" <?php checked( $room_data['status'], '1', true ); ?> />
											</li>
										<?php
										if ( $default_rate ) {
											$drate_status_class = $product->get_rate_status( $default_rate->term_id, $room_data['booking_date'], true ) ? 'unit_is_active' : 'unit_not_active';
											print '<li><input type="text" class="hbfwc-rate-price__field" name="price[rate_' . esc_attr( $default_rate->term_id ) . '][' . esc_attr( strtotime( $room_data['booking_date'] ) ) . ']" value="' . esc_attr( wc_format_decimal( $product->get_rate_price( 0, $room_data['booking_date'], true ), wc_get_price_decimals() ) ) . '" data-date="' . esc_attr( strtotime( $room_data['booking_date'] ) ) . '" data-rate="' . esc_attr( $default_rate->term_id ) . '" /></li>' . "\n";
											print '<li class="' . esc_attr( $drate_status_class ) . '"><input type="checkbox" role="switch" class="hbfwc-rate-status__field" name="status[rate_' . esc_attr( $default_rate->term_id ) . '][' . esc_attr( strtotime( $room_data['booking_date'] ) ) . ']" value="1" data-date="' . esc_attr( strtotime( $room_data['booking_date'] ) ) . '" data-rate="' . esc_attr( $default_rate->term_id ) . '" ' . checked( $product->get_rate_status( $default_rate->term_id, $room_data['booking_date'], true ), true, false ) . '/></li>' . "\n";
										}

										if ( ! empty( $crates ) ) {
											foreach ( $crates as $rate_db_id => $arr_key ) {
												$rate_db_id = str_replace( 'rate_', '', $rate_db_id );
												$rate_term  = get_term_by( 'term_id', absint( $rate_db_id ), 'rate_plan' );

												if ( ! $rate_term ) {
													continue;
												}

												$crate_status_class = $product->get_rate_status( $rate_term->term_id, $room_data['booking_date'] ) ? 'unit_is_active' : 'unit_not_active';

												print '<li class="pro_only"><input type="text" class="hbfwc-rate-price__field" name="price[rate_' . esc_attr( $rate_term->term_id ) . '][' . esc_attr( strtotime( $room_data['booking_date'] ) ) . ']" value="' . esc_attr( wc_format_decimal( $product->get_rate_price( $rate_term->term_id, $room_data['booking_date'] ), wc_get_price_decimals() ) ) . '" data-date="' . esc_attr( strtotime( $room_data['booking_date'] ) ) . '" data-rate="' . esc_attr( $rate_term->term_id ) . '" title="' . esc_attr__( 'PRO Only.', 'hotel-booking-for-woocommerce' ) . '"/></li>' . "\n";
												print '<li class="pro_only ' . esc_attr( $crate_status_class ) . '"><input type="checkbox" role="switch" class="hbfwc-rate-status__field" name="status[rate_' . esc_attr( $rate_term->term_id ) . '][' . esc_attr( strtotime( $room_data['booking_date'] ) ) . ']" value="1" data-date="' . esc_attr( strtotime( $room_data['booking_date'] ) ) . '" data-rate="' . esc_attr( $rate_term->term_id ) . '" ' . checked( $product->get_rate_status( $rate_term->term_id, $room_data['booking_date'] ), true, false ) . '/></li>' . "\n";
											}
										}
										?>
										</ul>
									</div>
								</div>
								<?php
							}
						}
						?>
							</div>
						</div>
					</div>
				</div>
			</div>

			<div id="hbfwc-bulk__form">
				<form id="hbfwc-bulk__form-the-form" action="edit.php?post_type=product&amp;page=availability&amp;accommodation_id=<?php echo absint( $product_id ); ?>" method="post">
					<input type="hidden" name="bulk_unit_id" value="<?php print esc_attr( $product->get_id() ); ?>" />
					<div class="hbfwc-bulk__form-wrap accommodation_avail_form">
						<h3><?php print esc_html__( 'Bulk update', 'hotel-booking-for-woocommerce' ); ?></h3>
						<div class="hbfwc-bulk__form-section bulk__column">
							<div class="hbfwc-bulk__form-field-wrap">
								<span class="hbfwc-bulk__form-label"><?php print esc_html__( 'Room/unit', 'hotel-booking-for-woocommerce' ); ?></span>
								<div class="hbfwc-bulk__form-field room__name">
									<?php print esc_html( $product->get_formatted_name() ); ?>
								</div>
							</div>

							<div class="hbfwc-bulk__form-field-wrap">
								<label for="bulk-rates"><?php print esc_html__( 'Rate plans', 'hotel-booking-for-woocommerce' ); ?></label>
								<div class="hbfwc-bulk__form-field">
									<select name="bulk_rates[]" id="bulk-rates" class="wc-enhanced-select" multiple required data-placeholder="<?php esc_attr_e( 'Select the rate plans', 'hotel-booking-for-woocommerce' ); ?>">
									<?php
									if ( $default_rate ) {
										print '<option value="' . esc_attr( $default_rate->term_id ) . '">' . esc_html( $default_rate->name ) . '</option>' . "\n";
									}

									if ( ! empty( $crates ) ) {
										foreach ( $crates as $rate_db_id => $arr_key ) {
											$rate_db_id = str_replace( 'rate_', '', $rate_db_id );
											$rate_term  = get_term_by( 'term_id', absint( $rate_db_id ), 'rate_plan' );

											if ( ! $rate_term ) {
												continue;
											}

											print '<option value="' . esc_attr( $rate_term->term_id ) . '" disabled>' . esc_html( $rate_term->name ) . ' ' . esc_html__( '(PRO Only)', 'hotel-booking-for-woocommerce' ) . '</option>' . "\n";
										}
									}
									?>
									</select>
								</div>
							</div>
						</div>

						<div class="hbfwc-bulk__form-section">
							<span class="hbfwc-bulk__form-label"><?php print esc_html__( 'Date range selection', 'hotel-booking-for-woocommerce' ); ?></span>
							<div id="hbfwc-bulk__form-dates-wrapper">	
								<div class="hbfwc-bulk__form-dates" data-mindate="<?php print esc_attr( $start_date->format( 'm/d/Y' ) ); ?>" data-output-format="d M, Y">
									<span class="hbfwc-bulk__form-date-output"><?php printf( '%s &#8674; %s', esc_html( wp_date( 'd M, Y' ) ), esc_html( wp_date( 'd M, Y', strtotime( '+ 7 days' ) ) ) ); ?></span>
									<a class="hbfwc-rmv-bulk-date" href="#">&times;</a>
									<input type="hidden" name="bulk_dates[]" value="<?php echo esc_attr( wp_date( 'm/d/Y' ) . '-' . wp_date( 'm/d/Y', strtotime( '+ 7 days' ) ) ); ?>" />
								</div>
							</div>

							<a href="#" class="button button-small" id="hbfwc-bulk-add-date"><?php print esc_html__( '+ Add date range', 'hotel-booking-for-woocommerce' ); ?></a>
						</div>

						<div class="hbfwc-bulk__form-section">
							<span class="hbfwc-bulk__form-label"><?php print esc_html__( 'Week days selection', 'hotel-booking-for-woocommerce' ); ?></span>
							<div class="hbfwc-bulk__form-weekdays-wrapper">
								<?php
									$days = array(
										__( 'Sunday', 'hotel-booking-for-woocommerce' ),
										__( 'Monday', 'hotel-booking-for-woocommerce' ),
										__( 'Tuesday', 'hotel-booking-for-woocommerce' ),
										__( 'Wednesday', 'hotel-booking-for-woocommerce' ),
										__( 'Thursday', 'hotel-booking-for-woocommerce' ),
										__( 'Friday', 'hotel-booking-for-woocommerce' ),
										__( 'Saturday', 'hotel-booking-for-woocommerce' ),
									);

									foreach ( $days as $day_code => $day ) {
										echo '<label><input type="checkbox" name="bulk_weekdays[]" value="' . esc_attr( $day_code ) . '" checked/> ' . esc_html( $day ) . '</label>';
									}
									?>
							</div>
						</div>

						<div class="hbfwc-bulk__form-section">
							<span class="hbfwc-bulk__form-label"><?php print esc_html__( 'Action selection', 'hotel-booking-for-woocommerce' ); ?></span>

							<div id="hbfwc-bulk__form-actions-wrapper"></div>

							<a href="#" class="button button-small" id="hbfwc-bulk-add-action"><?php print esc_html__( '+ Add action', 'hotel-booking-for-woocommerce' ); ?></a>
						</div>

						<div class="hbfwc-bulk__form-buttons">
							<a class="button button-cancel button-large hbfwc-button-cancel-bulk-update" href="#"><?php esc_html_e( '&larr; Cancel', 'hotel-booking-for-woocommerce' ); ?></a>
							<button type="submit" class="button button-primary button-large"><?php esc_html_e( 'Update', 'hotel-booking-for-woocommerce' ); ?></button>
						</div>
					</div>
					<?php wp_nonce_field( 'hbfwc-bulk-update_' . $product->get_id() ); ?>
				</form>
				<div id="hbfwc-bulk__form-backdrop">&nbsp;</div>
			</div>

			<div id="hbfwc-sync__form">
				<div class="hbfwc-sync__wrapper">
					<div class="hbfwc-sync__form-wrap accommodation_avail_form">
						<a href="https://uptonines.com/plugins/hotel-booking-for-woocommerce/#pro" target="_blank" rel="noopener noreferrer" class="banner-on-ical">
							<h3>Avoid double bookings by syncing calendars across OTAs. This feature is available with the "Hotel Booking for WooCommerce – PRO" add-on.</h3>
							<span>Get the PRO Add-on</span>
						</a>
					</div>
				</div>
				<div id="hbfwc-sync__form-backdrop">&nbsp;</div>
			</div>
		</div>

		<script type="text/template" id="hbfwc-jstm__action-selection-html">
			<div class="hbfwc-bulk__form-action">
				<a class="hbfwc-rmv-bulk-action" href="#">&times;</a>
				<select class="action-set_select" name="action_set[{{%loop%}}]">
					<option value="" disabled selected><?php esc_html_e( 'Select an action', 'hotel-booking-for-woocommerce' ); ?></option>
					<optgroup label="<?php esc_attr_e( 'Rate action', 'hotel-booking-for-woocommerce' ); ?>">
						<option value="price"><?php esc_html_e( 'Price', 'hotel-booking-for-woocommerce' ); ?></option>
						<option value="rate_status"><?php esc_html_e( 'Rate status', 'hotel-booking-for-woocommerce' ); ?></option>
					</optgroup>
					<optgroup label="<?php esc_attr_e( 'Unit action', 'hotel-booking-for-woocommerce' ); ?>">
						<option value="availability"><?php esc_html_e( 'Availability', 'hotel-booking-for-woocommerce' ); ?></option>
						<option value="status"><?php esc_html_e( 'Open/close unit', 'hotel-booking-for-woocommerce' ); ?></option>
					</optgroup>
				</select>
				<span class="action-set__next_availability">
					<select name="action_next_{{%loop%}}_availability">
						<option value="to" selected><?php esc_html_e( 'Set to', 'hotel-booking-for-woocommerce' ); ?></option>
						<option value="plus"><?php esc_html_e( 'Increase by', 'hotel-booking-for-woocommerce' ); ?></option>
						<option value="minus"><?php esc_html_e( 'Decrease by', 'hotel-booking-for-woocommerce' ); ?></option>
					</select>
					<input type="number" name="action_value_{{%loop%}}_availability" value="" placeholder="<?php esc_attr_e( 'Enter the value', 'hotel-booking-for-woocommerce' ); ?>">
				</span>
				<span class="action-set__next_price">
					<select name="action_next_{{%loop%}}_price">
						<option value="to" selected><?php esc_html_e( 'Set to', 'hotel-booking-for-woocommerce' ); ?></option>
						<option value="plus"><?php esc_html_e( 'Increase by', 'hotel-booking-for-woocommerce' ); ?></option>
						<option value="minus"><?php esc_html_e( 'Decrease by', 'hotel-booking-for-woocommerce' ); ?></option>
					</select>
					<input type="text" class="bulk__price" name="action_value_{{%loop%}}_price" value="" placeholder="<?php esc_attr_e( 'Enter the value', 'hotel-booking-for-woocommerce' ); ?>">
				</span>
				<span class="action-set__next_status">
					<input type="hidden" name="action_next_{{%loop%}}_status" value="to">
					<label><input type="radio" name="action_value_{{%loop%}}_status" value="1" checked/> <?php esc_html_e( 'Open unit/room', 'hotel-booking-for-woocommerce' ); ?></label>
					<label><input type="radio" name="action_value_{{%loop%}}_status" value="0" /> <?php esc_html_e( 'Close unit/room', 'hotel-booking-for-woocommerce' ); ?></label>
				</span>
				<span class="action-set__next_rate_status">
					<input type="hidden" name="action_next_{{%loop%}}_rate_status" value="to">
					<label><input type="radio" name="action_value_{{%loop%}}_rate_status" value="1" checked/> <?php esc_html_e( 'Activate rate', 'hotel-booking-for-woocommerce' ); ?></label>
					<label><input type="radio" name="action_value_{{%loop%}}_rate_status" value="0" /> <?php esc_html_e( 'Deactivate rate', 'hotel-booking-for-woocommerce' ); ?></label>
				</span>
			</div>
		</script>
		<?php
	}

	/**
	 * Processing the bulk update.
	 *
	 * @param int $product_id Current product id.
	 *
	 * @return mixed.
	 */
	private static function process_bulk_update( $product_id ) {
		// phpcs:disable WordPress.Security.NonceVerification.Missing
		$rates    = isset( $_POST['bulk_rates'] ) ? array_map( 'absint', wp_unslash( $_POST['bulk_rates'] ) ) : array();
		$dates    = isset( $_POST['bulk_dates'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['bulk_dates'] ) ) : array();
		$weekdays = isset( $_POST['bulk_weekdays'] ) ? array_map( 'absint', wp_unslash( $_POST['bulk_weekdays'] ) ) : array();
		$actions  = isset( $_POST['action_set'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['action_set'] ) ) : array();

		if ( empty( $rates ) || empty( $dates ) || empty( $weekdays ) || empty( $actions ) ) {
			self::$error_message = __( 'Cannot process the bulk updates. Please complete all fields', 'hotel-booking-for-woocommerce' );
		}

		if ( ! empty( $actions ) ) {
			try {
				foreach ( $dates as $date_set ) {
					$date_set = explode( '-', $date_set );
					if ( count( $date_set ) < 2 ) {
						continue;
					}

					// Set the date.
					$from = \DateTime::createFromFormat( 'm/d/Y', $date_set[0], wp_timezone() );
					$end  = \DateTime::createFromFormat( 'm/d/Y', $date_set[1], wp_timezone() );

					if ( ! $from || ! $end ) {
						continue;
					}

					// Read the actions.
					foreach ( $actions as $act_id => $act_value ) {
						// Get the action set.
						$action_next = isset( $_POST[ "action_next_{$act_id}_{$act_value}" ] ) ? sanitize_text_field( wp_unslash( $_POST[ "action_next_{$act_id}_{$act_value}" ] ) ) : '';
						$action_val  = isset( $_POST[ "action_value_{$act_id}_{$act_value}" ] ) ? sanitize_text_field( wp_unslash( $_POST[ "action_value_{$act_id}_{$act_value}" ] ) ) : '';

						// Dont bother if empty.
						if ( '' === $action_next || '' === $action_val ) {
							continue;
						}

						$args = array(
							'set'      => $action_next,
							'value'    => $action_val,
							'from'     => $from->format( 'Y-m-d' ),
							'end'      => $end->format( 'Y-m-d' ),
							'weekdays' => $weekdays,
						);

						// Get the global actions.
						if ( in_array( $act_value, array( 'availability', 'status' ), true ) ) {
							do_action( 'hbfwc_bulk_global_action', $act_value, $product_id, $args );
						} else {
							foreach ( $rates as $rate_id ) {
								do_action( 'hbfwc_bulk_rate_action', $act_value, $product_id, $rate_id, $args );
							}
						}
					}
				}

				self::$success_message = __( 'Bulk update is successfully performed.', 'hotel-booking-for-woocommerce' );

			} catch ( \Exception $e ) {
				self::$error_message = $e->getMessage();
			}
		}
		// phpcs:enable
	}

	/**
	 * The main screen.
	 */
	private static function availability_dashboard() {
		$args     = array(
			'type'  => 'accommodation',
			'limit' => -1,
		);
		$products = wc_get_products( $args );
		?>
		<div class="wrap woocommerce">
			<div class="hbfwc-admin-starter-box">
				<div class="hbfwc-admin-starter-box__header">
					<h3><?php print esc_html__( 'Select an accommodation to manage availability and prices.', 'hotel-booking-for-woocommerce' ); ?></h3>
				</div>
				<div class="hbfwc-admin-starter-box__content">
					<?php if ( $products ) : ?>
						<div class="hbfwc-availability-room-list">
							<div class="hbfwc-availability-room-list__item hbfwc-admin__heading">
								<div class="hbfwc-availability-room-list__name"><?php echo __( 'Name', 'hotel-booking-for-woocommerce' ); ?></div>
								<div class="hbfwc-availability-room-list__status"><?php echo __( 'Allotment', 'hotel-booking-for-woocommerce' ); ?></div>
								<div class="hbfwc-availability-room-list__action_head"><a class="button button-primary" style="visibility:hidden;" href="#"><?php print esc_html__( 'Manage &raquo;', 'hotel-booking-for-woocommerce' ); ?></a></div>
							</div>
							<?php foreach ( $products as $product ) : ?>
								<div class="hbfwc-availability-room-list__item">
									<div class="hbfwc-availability-room-list__name">
										<strong><?php print esc_html( $product->get_name() ); ?></strong>
										<span><?php printf( /* translators: Product ID */ esc_html__( 'ID: #%d', 'hotel-booking-for-woocommerce' ), absint( $product->get_id() ) ); ?> &mdash; <?php echo esc_html( $product->get_status() ); ?></span>
									</div>
									<div class="hbfwc-availability-room-list__status"><?php echo esc_html( $product->get_allotment() ); ?></div>
									<a class="button button-secondary" href="<?php echo esc_url( admin_url( 'edit.php?post_type=product&page=availability&accommodation_id=' . absint( $product->get_id() ) ) ); ?>"><?php print esc_html__( 'Manage &raquo;', 'hotel-booking-for-woocommerce' ); ?></a>
								</div>
							<?php endforeach; ?>
						</div>
					<?php else : ?>
						<p><?php print esc_html__( 'No accommodation found.', 'hotel-booking-for-woocommerce' ); ?></p>
					<?php endif; ?>
				</div>
			</div>
		</div>
		<?php
	}
}
