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

<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="guidance" name="guidance" class="form-control" asp-items="@(new SelectList(@ViewBag.Guidances,"Id", "Text"))"></select>
		</div>
	</div>
	<div class="row">
		<div class="col-4">Услуга:</div>
		<div class="col-8">
			<select name="service" class="form-control" id="service">
				@foreach (var service in ViewBag.Services)
				{
					<option value="@service.Id" id="@service.Id">@service.ServiceName</option>
				}
			</select>
		</div>
	</div>
	<div class="row">
		<div class="col-4">Текст:</div>
		<div class="col-8">
			<textarea id="text" name="text"  rows="5" cols="80">
				</textarea>
		</div>
	</div>
	<div class="row">
		<div class="col-8"></div>
		<div class="col-4">
			<input type="submit" value="Создать" class="btn
btn-primary" />
		</div>
	</div>
</form>
<script>

	function check() {

		var guidance = $('#guidance').val();
		$("#service option:selected").removeAttr("selected");
		if (guidance) {
			$.ajax({
				method: "GET",
				url: "/Home/GetGuidance",
				data: { guidanceId: guidance },
				success: function (result) {
					console.log(result.item2);
					$('#text').val(result.text);
					$(`option[data-name=${result.serviceId}]`).attr("selected", "selected")
				}

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