<?php

use Simple_Giveaways\SG_Reports;

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

/**
 * Class for creating menu pages
 */
class GiveASAP_Menu extends GiveASAP_WordPressSettings {

	/**
	 * Default options
	 *
	 * @var array
	 */
	public $defaultOptions = array(
		'slug'           => '', // Name of the menu item
		'title'          => '', // Title displayed on the top of the admin panel
		'page_title'     => '',
		'parent'         => null, // id of parent, if blank, then this is a top level menu
		'id'             => '', // Unique ID of the menu item
		'capability'     => 'manage_options', // User role
		'icon'           => 'dashicons-admin-generic', // Menu icon for top level menus only http://melchoyce.github.io/dashicons/
		'position'       => null, // Menu position. Can be used for both top and sub level menus
		'desc'           => '', // Description displayed below the title
		'function'       => '',
		'screen_options' => array(),
	);

	/**
	 * Gets populated on submenus, contains slug of parent menu
	 *
	 * @var null
	 */
	public $parent_id = null;

	/**
	 * Menu options
	 *
	 * @var array
	 */
	public $menu_options = array();

	/**
	 * Menu Screen Options
	 *
	 * @var array
	 */
	public $menu_screen_options = array();

	function __construct( $options ) {

		$this->menu_options = array_merge( $this->defaultOptions, $options );

		if ( $this->menu_options['slug'] == '' ) {
			return;
		}

		$this->settings_id = $this->menu_options['slug'];

		$this->prepopulate();

		add_action( 'admin_menu', array( $this, 'add_page' ) );

		if ( $this->menu_screen_options ) {
			add_filter( 'set-screen-option', array( $this, 'set_screen_options' ), 10, 3 );
		}

		add_action( 'wordpressmenu_page_save_' . $this->settings_id, array( $this, 'save_settings' ) );

	}

	/**
	 * Populate some of required options
	 *
	 * @return void
	 */
	public function prepopulate() {

		if ( $this->menu_options['title'] == '' ) {
			$this->menu_options['title'] = ucfirst( $this->menu_options['slug'] );
		}

		if ( $this->menu_options['page_title'] == '' ) {
			$this->menu_options['page_title'] = $this->menu_options['title'];
		}

		$this->menu_screen_options = $this->menu_options['screen_options'];

	}

	/**
	 * Add the menu page using WordPress API
	 *
	 * @return [type] [description]
	 */
	public function add_page() {

		$functionToUse = $this->menu_options['function'];

		if ( $functionToUse == '' ) {
			$functionToUse = array( $this, 'create_menu_page' );
		}

		if ( $this->menu_options['parent'] != null ) {

			$hook = add_submenu_page(
				$this->menu_options['parent'],
				$this->menu_options['page_title'],
				$this->menu_options['title'],
				$this->menu_options['capability'],
				$this->menu_options['slug'],
				$functionToUse
			);

		} else {

			$hook = add_menu_page(
				$this->menu_options['page_title'],
				$this->menu_options['title'],
				$this->menu_options['capability'],
				$this->menu_options['slug'],
				$functionToUse,
				$this->menu_options['icon'],
				$this->menu_options['position']
			);

		}

		if ( $this->menu_screen_options ) {
			add_action( 'load-' . $hook, array( $this, 'add_screen_options' ) );
		}

	}

	/**
	 * Screen Options.
	 */
	public function add_screen_options() {
		foreach ( $this->menu_screen_options as $option => $args ) {
			add_screen_option( $option, $args );
		}
	}

	/**
	 * @param $status
	 * @param $option
	 * @param $value
	 *
	 * @return mixed
	 */
	public function set_screen_options( $status, $option, $value ) {

		if ( ! $this->menu_screen_options ) {
			return $status;
		}

		if ( in_array( $option, wp_list_pluck( $this->menu_screen_options, 'option' ) ) ) {
			return $value;
		}

		return $status;

	}

	/**
	 * Render the registered tabs
	 *
	 * @param  string $active_tab the viewed tab
	 * @return void
	 */
	public function render_tabs( $active_tab = 'general' ) {

		if ( count( $this->tabs ) > 1 ) {

			echo '<h2 class="nav-tab-wrapper woo-nav-tab-wrapper">';

			foreach ( $this->tabs as $key => $value ) {

				echo '<a href="' . admin_url( 'admin.php?page=' . $this->menu_options['slug'] . '&tab=' . $key ) . '" class="nav-tab ' . ( ( $key == $active_tab ) ? 'nav-tab-active' : '' ) . ' ">' . $value . '</a>';

			}

			echo '</h2>';
			echo '<br/>';

		}
	}

	/**
	 * Render the save button
	 *
	 * @return void
	 */
	protected function save_button() {
		?>
		<button type="submit" name="<?php echo esc_attr( $this->settings_id ); ?>_save" class="button button-primary">
			<?php esc_html_e( 'Save', 'textdomain' ); ?>
		</button>
		<?php
	}

