var current = {};
var selected = {};
var inputs = {};
var path = "";
var program = null;

function formCatalogFilter(p, pr, props)
{
	console.log("FILTER", "program", pr);
	path = p;
	selected = props;
	current = props;
	program = pr;

	$(".filter_header .tabs .tab").on("click", function()
	{
		$(".filter_header .tabs .tab").removeClass("active");
		$(this).addClass("active");
	});
}

function updateCounter(parameters)
{
	console.log("updateCounter", parameters);

	$("#catalog_filter_spinner").show();
	$("#catalog_filter_search_button_counter").hide();

	var params = {};
	if(program !== null)
	{
		params['PROGRAM'] = program;
	}
	for(let i in parameters)
	{
		if(parameters[i] !== undefined) { params[i] = parameters[i]; }
	}

	$.get("/api/catalog/count/", params, function(response)
	{
		$("#catalog_filter_spinner").hide();
		$("#catalog_filter_search_button_counter").text("Показать "+response.total+" вариантов").show();
		$("#catalog_small_find").text("Показать "+response.total+" вариантов");
	}, "json");
}

function loadModels(uid)
{
	$.post("/api/catalog/models/", 
	{
		BRAND_UID: uid
	}, function(response)
	{
		console.log("loadModels", selected['MODEL_ID'], selected['MODEL']);

		$("#small_filter_models").empty();
		$("#filter_models").empty();

		if(selected['MODEL_ID'] !== undefined)
		{
			$("#small_filter_models").append("<option value=''>Любая</option>");
			$("#filter_models").append("<option value=''>Любая</option>");
		}
		else
		{
			if(selected['MODEL'] !== undefined)
			{
				$("#small_filter_models").append("<option value=''>Любая</option>");
				$("#filter_models").append("<option value=''>Любая</option>");
			}
			else
			{
				$("#small_filter_models").append("<option value='' disabled>Модель</option>");
				$("#filter_models").append("<option value='' disabled>Модель</option>");
			}
		}
		if($("#small_filter_models")[0])
		{
			$("#small_filter_models")[0].selectedIndex = 0;
		}
		$("#filter_models")[0].selectedIndex = 0;

		for(let i in response.models)
		{
			$("#small_filter_models").append("<option value='"+response.models[i]['CODE']+"' data-id='"+response.models[i]['ID']+"' data-uid='"+response.models[i]['UID']+"'>"+response.models[i]['NAME']+"</option>");
			$("#filter_models").append("<option value='"+response.models[i]['CODE']+"' data-id='"+response.models[i]['ID']+"' data-uid='"+response.models[i]['UID']+"'>"+response.models[i]['NAME']+"</option>");
		}

		if(selected['MODEL'] !== undefined)
		{
			$("#small_filter_models").val(selected['MODEL']);
			$("#filter_models").val(selected['MODEL']);

			let id = $("#filter_models").find(':selected').data('id');
			let uid = $("#filter_models").find(':selected').data('uid');
			selected['MODEL_ID'] = id;

			loadModifications(uid);
			updateCounter(selected);
		}
		else
		{
			updateCounter(selected);
		}
	}, "json");
}

function loadModifications(uid)
{
	console.log("loadModifications", uid);
	$.post("/api/catalog/modifications/", 
	{
		MODEL_UID: uid
	}, function(response)
	{
		console.log(response);
		console.log("loadModifications", selected['MODIFICATION']);

		$("#filter_modifications").empty();

		if(selected['MODIFICATION'] !== undefined)
		{
			$("#filter_modifications").append("<option value=''>Любая</option>");
		}
		else
		{
			$("#filter_modifications").append("<option value='' disabled>Модификация</option>");
		}

		$("#filter_modifications")[0].selectedIndex = 0;

		for(let i in response.modifications)
		{
			$("#filter_modifications").append("<option value='"+response.modifications[i]['ID']+"'>"+response.modifications[i]['NAME']+"</option>");
		}

		updateCounter(selected);
		if(selected['MODIFICATION'] !== undefined)
		{
			$("#filter_modifications").val(selected['MODIFICATION']);
		}
		else
		{
			updateCounter(selected);
		}
	}, "json");
}

