<?php
/**
 * Rate plans tab
 *
 * @package HBFWC
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$reg_price    = $product->get_regular_price();
$sale_price   = $product->get_sale_price();
$plans        = hbfwc_get_rateplans();
$default_rate = hbfwc_get_default_rate();
$crates       = get_post_meta( $product->get_id(), '_hbfwc_rate_plans', true );
$min_stay     = hbfwc_admin_get_minimum_stay( $product );

$rateMinStayInfo = '';

if ( ! is_array( $crates ) ) {
	$crates = array();
}
?>

<div id="hbfwc_rateplans_product_data" class="panel woocommerce_options_panel">
	<div class="hbfwc-toolbar">
		<span class="hbfwc-lead"><?php print esc_html__( 'Rate Plans', 'hotel-booking-for-woocommerce' ); ?></span>
		<div class="hbfwc-rp-selections">
			<select id="hbfwc-rp-select">
				<option value="0" disabled selected><?php print esc_html__( 'Select a rate plan', 'hotel-booking-for-woocommerce' ); ?></option>
				<optgroup label="<?php print esc_attr__( 'Available rate plans', 'hotel-booking-for-woocommerce' ); ?>">
				<?php
				foreach ( $plans as $plan ) {
					if ( $default_rate && $plan->term_id === $default_rate->term_id ) {
						continue;
					}
					$disabled_opt = array_key_exists( "rate_{$plan->term_id}", $crates ) ? 'disabled="disabled"' : '';
					print '<option value="' . esc_attr( $plan->term_id ) . '" ' . esc_html( $disabled_opt ) . '>' . esc_html( $plan->name ) . '</option>' . "\n";
				}
				?>
				</optgroup>
			</select>
			<a href="#" id="hbfwc-assign-rp" class="button"><?php print esc_html__( 'Insert a rate plan', 'hotel-booking-for-woocommerce' ); ?></a>
		</div>
	</div>
	<?php do_action( 'hbfwc_before_rate_table', $product ); ?>
	<div id="hbfwc-rateplan-list">
		<table id="hbfwc-rateplan-table" class="widefat wp-list-table ui-sortable" style="width:100%">
			<thead>
				<tr>
					<th scope="col" width="30px"></th>
					<th scope="col"><?php print esc_html__( 'Name', 'hotel-booking-for-woocommerce' ); ?></th>
					<th scope="col"><?php print esc_html__( 'Regular Price', 'hotel-booking-for-woocommerce' ); ?></th>
					<th scope="col"><?php print esc_html__( 'Sale Price', 'hotel-booking-for-woocommerce' ); ?></th>				<?php do_action( 'hbfwc_rate_table_header_extra_columns' ); ?>					<th scope="col">
						<?php print esc_html__( 'Minimum stay', 'hotel-booking-for-woocommerce' ); ?>
						<?php echo wc_help_tip( esc_html__( 'The minimum stay duration.', 'hotel-booking-for-woocommerce' ) ); ?>
					</th>
					<th scope="col" width="130px">
						<?php print esc_html__( 'Default Status', 'hotel-booking-for-woocommerce' ); ?>
						<?php echo wc_help_tip( esc_html__( 'Set the default rate status. You can set the rate status per date in "Availability & Prices" menu.', 'hotel-booking-for-woocommerce' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					</th>
				</tr>
				<?php if ( $default_rate ) : ?>
				<tr>
					<th>
						<?php echo wc_help_tip( esc_html__( 'This is the default rate plan and it cannot be deleted.', 'hotel-booking-for-woocommerce' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					</th>
					<th><strong><?php print esc_html( $default_rate->name ); ?></strong></th>
					<th>
						<input type="text" name="_hbfwc_regular_price" id="hbfwc_regular_price" class="short wc_input_price" value="<?php print esc_attr( wc_format_decimal( $reg_price, wc_get_price_decimals() ) ); ?>" />
						<span class="cursym"><?php print wp_kses_post( get_woocommerce_currency_symbol() ); ?></span>
					</th>
					<th>
						<input type="text" name="_hbfwc_sale_price" id="hbfwc_sale_price" class="short wc_input_price" value="<?php print esc_attr( wc_format_decimal( $sale_price, wc_get_price_decimals() ) ); ?>" />
						<span class="cursym"><?php print wp_kses_post( get_woocommerce_currency_symbol() ); ?></span>
					</th>
					<?php do_action( 'hbfwc_rate_table_default_row_extra_columns' ); ?>
					<th>
						<input type="number" name="_min_stay" id="hbfwc_min_stay" class="short" value="<?php print esc_attr( $min_stay ); ?>" />
					</th>
					<th><input type="checkbox" class="cb_rate_status" role="switch" name="stdpr" value="yes" checked disabled></th>
				</tr>
				<?php endif; ?>
			</thead>
			<tbody id="hbfwc-tbody-rates">
				<?php
				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;
						}

						$rate_status     = isset( $arr_key['status'] ) ? $arr_key['status'] : '';
						$unit_style      = 'fixed' === $arr_key['pricing_method'] ? 'display:none' : '';
						$unit_sale_style = 'fixed' === $arr_key['pricing_sale_method'] ? 'display:none' : '';

						$reg_price_val  = wc_format_decimal( $arr_key['pricing_value'], wc_get_price_decimals() );
						$sale_price_val = wc_format_decimal( $arr_key['pricing_sale_value'], wc_get_price_decimals() );

						$rate_min_stay = apply_filters( 'hbfwc_custom_rate_min_stay', $min_stay, $rate_db_id, $arr_key );
						?>
					<tr id="hbfwc-rp-<?php print esc_attr( $rate_db_id ); ?>">
						<td><span class="hbfwc-dragger"><svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M8 7h2V5H8v2zm0 6h2v-2H8v2zm0 6h2v-2H8v2zm6-14v2h2V5h-2zm0 8h2v-2h-2v2zm0 6h2v-2h-2v2z"></path></svg></span></td>
						<td>
							<strong><?php print esc_html( $rate_term->name ); ?></strong>
							<br/>
							<div class="actions"><a href="#" class="rate-del" data-rate-id="<?php print esc_attr( $rate_db_id ); ?>"><?php print esc_html__( 'Remove', 'hotel-booking-for-woocommerce' ); ?></a></div>
						</td>
						<td>
							<select name="hbfwc_rate[<?php print esc_attr( $rate_db_id ); ?>][pricing_method]" class="hbfwc-pricing-method" data-id="<?php print esc_attr( $rate_db_id ); ?>">
								<option value="increase" <?php selected( 'increase', $arr_key['pricing_method'], true ); ?>><?php print esc_html__( 'Increase the standard rate price', 'hotel-booking-for-woocommerce' ); ?></option>
								<option value="decrease" <?php selected( 'decrease', $arr_key['pricing_method'], true ); ?>><?php print esc_html__( 'Decrease the standard rate price', 'hotel-booking-for-woocommerce' ); ?></option>
								<option value="fixed" <?php selected( 'fixed', $arr_key['pricing_method'], true ); ?>><?php print esc_html__( 'Fixed price', 'hotel-booking-for-woocommerce' ); ?></option>
							</select>
							<br/>
							<input type="text" name="hbfwc_rate[<?php print esc_attr( $rate_db_id ); ?>][pricing_value]" class="short wc_input_price" value="<?php print esc_attr( $reg_price_val ); ?>" />
							<span class="gutter"></span>
							<select name="hbfwc_rate[<?php print esc_attr( $rate_db_id ); ?>][pricing_unit]" id="hbfwc-rate-<?php print esc_attr( $rate_db_id ); ?>-pricing-unit" style="<?php print esc_attr( $unit_style ); ?>">
								<option value="percent" <?php selected( 'percent', $arr_key['pricing_unit'], true ); ?>><?php print '%'; ?></option>
								<option value="currency" <?php selected( 'currency', $arr_key['pricing_unit'], true ); ?>><?php print wp_kses_post( get_woocommerce_currency_symbol() ); ?></option>
							</select>
						</td>
						<td>
							<select name="hbfwc_rate[<?php print esc_attr( $rate_db_id ); ?>][pricing_sale_method]" class="hbfwc-pricing-method" data-id="<?php print esc_attr( $rate_db_id ); ?>">
								<option value="none" <?php selected( 'none', $arr_key['pricing_sale_method'], true ); ?>><?php print esc_html__( 'No sale price', 'hotel-booking-for-woocommerce' ); ?></option>
								<option value="decrease" <?php selected( 'decrease', $arr_key['pricing_sale_method'], true ); ?>><?php print esc_html__( 'Decrease the regular price', 'hotel-booking-for-woocommerce' ); ?></option>
								<option value="fixed" <?php selected( 'fixed', $arr_key['pricing_sale_method'], true ); ?>><?php print esc_html__( 'Fixed price', 'hotel-booking-for-woocommerce' ); ?></option>
							</select>
							<br/>
							<input type="text" name="hbfwc_rate[<?php print esc_attr( $rate_db_id ); ?>][pricing_sale_value]" class="short wc_input_price" value="<?php print esc_attr( $sale_price_val ); ?>" />
							<span class="gutter"></span>
							<select name="hbfwc_rate[<?php print esc_attr( $rate_db_id ); ?>][pricing_sale_unit]" id="hbfwc-rate-<?php print esc_attr( $rate_db_id ); ?>-pricing-sale-unit" style="<?php print esc_attr( $unit_sale_style ); ?>">
								<option value="percent" <?php selected( 'percent', $arr_key['pricing_sale_unit'], true ); ?>><?php print '%'; ?></option>
								<option value="currency" <?php selected( 'currency', $arr_key['pricing_sale_unit'], true ); ?>><?php print wp_kses_post( get_woocommerce_currency_symbol() ); ?></option>
							</select>
							
						</td>							<?php do_action( 'hbfwc_rate_table_row_extra_columns', $rate_db_id, $arr_key ); ?>						<td>
							<?php
								$rateMinStayInfo = '<div class="hbfwc_text-center"><input type="number" name="booking_min_stay" value="' . esc_attr( $rate_min_stay ) . '" class="short" disabled="disabled" />';
								$rateMinStayInfo .= wc_help_tip( esc_html__( 'Install the "Pro" add-on now to define custom minimum stay rules for each rate plan!', 'hotel-booking-for-woocommerce' ) ) . '</div>' . "\n";

								$ksesTags = hbfwc_wp_kses_input();
								echo wp_kses( apply_filters( 'hbfwc_custom_rate_min_stay_edit_field', $rateMinStayInfo, $rate_db_id, $arr_key, $rate_min_stay ), $ksesTags );
							?>
						</td>
						<td><input type="checkbox" class="cb_rate_status" role="switch" name="hbfwc_rate[<?php print esc_attr( $rate_db_id ); ?>][status]" value="yes" <?php checked( 'yes', $rate_status, true ); ?>/></td>
						<input type="hidden" name="hbfwc_rate_ids[]" value="<?php print esc_attr( $rate_db_id ); ?>" />
					</tr>
							<?php
					}
				}
				?>
			</tbody>
		</table>
	</div>
</div>

<script type="text/template" id="hbfwc-panel-rp">
	<tr id="hbfwc-rp-{rate_id}">
		<td><span class="hbfwc-dragger"><svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M8 7h2V5H8v2zm0 6h2v-2H8v2zm0 6h2v-2H8v2zm6-14v2h2V5h-2zm0 8h2v-2h-2v2zm0 6h2v-2h-2v2z"></path></svg></span></td>
		<td><strong>{rate_name}</strong><br/><div class="actions"><a href="#" class="rate-del" data-rate-id="{rate_id}"><?php print esc_html__( 'Remove', 'hotel-booking-for-woocommerce' ); ?></a></div></td>
		<td>
			<select name="hbfwc_rate[{rate_id}][pricing_method]" class="hbfwc-pricing-method" data-id="{rate_id}">
				<option value="increase"><?php print esc_html__( 'Increase the standard price', 'hotel-booking-for-woocommerce' ); ?></option>
				<option value="decrease"><?php print esc_html__( 'Decrease the standard price', 'hotel-booking-for-woocommerce' ); ?></option>
				<option value="fixed"><?php print esc_html__( 'Fixed price', 'hotel-booking-for-woocommerce' ); ?></option>
			</select>
			<br/>
			<input type="text" name="hbfwc_rate[{rate_id}][pricing_value]" class="short wc_input_price" value=""/>
			<span class="gutter"></span>
			<select name="hbfwc_rate[{rate_id}][pricing_unit]" id="hbfwc-rate-{rate_id}-pricing-unit">
				<option value="percent"><?php print '%'; ?></option>
				<option value="currency"><?php print wp_kses_post( get_woocommerce_currency_symbol() ); ?></option>
			</select>
		</td>
		<td>
			<select name="hbfwc_rate[{rate_id}][pricing_sale_method]" class="hbfwc-pricing-method" data-id="{rate_id}">
				<option value="none"><?php print esc_html__( 'No sale price', 'hotel-booking-for-woocommerce' ); ?></option>
				<option value="decrease"><?php print esc_html__( 'Decrease the regular price', 'hotel-booking-for-woocommerce' ); ?></option>
				<option value="fixed"><?php print esc_html__( 'Fixed price', 'hotel-booking-for-woocommerce' ); ?></option>
			</select>
			<br/>
			<input type="text" name="hbfwc_rate[{rate_id}][pricing_sale_value]" class="short wc_input_price" value="" />
			<span class="gutter"></span>
			<select name="hbfwc_rate[{rate_id}][pricing_sale_unit]" id="hbfwc-rate-{rate_id}-pricing-sale-unit">
				<option value="percent"><?php print '%'; ?></option>
				<option value="currency"><?php print wp_kses_post( get_woocommerce_currency_symbol() ); ?></option>
			</select>
		</td>
		<?php do_action( 'hbfwc_rate_table_row_extra_columns', '{rate_id}', [] ); ?>
		<td><?php echo wp_kses( apply_filters( 'hbfwc_custom_rate_min_stay_edit_field', $rateMinStayInfo, '{rate_id}', [], $min_stay ), hbfwc_wp_kses_input() ); ?></td>
		<td><input type="checkbox" class="cb_rate_status" role="switch" name="hbfwc_rate[{rate_id}][status]" value="yes" checked /></td>
		<input type="hidden" name="hbfwc_rate_ids[]" value="{rate_id}" />
	</tr>
</script>
