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

$coupon_rows            = SftcyWordformBase::wordform_get_all_coupons();
$coupon_discount_types  = SftcyWordformBase::wordform_get_supported_coupon_discount_types();
$available_forms        = SftcyWordformBase::sc_wordform_get_all_forms_for_block_dynamic_callback();
?>
<div class="wrap sc-wordform-coupons-settings-wrapper">
	<div class="wordform-settings-overview-card">
		<div class="wordform-settings-overview-copy">
			<strong><?php esc_html_e( 'Coupon usage hints', 'wordform-pro' ); ?></strong>
			<p><?php esc_html_e( 'Create reusable coupon codes here, then add a Coupon element to payment forms later. Coupons can be time-limited, usage-limited, and restricted to selected forms.', 'wordform-pro' ); ?></p>
			<small><?php esc_html_e( 'Use percentage or fixed discounts, set start and expiry dates, and decide whether a coupon can also reduce custom amount fields.', 'wordform-pro' ); ?></small>
		</div>
	</div>

	<div class="wordform-coupons-toolbar-card">
		<div class="wordform-coupons-toolbar-copy">
			<h4><?php esc_html_e( 'Coupon Manager', 'wordform-pro' ); ?></h4>
			<p><?php esc_html_e( 'Manage coupon codes centrally so payment forms can reuse the same discount rules.', 'wordform-pro' ); ?></p>
		</div>
		<div class="wordform-coupons-toolbar-actions">
			<button type="button" class="button button-primary button-large sc-wordform-add-coupon">
				<i class="fa-solid fa-ticket"></i>
				<?php esc_html_e( 'Create Coupon', 'wordform-pro' ); ?>
			</button>
		</div>
	</div>

	<div id="wordformCouponsWrapperTable" class="wordform-coupons-table-wrapper">
		<table id="wordformCouponsTable" class="display hover stripe wp-list-table widefat fixed striped table-view-list wordform-coupons-table">
			<thead>
				<tr>
					<th><?php esc_html_e( 'Coupon Code', 'wordform-pro' ); ?></th>
					<th><?php esc_html_e( 'Type', 'wordform-pro' ); ?></th>
					<th><?php esc_html_e( 'Value', 'wordform-pro' ); ?></th>
					<th><?php esc_html_e( 'Status', 'wordform-pro' ); ?></th>
					<th><?php esc_html_e( 'Usage', 'wordform-pro' ); ?></th>
					<th><?php esc_html_e( 'Validity', 'wordform-pro' ); ?></th>
					<th><?php esc_html_e( 'Forms', 'wordform-pro' ); ?></th>
					<th><?php esc_html_e( 'Actions', 'wordform-pro' ); ?></th>
				</tr>
			</thead>
			<tbody id="wordformCouponsTableBody">
				<?php require SFTCY_WORDFORM_PLUGIN_DIR . 'admin/views/settings-callback-coupons-tab-table-rows.php'; ?>
			</tbody>
		</table>
	</div>

	<div id="wordformCouponsDialog" class="wordform-coupons-dialog" title="<?php esc_attr_e( 'Create Coupon', 'wordform-pro' ); ?>" style="display:none;">
		<form id="scWordformCouponForm">
			<input type="hidden" name="coupon-id" class="wordform-coupon-id" value="0" />
			<input type="hidden" name="coupon-apply-to-all-forms" class="wordform-coupon-apply-to-all-forms-input" value="yes" />
			<div class="wordform-coupon-form-grid">
				<div class="wordform-coupon-form-field">
					<label for="wordformCouponCode"><?php esc_html_e( 'Coupon Code', 'wordform-pro' ); ?></label>
					<input type="text" id="wordformCouponCode" name="coupon-code" class="wordform-coupon-code" placeholder="SAVE10" />
				</div>

				<div class="wordform-coupon-form-field">
					<label for="wordformCouponStatus"><?php esc_html_e( 'Status', 'wordform-pro' ); ?></label>
					<select id="wordformCouponStatus" name="coupon-status" class="wordform-coupon-status">
						<option value="active"><?php esc_html_e( 'Active', 'wordform-pro' ); ?></option>
						<option value="inactive"><?php esc_html_e( 'Inactive', 'wordform-pro' ); ?></option>
					</select>
				</div>

				<div class="wordform-coupon-form-field">
					<label for="wordformCouponDiscountType"><?php esc_html_e( 'Discount Type', 'wordform-pro' ); ?></label>
					<select id="wordformCouponDiscountType" name="coupon-discount-type" class="wordform-coupon-discount-type">
						<?php foreach ( $coupon_discount_types as $discount_type => $discount_label ) : ?>
							<option value="<?php echo esc_attr( $discount_type ); ?>"><?php echo esc_html( $discount_label ); ?></option>
						<?php endforeach; ?>
					</select>
				</div>

				<div class="wordform-coupon-form-field">
					<label for="wordformCouponDiscountValue"><?php esc_html_e( 'Discount Value', 'wordform-pro' ); ?></label>
					<input type="number" min="0" step="0.01" id="wordformCouponDiscountValue" name="coupon-discount-value" class="wordform-coupon-discount-value" value="0.00" />
				</div>

				<div class="wordform-coupon-form-field">
					<label for="wordformCouponStartDate"><?php esc_html_e( 'Start Date', 'wordform-pro' ); ?></label>
					<input type="date" id="wordformCouponStartDate" name="coupon-start-date" class="wordform-coupon-start-date" />
				</div>

				<div class="wordform-coupon-form-field">
					<label for="wordformCouponExpiryDate"><?php esc_html_e( 'Expiry Date', 'wordform-pro' ); ?></label>
					<input type="date" id="wordformCouponExpiryDate" name="coupon-expiry-date" class="wordform-coupon-expiry-date" />
				</div>

				<div class="wordform-coupon-form-field">
					<label for="wordformCouponUsageLimit"><?php esc_html_e( 'Usage Limit', 'wordform-pro' ); ?></label>
					<input type="number" min="0" step="1" id="wordformCouponUsageLimit" name="coupon-usage-limit" class="wordform-coupon-usage-limit" value="0" />
					<small><?php esc_html_e( 'Use 0 for unlimited.', 'wordform-pro' ); ?></small>
				</div>

				<div class="wordform-coupon-form-field">
					<label for="wordformCouponMinimumAmount"><?php esc_html_e( 'Minimum Order Amount', 'wordform-pro' ); ?></label>
					<input type="number" min="0" step="0.01" id="wordformCouponMinimumAmount" name="coupon-minimum-amount" class="wordform-coupon-minimum-amount" value="0.00" />
				</div>

				<div class="wordform-coupon-form-field wordform-coupon-form-field-full">
					<label class="wordform-coupon-inline-toggle">
						<input type="checkbox" name="coupon-apply-to-custom-amount" class="wordform-coupon-apply-to-custom-amount" />
						<span><?php esc_html_e( 'Allow coupon on custom amount fields', 'wordform-pro' ); ?></span>
					</label>
				</div>

				<div class="wordform-coupon-form-field wordform-coupon-form-field-full">
					<label><?php esc_html_e( 'Allowed Forms', 'wordform-pro' ); ?></label>
					<div class="wordform-coupon-forms-selector">
						<label class="wordform-coupon-inline-toggle wordform-coupon-inline-toggle-all">
							<input type="checkbox" class="wordform-coupon-all-forms" checked="checked" />
							<span><?php esc_html_e( 'Apply to all forms', 'wordform-pro' ); ?></span>
						</label>
						<div class="wordform-coupon-forms-list">
							<?php foreach ( $available_forms as $available_form ) : ?>
								<label class="wordform-coupon-inline-toggle">
									<input type="checkbox" name="coupon-allowed-forms[]" value="<?php echo esc_attr( $available_form['formID'] ); ?>" class="wordform-coupon-allowed-form" />
									<span><?php echo esc_html( $available_form['formName'] ); ?></span>
								</label>
							<?php endforeach; ?>
						</div>
					</div>
				</div>

				<div class="wordform-coupon-form-field wordform-coupon-form-field-full">
					<label for="wordformCouponNote"><?php esc_html_e( 'Internal Note', 'wordform-pro' ); ?></label>
					<textarea id="wordformCouponNote" name="coupon-note" class="wordform-coupon-note" rows="3" placeholder="<?php esc_attr_e( 'Optional admin note about this coupon.', 'wordform-pro' ); ?>"></textarea>
				</div>
			</div>

			<div class="wordform-coupon-form-update-info"></div>
		</form>
	</div>

	<div class="sc-wordform-coupons-tab-update-info"></div>
</div>
