@using AyCode.Core.Extensions
@model AuctionPublicInfoModel



<script asp-location="Footer">

    $(document).ready(function () {
		let currencyCode = @Html.Raw(Model.WorkingCurrencyCode.ToJson());
		let currencyRate = @Html.Raw(Model.WorkingCurrencyRate.ToJson());
		console.log(currencyCode);
	    console.log("PublicInfo ready enter; ProductId: " + @Model.ProductId);
	    let auctionDto = @Html.Raw(Model.AuctionDto.ToJson());
		
	    initialize@(Model.ProductId)(auctionDto);
    });

    function initialize@(Model.ProductId)(auctionDto) {
	    console.log("PublicInfo initialize" + @Model.ProductId + " enter; auctionDto: " + auctionDto);
		let currencyCode = @Html.Raw(Model.WorkingCurrencyCode.ToJson());
		let currencyRate = @Html.Raw(Model.WorkingCurrencyRate.ToJson());
		console.log(currencyCode);
	    if (!auctionDto) {
		    console.error("auctionDto == null");
		    return;
	    }

	    let productToAuctionDto = auctionDto.productToAuctionDtos[0];
	    let productId = productToAuctionDto.productId;
	    let isActive = productToAuctionDto.isActiveItem;

	    console.log("isActiveItem: " + isActive + "; productId:" + productId + "; productToAuctionId: " + productToAuctionDto.id);

	    let productItem = $('.product-item[data-productid="' + productId + '"]');
	    let publicInfoOverlayId = "publicInfoOverlay" + productId;

	    console.log("publicInfoOverlayId: " + publicInfoOverlayId + "; productItem:" + productItem);

	    var existingOverlay = document.getElementById(publicInfoOverlayId);
	    if (existingOverlay) {
		    console.log("remove because it exists");
		    existingOverlay.remove();
	    }

	    // Check if element exists
	    if (productItem.length > 0 && productToAuctionDto.id > 0) {
		    console.log("Product item length:", productItem.length);

		    var widgetPriceElements = productItem.find('.actual-price');
		    //console.log("widgetPriceElements:", widgetPriceElements);

		    if (widgetPriceElements && widgetPriceElements.length > 0) {
				if(currencyCode == "EUR") {
					widgetPriceElements[0].textContent = EURFormatter.format(productToAuctionDto.currentPrice * currencyRate);
				}
				else {

					widgetPriceElements[0].textContent = HUFFormatter.format(productToAuctionDto.currentPrice); // Update the price
				}
		    }

			let myDate = auctionDto.startDateUtc;
			console.log("dates: " + myDate);
			let fixedDate = new Date(myDate + 'Z');
			console.log("dates: " + myDate + ", " + fixedDate);
			let resultDate = new Date(fixedDate);
			console.log("dates: " + myDate + ", " + fixedDate + ", " + resultDate);
			let options = {  
				year: "numeric", month: "numeric",  
				day: "numeric", hour: "2-digit", minute: "2-digit"  
			};  
			let status = productToAuctionDto.auctionStatus;
		    // Add a new div as the first child
		    if (isActive) {
			    console.log("isActive: " + isActive);
			    productItem.prepend('<div id="' +
				    publicInfoOverlayId +
					'" class="bg-success p-1 text-white text-center" style="position: absolute; width: calc(100% - 1rem); height: 50px; z-index: 1;"><p class="m-0"><i class="fa-solid fa-gavel"></i> ' +
					'@T("Plugins.Misc.AuctionPlugin.LiveNow")' +
					'</p><span>' +
					"No.: " + productToAuctionDto.sortIndex +
					'</span></div>');
		    } 
			else if (status == AuctionStatus.Sold || status == AuctionStatus.NotSold) {
			    console.log("isActive: " + isActive);
			    productItem.prepend('<div id="' +
				    publicInfoOverlayId +
					'" class="bg-secondary p-1 text-white text-center" style="position: absolute; width: calc(100% - 1rem); height: 50px; z-index: 1;"><p class="m-0"><i class="fa-solid fa-gavel"></i> ' +
					'@T("Plugins.Misc.AuctionPlugin.Finished")' +
					'</p><span>' + 
					"No.: " + productToAuctionDto.sortIndex +
					'</span></div>');
		    }
			else {
			console.log("isActive: " + isActive);
			    productItem.prepend('<div id="' +
				    publicInfoOverlayId +
					'" class="bg-primary p-1 text-white text-center" style="position: absolute; width: calc(100% - 1rem); height: 50px; z-index: 1;"><p class="m-0"><i class="fa-solid fa-gavel"></i> ' +
					resultDate.toLocaleString([], options) +
					'</p><span>' + 
					"No.: " + productToAuctionDto.sortIndex +
					'</span></div>');
			
			}

	    } else {
		    console.error("Product item not found with productId:", productId);
	    }
    }

    window[`refreshPublicInfo${@Model.ProductId}`] = function(auctionDto) {

	    let productToAuctionDto = auctionDto.productToAuctionDtos[0];

	    console.log(auctionDto);
	    console.log("setting active to " + productToAuctionDto.isActiveItem);

	    initialize@(Model.ProductId)(auctionDto);
        console.log('Function called: refreshPublicInfo' + productToAuctionDto.productId);
    };

    console.log(`Function refreshPublicInfo${@Model.ProductId} added to DOM:`, window[`refreshPublicInfo${@Model.ProductId}`]);

</script>

