<?php
/**
 * Created by PhpStorm.
 * User: Villatheme-Thanh
 * Date: 29-03-19
 * Time: 8:44 AM
 */

namespace WACVP\Inc\Settings;

use WACVP\Inc\Check_Update;
use WACVP\Inc\Data;
use WACVP\Inc\Plugin_Updater;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Admin_Settings {

	public static $params;
	public static $data;
	protected static $instance = null;

	public function __construct() {
        add_action( 'init', array( $this, 'check_plugin_update' ), 5 );
		add_action( 'admin_init', array( $this, 'save_params' ) );
		add_action( 'admin_menu', array( $this, 'admin_menu_page' ), 40 );
	}

	public static function get_instance() {
		if ( null == self::$instance ) {
			self::$instance = new self;
		}

		return self::$instance;
	}

	public function admin_menu_page() {
		add_submenu_page(
			'wacv_sections',
			esc_html__( 'Settings', 'woo-abandoned-cart-recovery' ),
			esc_html__( 'Settings', 'woo-abandoned-cart-recovery' ),
			apply_filters( 'wacv_change_role', 'manage_woocommerce' ),
			'wacv_settings',
			array( $this, 'display_settings' )
		);
	}

	public function display_settings() {
		Data::get_update_params();
		do_action( 'wacv_before_settings' );
		?>
        <div id="wacv-admin-settings">
            <div class="wacv-header">
                <h1 class="vi-ui header"><?php esc_html_e( 'Settings', 'woo-abandoned-cart-recovery' ) ?></h1>
            </div>

            <div id="wacv-settings-container">
                <form class="vi-ui form" method="post">
					<?php echo ent2ncr( self::set_nonce() );// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
                    <div class="vi-ui top attached tabular menu">
                        <a class="active item"
                           data-tab="general"><?php esc_html_e( 'General', 'woo-abandoned-cart-recovery' ) ?></a>
                        <a class="item"
                           data-tab="email"><?php esc_html_e( 'Email', 'woo-abandoned-cart-recovery' ) ?></a>
                        <a class="item"
                           data-tab="popup"><?php esc_html_e( 'Email popup', 'woo-abandoned-cart-recovery' ) ?></a>
                        <a class="item" data-tab="sms"><?php esc_html_e( 'SMS', 'woo-abandoned-cart-recovery' ) ?></a>
                        <a class="item"
                           data-tab="update"><?php esc_html_e( 'Update', 'woo-abandoned-cart-recovery' ) ?></a>
                    </div>
					<?php
					General_Settings::get_instance()->setting_page();
					Email_Settings::get_instance()->setting_page();
					SMS_Settings::get_instance()->setting_page();
					Email_Popup_Settings::get_instance()->setting_page();
					Update::get_instance()->setting_page();
					?>
                    <div class="">
                        <button type="submit" class="vi-ui button labeled icon primary wacv-btn wacv-save-settings"
                                name="action"
                                value="save_params">
                            <i class="send icon"></i>
							<?php esc_html_e( 'Save settings', 'woo-abandoned-cart-recovery' ) ?>
                        </button>
                        <button type="submit" class="vi-ui button labeled icon  wacv-btn wacv-save-settings villatheme_check_key"
                                name="action"
                                value="save_n_check_key">
                            <i class="send icon"></i>
							<?php esc_html_e( 'Save & Check Key', 'woo-abandoned-cart-recovery' ) ?>
                        </button>
                    </div>
                </form>
            </div>
        </div>
		<?php
		do_action( 'villatheme_support_woocommerce-abandoned-cart-recovery', get_current_screen()->id );
	}

	protected static function set_nonce() {
		return wp_nonce_field( 'woo_abandoned_settings', '_woo_abandoned_cart_nonce' );
	}


	public static function get_field( $field ) {

		if ( ! self::$data ) {
			self::$data = Data::get_params();
		}

		return isset( self::$data[ $field ] ) ? self::$data[ $field ] : '';
	}

	public function get_categories() {
		$option = array();
		$args   = array(
			'taxonomy'   => "product_cat",
			'hide_empty' => 0,
			'orderby'    => 'name',
		);

		$categories = get_terms( $args );
		if ( count( $categories ) > 0 ) {
			foreach ( $categories as $category ) {
				$option[ $category->term_id ] = $category->name;
			}
		}

		return $option;
	}

	public function text_option( $field_name, $label = '', $placeholder = '', $multi = false, $multilingual = '', $explain = '', $class_custom = '' ) {
		$set_name = $this->set_field( $field_name, $multi );
		$class    = 'wacv-' . str_replace( '_', '-', $field_name );
        if ( $class_custom ) {
            $class .= ' ' . $class_custom . ' ';
        }
		?>
        <tr>
            <td class="col-1">
                <label class=""><?php echo esc_html( $label ) ?></label>
	            <?php if ( $explain )
		            echo '<span class="wacv-explain-group" data-tooltip="' . esc_attr( $explain ) . '" data-variation="wide"><i class="question circle icon "></i></span>' ?>
            </td>
            <td class="col-2">
                <div>
                    <input type="text"
                           name="<?php echo esc_attr( $set_name ) ?>"
                           value="<?php echo esc_html( stripslashes( self::get_field( $field_name ) ) ) ?>"
                           class="<?php echo esc_attr( $class ) ?> vlt-input vlt-border vlt-none-shadow vlt-round "
                           placeholder="<?php echo esc_attr( $placeholder ) ?>">
					<?php if ( $multilingual ) {
						if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
							global $sitepress;
							$default_lang = $sitepress->get_default_language();
							$languages    = $langs = icl_get_languages( 'skip_missing=N&orderby=KEY&order=DIR&link_empty_to=str' );
							if ( count( $languages ) ) {
								foreach ( $languages as $key => $language ) {
									if ( $key == $default_lang ) {
										continue;
									}
									$field_name_mlg = $field_name . '_' . $key;
									$class_mlg      = 'wacv-' . str_replace( '_', '-', $field_name_mlg );
									$set_name_mlg   = $this->set_field( $field_name_mlg, $multi );
									?>
                                    <p class="wacv-mlg-label"><?php echo esc_html( $language['native_name'] . ':' ) ?></p>
                                    <input type="text"
                                           name="<?php echo esc_attr( $set_name_mlg ) ?>"
                                           value="<?php echo esc_html( stripslashes( self::get_field( $field_name_mlg ) ) ) ?>"
                                           class="<?php echo esc_attr( $class_mlg ) ?> vlt-input vlt-border vlt-none-shadow vlt-round "
                                           placeholder="<?php echo esc_attr( $placeholder ) ?>">
									<?php
								}
							}
						} elseif ( class_exists( 'Polylang' ) ) {
							$languages = pll_languages_list();
							foreach ( $languages as $language ) {
								$default_lang = pll_default_language( 'slug' );

								if ( $language == $default_lang ) {
									continue;
								}
//			                    $content_multi = html_entity_decode( self::get_field( $field_name . '_' . $language ) );
								$field_name_mlg = $field_name . '_' . $language;
								$class_mlg      = 'wacv-' . str_replace( '_', '-', $field_name_mlg );
								$set_name_mlg   = $this->set_field( $field_name_mlg, $multi );
								?>
                                <p class="wacv-mlg-label"><?php echo esc_html( $language . ':' ) ?></p>
                                <input type="text"
                                       name="<?php echo esc_attr( $set_name_mlg ) ?>"
                                       value="<?php echo esc_html( stripslashes( self::get_field( $field_name_mlg ) ) ) ?>"
                                       class="<?php echo esc_attr( $class_mlg ) ?> vlt-input vlt-border vlt-none-shadow vlt-round "
                                       placeholder="<?php echo esc_attr( $placeholder ) ?>">
								<?php
							}
						}
					} ?>
                </div>
            </td>
            <td class="col-3"></td>
        </tr>
		<?php
	}

	public function set_field( $name, $multi = false ) {
		return $multi ? "wacv_params[$name][]" : "wacv_params[$name]";
	}

	public function text_option_read_only( $value, $label = '', $name = '', $class = '', $sub_icon = '', $tooltip = '', $multi = false ) {
		?>
        <tr>
            <td class="col-1">
                <label class=""><?php echo esc_html( $label ) ?></label>
            </td>
            <td class="col-2">
                <div class="wacv-input-readonly-block">

                    <input type="text" value="<?php echo esc_attr( $value ) ?>" readonly="readonly"
                           class="wacv-readonly <?php echo esc_attr( $class ) . '-input' ?>"
						<?php if ( $name )
							echo "name='wacv_params[" . esc_attr( $name ) . "]'" ?> >
                    <span class="wacv-copy-icon">
                        <i class="copy outline icon"></i>
                    </span>


                </div>
            </td>
            <td class="col-3">
				<?php if ( $sub_icon ) { ?>
                    <span class="wacv-suffix-icon <?php echo esc_attr( $class ) ?>"
                          data-tooltip="<?php echo esc_attr( $tooltip ) ?>">
                             <i class="<?php echo esc_attr( $sub_icon ) ?> icon"></i>
                        </span>
				<?php } ?>
            </td>
        </tr>
		<?php
	}

	public function number_option( $field_name, $label = '', $explain = '', $units = '', $multi = false, $min = 1, $max = 1000, $required = true ) {
		$set_name = $this->set_field( $field_name, $multi );
		$set_unit = $this->set_field( $field_name . '_unit', $multi );
		$class    = 'wacv-' . str_replace( '_', '-', $field_name );
		$col      = ! empty( $units ) ? 11 : 12;
		?>
        <tr>
            <td class="col-1">
                <label><?php echo esc_html( $label ) ?></label>
				<?php if ( $explain )
					echo '<span class="wacv-explain-group" data-tooltip="' . esc_attr( $explain ) . '" data-variation="wide"><i class="question circle icon "></i></span>' ?>
            </td>
            <td class="col-2">
                <div class="vlt-col s<?php echo esc_attr( $col ) ?>">
                    <input type="number" <?php echo $required ? 'required' : '' ?>
                           min="<?php echo esc_attr( $min ) ?>" <?php echo $max != 'unset' ? 'max="' . esc_attr( $max ) . '"' : ''; ?>
                           name="<?php echo esc_attr( $set_name ) ?>" <?php echo $max == 'unset' ? 'step="any"' : ''; ?>
                           value="<?php echo esc_attr( self::get_field( $field_name ) ) ?>"
                           class="<?php echo esc_attr( $class ) ?>">
                </div>

            </td>
            <td>
                <div>
					<?php
					if ( ! empty( $units ) && is_array( $units ) ) {
						echo "<select name='" . esc_attr( $set_unit ) . "' class='wacv-unit'>";
						foreach ( $units as $unit ) {
							$selected = self::get_field( $field_name . '_unit' ) == $unit ? 'selected' : '';
							echo "<option " . esc_attr( $selected ) . " >" . esc_html( $unit ) . "</option>";
						}
						echo "</select>";
					} elseif ( ! empty( $units ) ) {
						echo "<div type='text' class='wacv-unit'>" . esc_html( $units ) . "</div>";
					}
					?>
                </div>
            </td>
        </tr>
		<?php
	}

	public function date_time_option( $field_name, $label = '', $explain = '', $units = '', $multi = false, $min = 1, $max = 1000, $required = false ) {
		$set_name = $this->set_field( $field_name, $multi );
		$set_unit = $this->set_field( $field_name . '_unit', $multi );
		$class    = 'wacv-' . str_replace( '_', '-', $field_name );
		$col      = ! empty( $units ) ? 11 : 12;
		?>
        <tr>
            <td class="col-1">
                <label><?php echo esc_html( $label ) ?></label>
				<?php if ( $explain )
					echo '<span class="wacv-explain-group" data-tooltip="' . esc_attr( $explain ) . '" data-variation="wide"><i class="question circle icon "></i></span>' ?>
            </td>
            <td class="col-2">
                <div class="vlt-col s<?php echo esc_attr( $col ) ?>">
                    <input type="date" <?php echo $required ? 'required' : '' ?>
                           name="<?php echo esc_attr( $set_name ) ?>"
                           value="<?php echo esc_attr( self::get_field( $field_name ) ) ?>"
                           class="<?php echo esc_attr( $class ) ?>">
                </div>

            </td>
        </tr>
		<?php
	}

	public function textarea_option( $field_name, $label = '', $unit = '', $multi = false ) {
		$set_name = $this->set_field( $field_name, $multi );
		$class    = 'wacv-' . str_replace( '_', '-', $field_name );
		?>
        <div>
            <div>
                <label><?php echo esc_html( $label ) ?></label>
            </div>
            <div>
                <div>
                    <textarea rows="3"
                              class="<?php echo esc_attr( $class ) ?> vlt-textarea vlt-none-shadow vlt-round vlt-border"
                              name="<?php echo esc_attr( $set_name ) ?>"><?php echo esc_textarea( ( self::get_field( $field_name ) ) ) ?></textarea>
                </div>
                <div><?php echo esc_attr( $unit ) ?><span> </span></div>
            </div>
        </div>
		<?php
	}

	public function select_option( $field_name, $option = array(), $label = '', $explain = '', $units = '', $multi = false ) {
		$set_name   = $this->set_field( $field_name, $multi );
		$class      = 'wacv-' . str_replace( '_', '-', $field_name );
		$saved_data = self::get_field( $field_name );
		if ( $field_name == 'tracking_role_exclude' ) {
		    $class .= ' wacv-dropdown vi-ui dropdown fluid';
        }
		?>
        <tr>
            <td class="col-1">
                <label><?php echo esc_html( $label ) ?></label>
				<?php if ( $explain )
					echo '<span class="wacv-explain-group" data-tooltip="' . esc_attr( $explain ) . '" data-variation="wide"><i class="question circle icon "></i></span>' ?>
            </td>
            <td class="col-2">
                <div>
                    <select <?php echo $multi ? 'multiple' : '' ?> class="<?php echo esc_attr( $class ) ?>"
                                                                   name="<?php echo esc_attr( $set_name ) ?>">
						<?php
						if ( count( $option ) > 0 && is_array( $option ) ) {
							if ( 'def_phone_country' == $field_name ) {
								$pre_selected = $saved_data == 0 ? 'selected' : '';
								echo "<option value='0' " . esc_attr( $pre_selected ) . ">" . esc_html__( 'By locate IP' ) . "</option>";
							}
							foreach ( $option as $value => $view ) {
								if ( 'def_phone_country' == $field_name ) {
									if ( empty( $view ) ) {
										continue;
									}
									if ( is_array( $view ) ) {
										if ( isset( $view[0] ) ) {
											if ( is_array( $saved_data ) ) {
												$selected = in_array( $view[0], $saved_data ) ? 'selected' : '';
											} else {
												$selected = $saved_data == $view[0] ? 'selected' : '';
											}
											echo "<option value='" . esc_attr( $view[0] ) . "' " . esc_attr( $selected ) . ">" . esc_html( $value ) . esc_html( ' (' . $view[0] . ')' ) . "</option>";
										}
									} else {
										if ( is_array( $saved_data ) ) {
											$selected = in_array( $view, $saved_data ) ? 'selected' : '';
										} else {
											$selected = $saved_data == $view ? 'selected' : '';
										}
										echo "<option value='" . esc_attr( $view ) . "' " . esc_attr( $selected ) . ">" . esc_html( $value ) . esc_html( ' (' . $view . ')' ) . "</option>";
									}
								} else {
									if ( is_array( $saved_data ) ) {
										$selected = in_array( $value, $saved_data ) ? 'selected' : '';
									} else {
										$selected = $saved_data == $value ? 'selected' : '';
									}
									echo "<option value='" . esc_attr( $value ) . "' " . esc_attr( $selected ) . ">" . esc_html( $view ) . "</option>";
								}
							}
						} ?>
                    </select>
                </div>
            </td>
            <td class="col-3"></td>
        </tr>
		<?php
	}

	public function checkbox_option( $field_name, $label = '', $explain = '', $subffix = '' ) {
		$set_name = $this->set_field( $field_name );
		$class    = 'wacv-' . str_replace( '_', '-', $field_name );
		?>
        <tr class="">
            <td class="col-1">
                <label><?php echo esc_html( $label ) ?> </label>
				<?php if ( $explain )
					echo '<span class="wacv-explain-group" data-tooltip="' . esc_attr( $explain ) . '" data-variation="wide"><i class="question circle icon "></i></span>' ?>
            </td>
            <td class="col-2">
                <div class="vi-ui toggle checkbox">
                    <input type="checkbox" <?php checked( self::get_field( $field_name ), 1 ) ?>
                           value="1"
                           name="<?php echo esc_attr( $set_name ) ?>"
                           class="<?php echo esc_attr( $class ) ?>">
                    <label><?php echo esc_html( $subffix ) ?></label>
                </div>
            </td>
            <td class="col-3"></td>
        </tr>
		<?php
	}

	public function time_button_option( $field_name, $label = '', $explain = '', $units = '', $is_save = false, $required = false, $button = '' ) {
		$set_name = $this->set_field( $field_name );
		$class    = 'wacv-' . str_replace( '_', '-', $field_name );
		$col      = ! empty( $units ) ? 11 : 12;
		?>
        <tr class="<?php echo esc_attr( $class . '-time-button' ) ?>">
            <td class="col-1">
                <label><?php echo esc_html( $label ) ?> </label>
				<?php if ( $explain )
					echo '<span class="wacv-explain-group" data-tooltip="' . esc_attr( $explain ) . '" data-variation="wide"><i class="question circle icon "></i></span>' ?>
            </td>
            <td class="col-2">
                <div class="vlt-col s<?php echo esc_attr( $col . ' ' . $class . '-input' ) ?>">
                    <input type="date" <?php echo $required ? 'required' : '' ?>
                           name="<?php echo $is_save ? esc_attr( $set_name ) : '' ?>"
                           value="<?php echo esc_attr( self::get_field( $field_name ) ) ?>"
                           class="<?php echo esc_attr( $class ) ?>">
                </div>
	            <?php if ( $button ) { ?>
                    <div class="<?php echo esc_attr( $class . '-button' ) ?>">
                        <div class="vi-ui small icon green button"><?php echo esc_html( $button ) ?></div>
                        <span class="wacv-button-notice wacv-button-notice-complete wacv-notice-hidden"><?php
		                    esc_html_e( 'Complete', 'woo-abandoned-cart-recovery' ) ?></span>
                        <span class="wacv-button-notice wacv-button-notice-error wacv-notice-hidden"><?php
		                    esc_html_e( 'Something went wrong, please try again later', 'woo-abandoned-cart-recovery' ) ?></span>
                    </div>
	            <?php } ?>
            </td>
            <td class="col-3"></td>
        </tr>
		<?php
	}

	public function editor_option( $field_name, $label = '', $explain = '', $suffix = '', $multilingual = '' ) {
		wp_enqueue_editor();
		$content = html_entity_decode( self::get_field( $field_name ) );
		?>
        <tr class="">
            <td class="col-1">
                <label><?php echo esc_html( $label ) ?> </label>
				<?php if ( $explain )
					echo '<span class="wacv-explain-group" data-tooltip="' . esc_attr( $explain ) . '" data-variation="wide"><i class="question circle icon "></i></span>' ?>
            </td>
            <td class="col-2">
				<?php wp_editor( $content, $field_name, [ 'editor_height' => 50, 'media_buttons' => false ] );
				if ( $multilingual ) {
					if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
						global $sitepress;
						$default_lang = $sitepress->get_default_language();
						$languages    = $langs = icl_get_languages( 'skip_missing=N&orderby=KEY&order=DIR&link_empty_to=str' );
						if ( count( $languages ) ) {
							foreach ( $languages as $key => $language ) {
								if ( $key == $default_lang ) {
									continue;
								}
								$content_multi = html_entity_decode( self::get_field( $field_name . '_' . $key ) );
								?>
                                <p class="wacv-mlg-label"><?php echo esc_html( $language['native_name'] . ':' ) ?></p>
								<?php wp_editor( $content_multi, $field_name . '_' . $key, [
									'editor_height' => 50,
									'media_buttons' => false
								] ); ?>
								<?php
							}
						}
					} elseif ( class_exists( 'Polylang' ) ) {
						$languages = pll_languages_list();
						foreach ( $languages as $language ) {
							$default_lang = pll_default_language( 'slug' );

							if ( $language == $default_lang ) {
								continue;
							}
							$content_multi = html_entity_decode( self::get_field( $field_name . '_' . $language ) );
							?>
                            <p class="wacv-mlg-label"><?php echo esc_html( $language . ':' ) ?></p>
							<?php wp_editor( $content_multi, $field_name . '_' . $language, [
								'editor_height' => 50,
								'media_buttons' => false
							] );
						}
					}
				} ?>
            </td>
            <td class="col-3"></td>
        </tr>
		<?php
	}

	public function date_option( $field_name, $label = '' ) {
		$set_name = $this->set_field( $field_name );
		$class    = 'wacv-' . str_replace( '_', '-', $field_name );
		?>
        <div>
            <div>
                <label><?php echo esc_html( $label ) ?></label>
            </div>
            <div>
                <div>
                    <input type="date"
                           value=""
                           name="<?php echo esc_attr( $set_name ) ?>"
                           class="<?php echo esc_attr( $class ) ?>">
                </div>
            </div>
        </div>
		<?php
	}

	public function color_field( $field_name, $label = '', $explain = '', $suffix = '' ) {
		$set_name = $this->set_field( $field_name );
		$class    = 'wacv-' . str_replace( '_', '-', $field_name );
		?>
        <tr class="">
            <td class="col-1">
                <label><?php echo esc_html( $label ) ?> </label>
				<?php if ( $explain )
					echo '<span class="wacv-explain-group" data-tooltip="' . esc_attr( $explain ) . '" data-variation="wide"><i class="question circle icon "></i></span>' ?>
            </td>
            <td class="col-2">
                <div class="">
                    <input type="text" value="<?php echo esc_attr( ( self::get_field( $field_name ) ) ) ?>"
                           name="<?php echo esc_attr( $set_name ) ?>"
                           class="<?php echo esc_attr( $class ) ?> wacv-color-picker">
                    <label><?php echo esc_html( $suffix ) ?></label>
                </div>
            </td>
            <td class="col-3"></td>
        </tr>
		<?php
	}

	public function template_popup( $field_name, $label = '', $explain = '', $suffix = '' ) {
		$set_name = $this->set_field( $field_name );
		$class    = 'wacv-' . str_replace( '_', '-', $field_name );
		$value    = self::get_field( $field_name );
		?>
        <tr class="">
            <td class="col-1">
                <label><?php echo esc_html( $label ) ?> </label>
				<?php if ( $explain )
					echo '<span class="wacv-explain-group" data-tooltip="' . esc_attr( $explain ) . '" data-variation="wide"><i class="question circle icon "></i></span>' ?>
            </td>
            <td class="col-2">
                <table class="<?php echo esc_attr( $class ) ?>">
                    <tr>
                        <td>
                            <div class="wacv-select-popup-temp <?php echo $value == 'template-1' ? 'selected' : ''; ?>">
                                <div class="template-1">
                                    <p class="title">Title</p>
                                    <p class="desc">Description</p>
                                    <p class="email">Email</p>
                                    <p class="atc-btn">Add to Cart</p>
                                </div>
                                <input type="radio" value="template-1" name="<?php echo esc_attr( $set_name ) ?>"
                                       class="<?php echo esc_attr( $class ) . '-input' ?>" <?php checked( $value, 'template-1' ) ?>>
                            </div>
                        </td>
                        <td>
                            <div class="wacv-select-popup-temp <?php echo $value == 'template-2' ? 'selected' : ''; ?>">
                                <div class="template-2">
                                    <p class="title">Title</p>
                                    <p class="desc">Description</p>
                                    <div style="display: flex">
                                        <p class="email">Email</p>
                                        <p class="atc-btn">Add to Cart</p>
                                    </div>
                                </div>
                                <input type="radio" value="template-2" name="<?php echo esc_attr( $set_name ) ?>"
                                       class="<?php echo esc_attr( $class ) . '-input' ?>" <?php checked( $value, 'template-2' ) ?>>
                            </div>
                        </td>
                        <!--                        <td></td>-->
                        <!--                        <td></td>-->
                        <label><?php echo esc_html( $suffix ) ?></label>
                    </tr>
                </table>
            </td>
            <td class="col-3"></td>
        </tr>

		<?php
	}

	public function guide_option( $field_name, $label, $guide_type ) {
		?>
        <tr class="">
            <td class="col-1">
                <label><?php echo esc_html( $label ) ?> </label>
            </td>
            <td class="col-2">
                <div class="wacv-guide-type-<?php echo esc_attr( $guide_type ) ?>"><?php switch ( $guide_type ) {
						case 'captcha':
							?>
                            <div class="wacv-guide-info-header"><?php esc_html_e( 'Get Google reCAPTCHA V2 Site and Secret key', 'woo-abandoned-cart-recovery' ); ?></div>
                            <div class="wacv-guide-info-details">
                                <ul class="wacv-guide-info-line">
                                    <li><?php echo sprintf( '%s %s %s ',
		                                    esc_html__( '1, Visit', 'woo-abandoned-cart-recovery' ),
		                                    wp_kses_post( '<a target="_blank" href="http://www.google.com/recaptcha/admin">' . esc_html__( 'page', 'woo-abandoned-cart-recovery' ) . '</a>' ),
		                                    esc_html__( 'to sign up for an API key pair with your Gmail account', 'woo-abandoned-cart-recovery' ) ); ?></li>
                                    <li><?php esc_html_e( '2, Choose captcha version (reCAPTCHA v2/reCAPTCHA v3)', 'woo-abandoned-cart-recovery' ) ?></li>
                                    <li><?php esc_html_e( '3, Fill in authorized domains', 'woo-abandoned-cart-recovery' ) ?></li>
                                    <li><?php esc_html_e( '4, Accept terms of service and click Register button', 'woo-abandoned-cart-recovery' ) ?></li>
                                    <li><?php esc_html_e( '5, Copy and paste the site and secret key into the above field', 'woo-abandoned-cart-recovery' ) ?></li>
                                </ul>
                            </div>
							<?php break;
						default:
					} ?>
                </div>
            </td>
            <td class="col-3"></td>
        </tr>
		<?php
	}

	public function send_message_rules_settings( $field, $multilingual = '', $explain = '' ) {
		$data = self::get_field( $field );
		?>
        <tr>
            <td class="col-1">
                <label><?php esc_html_e( 'Rules', 'woo-abandoned-cart-recovery' ) ?></label>
				<?php if ( $explain )
					echo '<span class="wacv-explain-group" data-tooltip="' . esc_attr( $explain ) . '" data-variation="wide"><i class="question circle icon "></i></span>' ?>
            </td>

            <td class="col-2">
                <table class="wacv-<?php echo esc_attr( $field ) ?>-table vi-ui celled table">
                    <thead>
                    <tr>
                        <th class="cols-1"><?php esc_html_e( 'Send after', 'woo-abandoned-cart-recovery' ); ?></th>
                        <th class="cols-2"><?php esc_html_e( 'Unit', 'woo-abandoned-cart-recovery' ); ?></th>
                        <th class="cols-3"><?php esc_html_e( 'Message', 'woo-abandoned-cart-recovery' ); ?></th>
                        <th class="cols-4"><?php esc_html_e( 'Action', 'woo-abandoned-cart-recovery' ); ?></th>
                    </tr>
                    </thead>
                    <tbody class="wacv-<?php echo esc_attr( $field ) ?>-row-target">
					<?php
					if ( isset( $data['time_to_send'] ) ) {
						$loop = count( $data['time_to_send'] );

						for ( $i = 0; $i < $loop; $i ++ ) { ?>
                            <tr class="wacv-<?php echo esc_attr( $field ) ?>-row-target"
                                data-index="<?php echo esc_attr( $i ) ?>">
                                <td class=" wacv-messenger-time">
                                    <input type="number"
                                           name="wacv_params[<?php echo esc_attr( $field ) ?>][time_to_send][]"
                                           class="vlt-input"
                                           value="<?php echo esc_attr( $data['time_to_send'][ $i ] ) ?>" min="1">
                                </td>
                                <td class="wacv-messenger-unit">
                                    <select name="wacv_params[<?php echo esc_attr( $field ) ?>][unit][]"
                                            class="">
                                        <option value="minutes" <?php echo $data['unit'][ $i ] == 'minutes' ? 'selected' : ''; ?>><?php esc_html_e( 'minutes', 'woo-abandoned-cart-recovery' ); ?></option>
                                        <option value="hours" <?php echo $data['unit'][ $i ] == 'hours' ? 'selected' : ''; ?>><?php esc_html_e( 'hours', 'woo-abandoned-cart-recovery' ); ?></option>
                                    </select>
                                </td>
                                <td class="wacv-messenger-message">
                                    <div class="wacv-messenger-message-wrap">
                                        <input type="text" value="<?php echo esc_attr( stripslashes( $data['message'][ $i ] ) ) ?>"
                                               name="wacv_params[<?php echo esc_attr( $field ) ?>][message][]"
                                               class="wacv-message-content">
                                        <span class="wacv-message-length"
                                              data-tooltip="<?php esc_html_e( 'Characters left for a SMS', 'woo-abandoned-cart-recovery' ); ?>">
                                        </span>
                                    </div>
									<?php if ( $multilingual ) {
										if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
											global $sitepress;
											$default_lang = $sitepress->get_default_language();
											$languages    = $langs = icl_get_languages( 'skip_missing=N&orderby=KEY&order=DIR&link_empty_to=str' );
											if ( count( $languages ) ) {
												foreach ( $languages as $key => $language ) {
													if ( $key == $default_lang ) {
														continue;
													}
													$message_mlg_val = isset( $data[ 'message_' . $key ][ $i ] ) ? $data[ 'message_' . $key ][ $i ] : '';
													?>
                                                    <p class="wacv-mlg-label"><?php echo esc_html( $language['native_name'] . ':' ) ?></p>
                                                    <div class="wacv-messenger-message-wrap">
                                                        <input type="text"
                                                               value="<?php echo esc_attr( stripslashes( $message_mlg_val ) ) ?>"
                                                               name="wacv_params[<?php echo esc_attr( $field ) ?>][message_<?php echo esc_attr( $key ) ?>][]"
                                                               class="wacv-message-content">
                                                        <span class="wacv-message-length"
                                                              data-tooltip="<?php esc_html_e( 'Characters left for a SMS', 'woo-abandoned-cart-recovery' ); ?>">
                                                        </span>
                                                    </div>
													<?php
												}
											}
										} elseif ( class_exists( 'Polylang' ) ) {
											$languages = pll_languages_list();
											foreach ( $languages as $language ) {
												$default_lang = pll_default_language( 'slug' );

												if ( $language == $default_lang ) {
													continue;
												}
												$message_mlg_val = isset( $data[ 'message_' . $language ][ $i ] ) ? $data[ 'message_' . $language ][ $i ] : '';
												?>
                                                <p class="wacv-mlg-label"><?php echo esc_html( $language . ':' ) ?></p>
                                                <div class="wacv-messenger-message-wrap">
                                                    <input type="text"
                                                           value="<?php echo esc_attr( stripslashes( $message_mlg_val ) ) ?>"
                                                           name="wacv_params[<?php echo esc_attr( $field ) ?>][message_<?php echo esc_attr( $language ) ?>][]"
                                                           class="wacv-message-content">
                                                    <span class="wacv-message-length"
                                                          data-tooltip="<?php esc_html_e( 'Characters left for a SMS', 'woo-abandoned-cart-recovery' ); ?>">
                                                        </span>
                                                </div>
												<?php
											}
										}
									} ?>
                                </td>
                                <td align="center" class="">
                                    <button class="wacv-delete-<?php echo esc_attr( $field ) ?> vi-ui small icon red button"
                                            type="button">
                                        <i class="trash icon"> </i>
                                    </button>
                                </td>
                            </tr>
						<?php }
					} ?>
                    </tbody>
                </table>
                <button type="button" class="wacv-add-<?php echo esc_attr( $field ) ?> vi-ui small icon green button">
					<?php esc_html_e( 'Add rule', 'woo-abandoned-cart-recovery' ); ?>
                </button>
            </td>
            <td class="col-3"></td>
        </tr>
		<?php
	}

	public function save_params() {

		if ( isset( $_POST['wacv_params'] ) ) {
			if ( ! is_admin() || ! isset( $_POST['_woo_abandoned_cart_nonce'] ) || ! wp_verify_nonce( $_POST['_woo_abandoned_cart_nonce'], 'woo_abandoned_settings' ) ) {
				return;
			}
			if ( ! current_user_can( 'manage_woocommerce' ) ) { //apply_filters( 'wacv_change_role',
				return;
			}

			/*wpml*/
			$multi_lang = array();
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
				global $sitepress;
				$default_lang   = $sitepress->get_default_language();
				$wpml_languages = icl_get_languages( 'skip_missing=N&orderby=KEY&order=DIR&link_empty_to=str' );

				if ( count( $wpml_languages ) ) {
					foreach ( $wpml_languages as $key => $language ) {
						if ( $key != $default_lang ) {
							$multi_lang[] = $key;
						}
					}
				}
			} elseif ( class_exists( 'Polylang' ) ) {
				/*Polylang*/
				$languages    = pll_languages_list();
				$default_lang = pll_default_language( 'slug' );
				foreach ( $languages as $language ) {
					if ( $language == $default_lang ) {
						continue;
					}
					$multi_lang[] = $language;
				}
			}

			$input_data = wc_clean( wp_unslash( $_POST['wacv_params'] ) );

			foreach ( Data::$params_default as $key => $value ) {
				$data[ $key ] = isset( $input_data[ $key ] ) ? $input_data[ $key ] : 0;
			}
			$data                 = $this->sort_rules( $data );
			$data['gdpr_content'] = sanitize_text_field( htmlentities( wp_unslash( $_POST['gdpr_content'] ) ) );
			$data['email_immediate_content'] = isset( $_POST['email_immediate_content'] ) ? sanitize_text_field( htmlentities( wp_unslash( $_POST['email_immediate_content'] ) ) ) : '';

			if ( $multi_lang ) {
				$email_abd_exits = false;
				if ( ! empty( $input_data['email_rules'] ) ) {
					$email_abd_exits = true;
				}
				$email_order_exits = false;
				if ( ! empty( $input_data['abd_orders'] ) ) {
					$email_order_exits = true;
				}
				$messenger_exits = false;
				if ( ! empty( $input_data['messenger_rules'] ) ) {
					$messenger_exits = true;
				}
				$whatsapp_exits = false;
				if ( ! empty( $input_data['whatsapp_rules'] ) ) {
					$whatsapp_exits = true;
				}
				$sms_abd_cart_exits = false;
				if ( ! empty( $input_data['sms_abd_cart'] ) ) {
					$sms_abd_cart_exits = true;
				}
				$sms_abd_order_exits = false;
				if ( ! empty( $input_data['sms_abd_order'] ) ) {
					$sms_abd_order_exits = true;
				}
				foreach ( $multi_lang as $key => $value ) {
					$data[ 'checkout_gdpr_message_' . $value ] = isset( $input_data[ 'checkout_gdpr_message_' . $value ] ) ? $input_data[ 'checkout_gdpr_message_' . $value ] : '';
					$data[ 'checkout_gdpr_cancel_' . $value ]  = isset( $input_data[ 'checkout_gdpr_cancel_' . $value ] ) ? $input_data[ 'checkout_gdpr_cancel_' . $value ] : '';
					$data[ 'gdpr_content_' . $value ]          = sanitize_text_field( htmlentities( wp_unslash( $_POST[ 'gdpr_content_' . $value ] ) ) );
					$data[ 'email_immediate_subject_' . $value ] = isset( $input_data[ 'email_immediate_subject_' . $value ] ) ? $input_data[ 'email_immediate_subject_' . $value ] : '';
					$data[ 'email_immediate_content_' . $value ] = isset( $_POST[ 'email_immediate_content_' . $value ] ) ? sanitize_text_field( htmlentities( wp_unslash( $_POST[ 'email_immediate_content_' . $value ] ) ) ) : '';
					$data[ 'title_popup_' . $value ]           = isset( $input_data[ 'title_popup_' . $value ] ) ? $input_data[ 'title_popup_' . $value ] : '';
					$data[ 'sub_title_popup_' . $value ]       = isset( $input_data[ 'sub_title_popup_' . $value ] ) ? $input_data[ 'sub_title_popup_' . $value ] : '';
					$data[ 'add_to_cart_btn_' . $value ]       = isset( $input_data[ 'add_to_cart_btn_' . $value ] ) ? $input_data[ 'add_to_cart_btn_' . $value ] : '';
					$data[ 'popup_close_text_' . $value ]      = isset( $input_data[ 'popup_close_text_' . $value ] ) ? $input_data[ 'popup_close_text_' . $value ] : '';
					$data[ 'invalid_email_' . $value ]         = isset( $input_data[ 'invalid_email_' . $value ] ) ? $input_data[ 'invalid_email_' . $value ] : '';
					$data[ 'invalid_phone_' . $value ]         = isset( $input_data[ 'invalid_phone_' . $value ] ) ? $input_data[ 'invalid_phone_' . $value ] : '';
					$data[ 'unsub_title_' . $value ]           = isset( $input_data[ 'unsub_title_' . $value ] ) ? $input_data[ 'unsub_title_' . $value ] : '';
					$data[ 'unsub_content_' . $value ]         = isset( $input_data[ 'unsub_content_' . $value ] ) ? $input_data[ 'unsub_content_' . $value ] : '';
					$data[ 'unsub_button_' . $value ]          = isset( $input_data[ 'unsub_button_' . $value ] ) ? $input_data[ 'unsub_button_' . $value ] : '';
					if ( $email_abd_exits && isset( $input_data['email_rules'][ 'template_' . $value ] ) ) {
						$data['email_rules'][ 'template_' . $value ] = $input_data['email_rules'][ 'template_' . $value ];
					}
					if ( $email_order_exits && isset( $input_data['abd_orders'][ 'template_' . $value ] ) ) {
						$data['abd_orders'][ 'template_' . $value ] = $input_data['abd_orders'][ 'template_' . $value ];
					}
					if ( $messenger_exits && isset( $input_data['messenger_rules'][ 'message_' . $value ] ) ) {
						$data['messenger_rules'][ 'message_' . $value ] = $input_data['messenger_rules'][ 'message_' . $value ];
					}
					if ( $whatsapp_exits && isset( $input_data['whatsapp_rules'][ 'message_' . $value ] ) ) {
						$data['whatsapp_rules'][ 'message_' . $value ] = $input_data['whatsapp_rules'][ 'message_' . $value ];
					}
					if ( $sms_abd_cart_exits && isset( $input_data['sms_abd_cart'][ 'message_' . $value ] ) ) {
						$data['sms_abd_cart'][ 'message_' . $value ] = $input_data['sms_abd_cart'][ 'message_' . $value ];
					}
					if ( $sms_abd_order_exits && isset( $input_data['sms_abd_order'][ 'message_' . $value ] ) ) {
						$data['sms_abd_order'][ 'message_' . $value ] = $input_data['sms_abd_order'][ 'message_' . $value ];
					}
				}
			}

			update_option( 'wacv_params', $data );

			self::$data = wp_parse_args( $data, Data::$params_default );

		}
		if ( isset( $_POST['action'] ) && $_POST['action'] == 'save_n_check_key' ) {
			delete_transient( 'update_plugins' );
			delete_transient( 'villatheme_item_29427' );
			delete_option( 'woocommerce-abandoned-cart-recovery_messages' );
			do_action( 'villatheme_save_and_check_key_woocommerce-abandoned-cart-recovery', $data['update_key'] );
		}
	}

	public function sort_rules( $data ) {
		$type = array(
			'email_rules'     => 'template',
			'abd_orders'      => 'template',
			'messenger_rules' => 'message',
			'whatsapp_rules'  => 'message',
			'sms_abd_cart'    => 'message',
			'sms_abd_order'   => 'message'
		);
		$unit = Data::get_instance();
//		$new_data = array();
		foreach ( $type as $key => $value ) {
			if ( isset( $data[ $key ] ) ) {
				$rules = $data[ $key ];

				$count = isset( $rules['time_to_send'] ) && is_array( $rules['time_to_send'] ) ? count( $rules['time_to_send'] ) : 0;
				if ( ! $count ) {
					continue;
				}
				for ( $i = 0; $i < $count; $i ++ ) {
					if ( ! empty( $rules['time_to_send'] [ $i ] ) && ! empty( $rules[ $value ][ $i ] ) ) {
						$rules['sort'] [ $i ] = intval( $rules['time_to_send'] [ $i ] ) * $unit->case_unit( $rules['unit'] [ $i ] );
					}
				}

				asort( $rules['sort'] );
				$j         = 1;
				$new_rules = array();

				foreach ( $rules['sort'] as $k => $v ) {
					$new_rules['send_time'][]    = $j;
					$new_rules['time_to_send'][] = $rules['time_to_send'] [ $k ];
					$new_rules['unit'][]         = $rules['unit'] [ $k ];
					$new_rules[ $value ][]       = $rules[ $value ] [ $k ];
					$j ++;
				}

				$data[ $key ] = $new_rules;
			}
		}

		return $data;
	}

	public function check_plugin_update() {
		$key = self::get_field( 'update_key' );
		/*Check update*/
		if ( class_exists( 'VillaTheme_Plugin_Check_Update' ) ) {
			$setting_url = admin_url( 'admin.php?page=wacv_settings' );
			new \VillaTheme_Plugin_Check_Update (
				WACVP_VERSION,                    // current version
				'https://villatheme.com/wp-json/downloads/v4',  // update path
				'woocommerce-abandoned-cart-recovery/woocommerce-abandoned-cart-recovery.php',                  // plugin file slug
				'woocommerce-abandoned-cart-recovery', '29427', $key, $setting_url
			);
		}
	}

}
