<?php

/**
 * The Template for displaying spending limits tab in my account
 *
 * @package WooCommerce spending limits
 **/
defined( 'ABSPATH' ) || exit;
$user_id = get_current_user_id();
$options = get_option( 'slfw_options' );
$enable_limit_for_all = ( isset( $options['enable_limit_for_all'] ) && '1' === $options['enable_limit_for_all'] ? true : false );
$enable_limit_for_all_override = ( isset( $options['enable_limit_for_all_override'] ) && '1' === $options['enable_limit_for_all_override'] ? true : false );
$lock_until = get_user_meta( $user_id, 'spending_limit_lock_until', true );
$spend_limit_amount = floatval( get_user_meta( $user_id, 'spend_limit_amount', true ) ) ?? false;
$spend_limit_period = get_user_meta( $user_id, 'spend_limit_period', true ) ?? false;
$spend_limit_period_custom = get_user_meta( $user_id, 'spend_limit_period_custom', true ) ?? false;
$spend_limit_period_custom_time = get_user_meta( $user_id, 'spend_limit_period_custom_time', true ) ?? false;
$dateformat = get_option( 'date_format' );
$timeformat = get_option( 'time_format' );
$spend_limit_period_text = $spend_limit_period;
?>
<div limit-wraper>
	<h3><?php 
esc_html_e( 'Spending Limit', 'spending-limits-for-woocommerce' );
?></h3>
	<p><?php 
esc_html_e( 'Limit how much you can spend over specific period. Once limit is set, you can only reduce it - you can never increase or remove it. If you need help with removing limits please contact customer support.', 'spending-limits-for-woocommerce' );
?></p>
	<?php 
if ( $spend_limit_amount && $spend_limit_period || $enable_limit_for_all ) {
    ?>
		<div class="woocommerce-info">
		<?php 
    /* translators: 1: amount 2: period */
    printf( esc_html__( 'Your spending limit is set to %1$s per %2$s.', 'spending-limits-for-woocommerce' ), wp_kses_post( wc_price( $spend_limit_amount ) ), esc_html( $spend_limit_period_text ) ) . '.</div>';
    ?>
		</div>
		<?php 
}
?>
		<form name="spending-limit" method="post" action="">
			<div class="input-range-wrapper">
				<label for="spend_limit_amount"><?php 
esc_html_e( 'Spending Limit Amount', 'spending-limits-for-woocommerce' );
?></label>
				<input type="range" min="1" max="<?php 
echo ( !empty( $spend_limit_amount ) ? intval( $spend_limit_amount ) : '5000' );
?>" value="<?php 
echo intval( $spend_limit_amount );
?>"  class="slider" id="spending_limit_range">
				<?php 
echo get_woocommerce_currency_symbol();
// phpcs:ignore
?>
				<input type="number" name="spend_limit_amount" id="spend_limit_amount" value="<?php 
echo intval( $spend_limit_amount ) ?? '5000';
?>" >
			</div>

			<div>
				<h4><?php 
esc_html_e( 'Spending Limit Period', 'spending-limits-for-woocommerce' );
?></h4>
				<p>
				<input type="radio" id="spend_limit_period1" name="spend_limit_period" value="day" <?php 
checked( 'day' === $spend_limit_period );
?>>
				<label for="spend_limit_period1"><?php 
esc_html_e( 'Per Day', 'spending-limits-for-woocommerce' );
?></label><br>
				<input type="radio" id="spend_limit_period2" name="spend_limit_period" value="week" <?php 
checked( 'week' === $spend_limit_period );
?>>
				<label for="spend_limit_period2"><?php 
esc_html_e( 'Per Week', 'spending-limits-for-woocommerce' );
?></label><br>
				<input type="radio" id="spend_limit_period3" name="spend_limit_period" value="month" <?php 
checked( 'month' === $spend_limit_period );
?>>
				<label for="spend_limit_period3"><?php 
esc_html_e( 'Per Month', 'spending-limits-for-woocommerce' );
?></label><br>
				<input type="radio" id="spend_limit_period4" name="spend_limit_period" value="year" <?php 
checked( 'year' === $spend_limit_period );
?>>
				<label for="spend_limit_period4"><?php 
esc_html_e( 'Per Year', 'spending-limits-for-woocommerce' );
?></label><br>
				<?php 
?>
				<br>
					</p>
			</div>
			<button type="submit" name="spend_limit_submit" class="button"><?php 
esc_html_e( 'Limit Spending', 'spending-limits-for-woocommerce' );
?></button>
			<?php 
wp_nonce_field( 'spend_limit_submit', 'spend_limit_submit_nonce' );
?>
		</form>

</div>

<div class="lock-wraper">
	<h3><?php 
esc_html_e( 'Lock Account', 'spending-limits-for-woocommerce' );
?></h3>
	<p><?php 
esc_html_e( 'If you feel the need to take short break from buying on this webshop please choose lockout period below. This will prevent you from being able to make purchase for selected time period.', 'spending-limits-for-woocommerce' );
?></p>
	<?php 
if ( $lock_until && (strtotime( $lock_until ) > time() || 'permanent' === $lock_until) ) {
    ?>
	<div class="woocommerce-error">
		<?php 
    if ( 'permanent' === $lock_until ) {
        esc_html_e( 'Account is locked permanently.', 'spending-limits-for-woocommerce' );
    } else {
        printf( 
            // translators: date.
            esc_html__( 'Account is locked until %s.', 'spending-limits-for-woocommerce' ),
            esc_html( date_i18n( $dateformat, strtotime( $lock_until ) ) . ' ' . date_i18n( $timeformat, strtotime( $lock_until ) ) )
         );
    }
    ?>
	</div>
		<?php 
} else {
    ?>
		<form name="lock-length" method="post">
			<p><?php 
    esc_html_e( 'Lock Length', 'spending-limits-for-woocommerce' );
    ?></p>
			<input type="radio" id="locklength1" name="locklength" value="1w" >
			<label for="locklength1"><?php 
    esc_html_e( '1 Week', 'spending-limits-for-woocommerce' );
    ?></label><br>
			<input type="radio" id="locklength2" name="locklength" value="4w" >
			<label for="locklength2"><?php 
    esc_html_e( '4 Week', 'spending-limits-for-woocommerce' );
    ?></label><br>
			<input type="radio" id="locklength3" name="locklength" value="12w" >
			<label for="locklength3"><?php 
    esc_html_e( '12 Week', 'spending-limits-for-woocommerce' );
    ?></label><br>
			<input type="radio" id="locklength4" name="locklength" value="1y">
			<label for="locklength4"><?php 
    esc_html_e( '1 Year', 'spending-limits-for-woocommerce' );
    ?></label><br>
			<input type="radio" id="locklength5" name="locklength" value="permanent" >
			<label for="locklength5"><?php 
    esc_html_e( 'Self exclude permanently', 'spending-limits-for-woocommerce' );
    ?></label><br>
			<button type="submit" name="lock_length_submit" class="button"><?php 
    esc_html_e( 'Lock Account', 'spending-limits-for-woocommerce' );
    ?></button>
			<?php 
    wp_nonce_field( 'lock_length_submit', 'lock_length_submit_nonce' );
    ?>
		</form>

		<?php 
}
?>
</div>
