188 lines
7.3 KiB
Plaintext
188 lines
7.3 KiB
Plaintext
@using Contracts.ViewModels;
|
|
@model WorkshopViewModel;
|
|
@{
|
|
ViewData["Title"] = "CreateWorkshop";
|
|
ViewBag.Workers = Model.WorkerWorkshops;
|
|
}
|
|
<div class="text-center">
|
|
<h2 class="display-4">Создание цеха</h2>
|
|
</div>
|
|
<form id="workshopForm" 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="address" id="address" value="@Model.Address" />
|
|
<span id="addressError" class="text-danger"></span>
|
|
</div>
|
|
</div>
|
|
<div class="row">
|
|
<div class="col-4">ФИО директора цеха:</div>
|
|
<div class="col-8">
|
|
<input type="text" name="director" id="director" value="@Model.Director" />
|
|
<span id="directorError" class="text-danger"></span>
|
|
</div>
|
|
</div>
|
|
<div class="row">
|
|
<div class="col-4">Название продукции:</div>
|
|
<div class="col-8">
|
|
<input type="text" name="productionName" id="productionName" value="@Model.ProductionName" />
|
|
<span id="productionNameError" 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>@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">@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('td:eq(2)').text();
|
|
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 workerName = selectedWorker.text();
|
|
var workerData = selectedWorker.data('worker');
|
|
var workerPosition = workerData.Position;
|
|
var workerSalary = workerData.Salary;
|
|
|
|
var exists = false;
|
|
$('#workersTable tbody tr').each(function () {
|
|
if ($(this).data('worker-id') == workerId) {
|
|
exists = true;
|
|
return false;
|
|
}
|
|
});
|
|
|
|
if (exists) {
|
|
alert('Этот работник уже добавлен.');
|
|
return;
|
|
}
|
|
|
|
var newRow = `
|
|
<tr data-worker-id="${workerId}">
|
|
<td>
|
|
<input type="hidden" name="workerIds" value="${workerId}" />
|
|
${workerName}
|
|
</td>
|
|
<td>${workerPosition}</td>
|
|
<td>${workerSalary}</td>
|
|
<td><button type="button" class="deleteWorker" data-worker-id="${workerId}">Удалить</button></td>
|
|
</tr>
|
|
`;
|
|
$('#workersTable tbody').append(newRow);
|
|
|
|
updateTotalSalary();
|
|
$('#workerSelect').val('');
|
|
} else {
|
|
alert('Выберите работника для добавления');
|
|
}
|
|
});
|
|
|
|
$('#workshopForm').submit(function (event) {
|
|
var title = $('#title').val();
|
|
var address = $('#address').val();
|
|
var director = $('#director').val();
|
|
var isValid = true;
|
|
|
|
$('#titleError').text('');
|
|
$('#addressError').text('');
|
|
$('#directorError').text('');
|
|
|
|
if (title.length < 2 || title.length > 50) {
|
|
$('#titleError').text('Название цеха должно быть от 2 до 50 символов.');
|
|
isValid = false;
|
|
}
|
|
|
|
if (address.length < 5 || address.length > 100) {
|
|
$('#addressError').text('Адрес цеха должен быть от 5 до 100 символов.');
|
|
isValid = false;
|
|
}
|
|
|
|
if (director.length < 5 || director.length > 100) {
|
|
$('#directorError').text('ФИО директора должно быть от 5 до 100 символов.');
|
|
isValid = false;
|
|
}
|
|
|
|
var totalWorkers = $('#workersTable tbody tr').length;
|
|
if (totalWorkers == 0) {
|
|
alert('Пожалуйста, добавьте хотя бы одного работника.');
|
|
isValid = false;
|
|
}
|
|
|
|
if (!isValid) {
|
|
event.preventDefault();
|
|
}
|
|
});
|
|
|
|
updateTotalSalary();
|
|
});
|
|
</script>
|