	/**
	 * Save if the button for this menu is submitted
	 *
	 * @return void
	 */
	protected function save_if_submit() {
		if ( isset( $_POST[ $this->settings_id . '_save' ] ) ) {

			$nonce = isset( $_POST['giveasap_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['giveasap_nonce'] ) ) : false;

			if ( ! $nonce ) {
				wp_die( __( 'No hacking please', 'giveasap' ) );
			}

			if ( ! wp_verify_nonce( $nonce, 'giveasap_settings' ) ) {
				wp_die( __( 'No hacking please', 'giveasap' ) );
			}

			do_action( 'wordpressmenu_page_save_' . $this->settings_id );
		}
	}

	/**
	 * Create the menu page
	 *
	 * @return void
	 */
	public function create_menu_page() {

		$this->save_if_submit();

		$tab = 'general';

		if ( isset( $_GET['tab'] ) ) {
			$tab = \Simple_Giveaways\Helpers::unslash_and_clean( $_GET['tab'] );
		}

		$this->get_all_fields();
		$this->init_settings();

		?>
		<div class="giveasap-settings-header">
			<img src="<?php echo esc_url( trailingslashit( GASAP_URI ) . 'assets/images/logo-color.svg' ); ?>" />
			<?php esc_html_e( 'Simple Giveaways', 'giveasap' ); ?>
		</div>
		<div class="giveasap-settings-subheader">
			<h2><?php echo esc_html( $this->menu_options['page_title'] ); ?></h2>
			<?php
			if ( ! empty( $this->menu_options['desc'] ) ) {
				?>
				<p class='description'><?php echo esc_html( $this->menu_options['desc'] ); ?></p>
				<?php
			}
			?>
		</div>
		<div class="wrap">
			<?php

				$this->render_tabs( $tab );

			?>
			<form id="<?php echo esc_attr( $this->settings_id ); ?>" method="POST" action="">
				<input type="hidden" name="giveasap_tab" value = "<?php echo esc_attr( $tab ); ?>" />
				<?php wp_nonce_field( 'giveasap_settings', 'giveasap_nonce' ); ?>
				<div class="postbox">
					<div class="inside">
						<table class="form-table">
							<?php $this->render_fields( $tab ); ?>
						</table>
						<?php $this->save_button(); ?>
					</div>
				</div>
			</form>

			<?php do_action( 'sg_menu_page_bottom', $tab ); ?>
		</div>
		<?php
	}

}

/**
 * Class for creating tabs in a menu page
 */
class GiveASAP_MenuTab {

	/**
	 * Slug of the tab
	 *
	 * @var string
	 */
	public $slug;

	/**
	 * Title of the tab
	 *
	 * @var string
	 */
	public $title;

	/**
	 * Menu
	 *
	 * @var GiveASAP_Menu
	 */
	public $menu;

	/**
	 * Creating a tab
	 *
	 * @param array         $options
	 * @param GiveASAP_Menu $menu
	 */
	function __construct( $options, GiveASAP_Menu $menu ) {

		$this->slug  = $options['slug'];
		$this->title = $options['title'];
		$this->menu  = $menu;

		$this->menu->add_tab( $options );

	}

	/**
	 * Add field to this tab
	 *
	 * @param [type] $array [description]
	 */
	public function add_field( $array ) {

		$this->menu->add_field( $array, $this->slug );
	}
}

/**
 * Creating Menus
 */

$sg_subscribers_screen = new GiveASAP_Menu(
	array(
		'parent'         => 'edit.php?post_type=giveasap',
		'slug'           => 'giveasap-users',
		'title'          => __( 'Subscribers', 'giveasap' ),
		'desc'           => __( 'User List', 'giveasap' ),
		'position'       => 99,
		'function'       => 'giveasap_user_list_screen',
		'screen_options' => array(
			'per_page' => array(
				'label'   => __( 'Subscribers', 'giveasap' ),
				'default' => 10,
				'option'  => 'sg_subscribers_per_page',
			),
		),
	)
);

$sg_entries_screen = new GiveASAP_Menu(
	array(
		'parent'         => 'edit.php?post_type=giveasap',
		'slug'           => 'giveasap-subscriber-entries',
		'title'          => __( 'Entries', 'giveasap' ),
		'position'       => 99,
		'function'       => 'giveasap_subscriber_entries_screen',
		'screen_options' => array(
			'per_page' => array(
				'label'   => __( 'Entries', 'giveasap' ),
				'default' => 10,
				'option'  => 'sg_subscriber_entries_per_page',
			),
		),
	)
);

$giveASAP_menu = new GiveASAP_Menu(
	array(
		'parent'   => 'edit.php?post_type=giveasap',
		'slug'     => 'giveasap_settings',
		'title'    => __( 'Settings', 'giveasap' ),
		'position' => 99,
	)
);

$giveASAP_doc = new GiveASAP_Menu(
	array(
		'parent'   => 'edit.php?post_type=giveasap',
		'slug'     => 'giveasap_documentation',
		'title'    => __( 'Documentation', 'giveasap' ),
		'position' => 99,
		'function' => 'giveasap_documentation',
	)
);

$giveasap_integrations = new GiveASAP_Menu(
	array(
		'parent'   => 'edit.php?post_type=giveasap',
		'slug'     => 'giveasap_integrations',
		'title'    => __( 'Integrations', 'giveasap' ),
		'position' => 100,
		'function' => 'giveasap_integrations',
	)
);

$giveasap_integrations = new GiveASAP_Menu(
	array(
		'parent'   => 'edit.php?post_type=giveasap',
		'slug'     => 'sg_reports',
		'title'    => __( 'Reports', 'giveasap' ),
		'position' => 90,
		'function' => 'giveasap_reports',
	)
);

$giveASAP_doc = new GiveASAP_Menu(
	array(
		'parent'   => 'edit.php?post_type=giveasap',
		'slug'     => 'giveasap_about_page',
		'title'    => __( 'About', 'giveasap' ),
		'position' => 99,
		'function' => 'giveasap_about_page',
	)
);

$giveASAP_menu->add_field(
	array(
		'type'    => 'checkbox',
		'name'    => 'display_powered_by',
		'title'   => __( 'Display Powered By?', 'giveasap' ),
		'desc'    => __( 'If enabled, a text "Powered By Simple Giveaways" will appear at the bottom of landing page or shortcode.', 'giveasap' ),
		'default' => '0',
	)
);

$giveASAP_menu->add_field(
	array(
		'type'    => 'checkbox',
		'name'    => 'enable_email_login',
		'title'   => __( 'Email Login?', 'giveasap' ),
		'desc'    => __( 'If enabled, people can login back on the giveaway without needing the login link.', 'giveasap' ),
		'default' => '0',
	)
);

$giveASAP_menu->add_field(
	array(
		'type'    => 'checkbox',
		'name'    => 'activation_required',
		'title'   => __( 'Activation Required?', 'giveasap' ),
		'desc'    => __( 'If the activation is required, subscribers will have to activate their account by clicking on a link in the email. Please set {{ACTIVATE_LINK}} in the emails below to use that.', 'giveasap' ),
		'default' => '0',
	)
);

$giveASAP_menu->add_field(
	array(
		'type'    => 'checkbox',
		'name'    => 'collect_ip_address',
		'title'   => __( 'Collect IP Address', 'giveasap' ),
		'desc'    => __( 'Collect IP Address of each subscriber if available.', 'giveasap' ),
		'default' => '0',
	)
);

$giveASAP_menu->add_field(
    array(
        'type'    => 'checkbox',
        'name'    => 'show_visited_from',
        'title'   => __( 'Display "Visited From"', 'giveasap' ),
        'desc'    => __( 'Display from where the subscriber came from (if such data available) on "Subscribers" page and in CSV.', 'giveasap' ),
        'default' => '0',
    )
);

$giveASAP_menu->add_field(
	array(
		'type'    => 'checkbox',
		'name'    => 'allow_subscriber_edit',
		'title'   => __( 'Allow subscribers to edit their data.', 'giveasap' ),
		'desc'    => __( 'This will show a form where they can edit their data. Email will not be editable. If fields are required and still missing, it will show the form regardless.', 'giveasap' ),
		'default' => '0',
	)
);

$giveASAP_menu->add_field(
	array(
		'type'    => 'checkbox',
		'name'    => 'notify_on_subscribe',
		'title'   => __( 'Notify on each subscribe.', 'giveasap' ),
		'desc'    => __( 'It will notify you after each subscription to a giveaway. Will use the email set in Settings > General', 'giveasap' ),
		'default' => '0',
	)
);

$giveASAP_menu->add_field(
	array(
		'type'    => 'checkbox',
		'name'    => 'disallow_blocked_subscribers',
		'title'   => __( 'Don\'t allow Blocked Subscribers to Join.', 'giveasap' ),
		'desc'    => __( 'If checked, any blocked subscriber won\'t be able to join on future giveaways.', 'giveasap' ),
		'default' => '0',
	)
);

$giveASAP_menu->add_field(
	array(
		'type'    => 'checkbox',
		'name'    => 'clone_subscribers',
		'title'   => __( 'Clone Subscribers when cloning Giveaways', 'giveasap' ),
		'desc'    => __( 'When you want to clone a giveaway with all the options, clone their subscribers as well.', 'giveasap' ),
		'default' => '0',
	)
);

$giveASAP_menu->add_field(
	array(
		'type'    => 'checkbox',
		'name'    => 'display_winners',
		'title'   => __( 'Display Winners', 'giveasap' ),
		'desc'    => __( 'Display winners on the giveaway page', 'giveasap' ),
		'default' => '0',
	)
);

$giveASAP_menu->add_field(
	array(
		'type'    => 'checkbox',
		'name'    => 'display_winners_points',
		'title'   => __( 'Display Winner Points', 'giveasap' ),
		'desc'    => __( 'Display Points next to the winner', 'giveasap' ),
		'default' => '0',
	)
);

$giveASAP_menu->add_field(
	array(
		'name'    => 'rules_button',
		'title'   => __( 'Rules Button', 'giveasap' ),
		'desc'    => __( 'Text that will display on the Rules Button', 'giveasap' ),
		'type'    => 'text',
		'default' => __(
			'Show Rules',
			'giveasap'
		),
	)
);

$sg_emails_tab = new GiveASAP_MenuTab(
	array(
		'slug'  => 'emails',
		'title' => __( 'Emails', 'giveasap' ),
	),
	$giveASAP_menu
);

function giveasap_show_email_preview_button( $field ) {
	$preview_url  = site_url( '/?sg_customizer=email' );
	$redirect_url = add_query_arg(
		array(
			'autofocus[panel]' => 'sg_mailtpl',
			'sg_customizer'    => 'email',
			'return'           => admin_url( 'admin.php?page=giveasap_settings&tab=emails' ),
			'url'              => $preview_url,
		),
		admin_url( 'customize.php' )
	);
	echo '<a class="button button-secondary" href="' . esc_url( $redirect_url ) . '">' . __( 'Preview Email', 'giveasap' ) . '</a>';
}

$sg_emails_tab->add_field(
	array(
		'name'   => 'email_send',
		'title'  => __( 'Preview Email', 'giveasap' ),
		'type'   => 'custom',
		'render' => 'giveasap_show_email_preview_button',
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'email_disabled',
		'title'   => __( 'Disable Email', 'giveasap' ),
		'type'    => 'checkbox',
		'default' => 'no',
		'desc'    => __( 'If you want all Giveaway related emails to be disabled, check this. But, it will not disable emails sent by 3rd-party integrations such as MailChimp.', 'giveasap' ),
	)
);

$sg_emails_tab->add_field(
    array(
        'name'    => 'allow_email_unsubscribe',
        'title'   => __( 'Allow Email Unsubscribing', 'giveasap' ),
        'type'    => 'checkbox',
        'default' => 'no',
        'desc'    => __( 'If you want to enable Giveaway subscribers to unsubscribe from related giveaway emails, check this. But, it will not disable emails sent by 3rd-party integrations such as MailChimp.', 'giveasap' ),
    )
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'blocked_email_domains',
		'title'   => __( 'Blocked Email domains', 'giveasap' ),
		'type'    => 'textarea',
        'rows'    => 3,
		'default' => '',
		'desc'    => __( 'If you want to disable some email domains such as @gmail.com, @hotmail.com, enter them as "gmail.com,hotmail.com".', 'giveasap' ),
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'allowed_email_domains',
		'title'   => __( 'Allowed Email domains', 'giveasap' ),
		'type'    => 'textarea',
		'default' => '',
        'rows'    => 3,
		'desc'    => __( 'If you want to accept subscribers only from specific domains, use this setting. Enter them as "gmail.com,hotmail.com".', 'giveasap' ),
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'email_type',
		'title'   => __( 'Email Type', 'giveasap' ),
		'type'    => 'select',
		'options' => array(
			'html'  => __( 'HTML', 'giveasap' ),
			'plain' => __( 'Plain', 'giveasap' ),
		),
		'default' => 'html',
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'sender_email',
		'title'   => __( 'Sender Email', 'giveasap' ),
		'desc'    => __( 'From which email you will send giveaway emails.', 'giveasap' ),
		'default' => get_option( 'admin_email', '' ),
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'sender_name',
		'title'   => __( 'Sender Email Name', 'giveasap' ),
		'desc'    => __( 'What will be the sender email name.', 'giveasap' ),
		'default' => get_bloginfo( 'name' ),
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'subscriber_email_subject',
		'title'   => __( 'Subscriber Subject', 'giveasap' ),
		'desc'    => __( 'Subject that will be sent to new subscribers.<br/>Placeholders:<br/> {{TITLE}} : giveaway title', 'giveasap' ),
		'default' => __( 'You have enrolled to {{TITLE}}. Good Luck!', 'giveasap' ),
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'subscriber_email',
		'title'   => __( 'Subscriber Email', 'giveasap' ),
		'desc'    => __( 'Notification email with the links. <br/>Placeholders:<br/> {{TITLE}} : giveaway title<br/>{{ENTRY_LINK}} : link to see their entries with sharing button<br/>{{SHARE_LINK}} : link to share<br/>{{ACTIVATE_LINK}} : link to activate the subscription<br/> {{USER_EMAIL}} : User Email<br/>For form fields use field key as FIELD_KEY in {{FORM_FIELD_KEY}}. Example: field key phone_number, to display it, use {{FORM_PHONE_NUMBER}}', 'giveasap' ),
		'type'    => 'wpeditor',
		'default' => __( 'Thank you for subscribing to {{TITLE}}<br/>You can check your entries at {{ENTRY_LINK}}<br/>To get more entries and have a higher chance of winning, please do share your link: {{SHARE_LINK}}', 'giveasap' ),
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'request_email',
		'title'   => __( 'Request Link Email', 'giveasap' ),
		'desc'    => __( 'Request Link email with the links for those who forgot them. <br/>Placeholders:<br/> {{TITLE}} : giveaway title<br/>{{ENTRY_LINK}} : link to see their entries with sharing button<br/>{{SHARE_LINK}} : link to share', 'giveasap' ),
		'type'    => 'wpeditor',
		'default' => __( 'Hi, you are receiving this email because you have requested the links on the Giveaway page. If that was not done by you, just ignore this email.<br/>You can check your entries at {{ENTRY_LINK}}<br/>To get more entries and have a higher chance of winning, please do share your link: {{SHARE_LINK}}', 'giveasap' ),
	)
);

$sg_emails_tab->add_field(
	array(
		'title' => __( 'Referrers', 'giveasap' ),
		'type'  => 'title',
		'name'  => 'referrers_email_title',
	)
);
$sg_emails_tab->add_field(
	array(
		'name'    => 'send_referrer_email',
		'title'   => __( 'Notify Referrers', 'giveasap' ),
		'desc'    => __( 'If checked, Referrers will be notified when someone subscribes from their links.', 'giveasap' ),
		'default' => 'no',
		'type'    => 'checkbox',
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'referrer_email_subject',
		'title'   => __( 'Referrer Email Subject', 'giveasap' ),
		'desc'    => __( 'Subject that will be sent to referrers.', 'giveasap' ),
		'default' => __( 'Someone has subscribed from your link.', 'giveasap' ),
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'referrer_email',
		'title'   => __( 'Referrer Email', 'giveasap' ),
		'desc'    => __( 'Notify the referrer when someone subscribes from their link. <br/>Placeholders:<br/> {{USER_EMAIL}} : User Email<br/>{{ENTRIES_RECEIVED}} : entries that the referrer has now received <br/>{{ENTRIES_TOTAL}} : current total entries of the referrer', 'giveasap' ),
		'type'    => 'wpeditor',
		'default' => __( 'Great Job! {{USER_EMAIL}} has subscribed using your link.<br/>You have received {{ENTRIES_RECEIVED}} entries.<br/>You now have in total {{ENTRIES_TOTAL}} entries.', 'giveasap' ),
	)
);

$sg_emails_tab->add_field(
	array(
		'title' => __( 'Reminder', 'giveasap' ),
		'type'  => 'title',
		'name'  => 'reminder_email_title',
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'reminder_email_subject',
		'title'   => __( 'Reminder Email Subject', 'giveasap' ),
		'desc'    => __( 'Subject that will be sent when reminding subscribers.<br/>Placeholders:<br/>{{TITLE}} : Giveaway Title<br/>{{DAYS}} : in how much days it ends<br/>For form fields use field key as FIELD_KEY in {{FORM_FIELD_KEY}}. Example: field key phone_number, to display it, use {{FORM_PHONE_NUMBER}}', 'giveasap' ),
		'default' => __( 'Hello, {{TITLE}} ends in {{DAYS}} days', 'giveasap' ),
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'reminder_email',
		'title'   => __( 'Reminder Email', 'giveasap' ),
		'desc'    => __( 'Notify the subscriber about giveaway ending in X days. <br/>Placeholders:<br/> {{USER_EMAIL}} : User Email<br/>{{TITLE}} : Giveaway Title<br/>{{DAYS}} : in how much days it ends<br/>{{ENTRIES_RECEIVED}} : entries that the referrer has now received{{ENTRY_LINK}} : link to see their entries with sharing button<br/>For form fields use field key as FIELD_KEY in {{FORM_FIELD_KEY}}. Example: field key phone_number, to display it, use {{FORM_PHONE_NUMBER}}', 'giveasap' ),
		'type'    => 'wpeditor',
		'default' => __( 'Hi {{USER_EMAIL}},<br/>The giveaway "{{TITLE}}" is ending in {{DAYS}} days.<br/>Your currently have {{ENTRIES_TOTAL}} entries and if you want to earn more, you can check the giveaway at {{ENTRY_LINK}}', 'giveasap' ),
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'email_layout',
		'title'   => __( 'Layout', 'giveasap' ),
		'desc'    => __( 'Settings related to the Email Layout', 'giveasap' ),
		'default' => '',
		'type'    => 'title',
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'email_hide_links',
		'title'   => __( 'Hide links', 'giveasap' ),
		'desc'    => __( 'This will hide the links in the emails that are automatically generated: Entry, Activation and Share link. You will have to enter them manually in the email content.', 'giveasap' ),
		'default' => 'no',
		'type'    => 'checkbox',
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'email_logo',
		'title'   => __( 'Email Logo', 'giveasap' ),
		'desc'    => __( 'Logo used in the email template', 'giveasap' ),
		'default' => '',
		'type'    => 'image',
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'email_background',
		'title'   => __( 'Background Color', 'giveasap' ),
		'default' => '#a46497',
		'type'    => 'color',
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'email_body_bg',
		'title'   => __( 'Body Background', 'giveasap' ),
		'desc'    => __( 'Body of the Email where the content will be.', 'giveasap' ),
		'default' => '#ffffff',
		'type'    => 'color',
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'email_text_color',
		'title'   => __( 'Text Color', 'giveasap' ),
		'default' => '#000000',
		'type'    => 'color',
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'email_link_color',
		'title'   => __( 'Link Color', 'giveasap' ),
		'default' => '#61ce70',
		'type'    => 'color',
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'email_button_color',
		'title'   => __( 'Button Text Color', 'giveasap' ),
		'default' => '#ffffff',
		'type'    => 'color',
	)
);

$sg_emails_tab->add_field(
	array(
		'name'    => 'email_button_bg_color',
		'title'   => __( 'Button Background Color', 'giveasap' ),
		'default' => '#61ce70',
		'type'    => 'color',
	)
);

$giveASAP_menu_google = new GiveASAP_MenuTab(
	array(
		'slug'  => 'google_tab',
		'title' => __( 'Google Captcha', 'giveasap' ),
	),
	$giveASAP_menu
);

$giveASAP_menu_google->add_field(
	array(
		'name'  => 'google_site_key',
		'title' => __( 'Google CAPTCHA - Site Key', 'giveasap' ),
	)
);

$giveASAP_menu_google->add_field(
	array(
		'name'  => 'google_secret_key',
		'title' => __( 'Google CAPTCHA - Secret Key', 'giveasap' ),
	)
);

$ga_scripts = new GiveASAP_MenuTab(
	array(
		'slug'  => 'scripts',
		'title' => __( 'Scripts', 'giveasap' ),
	),
	$giveASAP_menu
);

$ga_scripts->add_field(
	array(
		'name'  => 'head_scripts',
		'title' => __( 'Head', 'giveasap' ),
		'desc'  => __( 'Scripts that will go in the head of the page', 'giveasap' ),
		'type'  => 'code',
	)
);

$ga_scripts->add_field(
	array(
		'name'  => 'countdown_lang_url',
		'title' => __( 'Countdown Localisation', 'giveasap' ),
		'desc'  => __( 'Enter the URL with the script from http://keith-wood.name/countdown.html > Localisation.', 'giveasap' ),
		'type'  => 'text',
	)
);

$ga_scripts->add_field(
	array(
		'name'  => 'footer_scripts',
		'title' => __( 'Footer', 'giveasap' ),
		'desc'  => __( 'Scripts that will go in the footer of the page', 'giveasap' ),
		'type'  => 'code',
	)
);

$ga_scripts->add_field(
	array(
		'name'  => 'head_styles',
		'title' => __( 'Styles', 'giveasap' ),
		'desc'  => __( 'Styles that will be applied on the giveaway page', 'giveasap' ),
		'type'  => 'code',
		'mode'  => 'css',
	)
);

$ga_conditions = new GiveASAP_MenuTab(
	array(
		'slug'  => 'conditions',
		'title' => __( 'Conditions', 'giveasap' ),
	),
	$giveASAP_menu
);

$ga_conditions->add_field(
	array(
		'name'   => 'text_conditions',
		'title'  => __( 'Conditions', 'giveasap' ),
		'desc'   => __( 'Show a text to encourage visitors to subscribe. Use {{count}} as a placeholder for the number of subscribed. Leave empty to disable.', 'giveasap' ),
		'type'   => 'custom',
		'render' => 'giveasap_field_text_condition',
	)
);

$ga_methods = new GiveASAP_MenuTab(
	array(
		'slug'  => 'methods',
		'title' => __( 'Sharing Methods', 'giveasap' ),
	),
	$giveASAP_menu
);

$ga_methods->add_field(
	array(
		'name'   => 'giveasap_methods',
		'title'  => __( 'Sharing Methods', 'giveasap' ),
		'type'   => 'custom',
		'render' => 'giveasap_option_sharing_methods',
        'validate' => 'giveasap_option_sharing_methods_validate'
	)
);

function giveasap_option_sharing_methods_validate( $data ) {
    foreach ( $data as $index => $method_info ) {
        $data[ $index ]['title'] = sanitize_text_field( $method_info['title'] );
    }

    return $data;
}

$giveASAP_menu_form = new GiveASAP_MenuTab(
	array(
		'slug'  => 'sg_form',
		'title' => __( 'Form', 'giveasap' ),
	),
	$giveASAP_menu
);

$giveASAP_menu_form->add_field(
	array(
		'name'   => 'form_fields',
		'title'  => __( 'Fields', 'giveasap' ),
		'type'   => 'custom',
		'render' => 'giveasap_option_form_fields',
        'validate' => 'giveasap_option_form_fields_validate'
	)
);

function giveasap_option_form_fields_validate( $form_data ) {

    foreach ( $form_data as $index => $field_data ) {
        $form_data[ $index ]['label'] = sanitize_text_field( $field_data['label'] );
    }

    return $form_data;
}

$giveASAP_menu_form->add_field(
	array(
		'name'   => 'form_fields_winner',
		'title'  => __( 'Winner Field', 'giveasap' ),
		'type'   => 'custom',
		'render' => '',
	)
);

$giveASAP_menu_gdpr = new GiveASAP_MenuTab(
	array(
		'slug'  => 'gdpr_tab',
		'title' => __( 'GDPR', 'giveasap' ),
	),
	$giveASAP_menu
);

$giveASAP_menu_gdpr->add_field(
	array(
		'name'    => 'gdpr_active',
		'title'   => __( 'Activate GDPR', 'giveasap' ),
		'type'    => 'checkbox',
		'default' => '0',
		'desc'    => __( 'By activating GDPR, your visitors will see a checkbox to consent to your Privacy Policy and Terms.', 'giveasap' ),
	)
);

$giveASAP_menu_gdpr->add_field(
	array(
		'name'    => 'gdpr_consent',
		'title'   => __( 'Consent', 'giveasap' ),
		'type'    => 'wpeditor',
		'default' => __( 'By signing up, you are agreeing to Terms & Conditions and our Privacy Policy.', 'giveasap' ),
		'desc'    => __( 'Be sure to include links to your TOS and Privacy Policy.', 'giveasap' ),
	)
);

/**
 * Option to define Sharing Methods
 *
 * @param array $field
 */
function giveasap_option_sharing_methods( $field ) {
	$methods = giveasap_get_saved_sharing_methods();
	?>
	<tr>
		<td colspan="2" class="giveaway-sharing-methods">
			<input name="giveasap_methods" type="hidden" value="" />
			<?php
			if ( $methods ) {
				foreach ( $methods as $index => $method ) {
					?>
					<div class="giveaway-sharing-method" data-index="<?php echo esc_attr( $index ); ?>">
						<div class="method-header">
							<strong><?php echo esc_html( $method['title'] ); ?></strong>
							<button type="button" class="button button-default button-delete">X</button>
						</div>
						<div class="method-form">
							<select class="method-select" name="giveasap_methods[<?php echo esc_attr( $index ); ?>][type]">
								<?php
								$sharing_methods = giveasap_get_registered_sharing_methods();
								if ( $sharing_methods ) {
									foreach ( $sharing_methods as $method_slug => $method_object ) {
										$method_object = new $method_object();
										echo '<option ' . selected( $method_slug, $method['type'], false ) . ' value="' . $method_slug . '">' . $method_object->get_method_title() . '</option>';
									}
								}
								?>
							</select>
							<p>
								<label>
									<strong><?php esc_html_e( 'Method Title', 'giveasap' ); ?></strong>
									<input type="text" name="giveasap_methods[<?php echo esc_attr( $index ); ?>][title]" value="<?php echo esc_attr( $method['title'] ); ?>" class="widefat method-title" placeholder="Facebook, Twitter ..." />
								</label>
							</p>
							<div class="giveasap-method-html">
								<?php
									do_action( 'giveasap_admin_method_html', $method, $index );
								?>
							</div>
						</div>
					</div>
					<?php
				}
			}
			?>
		</td>
	</tr>
	<tr>
		<td colspan="2">
			<button type="button" id="addShareMethod" class="button button-primary"><?php esc_html_e( 'Add Sharing Method', 'giveasap' ); ?></button>
			<?php
			do_action( 'sg_admin_after_sharing_method_button' );
			?>

			<script id="tmpl-sharing-method" type="text/template">
				<div class="giveaway-sharing-method" data-index="{{ data.index || 0 }}">
					<div class="method-header">
						<strong>{{ data.title || '' }}</strong>
						<button type="button" class="button button-default button-delete">X</button>
					</div>
					<div class="method-form">
						<select class="method-select" name="giveasap_methods[{{ data.index }}][type]">
							<?php
							 $sharing_methods = giveasap_get_registered_sharing_methods();
							if ( $sharing_methods ) {
								foreach ( $sharing_methods as $method_slug => $method_object ) {
									$method_object = new $method_object();
									echo '<option value="' . $method_slug . '">' . $method_object->get_method_title() . '</option>';
								}
							}
							?>
						</select>
						<p>
							<label>
								<strong><?php esc_html_e( 'Method Title', 'giveasap' ); ?></strong>
								<input type="text" name="giveasap_methods[{{ data.index }}][title]" value="{{ data.title || '' }}" class="widefat method-title" placeholder="Facebook, Twitter ..." />
							</label>
						</p>
						<div class="giveasap-method-html"></div>
					</div>
				</div>
			</script>
		</td>
	</tr>
	<?php
}

/**
 * A field HTML to show different conditions
 *
 * @param  array $field
 * @return void
 */
function giveasap_field_text_condition( $field ) {

	$first_condition        = isset( $field['default'][0] ) ? $field['default'][0] : false;
	$first_condition_text   = $first_condition ? $first_condition['text'] : '';
	$first_condition_number = $first_condition ? $first_condition['number'] : '';
	?>
	<tr>
		<td colspan="2">
			<label for="<?php echo esc_attr( $field['name'] ); ?>">
				<strong><?php echo esc_html( $field['title'] ); ?></strong>
			</label>
			<?php if ( $field['desc'] ) { ?>
				<p class="description"><?php echo esc_html( $field['desc'] ); ?></p>
			<?php } ?>
			<br/>
			<div>
				<input value="<?php echo esc_attr( $first_condition_text ); ?>" class="widefat" name="<?php echo esc_attr( $field['name'] ); ?>[0][text]" type="text" placeholder="<?php esc_attr_e( 'Currently subscribed: {{count}}. Get in and win.', 'giveasap' ); ?>" />
				<p class="description"><?php esc_html_e( 'Show that after ', 'giveasap' ); ?><input style="width:50px;" value="<?php echo esc_attr( $first_condition_number ); ?>"  type="number" name="<?php echo esc_attr( $field['name'] ); ?>[0][number]"/> <?php esc_html_e( 'subscribers', 'giveasap' ); ?></p>
			</div>

			<?php if ( giv_fs()->is_free_plan() ) { ?>
				<p class="description"><?php echo wp_kses_post( sprintf( __( 'Want more control? <a href="%s">Upgrade to PRO</a>', 'giveasap' ), giv_fs()->get_upgrade_url() ) ); ?></p>
			<?php } ?>

			<?php
				do_action( 'giveasap_field_text_condition', $field );
			?>
		</td>
	</tr>
	<?php
}

/**
 * A way to define form fields.
 *
 * @param $field
 */
function giveasap_option_form_fields( $field ) {

	$is_visual  = isset( $field['_render_context'] ) && 'visual' === $field['_render_context'];
	$id_suffix  = $is_visual ? '-vb' : '';
	$add_btn_id = $is_visual ? 'sgAddFormFieldVisual' : 'sgAddFormField';

	$fields = $field['default'] ? $field['default'] : array();

	if ( ! $fields ) {
		$fields = giveasap_get_default_form_fields();
	}

	?>
	<?php if ( $is_visual ) : ?>
	<div class="giveasap-field-group">
	<?php else : ?>
	<tr>
		<td colspan="2">
	<?php endif; ?>
			<div class="sg-form-fields-container" data-giveasap-sync-key="form_fields_repeater" data-giveasap-adapter="repeater">
				<?php
				foreach ( $fields as $index => $form_field ) {
					$name     = isset( $form_field['name'] ) ? $form_field['name'] : '';
					$required = isset( $form_field['required'] ) ? $form_field['required'] : '0';
					$winner   = isset( $form_field['winner'] ) ? $form_field['winner'] : '0';
					$query    = isset( $form_field['query_string'] ) ? $form_field['query_string'] : '0';
					$label    = isset( $form_field['label'] ) ? $form_field['label'] : '';
					$type     = isset( $form_field['type'] ) ? $form_field['type'] : 'text';
					$options  = isset( $form_field['options'] ) ? $form_field['options'] : array();

					$field_name = $label ? $label : __( 'Field Name', 'giveasap' );

					if ( 'user_email' === $name ) {
						$form_field['readonly'] = true;
					}

					?>

					<div class="sg-form-field" data-index="<?php echo esc_attr( $index ); ?>">
						<div class="sg-form-field-header">
							<div class="handle-column">
								<div class="sg-form-row-handle"><i class="fal fa-sort"></i></div>
							</div>
							<div class="sg-field-name">
								<?php
								echo esc_html( $field_name );
								?>
							</div>
						</div>
						<div class="sg-form-field-body">
							<table class="form-table sg-form-table">
								<tr class="sg-field-name">
									<th>
										<label for="sg-field-label-<?php echo esc_attr( $index ); ?>"><?php esc_html_e( 'Field Name', 'giveasap' ); ?></label>
									</th>
									<td>
										<input id="sg-field-label-<?php echo esc_attr( $index ); ?>" type="text" name="<?php echo esc_attr( $field['name'] ); ?>[<?php echo esc_attr( $index ); ?>][label]" value="<?php echo esc_attr( $label ); ?>" placeholder="<?php esc_attr_e( 'Field Name', 'giveasap' ); ?>" />
									</td>
								</tr>
								<tr class="sg-field-key">
									<th>
										<label for="sg-field-key-<?php echo esc_attr( $index ); ?>"><?php esc_html_e( 'Field Key', 'giveasap' ); ?></label>
									</th>
									<td>
										<?php
										if ( isset( $form_field['readonly'] ) && $form_field['readonly'] ) {
											?>
											<input type="hidden" name="<?php echo esc_attr( $field['name'] ); ?>[<?php echo esc_attr( $index ); ?>][readonly]" value="1" />
											<input type="hidden" readonly name="<?php echo esc_attr( $field['name'] ); ?>[<?php echo esc_attr( $index ); ?>][name]" value="<?php echo esc_attr( $name ); ?>" />
											<em><?php echo esc_html( $name ); ?></em>
											<?php
										} else {
											?>
											<input id="sg-field-key-<?php echo esc_attr( $index ); ?>" type="text" name="<?php echo esc_attr( $field['name'] ); ?>[<?php echo esc_attr( $index ); ?>][name]" value="<?php echo esc_attr( $name ); ?>" placeholder="<?php esc_html_e( 'Field Key', 'giveasap' ); ?>"  />

											<?php
										}
										?>
									</td>
								</tr>
								<tr class="sg-field-type-row">
									<th>
										<label for="sg-field-type-<?php echo esc_attr( $index ); ?>"><?php esc_html_e( 'Type', 'giveasap' ); ?></label>
									</th>
									<td class="sg-field-type-column">
										<?php
										if ( isset( $form_field['readonly'] ) && $form_field['readonly'] ) {
											?>
											<input type="hidden" name="<?php echo esc_attr( $field['name'] ); ?>[<?php echo esc_attr( $index ); ?>][type]" value="text" />
											<em><?php esc_html_e( 'Text', 'giveasap' ); ?></em>
											<?php
										} else {
											?>
											<select class="sg-field-type" id="sg-field-type-<?php echo esc_attr( $index ); ?>" name="<?php echo esc_attr( $field['name'] ); ?>[<?php echo esc_attr( $index ); ?>][type]">
												<option <?php selected( 'text', $type, true ); ?> value="text"><?php esc_html_e( 'Text', 'giveasap' ); ?></option>
												<option <?php selected( 'url', $type, true ); ?> value="url"><?php esc_html_e( 'URL', 'giveasap' ); ?></option>
												<option <?php selected( 'number', $type, true ); ?> value="number"><?php esc_html_e( 'Number', 'giveasap' ); ?></option>
												<option <?php selected( 'checkbox', $type, true ); ?> value="checkbox"><?php esc_html_e( 'Checkbox', 'giveasap' ); ?></option>
												<option <?php selected( 'select', $type, true ); ?> value="select"><?php esc_html_e( 'Dropdown', 'giveasap' ); ?></option>
												<option <?php selected( 'radio', $type, true ); ?> value="radio"><?php esc_html_e( 'Radio', 'giveasap' ); ?></option>
                                                <option <?php selected( 'image', $type, true ); ?> value="image"><?php esc_html_e( 'Image', 'giveasap' ); ?></option>
											</select>
											<?php
											if ( ( 'radio' === $type || 'select' === $type ) && $options ) {
												?>
												<div class="sg-form-field-options">
													<p><?php esc_html_e( 'Options', 'giveasap' ); ?></p>
													<div class="sg-form-field-options-container">
														<?php
														foreach ( $options as $option ) {
															if ( '' === trim( $option ) ) {
																continue; }
															?>
															<div class="sg-form-field-option">
																<input type="text" name="<?php echo esc_attr( $field['name'] ); ?>[<?php echo esc_attr( $index ); ?>][options][]" value="<?php echo esc_attr( $option ); ?>" />
																<button type="button" class="button button-secondary button-small sg-remove-form-field-option">-</button>
															</div>
															<?php
														}
														?>
													</div>
													<button type="button" class="button button-secondary sg-add-form-field-option">+</button>
												</div>
												<?php

											}
										}
										?>
									</td>
								</tr>
								<tr class="sg-field-required">
									<th>
										<label for="sg-field-required-<?php echo esc_attr( $index ); ?>"><?php esc_html_e( 'Required', 'giveasap' ); ?></label>
									</th>
									<td>
										<?php
										if ( isset( $form_field['readonly'] ) && $form_field['readonly'] ) {
											?>
											<input type="hidden" name="<?php echo esc_attr( $field['name'] ); ?>[<?php echo esc_attr( $index ); ?>][required]" value="1" />
											<input type="checkbox" checked="checked" disabled="disabled" />
											<?php
										} else {
											?>
											<input id="sg-field-required-<?php echo esc_attr( $index ); ?>" type="checkbox" <?php checked( $required, '1', true ); ?> name="<?php echo esc_attr( $field['name'] ); ?>[<?php echo esc_attr( $index ); ?>][required]" value="1" />
											<?php
										}
										?>
									</td>
								</tr>
								<tr class="sg-field-winner">
									<th>
										<label for="sg-field-winner-<?php echo esc_attr( $index ); ?>"><?php esc_html_e( 'Winner Field', 'giveasap' ); ?></label>
									</th>
									<td>
										<label for="sg-field-winner-<?php echo esc_attr( $index ); ?>">
											<input id="sg-field-winner-<?php echo esc_attr( $index ); ?>" type="checkbox" <?php checked( $winner, '1', true ); ?> name="<?php echo esc_attr( $field['name'] ); ?>[<?php echo esc_attr( $index ); ?>][winner]" value="1" />
											<?php esc_html_e( 'Use this field data when displaying the winner. If none is selected, it will default to user_email or user_name field keys.', 'giveasap' ); ?>
										</label>
									</td>
								</tr>
								<tr class="sg-field-param">
									<th>
										<label for="sg-field-param-<?php echo esc_attr( $index ); ?>"><?php esc_html_e( 'Add as Query String', 'giveasap' ); ?></label>
									</th>
									<td>
										<label for="sg-field-param-<?php echo esc_attr( $index ); ?>">
											<input id="sg-field-param-<?php echo esc_attr( $index ); ?>" type="checkbox" <?php checked( $query, '1', true ); ?> name="<?php echo esc_attr( $field['name'] ); ?>[<?php echo esc_attr( $index ); ?>][query_string]" value="1" />
											<?php esc_html_e( 'Append this field key and value to the URL when someone subscribes to the giveaway.', 'giveasap' ); ?>
										</label>
									</td>
								</tr>

								<?php do_action( 'giveasap_form_fields_table_tbody_columns', $form_field, $index, $field ); ?>

								<tr>
									<th colspan="2">
										<?php
										if ( ! isset( $form_field['readonly'] ) || ! $form_field['readonly'] ) {
											?>
											<button type="button" class="button button-secondary sg-delete-form-field"><?php esc_html_e( 'Delete Field' ); ?></button>
											<?php
										}

										do_action( 'giveasap_form_fields_table_tbody_column_actions', $form_field, $index, $field );
										?>
									</th>
								</tr>
							</table>
						</div>
					</div>
					<?php
				}
				?>
			</div>

			<?php if ( $is_visual ) : ?>
			<button type="button" id="sgAddFormFieldVisual" class="button button-secondary"><?php esc_html_e( 'Add Field', 'giveasap' ); ?></button>
			</div><!-- .giveasap-field-group -->
			<?php else : ?>
			<ul>
				<li>
					<strong><?php esc_html_e( 'Key', 'giveasap' ); ?></strong> - <?php esc_html_e( 'Reserved keys: user_email for Email and user_name for Name (CSV, Newsletter).', 'giveasap' ); ?>
				</li>
				<li>
					<strong><?php esc_html_e( 'User Fields', 'giveasap' ); ?></strong> - <?php esc_html_e( 'If a user is logged in, we will try to get the data using the field keys.', 'giveasap' ); ?> <a target="_blank" href="https://codex.wordpress.org/Function_Reference/get_userdata">https://codex.wordpress.org/Function_Reference/get_userdata</a>
				</li>
				<li>
					<strong><?php esc_html_e( 'Notice', 'giveasap' ); ?></strong> - <?php esc_html_e( 'If a field is deleted, data will use the saved field key for title if presented.', 'giveasap' ); ?>
				</li>
				<?php do_action( 'giveasap_form_fields_info_list', $field ); ?>
			</ul>
			<button type="button" id="sgAddFormField" class="button button-secondary"><?php esc_html_e( 'Add Field', 'giveasap' ); ?></button>
			<?php do_action( 'giveasap_form_fields_table_actions', $field ); ?>
			<script type="text/template" id="tmpl-sg-form-field-options">
				<div class="sg-form-field-options">
					<p><?php esc_html_e( 'Options', 'giveasap' ); ?></p>
					<div class="sg-form-field-options-container">
						<div class="sg-form-field-option">
							<input type="text" name="<?php echo esc_attr( $field['name'] ); ?>[{{ data.index }}][options][]" value="" />
							<button type="button" class="button button-secondary button-small sg-remove-form-field-option">-</button>

						</div>
					</div>
					<button type="button" class="button button-secondary sg-add-form-field-option">+</button>
				</div>
			</script>
			<script type="text/template" id="tmpl-sg-form-field-option">
				<div class="sg-form-field-option">
					<input type="text" name="<?php echo esc_attr( $field['name'] ); ?>[{{ data.index }}][options][]" value="" />
					<button type="button" class="button button-secondary button-small sg-remove-form-field-option">-</button>

				</div>
			</script>
			<script type="text/template" id="tmpl-sg-form-field">
				<div class="sg-form-field" data-index="{{ data.index }}">
					<div class="sg-form-field-header">
						<div class="handle-column">
							<div class="sg-form-row-handle"><i class="fal fa-sort"></i></div>
						</div>
						<div class="sg-field-name">
							<?php
							esc_html_e( 'Field Name', 'giveasap' );
							?>
						</div>
					</div>
					<div class="sg-form-field-body">
						<table class="form-table sg-form-table">
							<tr class="sg-field-name">
								<th>
									<label for="sg-field-label-{{ data.index }}"><?php esc_html_e( 'Field Name', 'giveasap' ); ?></label>
								</th>
								<td>
									<input id="sg-field-label-{{ data.index }}" type="text" name="<?php echo esc_attr( $field['name'] ); ?>[{{ data.index }}][label]" value="" placeholder="<?php esc_html_e( 'Field Name', 'giveasap' ); ?>" />
								</td>
							</tr>
							<tr class="sg-field-key">
								<th>
									<label for="sg-field-key-{{ data.index }}"><?php esc_html_e( 'Field Key', 'giveasap' ); ?></label>
								</th>
								<td>
									<input id="sg-field-key-{{ data.index }}" type="text" name="<?php echo esc_attr( $field['name'] ); ?>[{{ data.index }}][name]" value="" placeholder="<?php esc_html_e( 'Field Key', 'giveasap' ); ?>" />
								</td>
							</tr>
							<tr class="sg-field-type-row">
								<th>
									<label for="sg-field-type-{{ data.index }}"><?php esc_html_e( 'Type', 'giveasap' ); ?></label>
								</th>
								<td class="sg-field-type-column">
									<select class="sg-field-type" name="<?php echo esc_attr( $field['name'] ); ?>[{{ data.index }}][type]">
										<option value="text"><?php esc_html_e( 'Text', 'giveasap' ); ?></option>
										<option value="url"><?php esc_html_e( 'URL', 'giveasap' ); ?></option>
										<option value="number"><?php esc_html_e( 'Number', 'giveasap' ); ?></option>
										<option value="checkbox"><?php esc_html_e( 'Checkbox', 'giveasap' ); ?></option>
										<option value="select"><?php esc_html_e( 'Dropdown', 'giveasap' ); ?></option>
										<option value="radio"><?php esc_html_e( 'Radio', 'giveasap' ); ?></option>
                                        <option value="image"><?php esc_html_e( 'Image', 'giveasap' ); ?></option>
									</select>
								</td>
							</tr>
							<tr class="sg-field-required">
								<th>
									<label for="sg-field-required-{{ data.index }}"><?php esc_html_e( 'Required', 'giveasap' ); ?></label>
								</th>
								<td>

									<input id="sg-field-required-{{ data.index }}" type="checkbox" name="<?php echo esc_attr( $field['name'] ); ?>[{{ data.index }}][required]" value="1" />
								</td>
							</tr>
							<tr class="sg-field-winner">
								<th>
									<label for="sg-field-winner-{{ data.index }}"><?php esc_html_e( 'Winner Field', 'giveasap' ); ?></label>
								</th>
								<td>
									<label for="sg-field-winner-{{ data.index }}">
										<input id="sg-field-winner-{{ data.index }}" type="checkbox" <?php checked( $winner, '1', true ); ?> name="<?php echo esc_attr( $field['name'] ); ?>[{{ data.index }}][winner]" value="1" />
										<?php esc_html_e( 'Use this field data when displaying the winner. If none is selected, it will default to user_email or user_name field keys.', 'giveasap' ); ?>
									</label>
								</td>
							</tr>
							<tr class="sg-field-param">
								<th>
									<label for="sg-field-param-{{ data.index }}"><?php esc_html_e( 'Add as Query String', 'giveasap' ); ?></label>
								</th>
								<td>
									<label for="sg-field-param-{{ data.index }}">
										<input id="sg-field-param-{{ data.index }}" type="checkbox" name="<?php echo esc_attr( $field['name'] ); ?>[{{ data.index }}][query_string]" value="1" />
										<?php esc_html_e( 'Append this field key and value to the URL when someone subscribes to the giveaway.', 'giveasap' ); ?>
									</label>
								</td>
							</tr>

							<?php do_action( 'giveasap_form_fields_table_tbody_columns', array( 'name' => '' ), '{{ data.index }}', $field ); ?>

							<tr>
								<td colspan="2">

									<button type="button" class="button button-secondary sg-delete-form-field"><?php esc_html_e( 'Delete Field' ); ?></button>

									<?php
									do_action( 'giveasap_form_fields_table_tbody_column_actions', array(), '{{ data.index }}', $field );
									?>
								</td>
							</tr>
						</table>
					</div>
				</div>
				<?php
				/*
				<tr class="sg-form-field" data-index="{{ data.index }}">
					<td class="handle-column">
						<div class="sg-form-row-handle"><i class="fal fa-sort"></i></div>
					</td>
					<td>
						<input type="text" name="<?php echo esc_attr( $field['name'] ); ?>[{{ data.index }}][label]" value="" placeholder="<?php esc_html_e( 'Field Name', 'giveasap' ); ?>" />
					</td>
					<td>
						<input type="text" name="<?php echo esc_attr( $field['name'] ); ?>[{{ data.index }}][name]" value="" placeholder="<?php esc_html_e( 'Field Key', 'giveasap' ); ?>" />
					</td>
					<td>
						<select class="sg-field-type" name="<?php echo esc_attr( $field['name'] ); ?>[{{ data.index }}][type]">
							<option value="text"><?php esc_html_e( 'Text', 'giveasap' ); ?></option>
							<option value="checkbox"><?php esc_html_e( 'Checkbox', 'giveasap' ); ?></option>
							<option value="select"><?php esc_html_e( 'Dropdown', 'giveasap' ); ?></option>
							<option value="radio"><?php esc_html_e( 'Radio', 'giveasap' ); ?></option>
						</select>
					</td>
					<td>
						<input type="checkbox" name="<?php echo esc_attr( $field['name'] ); ?>[{{ data.index }}][required]" value="1" />
					</td>

					<?php do_action( 'sg_form_fields_table_tbody_columns', array(), '{{ data.index }}', $field ) ?>
					<td>
						<button type="button" class="button button-secondary sg-delete-form-field"><?php esc_html_e( 'Delete Field' ); ?></button>
						<?php do_action( 'sg_form_fields_table_tbody_column_actions', array(), '{{ data.index }}', $field ) ?>
					</td>
				</tr>*/
				?>
			</script>
			<?php do_action( 'giveasap_form_fields_after_templates', $field ); ?>
		</td>
	</tr>
			<?php endif; ?>
	<?php
}

/**
 * Showing Reports
 */
function giveasap_reports() {
	include_once untrailingslashit( GASAP_ROOT ) . '/admin/reports/class-sg-reports.php';
	$reports = new SG_Reports();
	$reports->render();
}
