CourseWork_Bank/Bank/BankClientApp/Views/Home/Purchase.cshtml
2024-05-01 00:31:46 +03:00

111 lines
3.9 KiB
Plaintext

@using BankContracts.ViewModels
@using Microsoft.AspNetCore.Mvc.TagHelpers
@model Tuple<BankContracts.ViewModels.PurchaseViewModel?, List<OperationViewModel>>
@{
ViewData["Title"] = Model.Item1 == null ? "Создание сделки" : "Изменение сделки";
}
<div>
<h2 class="display-4">@ViewData["Title"]</h2>
</div>
<form id="formId" name="formId">
<div class="row mb-2">
<div class="col-2">Дата сделки:</div>
<div class="col-sm-10">
<input type="date" class="form-control" name="datePurchase" id="datePurchase" placeholder="Дата сделки">
</div>
</div>
<div class="text-center mb-2">
<input type="submit" value="Создать" class="btn btn-primary" id="submit"/>
</div>
<table class="table">
<thead>
<tr>
<th>Включить в сделку</th>
<th>Кол-во операций</th>
<th>Логин сотрудника</th>
<th>Вид</th>
<th>Тип</th>
<th>Стоимость</th>
</tr>
</thead>
<tbody>
@foreach (var car in Model.Item2)
{
<tr>
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" name="cars" id="@car.Id">
</div>
</td>
<td>
<input type="number" class="custom-control-input" name="countOperations" id="date-@car.Id">
</td>
<td>@Html.DisplayFor(modelItem => car.EmployeePhoneNumber)</td>
<td>@Html.DisplayFor(modelItem => car.Mark)</td>
<td>@Html.DisplayFor(modelItem => car.Model)</td>
<td>@Html.DisplayFor(modelItem => car.Price)</td>
</tr>
}
</tbody>
</table>
</form>
@section Scripts
{
@if (Model.Item1 != null)
{
@foreach (var car in Model.Item1.OperationsModel)
{
<script>
$("#@car.Key").attr('checked', true);
$("#date-@car.Key").val("@car.Value.CountOperations");
</script>
}
<script>
$("#submit").val("Изменить");
$("#name").attr('readonly', true);
$("#datePurchase").attr('readonly', true);
$("#datePurchase").val("@Html.Raw(Model.Item1.DatePurchase.ToString("yyyy-MM-dd"))");
</script>
}
<script>
function getInputData() {
const ids = [];
const dates = [];
var res = $("input[name=cars]");
var resDates = $("input[name=countOperations]");
for (var i = 0; i < res.length; i++) {
if (res[i].checked) {
ids.push(res[i].id);
dates.push(resDates[i].value);
}
}
return [ids, dates];
}
const form = document.forms.namedItem("formId");
form.addEventListener("submit", function(event) {
event.preventDefault();
new FormData(form);
});
form.addEventListener("formdata", event => {
let result = getInputData();
$.post({
url: window.location,
data: {
datePurchase: $("#datePurchase").val(),
carsIds: result[0],
countOperations: result[1]
},
success: () => {
window.location.replace("/Home/Purchases");
}
});
});
</script>
}