﻿@using IceCreamShopContracts.ViewModels;
@using AbstractIceCreamShopDataModels.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="shop" name="shop" class="form-control" asp-items="@(new SelectList(@ViewBag.Shops, "Id", "Name"))"></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="adress" name="adress" 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="count" name="count" 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 shop = $('#shop').val();
			if (shop) {
				$.ajax({
					method: "GET",
					url: "/Home/GetShop",
					data: { shopId: shop },
					success: function (result) {
						$('#name').val(result.item1.name);
						$('#adress').val(result.item1.adress);
						$('#date').val(result.item1.openingDate);
						$('#count').val(result.item1.iceCreamMaxCount);
						$('#table-elements').html(result.item2);
					}
				});
			};
		}
		check();
		$('#shop').on('change', function () {
				check();
		});
	</script>
}