<div class="modal fade" id="AI-blog-modal" role="dialog">
	<div class="modal-dialog modal-lg wow_mat_mdl">
		<form method="post">
			<div class="modal-content">
				<img class="ai_alert_img" src="<?php echo $wo['config']['theme_url'];?>/img/robot.png">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-x"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg></span></button>
					<h4 class="modal-title"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="512" height="512" x="0" y="0" viewBox="0 0 512 512" xml:space="preserve" class=""><g><path fill="#4fcde1" d="M391 81h30V15c0-8.284-6.716-15-15-15-8.284 0-15 6.716-15 15zM331 81h30V15c0-8.284-6.716-15-15-15-8.284 0-15 6.716-15 15zM271 81h30V15c0-8.284-6.716-15-15-15-8.284 0-15 6.716-15 15z" data-original="#39326c" class=""></path><path fill="#4fcde1" d="M211 81h30V15c0-8.284-6.716-15-15-15-8.284 0-15 6.716-15 15zM151 81h30V15c0-8.284-6.716-15-15-15-8.284 0-15 6.716-15 15zM91 81h30V15c0-8.284-6.716-15-15-15-8.284 0-15 6.716-15 15z" data-original="#5f55af" class=""></path><path fill="#4fcde1" d="M406 512c8.284 0 15-6.716 15-15v-66h-30v66c0 8.284 6.716 15 15 15zM346 512c8.284 0 15-6.716 15-15v-66h-30v66c0 8.284 6.716 15 15 15zM286 512c8.284 0 15-6.716 15-15v-66h-30v66c0 8.284 6.716 15 15 15z" data-original="#39326c" class=""></path><g fill="#5f55af"><path d="M226 512c8.284 0 15-6.716 15-15v-66h-30v66c0 8.284 6.716 15 15 15zM166 512c8.284 0 15-6.716 15-15v-66h-30v66c0 8.284 6.716 15 15 15zM106 512c8.284 0 15-6.716 15-15v-66H91v66c0 8.284 6.716 15 15 15zM15 121h66V91H15c-8.284 0-15 6.716-15 15 0 8.284 6.716 15 15 15zM15 181h66v-30H15c-8.284 0-15 6.716-15 15 0 8.284 6.716 15 15 15zM15 241h66v-30H15c-8.284 0-15 6.716-15 15 0 8.284 6.716 15 15 15zM15 301h66v-30H15c-8.284 0-15 6.716-15 15 0 8.284 6.716 15 15 15zM15 361h66v-30H15c-8.284 0-15 6.716-15 15 0 8.284 6.716 15 15 15zM15 421h66v-30H15c-8.284 0-15 6.716-15 15 0 8.284 6.716 15 15 15z" fill="#4fcde1" data-original="#5f55af" class=""></path></g><path fill="#4fcde1" d="M431 91v30h66c8.284 0 15-6.716 15-15 0-8.284-6.716-15-15-15zM431 181h66c8.284 0 15-6.716 15-15 0-8.284-6.716-15-15-15h-66zM431 241h66c8.284 0 15-6.716 15-15 0-8.284-6.716-15-15-15h-66zM431 301h66c8.284 0 15-6.716 15-15 0-8.284-6.716-15-15-15h-66zM431 361h66c8.284 0 15-6.716 15-15 0-8.284-6.716-15-15-15h-66zM431 421h66c8.284 0 15-6.716 15-15 0-8.284-6.716-15-15-15h-66z" data-original="#39326c" class=""></path><path fill="#e8fcff" d="M446 51H66c-8.284 0-15 6.716-15 15v380c0 8.284 6.716 15 15 15h380c8.284 0 15-6.716 15-15V66c0-8.284-6.716-15-15-15z" data-original="#aed0ff" class=""></path><path fill="#e8fcff" d="M461 446V66c0-8.284-6.716-15-15-15H256v410h190c8.284 0 15-6.716 15-15z" data-original="#7c84e8" class=""></path><path fill="#4fcde1" d="M386 111H126c-8.284 0-15 6.716-15 15v260c0 8.284 6.716 15 15 15h260c8.284 0 15-6.716 15-15V126c0-8.284-6.716-15-15-15z" data-original="#5f55af" class=""></path><path fill="#4fcde1" d="M401 386V126c0-8.284-6.716-15-15-15H256v290h130c8.284 0 15-6.716 15-15z" data-original="#39326c" class=""></path><path fill="#ffffff" d="M247.626 192.389A17.657 17.657 0 0 0 231.077 181h-.018a17.658 17.658 0 0 0-16.517 11.351l-45.06 118.31c-2.949 7.742.937 16.408 8.679 19.356 7.742 2.95 16.408-.937 19.356-8.679l7.543-19.804h51.691l7.458 19.762c2.267 6.007 7.974 9.708 14.036 9.708 1.76 0 3.55-.312 5.294-.97 7.75-2.925 11.663-11.579 8.737-19.33zm-31.14 79.146 14.538-38.171 14.406 38.171z" data-original="#f9f9f9" class=""></path><g fill="#e2dff4"><path d="M264.209 321.296c2.267 6.007 7.974 9.708 14.036 9.708 1.76 0 3.55-.312 5.294-.97 7.75-2.925 11.663-11.579 8.737-19.33L256 214.578v86.956h.751zM328.5 181c-8.284 0-15 6.716-15 15v120c0 8.284 6.716 15 15 15s15-6.716 15-15V196c0-8.284-6.716-15-15-15z" fill="#ffffff" data-original="#e2dff4" class=""></path></g></g></svg> <?php echo $wo['lang']['create_new_article_ai']; ?></h4>
				</div>
				
				<div class="modal-body">
					<div class="row">
						<div class="<?php if ($wo['config']['text_credit_system'] == 1 && $wo['config']['generated_word_price'] > 0) { ?>col-lg-5<?php } else { ?>col-lg-12<?php } ?>">
							<?php if ($wo['config']['text_credit_system'] == 1 && $wo['config']['generated_word_price'] > 0) { ?>
								<div class="ai_alert_balance less_margin">
									<p><?php echo $wo['lang']['available_balance']?></p>
									<b id="creditTB"><?php echo getAvailableWordBalance() ?></b>
									<p><?php echo $wo['lang']['ai_words']?></p>

									<button type="button" class="btn btn-mat" id="buy-ai-image-button" onclick="openAICredit('AI-blog-modal')"><?php echo $wo['lang']['buy_credit']; ?></button>
								</div>
							<?php } ?>
						</div>
						<div class="<?php if ($wo['config']['text_credit_system'] == 1 && $wo['config']['generated_word_price'] > 0) { ?>col-lg-7<?php } else { ?>col-lg-12<?php } ?>">
							<div class="ai_generate_blog_alert"></div>
							<div class="wow_form_fields">
								<label><?php echo $wo['lang']['write_something_here']?></label>
								<textarea dir="auto" rows="6" name="text" id="aiBlogText" placeholder="<?php echo $wo['lang']['describe_article']; ?>"></textarea>
							</div>
							<div class="wow_form_fields">
								<input type="number" name="count" max="<?php echo($wo['config']['maxCharacters']); ?>" placeholder="<?php echo $wo['lang']['max_result_length_blog']; ?>" id="aiBlogCount" oninput="checkMaxAllowedBlogWords(this)">
							</div>
							<div class="wow_form_fields">
								<div class="round-check">
									<input type="checkbox" id="ai_thumbnail" name="ai_thumbnail">
									<label class="ai_blog_check_thumb" for="ai_thumbnail"><?php echo $wo['lang']['generate_thumbnail']; ?></label>
									<div class="help-block"><?php echo $wo['lang']['thumbnail_based_content']; ?></div>
								</div>
							</div>
						</div>
					</div>
					
					<input type="hidden" name="user" value="<?php echo $wo['user']['user_id'];?>">
				</div>
				<div class="modal-footer" style="border: none">
					<div class="ball-pulse"><div></div><div></div><div></div></div>

					<button type="button" class="btn main btn-mat" id="ai-generate-blog-button" onclick="generateBlog(this)"><?php echo $wo['lang']['generate']; ?></button>
				</div>
			</div>
		</form>
	</div>
