Coursach/Course/GuarantorAPP/Views/Home/CreateMachine.cshtml
2024-05-27 21:03:42 +04:00

164 lines
6.5 KiB
Plaintext

@using Contracts.ViewModels;
@model MachineViewModel;
@{
ViewData["Title"] = "CreateMachine";
ViewBag.Workers = Model.WorkerMachines;
}
<div class="text-center">
<h2 class="display-4">Создание станка</h2>
</div>
<form id="machineForm" method="post">
<div class="row">
<div class="col-4">Название станка:</div>
<div class="col-8">
<input type="text" name="title" id="title" value="@Model.Title" />
<span id="titleError" class="text-danger"></span>
</div>
</div>
<div class="row">
<div class="col-4">Страна производитель:</div>
<div class="col-8">
<input type="text" name="country" id="country" value="@Model.Country" />
<span id="countryError" class="text-danger"></span>
</div>
</div>
<div class="container">
<div>Работники, управляющие станком</div>
<div class="table-responsive-lg">
<table id="workersTable" class="display">
<thead>
<tr>
<th>Имя</th>
<th>Должность</th>
<th>Зарплата</th>
<th>Удалить</th>
</tr>
</thead>
<tbody>
@foreach (var worker in ViewBag.Workers)
{
<tr data-worker-id="@worker.Value.Id">
<td>
<input type="hidden" name="workerIds" value="@worker.Key" />
@worker.Value.Name
</td>
<td>@worker.Value.Position</td>
<td class="worker-salary" data-salary="@worker.Value.Salary">@worker.Value.Salary</td>
<td><button type="button" class="deleteWorker" data-worker-id="@worker.Value.Id">Удалить</button></td>
</tr>
}
</tbody>
</table>
</div>
<select id="workerSelect" class="form-control">
<option value="">Выберите работника</option>
@foreach (var worker in ViewBag.AllWorkers)
{
<option value="@worker.Id" data-salary="@worker.Salary">@worker.Name</option>
}
</select>
<button type="button" id="addWorker" class="btn btn-secondary">Добавить работника</button>
</div>
<div class="row">
<div class="col-4">Общая зарплата:</div>
<div class="col-8"><input type="text" id="totalSalary" name="totalSalary" 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 () {
function updateTotalSalary() {
var totalSalary = 0;
$('#workersTable tbody tr').each(function () {
var salary = $(this).find('.worker-salary').data('salary');
totalSalary += parseFloat(salary);
});
$('#totalSalary').val(totalSalary.toFixed(2));
}
$(document).on('click', '.deleteWorker', function () {
var row = $(this).closest('tr');
row.remove();
updateTotalSalary();
});
$('#addWorker').click(function () {
var selectedWorker = $('#workerSelect option:selected');
if (selectedWorker.val()) {
var workerId = selectedWorker.val();
var exists = false;
$('#workersTable tbody tr').each(function () {
if ($(this).data('worker-id') == workerId) {
exists = true;
return false;
}
});
if (exists) {
alert('Этот работник уже добавлен.');
return;
}
var workerName = selectedWorker.text();
var workerSalary = selectedWorker.data('salary');
var workerPosition = selectedWorker.data('position');
var newRow = `
<tr data-worker-id="${workerId}">
<td>
<input type="hidden" name="workerIds" value="${workerId}" />
${workerName}
</td>
<td>${workerPosition}</td>
<td class="worker-salary" data-salary="${workerSalary}">${workerSalary}</td>
<td><button type="button" class="deleteWorker" data-worker-id="${workerId}">Удалить</button></td>
</tr>
`;
$('#workersTable tbody').append(newRow);
$('#workerSelect').val('');
updateTotalSalary();
} else {
alert('Выберите работника для добавления');
}
});
$('#machineForm').submit(function (event) {
var title = $('#title').val();
var country = $('#country').val();
var isValid = true;
$('#titleError').text('');
$('#countryError').text('');
if (title.length < 2 || title.length > 50) {
$('#titleError').text('Название станка должно быть от 2 до 50 символов.');
isValid = false;
}
if (country.length < 2 || country.length > 50) {
$('#countryError').text('Страна производитель должна быть от 2 до 50 символов.');
isValid = false;
}
var totalWorkers = $('#workersTable tbody tr').length;
if (totalWorkers == 0) {
alert('Пожалуйста, добавьте хотя бы одного работника.');
isValid = false;
}
if (!isValid) {
event.preventDefault();
}
});
updateTotalSalary();
});
</script>