﻿@using VetClinicContracts.ViewModels;

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

<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="visit" name="visit" class="form-control" asp-items="@(new SelectList(@ViewBag.Visits, "Id", "NameVisit"))"></select>
		</div>
		
	</div>
	<div class="row">
		<div class="col-4">Название:</div>
		<div class="col-8"><input type="text" name="name" id="name" class="form-control" /></div>
	</div>
	<div class="row">
		<div class="col-4">Дата:</div>
		<div class="col-8">
			<input type="date" id="date" name="date" />
		</div>
	</div>
	<div class="row">
		<div class="col-4">Услуги:</div>
		<div class="col-8">
			<select name="services" class="form-control" multiple size="5" id="services">
				@foreach (var services in ViewBag.Services)
				{
					<option value="@services.Id" data-name="@services.ServiceName">@services.ServiceName</option>
				}
			</select>
		</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>

@section Scripts
{
	<script>
		function check() {
			var visit = $('#visit').val();
			if (visit) {
				$.ajax({
					method: "GET",
					url: "/Home/GetVisit",
					data: { visitId: visit },
					success: function (result) {
						$('#name').val(result.item1.vistName);
						$('#date').val(result.item1.date);
						$.map(result.item2, function (n) {
							console.log("#" + n);
							$(`option[data-name=${n}]`).attr("selected", "selected")
						});
					}
				});
			};
		}
		check();
		$('#visit').on('change', function () {
			check();
		});
	</script>
}