<?php
$wordai_username = '';
if ( isset( $this->settings['wordai_username'] ) ) {
	$wordai_username = $this->settings['wordai_username'];
}

$wordai_pass = '';
if ( isset( $this->settings['wordai_hash'] ) ) {
	$wordai_pass = $this->settings['wordai_hash'];
}

$status = 'Invalid';
$wordai_licence = '';
$licence_status_color = 'red';
$wordai_last_check = __( 'Never', 'feedzy-rss-feeds' );
if ( isset( $this->settings['wordai_licence'] ) ) {
	$wordai_licence = $this->settings['wordai_licence'];
	if ( $wordai_licence == 'yes' ) {
		$status = 'Valid';
		$licence_status_color = '#62c370';
	}
}
if ( isset( $this->settings['wordai_last_check'] ) ) {
	$wordai_last_check = $this->settings['wordai_last_check'];
}
?>
<div id="pro-features">

	<div class="pro-features-header">
		<p class="logo">Feedzy RSS Feeds PRO</p>
		<span class="slogan">by <a
				href="https://themeisle.com/">ThemeIsle</a></span>
	</div><!-- .pro-features-header -->
	<div class="pro-features-content">
		<div class="pro-feature">
			<div id="feedzy_import_feeds" class="pro-feature-features">
				<h2>WordAi</h2>
				<div class="form-group">
					<b><?php echo __( 'API Status:', 'feedzy-rss-feeds' ); ?> </b> <span id="wordai_api_status" style="color:<?php echo $licence_status_color; ?>"><?php echo $status; ?></span><span> <?php echo __( 'Last check: ', 'feedzy-rss-feeds' ) . $wordai_last_check; ?></span>
					<label><?php echo __( 'The WordAi email:', 'feedzy-rss-feeds' ); ?></label>
				</div>
				<div class="form-group">
					<input type="text" id="wordai_username" class="form-control" name="wordai_username" value="<?php echo $wordai_username; ?>" placeholder="<?php echo __( 'WordAi Email', 'feedzy-rss-feeds' ); ?>"/>
				</div>
				<div class="form-group">
					<label><?php echo __( 'The WordAi password:', 'feedzy-rss-feeds' ); ?></label>
				</div>
				<div class="form-group input-group">
					<input type="password" id="wordai_pass" class="form-control" name="wordai_pass" value="<?php echo $wordai_pass; ?>" placeholder="<?php echo __( 'WordAi Password', 'feedzy-rss-feeds' ); ?>"/>
					<div class="input-group-btn">
						<button id="check_wordai_api" type="button" class="btn btn-submit btn-activate" onclick="return ajaxUpdate();"><?php echo __( 'Check & Save', 'feedzy-rss-feeds' ); ?></button>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
	function ajaxUpdate() {

		var wordai_data = {
			'wordai_username': jQuery( '#wordai_username' ).val(),
			'wordai_pass': jQuery( '#wordai_pass' ).val(),
		}

		var data = {
			'action': 'update_settings_page',
			'feedzy_settings': wordai_data,
		};

		jQuery( '#check_wordai_api' ).prop( 'disabled', true );
		jQuery( '#check_wordai_api' ).html('<?php echo __( 'Checking ...', 'feedzy-rss-feeds' ); ?>');
		jQuery.post( ajaxurl, data, function( response ) {
			jQuery( '#check_wordai_api' ).prop( 'disabled', false );
			jQuery( '#check_wordai_api' ).html('<?php echo __( 'Check & Save', 'feedzy-rss-feeds' ); ?>');
			if( response.status != 'Success' ) {
				jQuery( '#wordai_api_status' ).html( 'Invalid! ' + response.error );
			} else {
				jQuery( '#wordai_api_status' ).html( 'Valid! ' );
				jQuery( '#wordai_api_status' ).css( 'color', '#62c370' );
			}
		}, 'json');

		return false;
	};
</script>
