@using HardwareShopContracts.ViewModels;

@model List<Tuple<PurchaseViewModel, int>>
@{
	ViewData["Title"] = "Привязка сборок";
	Layout = "~/Views/Shared/_LayoutWorker.cshtml";
}

<div class="text-center">
	<h1 class="display-4">Привязка сборок</h1>
</div>

<div class="text-center">
	@{
		<p>
			<button type="button" onclick="getBuild()" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#createModal">Добавить</button>
		</p>
		<table class="table">
			<thead>
				<tr>
					<th>
						Идентификатор покупки
					</th>
					<th>
						Цена
					</th>
					<th>
						Статус
					</th>
					<th>
						Количество сборок в заказе
					</th>
					<th>
						Действия
					</th>
				</tr>
			</thead>
			<tbody>
				@foreach (var item in Model)
				{
					<tr>
						<td>
							@Html.DisplayFor(modelItem => item.Item1.Id)
						</td>
						<td>
							@Html.DisplayFor(modelItem => item.Item1.Sum)
						</td>
						<td>
							@Html.DisplayFor(modelItem => item.Item1.PurchaseStatus)
						</td>
						<td>
							@Html.DisplayFor(modelItem => item.Item2)
						</td>
						<td>
							<div>
								<button onclick="getPurchase(@item.Item1.Id)" type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#updateModal">
									Изменить
								</button>
								<button onclick="getPurchase(@item.Item1.Id)" type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#deleteModal">
									Удалить
								</button>

							</div>
						</td>
					</tr>
				}
			</tbody>
		</table>
	}
</div>

<div class="modal fade" id="createModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<form method="get" asp-controller="home" asp-action="LinkBuildPurchase">
				<div class="modal-header">
					<h5 class="modal-title" id="exampleModalLabel">Создание сборку</h5>
					<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<label>Количество</label>
						<input type="number" class="form-control" required="required" name="count" />
					</div>
					<label class="form-label">Сборка</label>
					<select id="purchaseId" name="purchaseId" class="form-control" asp-items="@(new SelectList(@ViewBag.Purchase, "Id","Id"))"></select>
				</div>
				<input type="hidden" id="buildId" name="buildId" />
				<div class="modal-footer">
					<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
					<input type="submit" class="btn btn-primary" value="Сохранить">
				</div>
			</form>
		</div>
	</div>
</div>

<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<form method="post" asp-controller="home" asp-action="DeleteLinkPurchase">
				<div class="modal-header">
					<h5 class="modal-title" id="exampleModalLabel">Удаление покупки</h5>
					<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<h1>Вы уверенны что хотите удалить сборку?</h1>
					</div>
				</div>
				<input type="hidden" id="deleteBuildId" name="deleteBuildId" />
				<input type="hidden" id="deletePurchaseId" name="deletePurchaseId" />
				<div class="modal-footer">
					<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
					<input type="submit" class="btn btn-primary" value="Удалить">
				</div>
			</form>
		</div>
	</div>
</div>

@section Scripts
{
	<script>
		function getBuild() {
			let currentUrl = window.location.href;
			console.log(currentUrl);
			getURLParameter(currentUrl, 'buildId');
			console.log(getURLParameter(currentUrl, 'buildId'));		
			$('#buildId').val(getURLParameter(currentUrl, 'buildId'));
			$('#deleteBuildId').val(getURLParameter(currentUrl, 'buildId'));
			var id = document.getElementById("buildId");
			console.log(id.value);
		}

		function getURLParameter(sUrl, sParam) {
			let sPageURL = sUrl.substring(sUrl.indexOf('?') + 1);
			let sURLVariables = sPageURL.split('&');
			for (let i = 0; i < sURLVariables.length; i++) {
				let sParameterName = sURLVariables[i].split('=');
				if (sParameterName[0] == sParam) {
					return sParameterName[1];
				}
			}
		}
		function getPurchase(purchaseId) {
			getBuild();
			$('#deletePurchaseId').val(purchaseId);
			console.log(purchaseId);
		}
	</script>
}
@*

@{
	ViewData["Title"] = "LinkPurchase";
	Layout = "~/Views/Shared/_LayoutWorker.cshtml";
}



<form class="d-flex justify-content-evenly">
	<div class=" col-sm-8">
		<div class="text-center">
			<h2 class="display-4">Выбранные покупки</h2>
		</div>
		<div>
			<label class="form-label">Сборка</label>
			<select class="form-select" name="role">
				<option value="1">Сборка 1</option>
				<option value="2">Сборка 2</option>
			</select>
		</div>

		<div class="text-center" name="id">
			<table class="table">
				<thead>
					<tr>
						<th>
							Номер
						</th>
						<th>
							Дата оплаты
						</th>
						<th>
							Сумма
						</th>
						<th>
							Статус
						</th>
					</tr>
				</thead>
				<tbody>
				</tbody>
			</table>
		</div>
	</div>
	<div class="text-center d-flex flex-column mt-5">		
		<button type="button" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#exampleModal">Добавить</button>
		<button type="button" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#exampleModal">Изменить</button>
		<button type="button" class="btn btn-primary btn-lg mb-5">Удалить</button>
		<button type="button" class="btn btn-primary btn-lg mb-5">Обновить</button>
		<button type="submit" class="btn btn-primary btn-lg mb-5">Сохранить</button>
	</div>

	<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
				<h5 class="modal-title" id="exampleModalLabel">Покупка</h5>
					<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
				</div>
				<div class="modal-body">
					<div>
						<label class="form-label">Покупка</label>
						<select class="form-select">
							<option value="1">Покупка 1</option>
							<option value="2">Покупка 2</option>
						</select>
					</div>
					<div>
						<label class="form-label">Количество</label>
						<input type="number" class="form-control">
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
					<button type="button" class="btn btn-primary">Сохранить</button>
				</div>
			</div>
		</div>
	</div>
</form>*@