</div>
<?php
-$createFormUrl = Url::to(['/supplier/create-form']);
-$updateFormUrl = Url::to(['/supplier/update-form']);
-$createUrl = Url::to(['/supplier/create']);
-$updateUrl = Url::to(['/supplier/update']);
-$deleteUrl = Url::to(['/supplier/delete']);
-
-$js = <<<JS
-(function() {
- var supplierModal = new bootstrap.Modal(document.getElementById('supplier-modal'));
- var editingId = null;
-
- var loaderHtml = '<div class="text-center p-4"><i class="fa fa-spinner fa-spin fa-2x text-secondary"></i></div>';
-
- // Создание
- $('#btn-supplier-create').on('click', function() {
- var \$btn = $(this);
- if (\$btn.prop('disabled')) return;
- \$btn.prop('disabled', true);
- editingId = null;
- $('#supplier-modal-title').text('Добавить поставщика');
- $('#supplier-modal-body').html(loaderHtml);
- supplierModal.show();
- $.get('{$createFormUrl}', function(html) {
- $('#supplier-modal-body').html(html);
- }).always(function() {
- \$btn.prop('disabled', false);
- });
- });
-
- // Редактирование
- $(document).on('click', '.btn-supplier-edit', function(e) {
- e.preventDefault();
- var \$btn = $(this);
- if (\$btn.prop('disabled')) return;
- \$btn.prop('disabled', true);
- editingId = \$btn.data('id');
- $('#supplier-modal-title').text('Редактировать поставщика');
- $('#supplier-modal-body').html(loaderHtml);
- supplierModal.show();
- $.get('{$updateFormUrl}', {id: editingId}, function(html) {
- $('#supplier-modal-body').html(html);
- }).always(function() {
- \$btn.prop('disabled', false);
- });
- });
-
- // Сохранение
- $(document).on('click', '#btn-supplier-save', function() {
- var \$form = $('#supplier-form');
- var url = editingId ? '{$updateUrl}?id=' + editingId : '{$createUrl}';
-
- // Сбросить ошибки
- \$form.find('.is-invalid').removeClass('is-invalid');
- \$form.find('.invalid-feedback').remove();
-
- $.ajax({
- url: url,
- type: 'POST',
- data: \$form.serialize(),
- dataType: 'json',
- success: function(resp) {
- if (resp.success) {
- supplierModal.hide();
- $.pjax.reload({container: '#supplier-pjax'});
- } else if (resp.errors) {
- $.each(resp.errors, function(field, messages) {
- var \$input = \$form.find('[name="Supplier[' + field + ']"]');
- \$input.addClass('is-invalid');
- \$input.after('<div class="invalid-feedback">' + messages[0] + '</div>');
- });
- }
- },
- error: function() {
- alert('Ошибка сервера');
- }
- });
- });
-
- // Деактивация
- $(document).on('click', '.btn-supplier-delete', function(e) {
- e.preventDefault();
- var id = $(this).data('id');
- var name = $(this).data('name');
-
- if (!confirm('Деактивировать поставщика "' + name + '"?\\nСвязанные маркировки и маппинги также будут деактивированы.')) {
- return;
- }
-
- $.ajax({
- url: '{$deleteUrl}?id=' + id,
- type: 'POST',
- data: {_csrf: yii.getCsrfToken()},
- dataType: 'json',
- success: function(resp) {
- if (resp.success) {
- $.pjax.reload({container: '#supplier-pjax'});
- } else {
- alert(resp.message || 'Ошибка деактивации');
- }
- },
- error: function() {
- alert('Ошибка сервера');
- }
- });
- });
-
- // Очистка ошибок при вводе
- $(document).on('input change', '#supplier-form input, #supplier-form select', function() {
- $(this).removeClass('is-invalid');
- $(this).next('.invalid-feedback').remove();
- });
-})();
-JS;
+$config = [
+ 'urls' => [
+ 'index' => Url::to(['/supplier/index']),
+ 'createForm' => Url::to(['/supplier/create-form']),
+ 'updateForm' => Url::to(['/supplier/update-form']),
+ 'create' => Url::to(['/supplier/create']),
+ 'update' => Url::to(['/supplier/update']),
+ 'delete' => Url::to(['/supplier/delete']),
+ ],
+];
-$this->registerJs($js);
+$this->registerJs('window.supplierConfig = ' . \yii\helpers\Json::encode($config) . ';');
+$this->registerJsFile('/js/supplier/index.js', ['position' => \yii\web\View::POS_END]);
?>
--- /dev/null
+(function () {
+ var cfg = window.supplierConfig || {};
+ var urls = cfg.urls || {};
+
+ var supplierModal = new bootstrap.Modal(document.getElementById('supplier-modal'));
+ var editingId = null;
+
+ var loaderHtml = '<div class="text-center p-4"><i class="fa fa-spinner fa-spin fa-2x text-secondary"></i></div>';
+
+ function reloadGrid() {
+ $.ajax({
+ url: urls.index,
+ type: 'GET',
+ success: function (html) {
+ var $grid = $('<div>').html(html).find('#supplier-pjax');
+ if ($grid.length) {
+ $('#supplier-pjax').replaceWith($grid);
+ }
+ }
+ });
+ }
+
+ // Создание
+ $('#btn-supplier-create').on('click', function () {
+ var $btn = $(this);
+ if ($btn.prop('disabled')) return;
+ $btn.prop('disabled', true);
+ editingId = null;
+ $('#supplier-modal-title').text('Добавить поставщика');
+ $('#supplier-modal-body').html(loaderHtml);
+ supplierModal.show();
+ $.get(urls.createForm, function (html) {
+ $('#supplier-modal-body').html(html);
+ }).always(function () {
+ $btn.prop('disabled', false);
+ });
+ });
+
+ // Редактирование
+ $(document).on('click', '.btn-supplier-edit', function (e) {
+ e.preventDefault();
+ var $btn = $(this);
+ if ($btn.prop('disabled')) return;
+ $btn.prop('disabled', true);
+ editingId = $btn.data('id');
+ $('#supplier-modal-title').text('Редактировать поставщика');
+ $('#supplier-modal-body').html(loaderHtml);
+ supplierModal.show();
+ $.get(urls.updateForm, {id: editingId}, function (html) {
+ $('#supplier-modal-body').html(html);
+ }).always(function () {
+ $btn.prop('disabled', false);
+ });
+ });
+
+ // Сохранение
+ $(document).on('click', '#btn-supplier-save', function () {
+ var $form = $('#supplier-form');
+ var url = editingId ? urls.update + '?id=' + editingId : urls.create;
+
+ $form.find('.is-invalid').removeClass('is-invalid');
+ $form.find('.invalid-feedback').remove();
+
+ $.ajax({
+ url: url,
+ type: 'POST',
+ data: $form.serialize(),
+ dataType: 'json',
+ success: function (resp) {
+ if (resp.success) {
+ supplierModal.hide();
+ reloadGrid();
+ } else if (resp.errors) {
+ $.each(resp.errors, function (field, messages) {
+ var $input = $form.find('[name="Supplier[' + field + ']"]');
+ $input.addClass('is-invalid');
+ $input.after('<div class="invalid-feedback">' + messages[0] + '</div>');
+ });
+ }
+ },
+ error: function () {
+ alert('Ошибка сервера');
+ }
+ });
+ });
+
+ // Деактивация
+ $(document).on('click', '.btn-supplier-delete', function (e) {
+ e.preventDefault();
+ var id = $(this).data('id');
+ var name = $(this).data('name');
+
+ if (!confirm('Деактивировать поставщика "' + name + '"?\nСвязанные маркировки и маппинги также будут деактивированы.')) {
+ return;
+ }
+
+ $.ajax({
+ url: urls.delete + '?id=' + id,
+ type: 'POST',
+ data: {_csrf: yii.getCsrfToken()},
+ dataType: 'json',
+ success: function (resp) {
+ if (resp.success) {
+ reloadGrid();
+ } else {
+ alert(resp.message || 'Ошибка деактивации');
+ }
+ },
+ error: function () {
+ alert('Ошибка сервера');
+ }
+ });
+ });
+
+ // Очистка ошибок при вводе
+ $(document).on('input change', '#supplier-form input, #supplier-form select', function () {
+ $(this).removeClass('is-invalid');
+ $(this).next('.invalid-feedback').remove();
+ });
+})();