<?php
if ( ! defined( 'ABSPATH' ) ) exit;
$bingocr_s      = BingoCR_DB::get_settings();
$bingocr_is_pro = BingoCR_License::is_pro();
?>
<div class="wrap bingocr-admin-wrap">
	<h1>
		<?php esc_html_e( 'Settings', 'bingocar' ); ?>
		<span class="bcr-plan-badge bcr-plan-<?php echo $bingocr_is_pro ? 'pro' : 'free'; ?>">
			<?php echo $bingocr_is_pro ? esc_html__( 'Pro', 'bingocar' ) : esc_html__( 'Free', 'bingocar' ); ?>
		</span>
	</h1>

	<?php if ( ! $bingocr_is_pro ) : ?>
	<div class="notice notice-info bcr-upgrade-banner">
		<p>
			<strong><?php esc_html_e( 'Upgrade to Pro', 'bingocar' ); ?></strong>
			&mdash;
			<?php esc_html_e( 'Unlock extras at checkout, discount codes, online payments, out-of-hours fee, and more.', 'bingocar' ); ?>
			<a href="<?php echo esc_url( BingoCR_License::upgrade_url() ); ?>" target="_blank" class="button button-primary" style="margin-left:12px">
				<?php esc_html_e( 'Upgrade Now →', 'bingocar' ); ?>
			</a>
		</p>
	</div>
	<?php endif; ?>

	<div id="bingocr-settings-saved" class="notice notice-success" style="display:none;">
		<p><?php esc_html_e( 'Settings saved.', 'bingocar' ); ?></p>
	</div>

	<form id="bingocr-settings-form" novalidate>

		<h2><?php esc_html_e( 'Company', 'bingocar' ); ?></h2>
		<table class="form-table">
			<tr>
				<th><label for="s-company-name"><?php esc_html_e( 'Company Name', 'bingocar' ); ?></label></th>
				<td><input type="text" id="s-company-name" name="company_name" value="<?php echo esc_attr( $bingocr_s['company_name'] ); ?>" class="regular-text"></td>
			</tr>
		</table>

		<h2><?php esc_html_e( 'Currency', 'bingocar' ); ?></h2>
		<table class="form-table">
			<tr>
				<th><label for="s-currency"><?php esc_html_e( 'Currency Code', 'bingocar' ); ?></label></th>
				<td><input type="text" id="s-currency" name="currency" value="<?php echo esc_attr( $bingocr_s['currency'] ); ?>" class="small-text" maxlength="3"></td>
			</tr>
			<tr>
				<th><label for="s-currency-symbol"><?php esc_html_e( 'Currency Symbol', 'bingocar' ); ?></label></th>
				<td><input type="text" id="s-currency-symbol" name="currency_symbol" value="<?php echo esc_attr( $bingocr_s['currency_symbol'] ); ?>" class="small-text" maxlength="5"></td>
			</tr>
			<tr>
				<th><label for="s-currency-position"><?php esc_html_e( 'Symbol Position', 'bingocar' ); ?></label></th>
				<td>
					<select id="s-currency-position" name="currency_position">
						<option value="before" <?php selected( $bingocr_s['currency_position'], 'before' ); ?>><?php esc_html_e( 'Before (€100)', 'bingocar' ); ?></option>
						<option value="after"  <?php selected( $bingocr_s['currency_position'], 'after' ); ?>><?php esc_html_e( 'After (100€)', 'bingocar' ); ?></option>
					</select>
				</td>
			</tr>
		</table>

		<h2><?php esc_html_e( 'Booking Rules', 'bingocar' ); ?></h2>
		<table class="form-table">
			<tr>
				<th><label for="s-min-days"><?php esc_html_e( 'Min Rental Days', 'bingocar' ); ?></label></th>
				<td><input type="number" id="s-min-days" name="min_rental_days" value="<?php echo esc_attr( $bingocr_s['min_rental_days'] ); ?>" class="small-text" min="1"></td>
			</tr>
			<tr>
				<th><label for="s-max-days"><?php esc_html_e( 'Max Rental Days', 'bingocar' ); ?></label></th>
				<td><input type="number" id="s-max-days" name="max_rental_days" value="<?php echo esc_attr( $bingocr_s['max_rental_days'] ); ?>" class="small-text" min="1"></td>
			</tr>
			<tr>
				<th><label for="s-advance-days"><?php esc_html_e( 'Advance Booking (days)', 'bingocar' ); ?></label></th>
				<td><input type="number" id="s-advance-days" name="advance_booking_days" value="<?php echo esc_attr( $bingocr_s['advance_booking_days'] ); ?>" class="small-text" min="1"></td>
			</tr>
		</table>

		<h2><?php esc_html_e( 'Deposit', 'bingocar' ); ?></h2>
		<table class="form-table">
			<tr>
				<th><label for="s-default-deposit"><?php esc_html_e( 'Default Deposit', 'bingocar' ); ?></label></th>
				<td>
					<input type="number" id="s-default-deposit" name="default_deposit" value="<?php echo esc_attr( $bingocr_s['default_deposit'] ?? 0 ); ?>" class="small-text" min="0" step="0.01">
					<p class="description"><?php esc_html_e( 'Flat deposit amount applied when a car\'s deposit type is set to Default. Set to 0 to require no deposit.', 'bingocar' ); ?></p>
				</td>
			</tr>
		</table>

		<h2><?php esc_html_e( 'Business Hours & Out-of-Hours Fee', 'bingocar' ); ?></h2>
		<?php if ( BingoCR_License::can( 'out_of_hours' ) ) : ?>
		<table class="form-table">
			<tr>
				<th><label for="s-bh-start"><?php esc_html_e( 'Business Hours', 'bingocar' ); ?></label></th>
				<td>
					<input type="time" id="s-bh-start" name="business_hours_start" value="<?php echo esc_attr( $bingocr_s['business_hours_start'] ?? '09:00' ); ?>">
					&nbsp;–&nbsp;
					<input type="time" id="s-bh-end" name="business_hours_end" value="<?php echo esc_attr( $bingocr_s['business_hours_end'] ?? '18:00' ); ?>">
					<p class="description"><?php esc_html_e( 'Pickups and returns inside this range are free of surcharge.', 'bingocar' ); ?></p>
				</td>
			</tr>
			<tr>
				<th><?php esc_html_e( 'Allow Out-of-Hours Service', 'bingocar' ); ?></th>
				<td>
					<label>
						<input type="checkbox" name="out_of_hours_enabled" value="1" <?php checked( ! empty( $bingocr_s['out_of_hours_enabled'] ) ); ?>>
						<?php esc_html_e( 'Enable pickup/return outside business hours (with fee)', 'bingocar' ); ?>
					</label>
				</td>
			</tr>
			<tr>
				<th><label for="s-ooh-fee"><?php esc_html_e( 'Out-of-Hours Fee', 'bingocar' ); ?></label></th>
				<td>
					<input type="number" id="s-ooh-fee" name="out_of_hours_fee" value="<?php echo esc_attr( $bingocr_s['out_of_hours_fee'] ?? 25 ); ?>" class="small-text" min="0" step="0.01">
					<p class="description"><?php esc_html_e( 'Applied per occurrence (pickup + return).', 'bingocar' ); ?></p>
				</td>
			</tr>
		</table>
		<?php else : ?>
		<?php
		$bingocr_upsell_title = __( 'Out-of-Hours Fee — Pro Feature', 'bingocar' );
		$bingocr_upsell_desc  = __( 'Charge a surcharge for pickups or returns outside your business hours. Available in BingoCR Pro.', 'bingocar' );
		include __DIR__ . '/partial-pro-upsell.php';
		?>
		<?php endif; ?>

		<h2><?php esc_html_e( 'Email Notifications', 'bingocar' ); ?></h2>
		<table class="form-table">
			<tr>
				<th><label for="s-notify-email"><?php esc_html_e( 'Admin Notification Email', 'bingocar' ); ?></label></th>
				<td><input type="email" id="s-notify-email" name="notify_email" value="<?php echo esc_attr( $bingocr_s['notify_email'] ); ?>" class="regular-text"></td>
			</tr>
			<tr>
				<th><label for="s-from-name"><?php esc_html_e( 'From Name', 'bingocar' ); ?></label></th>
				<td><input type="text" id="s-from-name" name="from_name" value="<?php echo esc_attr( $bingocr_s['from_name'] ); ?>" class="regular-text"></td>
			</tr>
			<tr>
				<th><label for="s-from-email"><?php esc_html_e( 'From Email', 'bingocar' ); ?></label></th>
				<td><input type="email" id="s-from-email" name="from_email" value="<?php echo esc_attr( $bingocr_s['from_email'] ); ?>" class="regular-text"></td>
			</tr>
		</table>

		<h2><?php esc_html_e( 'Display', 'bingocar' ); ?></h2>
		<table class="form-table">
			<tr>
				<th><label for="s-language"><?php esc_html_e( 'Default Language', 'bingocar' ); ?></label></th>
				<td>
					<select id="s-language" name="language">
						<option value="en" <?php selected( $bingocr_s['language'], 'en' ); ?>><?php esc_html_e( 'English', 'bingocar' ); ?></option>
						<option value="ro" <?php selected( $bingocr_s['language'], 'ro' ); ?>><?php esc_html_e( 'Romanian', 'bingocar' ); ?></option>
					</select>
				</td>
			</tr>
			<tr>
				<th><?php esc_html_e( 'Auto-detect Language', 'bingocar' ); ?></th>
				<td>
					<label>
						<input type="checkbox" name="auto_detect_language" value="1" <?php checked( ! empty( $bingocr_s['auto_detect_language'] ) ); ?>>
						<?php esc_html_e( 'Detect visitor country via ipapi.co and switch language automatically', 'bingocar' ); ?>
					</label>
					<p class="description">
						<?php
						printf(
							/* translators: %s: link to privacy policy */
							esc_html__( 'When enabled, visitor IP is sent to ipapi.co. See their %s.', 'bingocar' ),
							'<a href="https://ipapi.co/privacy/" target="_blank" rel="noopener noreferrer">' . esc_html__( 'privacy policy', 'bingocar' ) . '</a>'
						);
						?>
					</p>
				</td>
			</tr>
			<tr>
				<th><?php esc_html_e( 'Theme', 'bingocar' ); ?></th>
				<td>
					<select name="theme">
						<option value="light" <?php selected( $bingocr_s['theme'], 'light' ); ?>><?php esc_html_e( 'Light', 'bingocar' ); ?></option>
						<option value="dark"  <?php selected( $bingocr_s['theme'], 'dark' ); ?>><?php esc_html_e( 'Dark', 'bingocar' ); ?></option>
					</select>
				</td>
			</tr>
			<tr>
				<th><label for="s-primary-color"><?php esc_html_e( 'Primary Color', 'bingocar' ); ?></label></th>
				<td>
					<input type="color" id="s-primary-color" name="primary_color" value="<?php echo esc_attr( $bingocr_s['primary_color'] ); ?>">
				</td>
			</tr>
			<tr>
				<th><?php esc_html_e( 'Show Attribution', 'bingocar' ); ?></th>
				<td>
					<label>
						<input type="checkbox" name="show_attribution" value="1" <?php checked( ! empty( $bingocr_s['show_attribution'] ) ); ?>>
						<?php esc_html_e( 'Show a "Powered by Bingo Car Rental" link below the booking widget', 'bingocar' ); ?>
					</label>
					<p class="description"><?php esc_html_e( 'Optional — off by default. Enable it only if you\'d like to credit the plugin.', 'bingocar' ); ?></p>
				</td>
			</tr>
		</table>

		<h2><?php esc_html_e( 'Terms & Conditions', 'bingocar' ); ?></h2>
		<table class="form-table">
			<tr>
				<th><label for="s-terms"><?php esc_html_e( 'Rental Terms', 'bingocar' ); ?></label></th>
				<td>
					<textarea id="s-terms" name="terms_conditions" rows="10" class="large-text code"><?php echo esc_textarea( $bingocr_s['terms_conditions'] ?? '' ); ?></textarea>
					<p class="description"><?php esc_html_e( 'Shown when a customer clicks "terms and conditions" in the booking form. Basic HTML is allowed (p, strong, em, ul, ol, li, a, h3, h4). Leave empty to show plain text without a link.', 'bingocar' ); ?></p>
				</td>
			</tr>
		</table>

		<p class="submit">
			<button type="submit" class="button button-primary"><?php esc_html_e( 'Save Settings', 'bingocar' ); ?></button>
			<span class="bingocr-form-message"></span>
		</p>

	</form>
</div>
