Coursach/Course/ImplementerApp/Views/Home/CreateProduction.cshtml

153 lines
5.7 KiB
Plaintext
Raw Normal View History

@using Contracts.ViewModels
2024-05-25 21:36:21 +04:00
@model ProductionViewModel
@{
2024-04-28 17:11:40 +04:00
ViewData["Title"] = "CreateProduction";
2024-05-25 21:36:21 +04:00
ViewBag.Details = Model.DetailProductions;
2024-04-28 17:11:40 +04:00
}
<div class="text-center">
<h2 class="display-4">Создание производства</h2>
</div>
2024-05-26 23:23:24 +04:00
<form id="productionForm" method="post">
2024-04-28 17:11:40 +04:00
<div class="row">
<div class="col-4">Название:</div>
2024-05-26 23:23:24 +04:00
<div class="col-8">
<input type="text" name="title" id="title" value="@Model.Name" />
<span id="titleError" class="text-danger"></span>
</div>
2024-04-28 17:11:40 +04:00
</div>
<div class="container">
2024-05-25 21:36:21 +04:00
<div>Детали</div>
<div class="table-responsive-lg">
<table id="detailsTable" class="display">
<thead>
<tr>
<th>Название</th>
2024-05-25 21:36:21 +04:00
<th>Стоимость</th>
<th>Удалить</th>
</tr>
</thead>
<tbody>
2024-05-25 21:36:21 +04:00
@foreach (var detail in ViewBag.Details)
{
2024-05-25 21:36:21 +04:00
<tr data-detail-id="@detail.Value.Id">
<td>
2024-05-26 23:23:24 +04:00
<input type="hidden" name="detailIds" value="@detail.Value.Id" />
2024-05-25 21:36:21 +04:00
@detail.Value.Name
</td>
2024-05-25 21:36:21 +04:00
<td class="detail-cost" data-cost="@detail.Value.Cost">@detail.Value.Cost</td>
<td><button type="button" class="deleteDetail" data-detail-id="@detail.Value.Id">Удалить</button></td>
</tr>
}
</tbody>
</table>
</div>
2024-05-25 21:36:21 +04:00
<select id="detailSelect" class="form-control">
<option value="">Выберите деталь</option>
@foreach (var detail in ViewBag.AllDetails)
{
<option value="@detail.Id" data-cost="@detail.Cost">@detail.Name</option>
}
</select>
<button type="button" id="addDetail" class="btn btn-secondary">Добавить деталь</button>
2024-04-28 17:11:40 +04:00
</div>
<div class="row">
<div class="col-4">Сумма:</div>
<div class="col-8"><input type="text" id="sum" name="sum" readonly /></div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Сохранить" class="btn btn-primary" /></div>
</div>
</form>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.js"></script>
<script>
$(document).ready(function () {
2024-05-25 21:36:21 +04:00
function updateSum() {
var sum = 0;
$('#detailsTable tbody tr').each(function () {
var cost = $(this).find('.detail-cost').data('cost');
sum += parseFloat(cost);
});
$('#sum').val(sum.toFixed(2));
}
2024-05-26 23:23:24 +04:00
$(document).on('click', '.deleteDetail', function () {
2024-05-25 21:36:21 +04:00
var row = $(this).closest('tr');
row.remove();
updateSum();
});
$('#addDetail').click(function () {
var selectedDetail = $('#detailSelect option:selected');
if (selectedDetail.val()) {
2024-05-26 23:23:24 +04:00
var detailId = selectedDetail.val();
2024-05-25 21:36:21 +04:00
var exists = false;
$('#detailsTable tbody tr').each(function () {
if ($(this).data('detail-id') == detailId) {
exists = true;
return false;
}
});
2024-05-26 23:23:24 +04:00
if (exists) {
alert('Эта деталь уже добавлена.');
return;
}
2024-05-25 21:36:21 +04:00
var detailName = selectedDetail.text();
var detailCost = selectedDetail.data('cost');
var newRow = `
2024-05-26 23:23:24 +04:00
<tr data-detail-id="${detailId}">
<td>
<input type="hidden" name="detailIds" value="${detailId}" />
${detailName}
</td>
<td class="detail-cost" data-cost="${detailCost}">${detailCost}</td>
<td><button type="button" class="deleteDetail" data-detail-id="${detailId}">Удалить</button></td>
</tr>
`;
2024-05-25 21:36:21 +04:00
$('#detailsTable tbody').append(newRow);
$('.deleteDetail').off('click').on('click', function () {
var row = $(this).closest('tr');
row.remove();
updateSum();
});
$('#detailSelect').val('');
updateSum();
} else {
alert('Выберите деталь для добавления');
}
});
2024-05-26 23:23:24 +04:00
$('#productionForm').submit(function (event) {
var title = $('#title').val();
var isValid = true;
$('#titleError').text('');
if (title.length < 2 || title.length > 50) {
$('#titleError').text('Название должно быть от 2 до 50 символов.');
isValid = false;
}
var totalDetails = $('#detailsTable tbody tr').length;
if (totalDetails == 0) {
alert('Пожалуйста, добавьте хотя бы одну деталь.');
isValid = false;
}
if (!isValid) {
event.preventDefault();
}
});
2024-05-25 21:36:21 +04:00
updateSum();
});
2024-05-25 21:36:21 +04:00
</script>