<div template-id="wpfc-modal-varnish" style="display:none; top: 10.5px; left: 226px; position: absolute; padding: 6px; height: auto; width: 560px; z-index: 10001;">
	<div style="height: 100%; width: 100%; background: none repeat scroll 0% 0% rgb(0, 0, 0); position: absolute; top: 0px; left: 0px; z-index: -1; opacity: 0.5; border-radius: 8px;">
	</div>
	<div style="z-index: 600; border-radius: 3px;">
		<div style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:12px;background: none repeat scroll 0px 0px rgb(255, 161, 0); z-index: 1000; position: relative; padding: 2px; border-bottom: 1px solid rgb(194, 122, 0); height: 35px; border-radius: 3px 3px 0px 0px;">
			<table width="100%" height="100%">
				<tbody>
					<tr>
						<td valign="middle" style="vertical-align: middle; font-weight: bold; color: rgb(255, 255, 255); text-shadow: 0px 1px 1px rgba(0, 0, 0, 0.5); padding-left: 10px; font-size: 13px; cursor: move;">Varnish Cache Settings</td>
						<td width="20" align="center" style="vertical-align: middle;"></td>
						<td width="20" align="center" style="vertical-align: middle; font-family: Arial,Helvetica,sans-serif; color: rgb(170, 170, 170); cursor: default;">
							<div title="Close Window" class="close-wiz"></div>
						</td>
					</tr>
				</tbody>
			</table>
		</div>
		<div class="window-content-wrapper" style="padding: 8px;">
			<div style="z-index: 1000; height: auto; position: relative; display: inline-block; width: 100%;" class="window-content">


				<div id="wpfc-wizard-varnish" class="wpfc-cdn-pages-container">
					<div wpfc-cdn-page="1" class="wiz-cont">

						<h1>Varnish Server</h1>		
						<p>Specify the IP address of your varnish instance. If you do not know the ip address, you can ask your hosting provider on what to set here.</p>
						<div class="wiz-input-cont">
							<?php
								$datas = get_option("WpFastestCacheVarnish");
								$ip = isset($datas["server"]) && $datas["server"] ? $datas["server"] : "127.0.0.1";
							?>
							<input value="<?php echo $ip; ?>" type="text" name="server" class="api-key" style="width: 100%;">
					    	<label class="wiz-error-msg"></label>
					    </div>

					    <p class="wpfc-bottom-note" style="margin-bottom:-10px;"><a target="_blank" href="https://www.wpfastestcache.com/features/using-varnish-cache-with-wp-fastest-cache/">Note: Please read this article to learn about this feature.</a></p>



					</div>

					<img style="border-radius:100px;" class="wiz-bg-img" src="data:image/png;base64,[encoded data, 10076 characters]"/>

				</div>
			</div>
		</div>
		<?php include WPFC_MAIN_PATH."templates/buttons.html"; ?>
	</div>
