﻿@using ShipyardContracts.ViewModels;
@using ShipyardDataModels.Models;

@{
	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="shopId" name="shopId" class="form-control" asp-items="@(new SelectList(@ViewBag.Shops, "Id", "ShopName"))"></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="text" id="address" name="address" class="form-control"/></div>
	</div>
	<div class="row">
		<div class="col-4">Дата открытия:</div>
		<div class="col-8"><input type="datetime-local" id="date" name="date" class="form-control" required/></div>
	</div>
	<div class="row">
		<div class="col-4">Вместимость:</div>
		<div class="col-8"><input type="number" id="capacity" name="capacity" class="form-control"/></div>
	</div>

	<table class="table">
		<thead>
			<tr>
				<th>
					Корабли
				</th>
				<th>
					Количество
				</th>
			</tr>
		</thead>
		<tbody id="table-elements">

		</tbody>
	</table>
	<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 shopId = $('#shopId').val();
				if (shopId) {
					$.ajax({
						method: "GET",
						url: "/Home/GetShop",
						data: { shopId: shopId },
						success: function (result) {
							$('#name').val(result.item1.shopName);
							$('#address').val(result.item1.address);
							$('#date').val(result.item1.dateOpen);
							$('#capacity').val(result.item1.capacity);
							$('#table-elements').html(result.item2);
						}
					});
				};
			}
			check();
			$('#shopId').on('change', function () {
					check();
			});
		</script>
}