<?php
if ( ! defined( 'ABSPATH' ) ) exit;
$bingocr_extras_ok   = BingoCR_License::can( 'extras' );
$bingocr_discount_ok = BingoCR_License::can( 'discounts' );
$bingocr_pro_badge   = ' <span class="bcr-plan-badge bcr-plan-pro">' . esc_html__( 'Pro', 'bingocar' ) . '</span>';
?>
<div class="wrap bingocr-admin-wrap" id="bingocr-pricing-app">
	<h1><?php esc_html_e( 'Pricing Rules', 'bingocar' ); ?></h1>

	<p class="description" style="margin-bottom:20px">
		<?php esc_html_e( 'Seasonal rules adjust the nightly rate by a percentage for each day inside their date range — they always apply. Duration rules set the nightly rate (or a fixed total) for rentals within a given day range; seasonal then adjusts that rate on top, night by night.', 'bingocar' ); ?>
	</p>

	<!-- Seasonal Pricing -->
	<div class="bingocr-section">
		<div class="bingocr-section-header">
			<h2><?php esc_html_e( 'Seasonal Pricing', 'bingocar' ); ?></h2>
			<button class="button button-primary" id="bingocr-add-seasonal">
				<?php esc_html_e( '+ Add Season', 'bingocar' ); ?>
			</button>
		</div>
		<div id="bingocr-seasonal-list">
			<p class="bingocr-empty"><?php esc_html_e( 'Loading…', 'bingocar' ); ?></p>
		</div>
	</div>

	<!-- Duration-Based Pricing -->
	<div class="bingocr-section">
		<div class="bingocr-section-header">
			<h2><?php esc_html_e( 'Duration-Based Pricing', 'bingocar' ); ?></h2>
			<button class="button button-primary" id="bingocr-add-period">
				<?php esc_html_e( '+ Add Rule', 'bingocar' ); ?>
			</button>
		</div>
		<div id="bingocr-period-list">
			<p class="bingocr-empty"><?php esc_html_e( 'Loading…', 'bingocar' ); ?></p>
		</div>
	</div>

	<!-- Extras / Add-ons -->
	<div class="bingocr-section">
		<div class="bingocr-section-header">
			<h2><?php esc_html_e( 'Extras / Add-ons', 'bingocar' ); ?><?php if ( ! $bingocr_extras_ok ) { echo wp_kses_post( $bingocr_pro_badge ); } ?></h2>
			<?php if ( $bingocr_extras_ok ) : ?>
				<button class="button button-primary" id="bingocr-add-extra">
					<?php esc_html_e( '+ Add Extra', 'bingocar' ); ?>
				</button>
			<?php endif; ?>
		</div>
		<p class="description"><?php esc_html_e( 'Optional add-ons customers can select during booking (GPS, child seat, insurance, etc.)', 'bingocar' ); ?></p>
		<?php if ( ! $bingocr_extras_ok ) :
			$bingocr_upsell_title = __( 'Sell optional add-ons', 'bingocar' );
			$bingocr_upsell_desc  = __( 'GPS, child seats, insurance and more — priced per day or as a flat fee, selectable by customers at checkout. Upgrade to Pro to create extras.', 'bingocar' );
			include __DIR__ . '/partial-pro-upsell.php';
		else : ?>
			<div id="bingocr-extras-list">
				<p class="bingocr-empty"><?php esc_html_e( 'Loading…', 'bingocar' ); ?></p>
			</div>
		<?php endif; ?>
	</div>

	<!-- Discount Codes -->
	<div class="bingocr-section">
		<div class="bingocr-section-header">
			<h2><?php esc_html_e( 'Discount Codes', 'bingocar' ); ?><?php if ( ! $bingocr_discount_ok ) { echo wp_kses_post( $bingocr_pro_badge ); } ?></h2>
			<?php if ( $bingocr_discount_ok ) : ?>
				<button class="button button-primary" id="bingocr-add-discount">
					<?php esc_html_e( '+ Add Discount', 'bingocar' ); ?>
				</button>
			<?php endif; ?>
		</div>
		<p class="description"><?php esc_html_e( 'Promotional codes that apply a percentage or flat discount at checkout.', 'bingocar' ); ?></p>
		<?php if ( ! $bingocr_discount_ok ) :
			$bingocr_upsell_title = __( 'Run promotional discount codes', 'bingocar' );
			$bingocr_upsell_desc  = __( 'Percentage or flat-amount codes with min-days, usage limits and expiry dates, applied at checkout. Upgrade to Pro to create discount codes.', 'bingocar' );
			include __DIR__ . '/partial-pro-upsell.php';
		else : ?>
			<div id="bingocr-discounts-list">
				<p class="bingocr-empty"><?php esc_html_e( 'Loading…', 'bingocar' ); ?></p>
			</div>
		<?php endif; ?>
	</div>

	<!-- ── Seasonal modal ────────────────────────────────────────────── -->
	<div id="bingocr-seasonal-form-wrap" class="bingocr-modal-wrap" style="display:none;">
		<div class="bingocr-modal bingocr-modal--panel">
			<div class="bingocr-modal-head">
				<h2 id="bingocr-seasonal-form-title"><?php esc_html_e( 'Add Season', 'bingocar' ); ?></h2>
			</div>
			<div class="bingocr-modal-body">
				<form id="bingocr-seasonal-form" novalidate>
					<input type="hidden" id="seasonal-id" value="">

					<div class="bingocr-form-row">
						<label><?php esc_html_e( 'Season Name *', 'bingocar' ); ?><br>
							<input type="text" name="name" class="regular-text" required>
						</label>
					</div>

					<div class="bingocr-form-row bingocr-two-col">
						<label><?php esc_html_e( 'Start Date *', 'bingocar' ); ?><br>
							<input type="date" name="start_date" required style="width:100%">
						</label>
						<label><?php esc_html_e( 'End Date *', 'bingocar' ); ?><br>
							<input type="date" name="end_date" required style="width:100%">
						</label>
					</div>

					<div class="bingocr-form-row bingocr-three-col">
						<div>
							<label><?php esc_html_e( 'Price Modifier (%)', 'bingocar' ); ?><br>
								<input type="number" name="modifier" step="0.01" class="small-text" value="0">
							</label>
							<p class="description"><?php esc_html_e( '+20 = 20 % surcharge, −10 = 10 % discount', 'bingocar' ); ?></p>
						</div>
						<label><?php esc_html_e( 'Applies To', 'bingocar' ); ?><br>
							<select name="applies_to" id="seasonal-applies-to" style="width:100%">
								<option value="all"><?php esc_html_e( 'All Cars', 'bingocar' ); ?></option>
								<option value="category"><?php esc_html_e( 'Category', 'bingocar' ); ?></option>
							</select>
						</label>
						<div id="seasonal-category-wrap" style="display:none;">
							<label><?php esc_html_e( 'Category', 'bingocar' ); ?><br>
								<input type="text" name="category" class="regular-text" placeholder="economy, suv…">
							</label>
						</div>
					</div>

					<div class="bingocr-form-row bingocr-two-col">
						<label><?php esc_html_e( 'Priority', 'bingocar' ); ?><br>
							<input type="number" name="priority" value="0" min="0" class="small-text">
						</label>
						<label><?php esc_html_e( 'Status', 'bingocar' ); ?><br>
							<select name="status" style="width:100%">
								<option value="active"><?php esc_html_e( 'Active', 'bingocar' ); ?></option>
								<option value="inactive"><?php esc_html_e( 'Inactive', 'bingocar' ); ?></option>
							</select>
						</label>
					</div>

					<div class="bingocr-form-actions">
						<button type="submit" class="button button-primary"><?php esc_html_e( 'Save', 'bingocar' ); ?></button>
						<button type="button" class="button" id="bingocr-cancel-seasonal"><?php esc_html_e( 'Cancel', 'bingocar' ); ?></button>
						<span class="bingocr-form-message"></span>
					</div>
				</form>
			</div>
		</div>
	</div>

	<!-- ── Duration pricing modal ────────────────────────────────────── -->
	<div id="bingocr-period-form-wrap" class="bingocr-modal-wrap" style="display:none;">
		<div class="bingocr-modal bingocr-modal--panel">
			<div class="bingocr-modal-head">
				<h2 id="bingocr-period-form-title"><?php esc_html_e( 'Add Duration Rule', 'bingocar' ); ?></h2>
			</div>
			<div class="bingocr-modal-body">
				<form id="bingocr-period-form" novalidate>
					<input type="hidden" id="period-id" value="">

					<div class="bingocr-form-row">
						<label><?php esc_html_e( 'Rule Name *', 'bingocar' ); ?><br>
							<input type="text" name="name" class="regular-text" required>
						</label>
					</div>

					<div class="bingocr-form-row bingocr-two-col">
						<label><?php esc_html_e( 'Min Days *', 'bingocar' ); ?><br>
							<input type="number" name="min_days" value="3" min="1" class="small-text" required>
						</label>
						<label><?php esc_html_e( 'Max Days *', 'bingocar' ); ?><br>
							<input type="number" name="max_days" value="7" min="1" class="small-text" required>
						</label>
					</div>

					<div class="bingocr-form-row">
						<label style="font-weight:600;margin-bottom:6px;display:block"><?php esc_html_e( 'Pricing Type', 'bingocar' ); ?></label>
						<label style="font-weight:normal;display:inline-flex;align-items:center;gap:6px;margin-right:20px">
							<input type="radio" name="pricing_type" value="rate_per_day" checked> <?php esc_html_e( 'Rate per Day', 'bingocar' ); ?>
						</label>
						<label style="font-weight:normal;display:inline-flex;align-items:center;gap:6px">
							<input type="radio" name="pricing_type" value="fixed_total"> <?php esc_html_e( 'Fixed Total', 'bingocar' ); ?>
						</label>
					</div>

					<div class="bingocr-form-row bingocr-two-col">
						<div id="period-day-rate-wrap">
							<label><?php esc_html_e( 'Price per Day', 'bingocar' ); ?><br>
								<input type="number" name="price_per_day" step="0.01" min="0" value="0" class="small-text">
							</label>
						</div>
						<div id="period-fixed-wrap" style="display:none;">
							<label><?php esc_html_e( 'Fixed Total Price', 'bingocar' ); ?><br>
								<input type="number" name="fixed_price" step="0.01" min="0" value="0" class="small-text">
							</label>
						</div>
					</div>

					<div class="bingocr-form-row bingocr-three-col">
						<label><?php esc_html_e( 'Applies To', 'bingocar' ); ?><br>
							<select name="applies_to" id="period-applies-to" style="width:100%">
								<option value="all"><?php esc_html_e( 'All Cars', 'bingocar' ); ?></option>
								<option value="category"><?php esc_html_e( 'Category', 'bingocar' ); ?></option>
								<option value="car"><?php esc_html_e( 'Specific Car', 'bingocar' ); ?></option>
							</select>
						</label>
						<div id="period-category-wrap" style="display:none;">
							<label><?php esc_html_e( 'Category', 'bingocar' ); ?><br>
								<input type="text" name="category" placeholder="economy, suv…">
							</label>
						</div>
						<div id="period-car-wrap" style="display:none;">
							<label><?php esc_html_e( 'Car', 'bingocar' ); ?><br>
								<select name="car_id" id="period-car-select" style="width:100%">
									<option value=""><?php esc_html_e( '— select —', 'bingocar' ); ?></option>
								</select>
							</label>
						</div>
					</div>

					<div class="bingocr-form-row bingocr-two-col">
						<label><?php esc_html_e( 'Priority', 'bingocar' ); ?><br>
							<input type="number" name="priority" value="0" min="0" class="small-text">
						</label>
						<label><?php esc_html_e( 'Status', 'bingocar' ); ?><br>
							<select name="status" style="width:100%">
								<option value="active"><?php esc_html_e( 'Active', 'bingocar' ); ?></option>
								<option value="inactive"><?php esc_html_e( 'Inactive', 'bingocar' ); ?></option>
							</select>
						</label>
					</div>

					<div class="bingocr-form-actions">
						<button type="submit" class="button button-primary"><?php esc_html_e( 'Save', 'bingocar' ); ?></button>
						<button type="button" class="button" id="bingocr-cancel-period"><?php esc_html_e( 'Cancel', 'bingocar' ); ?></button>
						<span class="bingocr-form-message"></span>
					</div>
				</form>
			</div>
		</div>
	</div>

	<!-- ── Extras modal ──────────────────────────────────────────────── -->
	<div id="bingocr-extra-form-wrap" class="bingocr-modal-wrap" style="display:none;">
		<div class="bingocr-modal bingocr-modal--panel">
			<div class="bingocr-modal-head">
				<h2 id="bingocr-extra-form-title"><?php esc_html_e( 'Add Extra', 'bingocar' ); ?></h2>
			</div>
			<div class="bingocr-modal-body">
				<form id="bingocr-extra-form" novalidate>
					<input type="hidden" id="extra-id" value="">
					<div class="bingocr-form-row">
						<label><?php esc_html_e( 'Name *', 'bingocar' ); ?><br>
							<input type="text" name="name" required class="regular-text">
						</label>
					</div>
					<div class="bingocr-form-row">
						<label><?php esc_html_e( 'Description', 'bingocar' ); ?><br>
							<textarea name="description" rows="2" class="large-text"></textarea>
						</label>
					</div>
					<div class="bingocr-form-row bingocr-three-col">
						<label><?php esc_html_e( 'Price *', 'bingocar' ); ?><br>
							<input type="number" name="price" min="0" step="0.01" required class="small-text">
						</label>
						<label><?php esc_html_e( 'Price Type', 'bingocar' ); ?><br>
							<select name="price_type" style="width:100%">
								<option value="per_day"><?php esc_html_e( 'Per Day', 'bingocar' ); ?></option>
								<option value="flat"><?php esc_html_e( 'Flat (one-time)', 'bingocar' ); ?></option>
							</select>
						</label>
						<label><?php esc_html_e( 'Status', 'bingocar' ); ?><br>
							<select name="status" style="width:100%">
								<option value="active"><?php esc_html_e( 'Active', 'bingocar' ); ?></option>
								<option value="inactive"><?php esc_html_e( 'Inactive', 'bingocar' ); ?></option>
							</select>
						</label>
					</div>
					<div class="bingocr-form-actions">
						<button type="submit" class="button button-primary"><?php esc_html_e( 'Save', 'bingocar' ); ?></button>
						<button type="button" class="button" id="bingocr-cancel-extra"><?php esc_html_e( 'Cancel', 'bingocar' ); ?></button>
						<span class="bingocr-form-message"></span>
					</div>
				</form>
			</div>
		</div>
	</div>

	<!-- ── Discount modal ────────────────────────────────────────────── -->
	<div id="bingocr-discount-form-wrap" class="bingocr-modal-wrap" style="display:none;">
		<div class="bingocr-modal bingocr-modal--panel">
			<div class="bingocr-modal-head">
				<h2 id="bingocr-discount-form-title"><?php esc_html_e( 'Add Discount Code', 'bingocar' ); ?></h2>
			</div>
			<div class="bingocr-modal-body">
				<form id="bingocr-discount-form" novalidate>
					<input type="hidden" id="discount-id" value="">

					<div class="bingocr-form-row bingocr-two-col">
						<label><?php esc_html_e( 'Code *', 'bingocar' ); ?><br>
							<input type="text" name="code" required class="regular-text" placeholder="SUMMER20" style="text-transform:uppercase">
						</label>
						<label><?php esc_html_e( 'Status', 'bingocar' ); ?><br>
							<select name="status" style="width:100%">
								<option value="active"><?php esc_html_e( 'Active', 'bingocar' ); ?></option>
								<option value="inactive"><?php esc_html_e( 'Inactive', 'bingocar' ); ?></option>
							</select>
						</label>
					</div>

					<div class="bingocr-form-row bingocr-two-col">
						<label><?php esc_html_e( 'Type', 'bingocar' ); ?><br>
							<select name="type" style="width:100%">
								<option value="percentage"><?php esc_html_e( 'Percentage (%)', 'bingocar' ); ?></option>
								<option value="flat"><?php esc_html_e( 'Flat amount', 'bingocar' ); ?></option>
							</select>
						</label>
						<label><?php esc_html_e( 'Value *', 'bingocar' ); ?><br>
							<input type="number" name="value" min="0" step="0.01" value="0" required class="small-text">
						</label>
					</div>

					<div class="bingocr-form-row bingocr-three-col">
						<div>
							<label><?php esc_html_e( 'Min Days', 'bingocar' ); ?><br>
								<input type="number" name="min_days" value="1" min="1" class="small-text">
							</label>
							<p class="description"><?php esc_html_e( 'Minimum rental length to use this code.', 'bingocar' ); ?></p>
						</div>
						<div>
							<label><?php esc_html_e( 'Max Uses', 'bingocar' ); ?><br>
								<input type="number" name="max_uses" value="" min="1" class="small-text" placeholder="&#8734;">
							</label>
							<p class="description"><?php esc_html_e( 'Leave empty for unlimited.', 'bingocar' ); ?></p>
						</div>
						<div>
							<label><?php esc_html_e( 'Expires', 'bingocar' ); ?><br>
								<input type="date" name="expires_at" style="width:100%">
							</label>
							<p class="description"><?php esc_html_e( 'Leave empty — no expiry.', 'bingocar' ); ?></p>
						</div>
					</div>

					<div class="bingocr-form-actions">
						<button type="submit" class="button button-primary"><?php esc_html_e( 'Save', 'bingocar' ); ?></button>
						<button type="button" class="button" id="bingocr-cancel-discount"><?php esc_html_e( 'Cancel', 'bingocar' ); ?></button>
						<span class="bingocr-form-message"></span>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>
