﻿@using SoftwareInstallationContracts.ViewModels;
@using SoftwareInstallationDataModels.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" /></div>
	</div>
	<div class="row">
		<div class="col-4">Адрес магазина:</div>
		<div class="col-8"><input type="text" id="address" name="address"/></div>
	</div>

	<table class="table">
					<thead>
						<tr>
							<th>
								Название изделия
							</th>
							<th>
								Цена
							</th>
							<th>
								Количество
							</th>
						</tr>
					</thead>
					<tbody id="table-packages">
					</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>

<script>
	function check() {
		var shop = $('#shop').val();
		if (shop) {
			$.ajax({
				method: "GET",
				url: "/Home/GetTablePackagesFromShop",
				data: { shop: shop  },
				success: function (result) {
					if (result != null)
					{
						$('#name').val(result.item2.name);
						$('#address').val(result.item2.address);
						$('#table-packages').html(result.item1);
					}
				}
			});
		};
	}
	check();
	$('#shop').on('change', (e) => check());
</script>