<div class="modal fade" id="AI-post-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 TextForMode('generate_ai_post') ?> </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="creditTT"><?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-post-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_post_alert"></div>
							<div class="wow_form_fields">
								<label><?php echo $wo['lang']['write_something_here']?></label>
								<textarea dir="auto" rows="6" name="text" id="aiPostText" placeholder="<?php echo $wo['lang']['tell_me_a_joke']; ?>"></textarea>
							</div>
							<div class="wow_form_fields">
								<label><?php echo $wo['lang']['max_result_length']?></label>
								<input type="number" name="count" max="<?php echo($wo['config']['maxCharacters']); ?>" placeholder="<?php echo $wo['lang']['max_result_length']; ?>" id="aiPostCount" oninput="checkMaxAllowedPostWords(this)">
							</div>
							<div class="wow_form_fields">
								<textarea dir="auto" rows="6" name="text" id="generatedAiPostText" placeholder="<?php echo $wo['lang']['generated_post']; ?>" readonly style="display: none;"></textarea>
							</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-post-button" onclick="openAIPost()"><?php echo $wo['lang']['generate']; ?></button>
				</div>
			</div>
		</form>
	</div>
</div>
<script type="text/javascript">
	function checkMaxAllowedPostWords(self) {
		let max = parseInt("<?php echo($wo['config']['maxCharacters']); ?>");
		<?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 openAIPost() {
		$("#ai-generate-post-button").text("<?php echo($wo['lang']['please_wait']) ?>");
		$("#ai-generate-post-button").attr('disabled',true);
		$('#ai-selected-post-button').remove();
		$.post(Wo_Ajax_Requests_File()+'?f=ai&s=openai_post', {text: $('#aiPostText').val(),count: $('#aiPostCount').val()}, function(data, textStatus, xhr) {
			if (data.status == 200) {
				$('#generatedAiPostText').show();
				<?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']); ?>";
					$('#creditTT').text( getAvailableByType('text'));
					checkMaxAllowedPostWords('#aiPostCount');
				<?php } ?>

				$("#ai-generate-post-button").text("<?php echo $wo['lang']['regenerate']; ?>");
				$("#ai-generate-post-button").removeAttr('disabled');
				$('#generatedAiPostText').val(data.output);
				$("#AI-post-modal .modal-footer").prepend('<button type="button" class="btn main btn-mat" id="ai-selected-post-button" onclick="addAIPost()"><?php echo $wo['lang']['use_text']; ?></button>');
			}
			else{
				$("#ai-generate-post-button").removeAttr('disabled');
				$("#ai-generate-post-button").text("<?php echo($wo['lang']['generate']) ?>");
		    	$('.ai_generate_post_alert').html('<div class="alert alert-danger">' + data.message + '</div>');
		    	setTimeout(() => {
			  		$('.ai_generate_post_alert').html('');
			  	},3000);
			}
		});
	}

	function addAIPost() {
		$('#post-textarea .postText').val($('#generatedAiPostText').val());
		$('#AI-post-modal').modal('hide');
		$('#postSticker').val('');
		$('#isAiPost').val('on');
		$('#ai-selected-post-button').remove();
		$('#AI-post-modal textarea').val('');
	}
	function openPostModel() {
		$('#ai-selected-post-button').remove();
		$('#generatedAiPostText').slideUp();
		$("#ai-generate-post-button").html("<?php echo $wo['lang']['generate']; ?>");
		$('#aiPostText').val('');
		$('#aiPostCount').val('');
		$('#generatedAiPostText').val('');
		$("#ai-generate-post-button").removeAttr('disabled');
		$('.ai_generate_post_alert').html("");
		<?php if ($wo['config']['text_credit_system'] == 1 && $wo['config']['generated_word_price'] > 0) { ?>
			var text = "<?php echo($wo['lang']['available_words_credits']); ?>";
			$('#creditTT').text( getAvailableByType('text'));
			if ($('#current_user_credits').val() < <?php echo($wo['config']['generated_word_price']) ?>) {
				$('#ai-generate-post-button').attr('disabled',true);
				$('.ai_generate_post_alert').html("<div class='alert alert-danger'><?php echo $wo["lang"]["you_dont_have_enough_credits"]?></div>");
			}
		<?php } ?>
		$('#AI-post-modal').modal('show');
	}
</script>