</div>
<script type="text/javascript">
	var WpFcVarnish = {
		init: function(){
			this.click_event_for_add_button();
		},
		click_event_for_add_button: function(){
			let self = this;

			jQuery(".varnish-cache-list > div").click(function(){
				self.open_modal();

			});
		},
		open_modal: function(item){
			var self = this;

			Wpfc_New_Dialog.dialog("wpfc-modal-varnish", {
				close: function(e){
					let modal = jQuery(e).closest("div[id^='wpfc-modal-varnish']");
					let modal_id = modal.attr("id");

					modal.find("label.wiz-error-msg").text("");
					
					Wpfc_New_Dialog.id = modal_id;
					Wpfc_New_Dialog.disable_button("close");

					jQuery.ajax({
						type: 'POST',
						dataType: "json",
						url: ajaxurl,
						data : {"action": "wpfc_purgecache_varnish", security: '<?php echo wp_create_nonce( "wpfc-varnish-ajax-nonce" ); ?>'},
					    success: function(res){
					    	if(!res.success){
					    		modal.find("label.wiz-error-msg").text(res.message);
					    	}

					    	Wpfc_New_Dialog.enable_button("close");
					    },
					    error: function(e) {
					    	alert("unknown error");
					    }
					});

				},
				start: function(e){
					let modal = jQuery(e).closest("div[id^='wpfc-modal-varnish']");
					let modal_id = modal.attr("id");

					modal.find("label.wiz-error-msg").text("");

					Wpfc_New_Dialog.id = modal_id;
					Wpfc_New_Dialog.disable_button("start");

					jQuery.ajax({
						type: 'POST',
						dataType: "json",
						url: ajaxurl,
						data : {"action": "wpfc_start_varnish", security: '<?php echo wp_create_nonce( "wpfc-varnish-ajax-nonce" ); ?>'},
					    success: function(res){
					    	Wpfc_New_Dialog.enable_button("start");

					    	if(res.success === true){
					    		jQuery("div.varnish-cache-list").find("div.meta").removeClass("pause");

					    		Wpfc_New_Dialog.show_button("pause");
					    		Wpfc_New_Dialog.hide_button("start");
					    	}else{
					    		modal.find("label.wiz-error-msg").text(res.message);
					    	}
					    },
					    error: function(e) {
					    	alert("unknown error");
					    }
					});
				},
				pause: function(e){
					let modal = jQuery(e).closest("div[id^='wpfc-modal-varnish']");
					let modal_id = modal.attr("id");

					modal.find("label.wiz-error-msg").text("");

					Wpfc_New_Dialog.id = modal_id;
					Wpfc_New_Dialog.disable_button("pause");

					jQuery.ajax({
						type: 'POST',
						dataType: "json",
						url: ajaxurl,
						data : {"action": "wpfc_pause_varnish", security: '<?php echo wp_create_nonce( "wpfc-varnish-ajax-nonce" ); ?>'},
					    success: function(res){
					    	if(res.success === true){
					    		Wpfc_New_Dialog.enable_button("pause");

					    		jQuery("div.varnish-cache-list").find("div.meta").addClass("pause");

					    		Wpfc_New_Dialog.show_button("start");
					    		Wpfc_New_Dialog.hide_button("pause");
					    	}
					    },
					    error: function(e) {
					    	alert("unknown error");
					    }
					});
				},
				remove: function(e){
					let modal = jQuery(e).closest("div[id^='wpfc-modal-varnish']");
					let modal_id = modal.attr("id");

					Wpfc_New_Dialog.id = modal_id;
					Wpfc_New_Dialog.disable_button("remove");

					jQuery.ajax({
						type: 'POST',
						dataType: "json",
						url: ajaxurl,
						data : {"action": "wpfc_remove_varnish", security: '<?php echo wp_create_nonce( "wpfc-varnish-ajax-nonce" ); ?>'},
					    success: function(res){
					    	if(res.success === true){
					    		Wpfc_New_Dialog.enable_button("remove");

					    		jQuery("div.varnish-cache-list").find("div.meta").removeClass("isConnected");
					    		jQuery("div.varnish-cache-list").find("div.meta").removeClass("pause");
					    		
					    		modal.remove();
					    	}
					    },
					    error: function(e) {
					    	alert("unknown error");
					    }
					});

				},
				finish: function(e){
					let modal = jQuery(e).closest("div[id^='wpfc-modal-varnish']");
					let modal_id = modal.attr("id");
					let server = modal.find("input[name='server']").val();
					let error_label = modal.find("label.wiz-error-msg");

					if(server.length == 0){
						server = "127.0.0.1";
						modal.find("input[name='server']").val(server);
					}

					modal.find("label.wiz-error-msg").text("");

					modal.find("input").attr('disabled', true);

					Wpfc_New_Dialog.id = modal_id;
					Wpfc_New_Dialog.disable_button("finish");

					jQuery.ajax({
						type: 'POST',
						dataType: "json",
						url: ajaxurl,
						data : {"action": "wpfc_save_varnish", "server" : server, security: '<?php echo wp_create_nonce( "wpfc-varnish-ajax-nonce" ); ?>'},
					    success: function(res){
					    	Wpfc_New_Dialog.enable_button("finish");

					    	modal.find("input").attr('disabled', false);

					    	if(!res.success){
					    		modal.find("label.wiz-error-msg").text(res.message);
					    	}else{
					    		jQuery("div#wpfc-wizard-varnish").find("input[name='server']").val(server);
						    	jQuery("div.varnish-cache-list").find("div.meta").addClass("isConnected");

						    	modal.remove();
					    	}
					    },
					    error: function(e) {
					    	Wpfc_New_Dialog.enable_button("finish");

					    	modal.find("input").attr('disabled', false);
					    	modal.find("label.wiz-error-msg").text("Please make sure that your server has Varnish Cache");

					    }
					});

				}
			}, function(dialog){
				if(jQuery("div.varnish-cache-list").find("div.meta.isConnected.pause").length == 1){
					Wpfc_New_Dialog.show_button("remove");
					Wpfc_New_Dialog.show_button("start");
					Wpfc_New_Dialog.show_button("close");
				}else if(jQuery("div.varnish-cache-list").find("div.meta.isConnected").length == 1){
					Wpfc_New_Dialog.show_button("remove");
					Wpfc_New_Dialog.show_button("pause");
					Wpfc_New_Dialog.show_button("close");
				}

				Wpfc_New_Dialog.clone.find("div.window-buttons-wrapper").find("button[action='close']").text("Purge Varnish Cache");
				Wpfc_New_Dialog.show_button("finish");
			});
		}
	}

	WpFcVarnish.init();
</script>
