Bicycle/App/media/js/accounts.js
Egor Isaev cff643ebc5 dev
2026-08-12 17:08:46 +03:00

127 lines
4.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

$(function () {
var modalEl = document.getElementById('account-modal');
if (!modalEl) {
return;
}
var modal = new bootstrap.Modal(modalEl);
var $form = $('#account-form');
var $error = $('#account-form-error');
var $rate = $('#account-rate-field');
var archiveEl = document.getElementById('archive-modal');
var archiveModal = archiveEl ? new bootstrap.Modal(archiveEl) : null;
function toggleRateField() {
$rate.toggle($form.find('[name=type_acc]').val() === 'savings');
}
function resetForm() {
$form[0].reset();
$form.find('[name=id]').val('');
$error.hide().text('');
toggleRateField();
}
$(document).on('click', '.js-account-add', function () {
resetForm();
$form.data('mode', 'create').attr('action', '/accounts/create');
$('#account-modal-title').text('Новый счёт');
$('#account-rate-group').show();
modal.show();
});
$(document).on('click', '.js-account-archive-open', function () {
if (archiveModal) {
archiveModal.show();
}
});
$(document).on('click', '.js-account-restore', function () {
var $row = $(this).closest('.archive-row');
var csrf = $form.find('[name=csrf_token]').val();
$.post('/accounts/restore/' + $row.data('id'), {csrf_token: csrf})
.done(function () {
window.location.reload();
})
.fail(function () {
window.alert('Не удалось восстановить счёт');
});
});
$(document).on('click', '.js-account-edit', function (e) {
e.preventDefault();
var $card = $(this).closest('.room-card');
resetForm();
$form.data('mode', 'edit').attr('action', '/accounts/edit/' + $card.data('id'));
$form.find('[name=title]').val($card.data('title'));
$form.find('[name=type_acc]').val($card.data('type'));
$form.find('[name=summa]').val($card.data('summa'));
$form.find('[name=currency]').val($card.data('currency'));
$form.find('[name=description]').val($card.data('description'));
$form.find('[name=include_in_total]').prop('checked', !!$card.data('include'));
$('#account-modal-title').text('Редактировать счёт');
// ставка правится не здесь — см. бюджет_текущий_план.md → Account_rates
$('#account-rate-group').hide();
toggleRateField();
modal.show();
});
$(document).on('click', '.js-account-delete', function (e) {
e.preventDefault();
var $card = $(this).closest('.room-card');
var title = $card.data('title');
var csrf = $form.find('[name=csrf_token]').val();
if (!window.confirm('Архивировать счёт «' + title + '»?')) {
return;
}
$.post('/accounts/delete/' + $card.data('id'), {csrf_token: csrf})
.done(function () {
window.location.reload();
})
.fail(function () {
window.alert('Не удалось архивировать счёт');
});
});
$(document).on('change', '[name=type_acc]', function () {
toggleRateField();
if ($form.data('mode') === 'create') {
$('#account-include').prop('checked', $(this).val() !== 'savings');
}
});
$(document).on('submit', '#account-form', function (e) {
e.preventDefault();
$error.hide().text('');
$.post($form.attr('action'), $form.serialize())
.done(function () {
window.location.reload();
})
.fail(function (xhr) {
var errors = xhr.responseJSON && xhr.responseJSON.errors;
var message = errors ? Object.values(errors).join(' ') : 'Не удалось сохранить счёт';
$error.text(message).show();
});
});
$(document).on('click', '.room-menu-btn', function () {
$(this).closest('.room-menu').toggleClass('open');
});
$(document).on('click', function (e) {
if (!$(e.target).closest('.room-menu').length) {
$('.room-menu.open').removeClass('open');
}
});
});