<?php
/**
 * Coupon HTML Form Oupout.
 *
 * @package EverestForms\Pro\Addons\Coupons
 * @since   1.0.0
 */

namespace EverestForms\Pro\Addons\Coupons;

use EverestForms\Pro\Addons\Coupons\Process\Process;

defined( 'ABSPATH' ) || exit;

/**
 * Coupons table list class.
 */
class CouponForm {
	/**
	 * Output form html.
	 *
	 * @param string $action Action.
	 * @param array  $coupon Coupon.
	 */
	public static function output_form_html( $action = 'insert', $coupon = array() ) {
		$title   = 'insert' === $action ? 'Create New Single Coupon' : 'Edit Coupon';
		$post_id = isset( $_REQUEST['coupon'] ) ? absint( wp_unslash( $_REQUEST['coupon'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification

		if ( ! empty( $coupon ) ) {
			$data = $coupon;
		}

		$errors = Process::evf_coupons_errors();

		?>
		<div class="wrap">

		<?php include_once plugin_dir_path( EFP_PLUGIN_FILE ) . 'src/Addons/Coupons/views/header.php'; ?>

			<div class="everest-forms-coupons-create-coupon__wrapper">
				<div id="icon-users" class="icon32"></div>
				<div class="everest-forms-coupons-create__header">
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=evf-coupons' ) ); ?>" id="coupon-back__icon">
					<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none">
						<path d="M13.75 18.3333C13.6404 18.334 13.5316 18.313 13.4301 18.2715C13.3286 18.23 13.2362 18.1689 13.1584 18.0917L5.65836 10.5917C5.58026 10.5142 5.51826 10.422 5.47595 10.3205C5.43365 10.2189 5.41187 10.11 5.41187 10C5.41187 9.89 5.43365 9.78108 5.47595 9.67953C5.51826 9.57798 5.58026 9.48581 5.65836 9.40834L13.1584 1.90835C13.3153 1.75143 13.5281 1.66327 13.75 1.66327C13.9719 1.66327 14.1848 1.75143 14.3417 1.90835C14.4986 2.06527 14.5868 2.27809 14.5868 2.50001C14.5868 2.72193 14.4986 2.93476 14.3417 3.09168L7.42503 10L14.3417 16.9083C14.4198 16.9858 14.4818 17.078 14.5241 17.1795C14.5664 17.2811 14.5882 17.39 14.5882 17.5C14.5882 17.61 14.5664 17.7189 14.5241 17.8205C14.4818 17.922 14.4198 18.0142 14.3417 18.0917C14.2638 18.1689 14.1715 18.23 14.07 18.2715C13.9684 18.313 13.8597 18.334 13.75 18.3333Z" fill="#383838"/>
					</svg>
					</a>
					<h1 class="wp-heading-inline" id="everest-forms-coupons-create__heading">
						<?php
							/* translators: %s: title. */
							printf( esc_html__( '%s', 'everest-forms-pro' ), $title ); // phpcs:ignore
						?>
					</h1>
				</div>
				<hr class="wp-header-end" style="margin-top: 0; border-color: #E9E9E9; border-top: 0;">
				<?php settings_errors(); ?>
				<form class="evf-coupons-form" method="post">
					<?php if ( 'edit' === $action ) : ?>
						<input type="hidden" name="evf_coupon_update_id" value="<?php echo esc_attr( $post_id ); ?>">
					<?php endif; ?>
					<div class="evf-field">
						<label for="evf-coupon-name" class="evf-field-label"><?php esc_html_e( 'Coupon Name', 'everest-forms-pro' ); ?></label>
					<div class="evf-field-container">
						<?php
							/* translators: %s,%s: Coupon Title. */
							$value = isset( $_POST['coupons']['title'] ) ? sprintf( esc_attr__( '%s', 'everest-forms-pro' ), sanitize_text_field( wp_unslash( $_POST['coupons']['title'] ) ) ) : ( isset( $data['title'] ) ? sprintf( esc_attr__( '%s', 'everest-forms-pro' ), $data['title'] ): '' );  // phpcs:ignore
						?>
						<input class="evf-coupon-name" type="text" name="coupons[title]" id="evf-coupon-name" value="<?php echo $value; // phpcs:ignore ?>">
						<?php if ( ! empty( $errors->get_error_message( 'evf_coupon_title' ) ) ) : ?>
						<label class="evf-error"><?php echo esc_html( $errors->get_error_message( 'evf_coupon_title' ) ); ?></label>
						<?php endif; ?>
					</div>
					</div>
					<div class="evf-field">
						<label for="evf-coupon-code" class="evf-field-label"><?php esc_html_e( 'Coupon Code', 'everest-forms-pro' ); ?></label>
					<div class="evf-field-container">
						<?php
						/* translators: %s,%s: Coupon Code. */
						$value = isset( $_POST['coupons']['code'] ) ? sprintf( esc_attr__( '%s', 'everest-forms-pro' ), sanitize_text_field( wp_unslash( $_POST['coupons']['code'] ) ) ) : (  isset( $data['code'] ) ? sprintf( esc_attr__( '%s', 'everest-forms-pro' ), $data['code'] ): '' ); // phpcs:ignore
						?>
						<input class="evf-coupon-code" type="text" name="coupons[code]" id="evf-coupon-code" value="<?php echo $value; // phpcs:ignore ?>">
						<?php if ( ! empty( $errors->get_error_message( 'evf_coupon_code' ) ) ) : ?>
						<label class="evf-error"><?php echo esc_html( $errors->get_error_message( 'evf_coupon_code' ) ); ?></label>
						<?php endif; ?>
					</div>
					</div>
					<div class="evf-field">
						<label for="" class="evf-field-label"><?php esc_html_e( 'Discount Type', 'everest-forms-pro' ); ?></label>
						<label class="evf-coupon-discount__type">
							<?php

							$checked = esc_attr( 'checked' );

							if ( isset( $_POST['coupons']['discount_type'] ) ) {  // phpcs:ignore WordPress.Security.NonceVerification
								$checked = 'fixed' === sanitize_text_field( wp_unslash( $_POST['coupons']['discount_type'] ) ) ? esc_attr( 'checked' ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
							} elseif ( isset( $data['type'] ) ) {
								$checked = 'fixed' === $data['type'] ? esc_attr( 'checked' ) : '';
							}

							?>
							<input class="evf-coupon-discount-type-fixed" type="radio" name="coupons[discount_type]" value="fixed" id="evf-coupon-discount-type-fixed" <?php echo $checked; // phpcs:ignore  ?>>
							<?php esc_html_e( 'Fixed Discount', 'everest-forms-pro' ); ?>
							<p class="evf-coupon-discount__desc">
								<?php esc_html_e( 'This involves a specific amount of money deducted from the original price of a product.', 'everest-forms-pro' ); ?>
							</p>
						</label>

						<label class="evf-coupon-discount__type">
							<?php

							$checked = '';

							if ( isset( $_POST['coupons']['discount_type'] ) ) {  // phpcs:ignore WordPress.Security.NonceVerification
								$checked = 'percent' === sanitize_text_field( wp_unslash( $_POST['coupons']['discount_type'] ) ) ? esc_attr( 'checked' ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
							} elseif ( isset( $data['type'] ) ) {
								$checked = 'percent' === $data['type'] ? esc_attr( 'checked' ) : '';
							}

							?>
							<input class="evf-coupon-discount-type-percentage" type="radio" name="coupons[discount_type]" value="percent" id="evf-coupon-discount-type-percent" <?php echo $checked; // phpcs:ignore ?>>
							<?php esc_html_e( 'Percent Based Discount', 'everest-forms-pro' ); ?>
							<p class="evf-coupon-discount__desc">
								<?php esc_html_e( 'This is a discount calculated as a percentage of the original price of the product.', 'everest-forms-pro' ); ?>
							</p>
						</label>
					</div>
					<div class="evf-field">
						<label for="evf-coupon-discount-amount" class="evf-field-label"><?php esc_html_e( 'Discount Amount/Percent', 'everest-forms-pro' ); ?></label>
						<div class="evf-field-container">
						<?php
							/* translators: %s,%s: Discount Amount. */
							$value = isset( $_POST['coupons']['discount_amount'] ) ? sprintf( esc_attr__( '%s', 'everest-forms-pro' ), sanitize_text_field( wp_unslash( $_POST['coupons']['discount_amount'] ) ) ): (  isset( $data['amount'] ) ? sprintf( esc_attr__( '%s', 'everest-forms-pro' ), $data['amount'] ): '' ); // phpcs:ignore
						?>
						<input class="evf-coupon-discount-amount" type="text" name="coupons[discount_amount]" id="evf-coupon-discount-amount" value="<?php echo $value; // phpcs:ignore ?>">
						<?php if ( ! empty( $errors->get_error_message( 'evf_coupon_amount' ) ) ) : ?>
						<label class="evf-error"><?php echo esc_html( $errors->get_error_message( 'evf_coupon_amount' ) ); ?></label>
						<?php endif; ?>
						</div>
					</div>
					<div class="evf-field">
						<label for="evf-coupon-start-date" class="evf-field-label"><?php esc_html_e( 'Start Date', 'everest-forms-pro' ); ?></label>
						<div class="evf-field-container">
						<?php /* translators: %s,%s: Start Date. */ ?>
						<input class="evf-coupon-start-date" type="date" name="coupons[start_date]" id="evf-coupon-start-date" value="<?php isset( $_POST['coupons']['start_date'] ) ? printf( esc_attr__( '%s', 'everest-forms-pro' ), wp_unslash( $_POST['coupons']['start_date'] ) )  : (  isset( $data['start_date'] ) ? printf( esc_attr__( '%s', 'everest-forms-pro' ), $data['start_date'] ) : '' ); // phpcs:ignore ?>">
						<?php if ( ! empty( $errors->get_error_message( 'evf_coupon_start_date' ) ) ) : ?>
						<label class="evf-error"><?php echo esc_html( $errors->get_error_message( 'evf_coupon_start_date' ) ); ?></label>
						<?php endif; ?>
						</div>
					</div>
					<div class="evf-field">
						<label for="evf-coupon-end-date" class="evf-field-label"><?php esc_html_e( 'End Date', 'everest-forms-pro' ); ?></label>
						<div class="evf-field-container">
						<?php /* translators: %s,%s: End Date. */ ?>
						<input class="evf-coupon-end-date" type="date" name="coupons[end_date]" id="evf-coupon-end-date" value="<?php isset( $_POST['coupons']['end_date'] ) ? printf( esc_attr__( '%s', 'everest-forms-pro' ), wp_unslash( $_POST['coupons']['end_date'] ) ) : ( isset( $data['end_date'] ) ? printf( esc_attr__( '%s', 'everest-forms-pro' ), $data['end_date'] ) : '' ); // phpcs:ignore ?>">
						<?php if ( ! empty( $errors->get_error_message( 'evf_coupon_end_date' ) ) ) : ?>
						<label class="evf-error"><?php echo esc_html( $errors->get_error_message( 'evf_coupon_end_date' ) ); ?></label>
						<?php endif; ?>
						</div>
					</div>
					<div class="evf-field">
						<label class="evf-field-label"><?php esc_html_e( 'Status', 'everest-forms-pro' ); ?></label>
					<div class="evf-field-container">
						<div style="display: flex;">
						<label>
							<?php
								$checked = esc_attr( 'checked' );

							if ( isset( $_POST['coupons']['status'] ) ) {  // phpcs:ignore WordPress.Security.NonceVerification
								$checked = 'active' === sanitize_text_field( wp_unslash( $_POST['coupons']['status'] ) ) ? esc_attr( 'checked' ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
							} elseif ( isset( $data['status'] ) ) {
								$checked = '1' === $data['status'] ? esc_attr( 'checked' ) : '';
							}

							?>
							<input class="evf-coupon-status-active" type="radio" name="coupons[status]" value="active" id="evf-coupon-status-active" <?php echo $checked; // phpcs:ignore ?>>
							<?php esc_html_e( 'Active', 'everest-forms-pro' ); ?>
						</label>
						<label>
						<?php

						$checked = '';

						if ( isset( $_POST['coupons']['status'] ) ) {  // phpcs:ignore WordPress.Security.NonceVerification
							$checked = 'inactive' === sanitize_text_field( wp_unslash( $_POST['coupons']['status'] ) ) ? esc_attr( 'checked' ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
						} elseif ( isset( $data['status'] ) ) {
							$checked = '0' === $data['status'] ? esc_attr( 'checked' ) : '';
						}

						?>
							<input class="evf-coupon-status-inactive" type="radio" name="coupons[status]" value="inactive" id="evf-coupon-status-inactive" <?php echo $checked; // phpcs:ignore  ?>>
						<?php esc_html_e( 'Inactive', 'everest-forms-pro' ); ?>
						</label>
						</div>
						<?php if ( ! empty( $errors->get_error_message( 'evf_coupon_status' ) ) ) : ?>
						<label class="evf-error"><?php echo esc_html( $errors->get_error_message( 'evf_coupon_status' ) ); ?></label>
						<?php endif; ?>
					</div>
					</div>
					<div class="evf-field">
						<label class="evf-field-label"><?php esc_html_e( 'Applicable Forms', 'everest-forms-pro' ); ?></label>
						<?php
						$forms = evf_get_all_forms( true );
						if ( ! empty( $forms ) ) {
							echo '<select class="evf-enhanced-select everest-forms-pro-applicable-forms" style="min-width: 350px;" name="coupons[applicable_form_ids][]" data-placeholder="' . esc_attr__( 'Select Form(s)', 'everest-forms-pro' ) . '" multiple>';
							$selected = '';

							foreach ( $forms as $id => $form ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride
								if ( isset( $_POST['coupons']['applicable_form_ids'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
									$selected = in_array( absint( $id ), array_map( 'absint', $_POST['coupons']['applicable_form_ids'] ), true ) ? esc_attr( 'selected' ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
								} elseif ( isset( $data['applicable_forms'] ) ) {
									$selected = in_array( absint( $id ), $data['applicable_forms'], true ) ? esc_attr( 'selected' ) : '';
								}

								echo '<option value="' . esc_attr( $id ) . '" ' . $selected . '>' . esc_html( $form ) . '</option>'; // phpcs:ignore
							}
							echo '</select>';
						} else {
							echo '<p>' . esc_html__( 'You need to create a form before you can apply coupon.', 'everest-forms-pro' ) . '</p>';
						}
						?>
					</div>
					<?php wp_nonce_field( 'evf_coupons_nonce', 'evf-coupons-nonce' ); ?>
					<div id="publishing-action" class="evf-coupons-submit">
						<input type="submit" class="everest-forms-btn everest-forms-btn-primary everest-forms-coupons__save" value="<?php echo 'insert' === $action ? esc_attr__( 'Save', 'everest-forms-pro' ) : esc_attr__( 'Update Coupon', 'everest-forms-pro' ); ?>" name="<?php echo 'insert' === $action ? esc_attr( 'evf-coupon-insert' ) : esc_attr( 'evf-coupon-update' ); ?>">
						<a href="<?php echo esc_url( admin_url( 'admin.php?page=evf-coupons' ) ); ?>" class="everest-forms-coupon__cancel everest-forms-btn"><?php esc_html_e( 'Cancel', 'everest-forms-pro' ); ?></a>
					</div>
				</form>
			</div>
		</div>
		<?php
	}
}
