@{
	ViewData["Title"] = "UpdateVaccination";
}

<div class="text-center">
	<h2 class="display-4">Редактирование прививки</h2>
</div>
<form method="post">
	<div class="row">
		<div class="col-4">Прививка:</div>
		<div class="col-8">
			<select id="vaccination" name="vaccination" class="form-control" asp-items="@(new SelectList(@ViewBag.Vaccinations, "Id", "NameVaccination"))"></select>
		</div>
	</div>
	<div class="row">
		<div class="col-4">Животное:</div>
		<div class="col-8">
			<select name="animal" class="form-control" id="animal">
				@foreach (var animal in ViewBag.Animals)
				{
					<option value="@animal.Id" id="@animal.Id">@animal.AnimalName</option>
				}
			</select>
		</div>
	</div>
	<div class="row">
		<div class="col-4">Название:</div>
		<div class="col-8">
			<input type="text" id="name" name="name" />
		</div>
	</div>
	<div class="row">
		<div class="col-4">Цена:</div>
		<div class="col-8">
			<input type="text" id="cost" name="cost" />
		</div>
	</div>
<div class="row">
		<div class="col-4">Дата:</div>
		<div class="col-8">
			<input type="datetime-local" id="date" name="date" />
		</div>
	</div>
	<div class="row">
		<div class="col-8"></div>
		<div class="col-4"><input type="submit" value="Сохранить" class="btn btn-primary" /></div>
	</div>
	<script src="~/lib/jquery/dist/jquery.min.js"></script>
</form>
<script>
	function check() {

		var vaccination = $('#vaccination').val();
		$("#animal option:selected").removeAttr("selected");
		if (vaccination) {
			$.ajax({
				method: "GET",
				url: "/Home/GetVaccination",
				data: { vaccinationId: vaccination },
				success: function (result) {
					console.log(result.item2);
					$('#name').val(result.nameVaccination);
					$('#cost').val(result.costVaccination);
					$('#date').val(result.dateStamp);
					$(`option[data-name=${result.vaccinationId}]`).attr("selected", "selected")
				}

			});
		};
	}
	check();
	$('#vaccination').on('change', function () {
		check();
	});
</script>

