$(document).ready(function () {
	search_operation = $("#news_inmotek").data("ope") != '' ? $("#news_inmotek").data("ope") : '';
	search_group = $("#news_inmotek").data("sg") == '1' ? true : false;
	/*$.post(get_base_url() + '/' + get_lang() + '/action/get_data_property_news', {
		operation: search_operation,
		search_group: search_group,
		check_filter_groups: "1",
		limit: $("#news_inmotek").data("nw")
	}).done(function (result) {
		let obj = JSON.parse(result);
		create_box_news(obj);
	});*/
});

function create_box_news(obj) {
	let is_mobile = false;
	/*if(navigator.userAgent.match(/Android/i)
	|| navigator.userAgent.match(/webOS/i)
	|| navigator.userAgent.match(/iPhone/i)
	|| navigator.userAgent.match(/iPad/i)
	|| navigator.userAgent.match(/iPod/i)
	|| navigator.userAgent.match(/BlackBerry/i)
	|| navigator.userAgent.match(/Windows Phone/i)) {
		is_mobile = true;
	}*/
	if (window.innerWidth <= 767) {
		is_mobile = true;
	}
	obj = obj['news'];
	let $html_content = "";
	$("#news_inmotek").html($html_content);

	html_content = '<div class="row news_row">';
	i = 1;
	locale = get_locale();
	obj.forEach(function (data) {

		data['propertyMedia']['url_large'] = clean_image_path(data['propertyMedia']['url_large']);

		let price_sale = data['propertyOperation']['operationType'].includes("v") ? data['propertyOperation']['operationPriceSale'] : "";
		let price_rent = data['propertyOperation']['operationType'].includes("a") ? data['propertyOperation']['operationPriceRent'] : "";
		let price_sale_before = data['propertyOperation']['operationType'].includes("v") ? data['propertyOperation']['operationPricePreviousSale'] : 0;
		price_sale_before = numberWithCommas(price_sale_before, locale.separador_miles);
		let price_rent_before = data['propertyOperation']['operationType'].includes("a") ? data['propertyOperation']['operationPricePreviousRent'] : 0;
		price_rent_before = numberWithCommas(price_rent_before, locale.separador_miles);
		let price_sale_visibility = data['propertyOperation']['operationType'].includes("v") ? data['propertyOperation']['operationVisibilitySale'] : false;
		let price_rent_visibility = data['propertyOperation']['operationType'].includes("a") ? data['propertyOperation']['operationVisibilityRent'] : false;

		if (price_sale != "") {
			price_sale = ((price_sale * 1) > 1 && price_sale_visibility) ? numberWithCommas(price_sale, locale.separador_miles) + " €" : T("PRECIO_A_CONSULTAR");
		}

		if (price_rent != "") {
			price_rent = ((price_rent * 1) > 1 && price_rent_visibility) ? numberWithCommas(price_rent, locale.separador_miles) + " €" : T("PRECIO_A_CONSULTAR");
		}

		let habs = "";
		if (data['propertyFeatures']['featuresType'] == "Local" || data['propertyFeatures']['featuresType'] == "Oficina" || data['propertyFeatures']['featuresType'] == "Nave") {
			habs = (data['propertyFeatures']['featuresRooms'] * 1) >= 1 ? data['propertyFeatures']['featuresRooms'] + " <i class='google-symbols-meeting-room' title='" + T("HABITACIONES") + "'></i> " : "";
		} else {
			habs = (data['propertyFeatures']['featuresRooms'] * 1) >= 1 ? data['propertyFeatures']['featuresRooms'] + " <i class='google-symbols-bed' title='" + T("HABITACIONES") + "'></i> " : "";
		}

		let banos = "";
		if (data['propertyFeatures']['featuresType'] == "Local" || data['propertyFeatures']['featuresType'] == "Oficina" || data['propertyFeatures']['featuresType'] == "Nave") {
			banos = (data['propertyFeatures']['featuresBathroomNumber'] * 1) >= 1 ? (data['propertyFeatures']['featuresBathroomNumber'] + " <i class='google-symbols-wc' title='" + T("BANOS") + "'></i> ") : "";
		} else {
			banos = (data['propertyFeatures']['featuresBathroomNumber'] * 1) >= 1 ? (data['propertyFeatures']['featuresBathroomNumber'] + " <i class='google-symbols-bathtub' title='" + T("BANOS") + "'></i> ") : "";
		}

		let garaje = data['propertyFeatures']['featuresGarage'] === true ? " <i class='google-symbols-directions-car' title='" + T("GARAJE") + "'></i> " : "";
		let piscina = data['propertyFeatures']['featuresPool'] === true ? " <i class='google-symbols-pool' title='" + T("PISCINA") + "'></i> " : "";
		let trastero = data['propertyFeatures']['featuresStorage'] === true ? " <i class='google-symbols-garage-home' title='" + T("TRASTERO") + "'></i> " : "";
		let ascensor = data['propertyFeatures']['featuresLift'] === true ? " <i class='google-symbols-elevator' title='" + T("ASCENSOR") + "'></i> " : "";
		let terraza = data['propertyFeatures']['featuresTerrace'] === true ? " <i class='google-symbols-deck' title='" + T("TERRAZA") + "'></i> " : "";
		let calefaccion = data['propertyFeatures']['featuresHeating'] === true ? " <i class='google-symbols-heat-pump-balance' title='" + T("CALEFACCION") + "'></i> " : "";
		let aire_acondicionado = data['propertyFeatures']['featuresConditionedAir'] === true ? " <i class='google-symbols-air-ware' title='" + T("AIRE") + "'></i> " : "";
		let virtual_tour_3d = data['propertyFeatures']['propertyVirtualTours3D'] === true ? " <i class='google-symbols-3d-rotation' title='" + T("TOUR_VIRTUAL") + "'></i> " : "";
		let title = data['propertyTitle'] == "" ? T("X_EN_X", { param1: data['propertyFeatures']['featuresType'], param2: ((data['propertyAddress']['addressTown'] == "" || data['propertyAddress']['addressTown'] == null) ? (data['propertyOperation']['operationType'] == "a" ? T("ALQUILER") : T("VENTA")) : capitalizeFirstLetter(data['propertyAddress']['addressTown'])) }) : capitalizeFirstLetter(data['propertyTitle']);
		let text_seo = "";
		let image_default = document.documentElement.dataset.medefault;

		//Obtenemos el dato de las metaetiquetas
		if (document.documentElement.dataset.cmsprm1 == "1") {
			text_seo = url_text_seo_generate(data['propertyFeatures']['featuresType'], data['propertyOperation']['operationType'], data['propertyAddress']['addressTown'], title)
		} else if (document.documentElement.dataset.cmsprm1 == "2") {
			text_seo = url_text_seo_generate(data['propertyFeatures']['featuresType'], data['propertyOperation']['operationType'], data['propertyAddress']['addressTown'])
		}

		html_content +=
			`<div class="col-lg-${((12 / $('#news_inmotek').data('cols')))} col-md-6 col-sm-12">
				<a href="/${get_lang()}/${get_name_controller_by_lang('property')}${(text_seo != "") ? (text_seo + "-") : "/"}${data['propertyCode']}">
					<div class="img-card-nw">
						<div class="labels-wrap labels-right" style="position: absolute; z-index: 1; top: 17px; right: 20px;">
							<span class="label-operation label background-primary-color" style="font-size: 10px;line-height: 11px;font-weight: 500;margin: 0;text-transform: uppercase;padding: 3px 5px;color: #fff;border-radius: 2px;">
							${T("NOVEDAD")}
							</span>
						</div>`;
		if (is_mobile && get_codename() != "ceren") {
			html_content += `<img alt="${data['propertyCode']}" loading="lazy" onerror="this.onerror=null;this.src="${image_default}" src="${clean_image_path(data['propertyMedia']['url'])}"/>`;
		} else {
			html_content += `<img alt="${data['propertyCode']}" loading="lazy" onerror="this.onerror=null;this.src='${image_default}'" src="${clean_image_path(data['propertyMedia']['url_large'])}"/>`;
		}
		html_content += `<div class="text-card-nw">
		<div class="row">
			<div class="col-12 title-text-card-nw">${title}</div>
			<div class="col-12 address-text-card-nw">Ref: <span class="text-primary-color">${(data['propertyReference'] == '' ? "<span class='p_c'>" + data['propertyCode'] + "</span>" : "<span class='p_r'>" + data['propertyReference'] + "</span>")} - ${(data['propertyAddress']['addressZone'] !== null ? "<span class='p_z'>" + data['propertyAddress']['addressZone'] + ',</span> ' : '')}${(data['propertyAddress']['addressTown'] != null ? ("<span class='p_t'>" + data['propertyAddress']['addressTown'] + "</span>") : "")}</span></div>
		</div>
		<div class="row">
			<div class="col-12 card-features">
			${habs}
			${banos}
			${ascensor}
			${garaje}
			${piscina}
			${terraza}
			${trastero}
			${calefaccion}
			${aire_acondicionado}
			${virtual_tour_3d}
			</div>`;
		if (((price_sale_before * 1) > 1 && price_sale_visibility) || ((price_rent_before * 1) > 1 && price_rent_visibility)) {
			html_content +=
				`<div class="col-12 text-right price-before-text-card-nw">`;
			if ((price_sale_before * 1) > 1) {
				html_content +=
					`<span>${T("ANTES_VENTA")}: </span>
											<span style="text-decoration: line-through;">${price_sale_before}€</span>`;
			}
			if ((price_sale_before * 1) > 1 && (price_rent_before * 1) > 1) {
				html_content += " - ";
			}
			if ((price_rent_before * 1) > 1) {
				html_content +=
					`<span>${T("ANTES_ALQUILER")}: </span>
											<span style="text-decoration: line-through;">${price_rent_before}€</span>`;
			}
			html_content +=
				`</div>`;
		}
		html_content += `<div class="col-12 text-right">	
									<div class="price-text-card-nw text-primary-color">${price_sale != "" ? (T("VENTA") + " " + price_sale) : ""}${(price_sale != "" && price_rent != "") ? " - " : ""}${price_rent != "" ? (T("ALQUILER") + " " + price_rent) : ""}${(price_sale == "" && price_rent == "") ? T("PRECIO_A_CONSULTAR") : ""}</div>
								</div>
							</div>
						</div>
					</div>	
				</a>
			</div>`;
		i++;
	});
	html_content += '</div>';
	$("#news_inmotek").append(html_content);
}