edit.blade.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. @extends('layouts.app')
  2. @section('content')
  3. <div class="px-3">
  4. <form class="row" action="{{ route('responsible.update', $responsible) }}" method="post">
  5. <div class="col-xxl-6">
  6. <h4>Ответственный</h4>
  7. @csrf
  8. <div class="row mb-2">
  9. <label for="area_search" class="col-form-label small col-md-4 text-md-end">
  10. Район
  11. <sup>*</sup>
  12. </label>
  13. <div class="col-md-8">
  14. <div class="position-relative">
  15. <input type="text"
  16. class="form-control form-control-sm"
  17. id="area_search"
  18. placeholder="Введите район..."
  19. autocomplete="off"
  20. value="{{ old('area_name', $responsible->area?->name ?? '') }}"
  21. required>
  22. <input type="hidden"
  23. name="area_id"
  24. id="area_id"
  25. value="{{ old('area_id', $responsible->area?->id ?? '') }}"
  26. required>
  27. <div class="autocomplete-dropdown autocomplete-dropdown--order" id="area_dropdown"></div>
  28. </div>
  29. <div class="form-text" id="area_hint"></div>
  30. </div>
  31. </div>
  32. @include('partials.input', ['name' => 'name', 'title' => 'ФИО', 'required' => true, 'value' => $responsible->name])
  33. @include('partials.input', ['name' => 'phone', 'title' => 'Телефон', 'required' => true, 'value' => $responsible->phone])
  34. @include('partials.input', ['name' => 'post', 'title' => 'Должность', 'value' => $responsible->post])
  35. @include('partials.submit', ['name' => 'Сохранить', 'delete' => ['form_id' => 'destroy', 'title' => 'Удалить']])
  36. </div>
  37. </form>
  38. </div>
  39. <div class="visually-hidden d-none">
  40. <form action="{{ route('responsible.destroy', $responsible) }}" id="destroy" method="post">
  41. @csrf
  42. @method('DELETE')
  43. </form>
  44. </div>
  45. @endsection
  46. @push('scripts')
  47. <script>
  48. waitForJQuery(function () {
  49. const $form = $('form.row[action*="responsible"]');
  50. const $input = $('#area_search');
  51. const $dropdown = $('#area_dropdown');
  52. const $hiddenInput = $('#area_id');
  53. const $hint = $('#area_hint');
  54. let currentFocus = -1;
  55. const areas = @json($areasForSearch ?? []);
  56. function escapeHtml(text) {
  57. return String(text)
  58. .replace(/&/g, '&amp;')
  59. .replace(/</g, '&lt;')
  60. .replace(/>/g, '&gt;')
  61. .replace(/"/g, '&quot;')
  62. .replace(/'/g, '&#039;');
  63. }
  64. function escapeRegex(str) {
  65. return String(str).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  66. }
  67. function highlightMatch(text, query) {
  68. if (!text || !query) {
  69. return escapeHtml(text || '');
  70. }
  71. const safeText = escapeHtml(text);
  72. const regex = new RegExp('(' + escapeRegex(query) + ')', 'gi');
  73. return safeText.replace(regex, '<strong>$1</strong>');
  74. }
  75. function showDropdown(items, query) {
  76. $dropdown.empty();
  77. currentFocus = -1;
  78. if (!items.length) {
  79. $dropdown.html('<div class="autocomplete-item text-muted">Ничего не найдено</div>');
  80. $dropdown.show();
  81. return;
  82. }
  83. items.forEach(function (item) {
  84. const html = '<div class="autocomplete-item" data-id="' + item.id + '" data-name="' + escapeHtml(item.name) + '">' +
  85. '<div class="article">' + highlightMatch(item.name, query) + '</div>' +
  86. '</div>';
  87. $dropdown.append(html);
  88. });
  89. $dropdown.show();
  90. }
  91. function hideDropdown() {
  92. $dropdown.hide();
  93. }
  94. function selectItem($item) {
  95. const id = String($item.data('id') || '');
  96. const name = String($item.data('name') || '');
  97. $input.val(name);
  98. $hiddenInput.val(id);
  99. $hint.html('<i class="bi bi-check-circle text-success"></i> Выбрано: ' + name);
  100. $input.removeClass('is-invalid');
  101. hideDropdown();
  102. }
  103. function setActive($items) {
  104. $items.removeClass('active');
  105. if (currentFocus >= 0 && currentFocus < $items.length) {
  106. const $active = $items.eq(currentFocus);
  107. $active.addClass('active');
  108. $active[0].scrollIntoView({block: 'nearest'});
  109. }
  110. }
  111. function searchAreas(query) {
  112. const normalized = String(query || '').trim().toLowerCase();
  113. if (normalized.length < 1) {
  114. hideDropdown();
  115. return;
  116. }
  117. const items = areas.filter(function (item) {
  118. return String(item.name).toLowerCase().includes(normalized);
  119. });
  120. showDropdown(items.slice(0, 50), normalized);
  121. }
  122. $input.on('input', function () {
  123. $hiddenInput.val('');
  124. $hint.html('');
  125. $input.removeClass('is-invalid');
  126. searchAreas($(this).val());
  127. });
  128. $input.on('focus', function () {
  129. if (!$hiddenInput.val()) {
  130. searchAreas($(this).val());
  131. }
  132. });
  133. $input.on('keydown', function (e) {
  134. const $items = $dropdown.find('.autocomplete-item:not(.text-muted)');
  135. if (e.key === 'ArrowDown') {
  136. e.preventDefault();
  137. currentFocus++;
  138. if (currentFocus >= $items.length) {
  139. currentFocus = 0;
  140. }
  141. setActive($items);
  142. } else if (e.key === 'ArrowUp') {
  143. e.preventDefault();
  144. currentFocus--;
  145. if (currentFocus < 0) {
  146. currentFocus = $items.length - 1;
  147. }
  148. setActive($items);
  149. } else if (e.key === 'Enter') {
  150. if ($items.length > 0 && currentFocus > -1) {
  151. e.preventDefault();
  152. selectItem($items.eq(currentFocus));
  153. }
  154. } else if (e.key === 'Escape') {
  155. hideDropdown();
  156. }
  157. });
  158. $dropdown.on('click', '.autocomplete-item:not(.text-muted)', function () {
  159. selectItem($(this));
  160. });
  161. $(document).on('click', function (e) {
  162. if (!$(e.target).closest('#area_search, #area_dropdown').length) {
  163. hideDropdown();
  164. }
  165. });
  166. $form.on('submit', function (e) {
  167. if (!$hiddenInput.val()) {
  168. e.preventDefault();
  169. $input.addClass('is-invalid').focus();
  170. $hint.html('<span class="text-danger">Выберите район из списка</span>');
  171. }
  172. });
  173. const initialAreaId = String($hiddenInput.val() || '');
  174. const initialArea = areas.find(function (item) {
  175. return String(item.id) === initialAreaId;
  176. });
  177. if (initialArea) {
  178. $input.val(initialArea.name);
  179. $hint.html('<i class="bi bi-check-circle text-success"></i> Выбрано: ' + initialArea.name);
  180. }
  181. });
  182. </script>
  183. @endpush