$(function()
{
	$("#catalog_filter_form").on("submit", function(event)
	{
		event.preventDefault();
	});

	$.get("/api/catalog/brands/", 
	{
		params: {}, 
	}, function(response)
	{
		console.log("selected", selected);
		//$("#small_filter_brands").empty();
		//$("#small_filter_brands").find("option").first().text("Любая").attr("disabled", false);
		//$("#filter_brands").find("option").first().text("Любая").attr("disabled", false);

		for(let i in response.brands)
		{
			$("#small_filter_brands").append("<option value='"+response.brands[i]['CODE']+"' data-id='"+response.brands[i]['ID']+"' data-uid='"+response.brands[i]['UID']+"'>"+response.brands[i]['NAME']+"</option>");
			$("#filter_brands").append("<option value='"+response.brands[i]['CODE']+"' data-id='"+response.brands[i]['ID']+"' data-uid='"+response.brands[i]['UID']+"'>"+response.brands[i]['NAME']+"</option>");
		}

		if(selected['BRAND'] !== undefined)
		{
			$("#small_filter_brands").find("option").first().text("Любая").attr("disabled", false).val('');
			$("#filter_brands").find("option").first().text("Любая").attr("disabled", false).val('');

			$("#small_filter_brands").val(selected['BRAND']);
			$("#filter_brands").val(selected['BRAND']);

			let uid = $("#filter_brands").find(':selected').data('uid');
			let id = $("#filter_brands").find(':selected').data('id');
			selected['BRAND_ID'] = id;

			loadModels(uid);
		}
		else
		{
			updateCounter(selected);
		}
	}, "json");

	$("#small_filter_brands").on("change", function(event)
	{
		$("#filter_brands").val(event.target.value);
		selected['MODEL'] = undefined;
		selected['MODEL_ID'] = undefined;
		selected['MODIFICATION'] = undefined;

		if($(this).val() === "")
		{
			selected['BRAND'] = undefined;
			selected['BRAND_ID'] = undefined;

			$("#small_filter_brands").find("option").first().text("Марка").attr("disabled", "disabled");
			$("#small_filter_models").empty();
			$("#small_filter_models").append("<option value='' disabled selected>Модель</option>");
			$("#filter_brands").find("option").first().text("Марка").attr("disabled", "disabled");
			$("#filter_models").empty();
			$("#filter_models").append("<option value='' disabled selected>Модель</option>");
			$("#filter_modifications").empty();
			$("#filter_modifications").append("<option disabled selected>Модификация</option>");

			updateCounter(selected);
		}
		else
		{
			let uid = $(this).find(':selected').data('uid');
			let id = $(this).find(':selected').data('id');
			selected['BRAND_ID'] = id;

			$("#small_filter_brands").find("option").first().text("Любая").attr("disabled", false);
			$("#filter_brands").find("option").first().text("Любая").attr("disabled", false);
			$("#filter_modifications").empty();
			$("#filter_modifications").append("<option disabled selected>Модификация</option>");

			loadModels(uid);
		}
	});

	$("#small_filter_models").on("change", function(event)
	{
		if($(this).val() === "")
		{
			$("#filter_models").val();

			selected['MODEL'] = undefined;
			selected['MODEL_ID'] = undefined;
			selected['MODIFICATION'] = undefined;

			$("#small_filter_models").find("option").first().text("Модель").attr("disabled", "disabled");
			$("#filter_models").find("option").first().text("Модель").attr("disabled", "disabled");
			$("#filter_modifications").empty();
			$("#filter_modifications").append("<option disabled selected>Модификация</option>");
		}
		else
		{
			let id = $(this).find(':selected').data('id');
			let uid = $(this).find(':selected').data('uid');
			$("#filter_models").val(event.target.value);

			selected['MODEL'] = id;
			selected['MODEL_ID'] = id;
			selected['MODIFICATION'] = undefined;

			$("#small_filter_models").find("option").first().text("Любая").attr("disabled", false);
			$("#filter_models").find("option").first().text("Любая").attr("disabled", false);
			$("#filter_modifications").empty();
			$("#filter_modifications").append("<option disabled selected>Модификация</option>");

			loadModifications(uid);
		}

		updateCounter(selected);
	});

	$("#filter_brands").on("change", function(event)
	{
		console.log("BBBBB", $(this).val());
		if($(this).val() === '')
		{
			selected['BRAND'] = undefined;
			selected['BRAND_ID'] = undefined;
			selected['MODEL'] = undefined;
			selected['MODEL_ID'] = undefined;
			selected['MODIFICATION'] = undefined;

			$("#small_filter_brands").find("option").first().text("Марка").attr("disabled", "disabled");
			$("#small_filter_models").empty();
			$("#small_filter_models").append("<option disabled selected>Модель</option>");
			$("#filter_brands").find("option").first().text("Марка").attr("disabled", "disabled");
			$("#filter_models").empty();
			$("#filter_models").append("<option disabled selected>Модель</option>");
			$("#filter_modifications").empty();
			$("#filter_modifications").append("<option disabled selected>Модификация</option>");

			updateCounter(selected);
		}
		else
		{
			let id = $(this).find(':selected').data('id');
			let uid = $(this).find(':selected').data('uid');
			selected['BRAND'] = $(this).val();
			selected['BRAND_ID'] = id;
			selected['MODEL'] = undefined;
			selected['MODEL_ID'] = undefined;
			selected['MODIFICATION'] = undefined;

			$("#small_filter_brands").find("option").first().text("Любая").attr("disabled", false);
			$("#filter_brands").find("option").first().text("Любая").attr("disabled", false);
			$("#filter_modifications").empty();
			$("#filter_modifications").append("<option disabled selected>Модификация</option>");

			loadModels(uid);
		}
	});

	$("#filter_models").on("change", function(event)
	{
		if($(this).val() === "")
		{
			selected['MODEL'] = undefined;
			selected['MODEL_ID'] = undefined;
			selected['MODIFICATION'] = undefined;

			$("#filter_models").find("option").first().text("Модель").attr("disabled", "disabled");
		}
		else
		{
			let id = $(this).find(':selected').data('id');
			let uid = $(this).find(':selected').data('uid');
			selected['MODEL_ID'] = id;
			selected['MODIFICATION'] = undefined;

			$("#filter_models").find("option").first().text("Любая").attr("disabled", false);
			loadModifications(uid);
		}

		updateCounter(selected);
	});

	$("#filter_modifications").on("change", function(event)
	{
		if($(this).val() === "")
		{
			selected['MODIFICATION'] = undefined;

			$("#filter_modifications").find("option").first().text("Модификация").attr("disabled", "disabled");
		}
		else
		{
			selected['MODIFICATION'] = $(this).val();
			$("#filter_modifications").find("option").first().text("Любая").attr("disabled", false);
		}

		updateCounter(selected);
	});

	$("#catalog_small_form").on("submit", function(e)
	{
		e.preventDefault();
	});

	$("#catalog_small_find").on("click", function()
	{
		var p = path;

		if($("#small_filter_brands").val() !== "" && $("#small_filter_brands").val() !== null)
		{
			p = p + $("#small_filter_brands").val()+"/";
		}
		if($("#small_filter_models").val() !== "" && $("#small_filter_models").val() !== null)
		{
			p = p + $("#small_filter_models").val()+"/";
		}

		window.location.href = p;
		//$("#catalog_small_form").submit();
	});

	$("#rich_filter_toggle").on("click", function()
	{
		updateCounter(selected);
		$("#filter").addClass("visible").css("display", "block");
		$("#catalog_small_filter").addClass("hidden").css("display", "none");
	});

	$.get("/api/catalog/parameters/", 
	{
		params: {}, 
	}, function(response)
	{
		for(let i in response.bodies)
		{
			$("#filter_bodies").append("<option value='"+response.bodies[i]+"'>"+response.bodies[i]+"</option>");
		}
		if(selected['BODY'] !== undefined && selected['BODY'] !== null && selected['BODY'] !== "")
		{
			$("#filter_bodies").val(selected['BODY']);
			$("#filter_bodies").find("option").first().text("Любой").attr("disabled", false);
		}

		for(let i in response.gears)
		{
			$("#filter_gears").append("<option value='"+response.gears[i]+"'>"+response.gears[i]+"</option>");
		}
		if(selected['GEAR'] !== undefined && selected['GEAR'] !== null && selected['GEAR'] !== "")
		{
			$("#filter_gears").val(selected['GEAR']);
			$("#filter_gears").find("option").first().text("Любая").attr("disabled", false);
		}

		for(let i in response.engine_fuels)
		{
			$("#filter_engine_fuels").append("<option value='"+response.engine_fuels[i]+"'>"+response.engine_fuels[i]+"</option>");
		}
		if(selected['ENGINE_FUEL'] !== undefined && selected['ENGINE_FUEL'] !== null && selected['ENGINE_FUEL'] !== "")
		{
			$("#filter_engine_fuels").val(selected['ENGINE_FUEL']);
			$("#filter_engine_fuels").find("option").first().text("Любой").attr("disabled", false);
		}

		for(let i in response.drives)
		{
			$("#filter_drives").append("<option value='"+response.drives[i]+"'>"+response.drives[i]+"</option>");
		}
		if(selected['DRIVE'] !== undefined && selected['DRIVE'] !== null && selected['DRIVE'] !== "")
		{
			$("#filter_drives").val(selected['DRIVE']);
			$("#filter_drives").find("option").first().text("Любой").attr("disabled", false);
		}

		if(selected['ENGINE_VOLUME_FROM'] !== undefined && selected['ENGINE_VOLUME_FROM'] !== null && selected['ENGINE_VOLUME_FROM'] !== "")
		{
			$("#filter_engine_volume_from").val(selected['ENGINE_VOLUME_FROM']);
			$("#filter_engine_volume_from").find("option").first().text("Любой").attr("disabled", false);
		}

		if(selected['ENGINE_VOLUME_TO'] !== undefined && selected['ENGINE_VOLUME_TO'] !== null && selected['ENGINE_VOLUME_TO'] !== "")
		{
			$("#filter_engine_volume_to").val(selected['ENGINE_VOLUME_TO']);
			$("#filter_engine_volume_to").find("option").first().text("Любой").attr("disabled", false);
		}

		console.log("response", response);
	}, "json");

	$("#filter_bodies").on("change", function()
	{
		if($(this).val() !== "")
		{ $(this).find("option").first().text("Любой").attr("disabled", false); }
		else
		{ $(this).find("option").first().text("Кузов").attr("disabled", "disabled"); }

		selected['BODY'] = $(this).val();;
		updateCounter(selected);
	});

	$("#filter_drives").on("change", function()
	{
		if($(this).val() !== "")
		{ $(this).find("option").first().text("Любой").attr("disabled", false); }
		else
		{ $(this).find("option").first().text("Привод").attr("disabled", "disabled"); }

		selected['DRIVE'] = $(this).val();;
		updateCounter(selected);
	});

	$("#filter_engine_fuels").on("change", function()
	{
		if($(this).val() !== "")
		{ $(this).find("option").first().text("Любой").attr("disabled", false); }
		else
		{ $(this).find("option").first().text("Двигатель").attr("disabled", "disabled"); }

		selected['ENGINE_FUEL'] = $(this).val();;
		updateCounter(selected);
	});

	$("#filter_gears").on("change", function()
	{
		if($(this).val() !== "")
		{ $(this).find("option").first().text("Любая").attr("disabled", false); }
		else
		{ $(this).find("option").first().text("Коробка").attr("disabled", "disabled"); }

		selected['GEAR'] = $(this).val();;
		updateCounter(selected);
	});

	$("#catalog_filter_search_button").on("click", function(e)
	{
		e.preventDefault();
		var p = path;

		if($("#filter_brands").val() !== undefined && $("#filter_brands").val() !== null && $("#filter_brands").val() !== "")
		{
			p = p + $("#filter_brands").val()+"/";
		}
		if($("#filter_models").val() !== undefined && $("#filter_models").val() !== null && $("#filter_models").val() !== "")
		{
			p = p + $("#filter_models").val()+"/";
		}

		var pa = {};
		for(let i in selected)
		{
			if(i !== "BRAND" && i !== "BRAND_ID" && i !== "MODEL" && i !== "MODEL_ID")
			{
				pa[i] = selected[i];
			}
		}

		p = p + "?"+$.param(pa);
		window.location.href = p;
	});

	$("#filter_engine_volume_from").on("change", function()
	{
		if($(this).val() !== "")
		{ $(this).find("option").first().text("Любой").attr("disabled", false); }
		else
		{ $(this).find("option").first().text("Объем, от").attr("disabled", "disabled"); }

		selected['ENGINE_VOLUME_FROM'] = $(this).val();;
		updateCounter(selected);
	});

	$("#filter_engine_volume_to").on("change", function()
	{
		if($(this).val() !== "")
		{ $(this).find("option").first().text("Любой").attr("disabled", false); }
		else
		{ $(this).find("option").first().text("Объем, до").attr("disabled", "disabled"); }

		selected['ENGINE_VOLUME_TO'] = $(this).val();;
		updateCounter(selected);
	});

	/*
	$("#engine_power_from").on("change", function(event)
	{
		inputs['engine_power_from'] = event.target.value;
	});

	$("#engine_power_to").on("change", function(event)
	{
		inputs['engine_power_to'] = event.target.value;
	});

	$("#engine_volume_from").on("change", function(event)
	{
		inputs['engine_volume_from'] = event.target.value;
	});

	$("#engine_volume_to").on("change", function(event)
	{
		inputs['engine_volume_to'] = event.target.value;
	});
	*/

	$(".sort_option").on("click", function()
	{
		console.log("path", path);
		var value = $(this).data("value");
		var p = path;
		var o = {};
		for(let i in current)
		{
			if(i !== "SORT" && i !== "BRAND" && i !== "MODEL" && i !== "BRAND_ID" && i !== "MODEL_ID")
			{
				o[i] = current[i];
			}
		}
		if(current['BRAND'] !== undefined)
		{
			p = p + current['BRAND']+"/";
		}
		if(current['MODEL'] !== undefined)
		{
			p = p + current['MODEL']+"/";
		}
		p = p+"?";
		//if(value !== "name")
		//{
			p = p + "SORT="+value;
		//}

		var params = $.param(o);
		if(params.length > 0)
		{
			p = "&"+params;
		}

		document.location.href = p;
	});

	$(".i-filter").on("click", function()
	{
		$(".filter_selector").css("display", "none");
		//$(this).parent().parent().css("display", "none");
		$("#catalog_small_filter").css("display", "block");
	});
});