</div>
<script type="text/javascript">
	function refreshBlogInfo() {
		var text = "<?php echo($wo['lang']['available_words_credits']); ?>";
		console.log(text.replace("{credit}", getAvailableByType('text')))
		$('#creditTB').text( getAvailableByType('text'));
		$('#aiBlogCount').attr('max',getAvailableByType('text'));
		$('#aiBlogCount').val(getAvailableByType('text'));
	}
	function checkMaxAllowedBlogWords(self) {
		let max = 10000;
		<?php if ($wo['config']['text_credit_system'] == 1 && $wo['config']['generated_word_price'] > 0) { ?>
			let paid = parseInt(parseInt($('#current_user_credits').val()) / parseInt("<?php echo($wo['config']['generated_word_price']) ?>"));
			if (paid < max) {
				max = paid;
			}
		<?php } ?>

		if (parseInt($(self).val()) > max) {
			$(self).val(max);
			$(self).attr('max',max);
		}
	}
	function openAIBlog() {
		$("#ai-generate-blog-button").html("<?php echo $wo['lang']['generate']; ?>");
		$('#aiBlogCount').val('');
		$( "#ai_thumbnail" ).prop( "checked", false );
		$('#AI-blog-modal').modal('show');
	}
	function generateBlog(self) {
		$("#AI-blog-modal .ball-pulse").fadeIn();
		$(self).html("<?php echo($wo['lang']['please_wait']) ?>");
		$(self).attr('disabled',true);
		thumbnail = 'off';
        if ($('#ai_thumbnail').is(":checked") === true) {
            thumbnail = 'on';
        }
		$.post(Wo_Ajax_Requests_File()+'?f=ai&s=generateBlog', {text: $('#aiBlogText').val(),count: $('#aiBlogCount').val(),thumbnail:thumbnail}, function(data, textStatus, xhr) {
			$(self).html("<?php echo $wo['lang']['generate']; ?>");
			$("#AI-blog-modal .ball-pulse").fadeOut();
			$(self).removeAttr('disabled');
			if (data.status == 200) {
				$('#AI-blog-modal').modal('hide');
				<?php if ($wo['config']['text_credit_system'] == 1 && $wo['config']['generated_word_price'] > 0) { ?>
					$('#current_user_credits').val(data.credits);
					var text = "<?php echo($wo['lang']['available_words_credits']); ?>";
					$('#creditTB').text( getAvailableByType('text'));
					$('#aiBlogCount').attr('max',getAvailableByType('text'));
					//$('#aiBlogCount').val(getAvailableByType('text'));
				<?php } ?>
				$('#blog_title').val(data.title);
				$('#new-blog-desc').val(data.description);
				$('#blog').val(data.content);
				$('#blog_tags').tagsinput('add', data.tags);
				tinymce.get('blog').setContent(data.content);
				if (data.output && data.output.length > 0) {
					let images = getConvertedImages(data.output);
				}
				$('#isAiPost').val('on');
			}
			else{
				$('.ai_generate_blog_alert').html('<div class="alert alert-danger">' + data.message + '</div>');
		    	setTimeout(() => {
			  		$('.ai_generate_blog_alert').html('');
			  	},3000);
			}
		});
	}

	function getConvertedImages(output) {
		let outputImages = [];
		var ii = 0;
		for (var i = 0; i < output.length; i++) {
			dataUrl = output[i];
		    var fileData = imageDataToFile(dataUrl, "Ai_image_"+ii+".jpg");
		    outputImages.push(fileData);
		    $("#thumbnail")[0].files = new FileListItems(outputImages);
          	$("#wow_fcov_img_holder").html("<img src='" + dataUrl + "' alt='Picture'>")
		}
	}
</script>