/**
 * Admin Script for Portfolio Settings
 * 
 * @package Responsive Portfolio Image Gallery 1.3 - 10 July, 2025
 * @link https://www.realwebcare.com/
 */
jQuery(document).ready(function ($) {
	"use strict";

	// Switches option sections
	$('.group').hide();
	var activetab = '';
	if (typeof (localStorage) != 'undefined') {
		activetab = localStorage.getItem("activetab");
	}
	//if url has section id as hash then set it as active or override the current local storage value
	if (window.location.hash) {
		activetab = window.location.hash;
		if (typeof (localStorage) != 'undefined') {
			localStorage.setItem("activetab", activetab);
		}
	}
	if (activetab != '' && $(activetab).length) {
		$(activetab).fadeIn();
	} else {
		$('.group:first').fadeIn();
	}
	$('.group .collapsed').each(function () {
		$(this).find('input:checked').parent().parent().parent().nextAll().each(
			function () {
				if ($(this).hasClass('last')) {
					$(this).removeClass('hidden');
					return false;
				}
				$(this).filter('.hidden').removeClass('hidden');
			});
	});
	if (activetab != '' && $(activetab + '-tab').length) {
		$(activetab + '-tab').addClass('nav-tab-active');
	}
	else {
		$('.nav-tab-wrapper a:first').addClass('nav-tab-active');
	}
	$('.rpgal-notice .rpgal-close-icon').on('click', function (e) {
		$(this).closest('.rpgal-notice').fadeOut(300, function () {
			$(this).remove();
		});
	});
	$('.nav-tab-wrapper a').click(function (evt) {
		$('.nav-tab-wrapper a').removeClass('nav-tab-active');
		$(this).addClass('nav-tab-active').blur();
		var clicked_group = $(this).attr('href');
		if (typeof (localStorage) != 'undefined') {
			localStorage.setItem("activetab", $(this).attr('href'));
		}
		$('.group').hide();
		$(clicked_group).fadeIn();
		evt.preventDefault();
	});
	$('body').on('click', '.rpgal-browse', function (event) {
		event.preventDefault();
		var self = $(this);
		// Create the media frame.
		var file_frame = wp.media.frames.file_frame = wp.media({
			title: self.data('uploader_title'),
			button: {
				text: self.data('uploader_button_text'),
			},
			multiple: false
		});
		file_frame.on('select', function () {
			attachment = file_frame.state().get('selection').first().toJSON();
			self.prev('.rpgal-url').val(attachment.url).change();
		});
		// Finally, open the modal
		file_frame.open();
	});

	var frame;

	$('#rpgal_image_upload').on('click', function (e) {
		e.preventDefault();

		// If we already created the frame, just re‑open it.
		if (frame) {
			frame.open();
			return;
		}

		// Create a new media frame
		frame = wp.media({
			title: 'Select or Upload Images',
			button: {
				text: 'Use this image'
			},
			multiple: true  // allow multiple selection
		});

		// When an image is selected in the media frame...
		frame.on('select', function () {
			var selection = frame.state().get('selection').toArray();
			var $input = $('#image_upload_val');

			selection.forEach(function (attachment) {
				// ensure we have the URL & id
				var url = attachment.attributes.url,
					id = attachment.id;

				// append to our hidden input
				var current = $input.val() ? $input.val().split(',') : [];
				current.push(id);
				$input.val(current.join(','));

				// show the thumbnail preview
				$input.before(
					'<img width="63" height="63" src="' + url + '" data-id="' + id + '" class="attachment-63x63" />'
				);
			});
		});

		// Finally, open the modal
		frame.open();
	});

	$('body').on('click', '#rpgal_portfolio_meta img', function () {
		var answer = confirm("Are you sure you want to delete?");
		if (answer) {
			var valArr = $('#image_upload_val').val().split(',');
			console.log(valArr);
			var index = valArr.indexOf($(this).attr('data-id'));
			if (index > -1) {
				valArr.splice(index, 1);
				$(this).remove();
			}
			console.log(valArr);
			$('#image_upload_val').val(valArr.toString());
		}
	});

	$('body').on('click', ".copy-tooltip", function () {
		var shortcode = "[rpgal-gallery]"; // Define the shortcode to copy

		// Create a temporary input element
		var tempInput = $("<input>");
		$("body").append(tempInput);
		tempInput.val(shortcode).select();
		document.execCommand("copy");
		tempInput.remove();

		// Update tooltip text
		$("#rpgalTooltip").text("Copied!");

		// Reset tooltip text after 2 seconds
		setTimeout(function () {
			$("#rpgalTooltip").text("Click to Copy Shortcode!");
		}, 2000);
	});
});