@using HardwareShopContracts.ViewModels
@model List<CommentViewModel>
@{
    ViewData["Title"] = "Comments";
	Layout = "~/Views/Shared/_LayoutWorker.cshtml";
}



<div class="d-flex justify-content-evenly">
			@{
		if (Model == null)
		{
			<h3 class="display-4">Введите пароль</h3>
			return;
		}
	<div class=" col-sm-8">
		<div class="text-center">
			<h2 class="display-4">Комментарии</h2>
		</div>
		<div class="text-center" name="id">
			<table class="table">
				<thead>
					<tr>
						<th>
							Номер
						</th>
						<th>
							Текст
						</th>
						<th>
							Название сборки к которой относиться комментарий
						</th>
						<th>
							Действия
						</th>
					</tr>
				</thead>
				<tbody>
					@foreach (var item in Model)
					{
						<tr>
							<td>
								@Html.DisplayFor(modelItem => item.Id)
							</td>
							<td>
								@Html.DisplayFor(modelItem => item.Text)
							</td>
							<td>
								@Html.DisplayFor(modelItem => item.BuildName)
							</td>
							<td>
								<div>
									<button onclick="getComment(@item.Id)" type="button" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#updateModal">Изменить</button>
									<button onclick="getComment(@item.Id)" type="button" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#deleteModal">Удалить</button>
								</div>
							</td>
						</tr>
					}
				</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="#createModal">Добавить</button>
		<button type="button" class="btn btn-primary btn-lg mb-5">Обновить</button>
	</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="post" asp-controller="home" asp-action="CreateComment">
				<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 id="buildId" name="buildId" class="form-control" asp-items="@(new SelectList(@ViewBag.Builds,"Id", "BuildName"))"></select>
					</div>
					<div>
						<label class="form-label">Текст</label>
						<input type="text" class="form-control" required="required" name="text" />
					</div>
				</div>
				<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="updateModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<form method="post" asp-controller="home" asp-action="UpdateComment">
					<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 id="buildId" name="buildId" class="form-control" asp-items="@(new SelectList(@ViewBag.Builds,"Id", "BuildName"))"></select>
						</div>
						<div>
							<label class="form-label">Текст</label>
								<input type="text" class="form-control" required="required" id="text" name="text" />
						</div>
						<input type="hidden" id="commentId" name="commentId" />
					</div>
					<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="DeleteComment">
					<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="deleteCommentId" name="deleteCommentId" />
					<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>
@section Scripts
	{
	<script>
		function getComment(CommentId) {
			$.ajax({
				method: "GET",
				url: "/Home/GetComment",
				data: { CommentId: CommentId },
				success: function (result) {
					if (result != null) {
						$('#text').val(result.text);
						$('#commentId').val(result.id);
						$('#deleteCommentId').val(result.id);
						console.log(result);
					}
					console.log(result);
				}
			});
		}
	</script>
}