Advertisement
Guest User

create_form

a guest
Nov 21st, 2023
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.61 KB | None | 0 0
  1. <div class="container">
  2. @include('shop.modal')
  3. <div class="card mt-4">
  4. <div>
  5. <button class="btn btn-primary" id="addBoxButton">dodaj box</button>
  6. <button class="btn btn-primary" id="addProductButton">dodaj produkt</button>
  7.  
  8. {{-- <a class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#modal-notification"><span class="bi bi-plus-lg"></span>{% trans %}button.addNewNotification{% endtrans %}</a>--}}
  9. {{-- <button class="btn btn-primary" id="exitBox" style="display: none;">Wyjdz</button>--}}
  10. {{-- <button class="btn btn-primary" id="editBox" style="display: none;">Edytuj box</button>--}}
  11. </div>
  12. <div>
  13. {{-- <form action="" method="post" id="form">--}}
  14. {{-- @csrf--}}
  15. <div>
  16. <div class="col-12" id="tableItem">
  17. <table id="boxTable" class="table">
  18. <thead>
  19. <tr>
  20. <th>Purchase Date</th>
  21. <th>Cost Price</th>
  22. <th>Additional Costs</th>
  23. <th>Quantity</th>
  24. <th>Action</th>
  25. </tr>
  26. </thead>
  27. <tbody></tbody>
  28. </table>
  29. <table id="productTable" class="table" style="max-width: 200px">
  30. <thead>
  31. <tr>
  32. <th>name</th>
  33. <th>purchase_date</th>
  34. <th>cost_price_product</th>
  35. <th>additional_costs_product</th>
  36. <th>condition_product</th>
  37. <th>color_product</th>
  38. <th>dictionary_product</th>
  39. <th>new_product</th>
  40. <th>photo</th>
  41. <th>photo_product</th>
  42. <th>Action</th>
  43. </tr>
  44. </thead>
  45. <tbody></tbody>
  46. </table>
  47. </div>
  48. </div>
  49. {{-- <button type="submit" class="btn btn-primary">Wyślij</button>--}}
  50. {{-- </form>--}}
  51. </div>
  52. </div>
  53. </div>
  54. <script>
  55. $(document).ready(function () {
  56. var box = [];
  57. var boxTables = $('#boxTable').DataTable({
  58. dom: 'rt',
  59. });
  60. var product = [];
  61. var productTables = $('#productTable').DataTable();
  62. var photos = [];
  63. var rowId;
  64.  
  65. $("#addBoxButton").click(function () {
  66. $("#modal-add-box").modal("show");
  67. });
  68.  
  69. $("#add-box").click(function () {
  70.  
  71. var newBox = {
  72. purchase_date_box: $("#purchase_date_box").val(),
  73. cost_price_box: $("#cost_price_box").val(),
  74. additional_costs_box: $("#additional_costs_box").val(),
  75. Quantity_box: $("#Quantity_box").val(),
  76. };
  77.  
  78. box.push(newBox);
  79. boxTables.row.add([
  80. newBox.purchase_date_box,
  81. newBox.cost_price_box,
  82. newBox.additional_costs_box,
  83. newBox.Quantity_box,
  84. '<div style="display: flex;"><button class="btn btn-success editBoxButton">Edytuj</button> <button class="btn btn-danger deleteBoxButton">Usuń</button></div>'
  85. ]).node().setAttribute('data-id', box.length - 1);
  86. boxTables.draw();
  87.  
  88. $("#modal-add-box").modal("hide");
  89.  
  90. $("#purchase_date_box").val("");
  91. $("#cost_price_box").val("");
  92. $("#additional_costs_box").val("");
  93. $("#Quantity_box").val("");
  94.  
  95. });
  96.  
  97. $('#boxTable').on('click', '.deleteBoxButton', function () {
  98. rowId = $(this).closest('tr').data('id');
  99. boxTables.row($(this).closest('tr')).remove().draw(false);
  100. box.splice(rowId, 1);
  101. rowId = null;
  102. });
  103.  
  104. $('#boxTable').on('click', '.editBoxButton', function () {
  105. rowId = $(this).closest('tr').data('id');
  106. console.log("Przed aktualizacją danych w tabeli:", boxTables.row($(this).closest('tr')).data());
  107. // Pobierz dane z tablicy 'product' dla wybranego wiersza
  108. var selectedBox = box[rowId];
  109.  
  110. // Wypełnij pola formularza danymi z wybranego wiersza
  111. $("#purchase_date_box_edit").val(selectedBox.purchase_date_box);
  112. $("#cost_price_box_edit").val(selectedBox.cost_price_box);
  113. $("#additional_costs_box_edit").val(selectedBox.additional_costs_box);
  114. $("#Quantity_box_edit").val(selectedBox.Quantity_box);
  115.  
  116. // Otwórz modal edycji
  117. $("#modal-edit-box").modal("show");
  118. });
  119.  
  120. $("#edit-box").click(function () {
  121. var $this = $(this);
  122. var editedBox = {
  123. purchase_date_box: $("#purchase_date_box_edit").val(),
  124. cost_price_box: $("#cost_price_box_edit").val(),
  125. additional_costs_box: $("#additional_costs_box_edit").val(),
  126. Quantity_box: $("#Quantity_box_edit").val(),
  127. };
  128.  
  129. box[rowId] = editedBox;
  130.  
  131. boxTables.row(rowId).data([
  132. editedBox.purchase_date_box,
  133. editedBox.cost_price_box,
  134. editedBox.additional_costs_box,
  135. editedBox.Quantity_box,
  136. '<div style="display: flex;"><button class="btn btn-success editBoxButton">Edytuj</button> <button class="btn btn-danger deleteBoxButton">Usuń</button></div>'
  137. ]).draw();
  138. console.log("Po aktualizacji danych w tabeli:", boxTables.row($this.closest('tr')).data());
  139.  
  140. $("#modal-edit-box").modal("hide");
  141.  
  142. });
  143.  
  144. $("#addProductButton").click(function () {
  145. $("#modal-add-product").modal("show");
  146. });
  147.  
  148. $("#add-product").click(function () {
  149.  
  150. var newProduct = {
  151. product_name: $("#name_product").val(),
  152. product_purchase_date: $("#purchase_date_product").val(),
  153. product_cost_price: $("#cost_price_product").val(),
  154. product_additional_costs: $("#additional_costs_product").val(),
  155. product_condition: $("#condition_product").val(),
  156. product_color: $("#color_product").val(),
  157. product_dictionary: $("#dictionary_product").val(),
  158. product_new: $("#new_product").val(),
  159. photos: []
  160. };
  161.  
  162. const photoContainer = $('#thumbnail-container .thumbnail img');
  163. const photos = [];
  164. photoContainer.each(function () {
  165. const originalSrc = $(this).data('original-src');
  166. photos.push(originalSrc);
  167. });
  168. newProduct.photos = photos; // Przypisz tablicę ze zdjęciami do właściwości 'photos'
  169. const photoStatus = photos.length > 0 ? "dodane " : "nie dodano";
  170.  
  171. product.push(newProduct);
  172. productTables.row.add([
  173. newProduct.product_name,
  174. newProduct.product_purchase_date,
  175. newProduct.product_cost_price,
  176. newProduct.product_additional_costs,
  177. newProduct.product_condition,
  178. newProduct.product_color,
  179. newProduct.product_dictionary,
  180. newProduct.product_new,
  181. newProduct.photos,
  182. photoStatus,
  183. '<div style="display: flex;"><button class="btn btn-success editProductButton">Edytuj</button> <button class="btn btn-danger deleteProductButton">Usuń</button></div>'
  184. ]).node().setAttribute('data-id', product.length - 1);
  185. productTables.columns([8]).visible(false);
  186.  
  187. productTables.draw();
  188.  
  189. $("#modal-add-product").modal("hide");
  190.  
  191. $("#name_product").val("");
  192. $("#purchase_date_product").val("");
  193. $("#cost_price_product").val("");
  194. $("#additional_costs_product").val("");
  195. $("#condition_product").val("");
  196. $("#color_product").val("");
  197. $("#dictionary_product").val("");
  198. $("#new_product").val("");
  199. $('#thumbnail-container .thumbnail').remove();
  200. });
  201.  
  202. $('#productTable').on('click', '.deleteProductButton', function () {
  203. rowId = $(this).closest('tr').data('id');
  204. productTables.row($(this).closest('tr')).remove().draw(false);
  205. product.splice(rowId, 1);
  206. rowId = null;
  207. });
  208.  
  209. $('#productTable tbody').on('click', '.editProductButton', function (event) {
  210. event.preventDefault();
  211. rowId = $(this).closest('tr').data('id');
  212.  
  213. var selectedProduct = product[rowId];
  214.  
  215. // Wypełnij pola formularza danymi z wybranego wiersza
  216. $("#name_product_edit").val(selectedProduct.product_name);
  217. $("#purchase_date_product_edit").val(selectedProduct.product_purchase_date);
  218. $("#cost_price_product_edit").val(selectedProduct.product_cost_price);
  219. $("#additional_costs_product_edit").val(selectedProduct.product_additional_costs);
  220. $("#condition_product_edit").val(selectedProduct.product_condition);
  221. $("#color_product_edit").val(selectedProduct.product_color);
  222. $("#dictionary_product_edit").val(selectedProduct.product_dictionary);
  223. $("#new_product_edit").val(selectedProduct.product_new);
  224. console.log(photos)
  225. photos = selectedProduct.photos;
  226. // loadThumbnails(selectedProduct.photos);
  227. // Otwórz modal edycji
  228. $("#modal-edit-product").modal("show");
  229. });
  230.  
  231. $("#edit-product").click(function () {
  232. var $this = $(this);
  233. var editedProduct = {
  234. product_name: $("#name_product_edit").val(),
  235. product_purchase_date: $("#purchase_date_product_edit").val(),
  236. product_cost_price: $("#cost_price_product_edit").val(),
  237. product_additional_costs: $("#additional_costs_product_edit").val(),
  238. product_condition: $("#condition_product_edit").val(),
  239. product_color: $("#color_product_edit").val(),
  240. product_dictionary: $("#dictionary_product_edit").val(),
  241. product_new: $("#new_product_edit").val(),
  242. photos: []
  243. };
  244.  
  245. const photoContainer = $('#thumbnail-container-edit .thumbnail img');
  246. // const photos = [];
  247. photoContainer.each(function () {
  248. const originalSrc = $(this).data('original-src');
  249. photos.push(originalSrc);
  250. });
  251. editedProduct.photos = photos; // Przypisz tablicę ze zdjęciami do właściwości 'photos'
  252. const photoStatus = photos.length > 0 ? "dodane " : "nie dodano";
  253. product[rowId] = editedProduct;
  254.  
  255. productTables.row(rowId).data([
  256. editedProduct.product_name,
  257. editedProduct.product_purchase_date,
  258. editedProduct.product_cost_price,
  259. editedProduct.product_additional_costs,
  260. editedProduct.product_condition,
  261. editedProduct.product_color,
  262. editedProduct.product_dictionary,
  263. editedProduct.product_new,
  264. editedProduct.photos,
  265. photoStatus,
  266. '<div style="display: flex;"><button class="btn btn-success editProductButton">Edytuj</button> <button class="btn btn-danger deleteProductButton">Usuń</button></div>'
  267. ]).node().setAttribute('data-id', product.length - 1);
  268. productTables.columns([8]).visible(false);
  269. productTables.draw();
  270. photos = [];
  271. $("#modal-edit-product").modal("hide");
  272.  
  273. });
  274.  
  275. // function loadThumbnails(photos) {
  276. // const thumbnailContainer = $('#thumbnail-container-edit');
  277. // thumbnailContainer.empty();
  278. // console.log(thumbnailContainer)
  279. //
  280. // for (let i = 0; i < photos.length; i++) {
  281. // const thumbnail = `
  282. // <div class="thumbnail small-thumbnail">
  283. // <img src="${photos[i]}" alt="Thumbnail">
  284. // <button class="delete-button">X</button>
  285. // </div>`;
  286. // thumbnailContainer.append(thumbnail);
  287. // }
  288. // }
  289.  
  290. $('#thumbnail-container-edit').on('click', '.thumbnail img', function () {
  291. const originalSrc = $(this).data('original-src');
  292. displayLargeImage(originalSrc);
  293. });
  294.  
  295. $('#thumbnail-container-edit').on('click', '.delete-button', function () {
  296. $(this).parent().remove(); // Usunięcie rodzica przycisku (czyli obrazka) z kontenera
  297. });
  298.  
  299. // Przypisz zdarzenie dla input #photo_product w kontenerze #modal-edit-product
  300. $('#photo_product_edit').on('change', function () {
  301. displayThumbnailsEdit(this);
  302. // Wyczyść wartość pola wejściowego, aby umożliwić ponowne dodanie tego samego pliku
  303. $(this).val('');
  304. });
  305.  
  306. $('#photo_product').on('change', function () {
  307. displayThumbnails(this);
  308. // Wyczyść wartość pola wejściowego, aby umożliwić ponowne dodanie tego samego pliku
  309. $(this).val('');
  310. });
  311.  
  312. $('#thumbnail-container').on('click', '.thumbnail img', function () {
  313. const originalSrc = $(this).data('original-src');
  314. displayLargeImage(originalSrc);
  315. });
  316.  
  317. $('#thumbnail-container').on('click', '.delete-button', function () {
  318. $(this).parent().remove(); // Usunięcie rodzica przycisku (czyli obrazka) z kontenera
  319. });
  320.  
  321.  
  322. $('body').on('click', '#largeImageModal .btn-secondary', function () {
  323. $('#largeImageModal').modal('hide');
  324. });
  325.  
  326. function displayLargeImage(src) {
  327. const modalContent = `
  328. <div class="modal fade" id="largeImageModal" tabindex="-1" role="dialog" aria-labelledby="largeImageModalLabel" aria-hidden="true">
  329. <div class="modal-dialog modal-lg" role="document">
  330. <div class="modal-content">
  331. <div class="modal-body">
  332. <img src="${src}" class="img-fluid" alt="Large Image">
  333. </div>
  334. <div class="modal-footer">
  335. <button type="button" class="btn btn-secondary" data-dismiss="modal">Zamknij</button>
  336. </div>
  337. </div>
  338. </div>
  339. </div>`;
  340.  
  341. // Usuń poprzedni modal, jeśli istnieje
  342. $('#largeImageModal').remove();
  343.  
  344. // Dodaj nowy modal do body
  345. $('body').append(modalContent);
  346.  
  347. // Wywołaj modal
  348. $('#largeImageModal').modal('show');
  349. }
  350.  
  351. function displayThumbnails(input) {
  352. if (input.files && input.files.length > 0) {
  353. const thumbnailContainer = $('#thumbnail-container');
  354. const maxThumbnailSize = 50; // Zmniejszono rozmiar miniaturki
  355.  
  356. // Sprawdź, czy istnieje już blok miniatur, jeśli nie, utwórz go
  357. let thumbnailsWrapper = thumbnailContainer.find('.thumbnails-wrapper');
  358. if (!thumbnailsWrapper.length) {
  359. thumbnailsWrapper = $('<div class="thumbnails-wrapper"></div>');
  360. }
  361.  
  362. for (let i = 0; i < input.files.length; i++) {
  363. const reader = new FileReader();
  364.  
  365. reader.onload = function (e) {
  366. const img = new Image();
  367. img.src = e.target.result;
  368.  
  369. img.onload = function () {
  370. const canvas = document.createElement('canvas');
  371. const ctx = canvas.getContext('2d');
  372.  
  373. let width = img.width;
  374. let height = img.height;
  375.  
  376. if (width > height) {
  377. if (width > maxThumbnailSize) {
  378. height *= maxThumbnailSize / width;
  379. width = maxThumbnailSize;
  380. }
  381. } else {
  382. if (height > maxThumbnailSize) {
  383. width *= maxThumbnailSize / height;
  384. height = maxThumbnailSize;
  385. }
  386. }
  387.  
  388. canvas.width = width;
  389. canvas.height = height;
  390. ctx.drawImage(img, 0, 0, width, height);
  391.  
  392. // Dodaj pojedynczą miniaturę do bloku
  393. const thumbnail = `
  394. <div class="thumbnail">
  395. <img src="${canvas.toDataURL('image/jpeg')}" data-original-src="${e.target.result}" alt="Thumbnail">
  396. <button class="delete-button">X</button>
  397. </div>`;
  398.  
  399. thumbnailsWrapper.append(thumbnail);
  400. };
  401. };
  402.  
  403. reader.readAsDataURL(input.files[i]);
  404. }
  405.  
  406. // Dodaj blok z miniaturami do kontenera
  407. thumbnailContainer.empty().append(thumbnailsWrapper); // Usuń istniejące miniatury i dodaj nowe
  408. }
  409. }
  410.  
  411. function displayThumbnailsEdit(input) {
  412. if (input.files && input.files.length > 0) {
  413. const thumbnailContainer = $('#thumbnail-container-edit');
  414. const maxThumbnailSize = 50;
  415.  
  416. // Wyczyść kontener z istniejących miniatur
  417. thumbnailContainer.empty();
  418.  
  419. // Utwórz zmienną lokalną dla zdjęć
  420. // const newPhotos = [];
  421.  
  422. // Przetwarzaj istniejące zdjęcia
  423. for (let i = 0; i < photos.length; i++) {
  424. const thumbnail = `
  425. <div class="thumbnail small-thumbnail">
  426. <img src="${photos[i]}" alt="Thumbnail">
  427. <button class="delete-button">X</button>
  428. </div>`;
  429. thumbnailContainer.append(thumbnail);
  430.  
  431. // Dodaj istniejące zdjęcia do zmiennej lokalnej
  432. // newPhotos.push(photos[i]);
  433. }
  434.  
  435. // Przetwarzaj nowe zdjęcia
  436. for (let i = 0; i < input.files.length; i++) {
  437. const reader = new FileReader();
  438.  
  439. reader.onload = function (e) {
  440. const img = new Image();
  441. img.src = e.target.result;
  442.  
  443. img.onload = function () {
  444. const canvas = document.createElement('canvas');
  445. const ctx = canvas.getContext('2d');
  446.  
  447. let width = img.width;
  448. let height = img.height;
  449.  
  450. if (width > height) {
  451. if (width > maxThumbnailSize) {
  452. height *= maxThumbnailSize / width;
  453. width = maxThumbnailSize;
  454. }
  455. } else {
  456. if (height > maxThumbnailSize) {
  457. width *= maxThumbnailSize / height;
  458. height = maxThumbnailSize;
  459. }
  460. }
  461.  
  462. canvas.width = width;
  463. canvas.height = height;
  464. ctx.drawImage(img, 0, 0, width, height);
  465.  
  466. // Dodaj nowe zdjęcia do zmiennej lokalnej
  467. photos.push(canvas.toDataURL('image/jpeg'));
  468.  
  469. // Dodaj miniaturę do kontenera
  470. const thumbnail = `
  471. <div class="thumbnail">
  472. <img src="${canvas.toDataURL('image/jpeg')}" data-original-src="${e.target.result}" alt="Thumbnail">
  473. <button class="delete-button">X</button>
  474. </div>`;
  475. thumbnailContainer.append(thumbnail);
  476. };
  477. };
  478.  
  479. reader.readAsDataURL(input.files[i]);
  480. }
  481.  
  482. // Zaktualizuj globalną tablicę photos za pomocą zmiennej lokalnej
  483. // photos = newPhotos;
  484. }
  485. }
  486.  
  487. // Dodaj nasłuchiwanie zdarzeń dla wysyłki formularza
  488. $('form').submit(function(event) {
  489.  
  490. event.preventDefault();
  491.  
  492. // Pobierz dane z DataTables
  493. var boxData = boxTables.rows().data().toArray();
  494. var productData = productTables.rows().data().toArray();
  495.  
  496. // Połącz dane w jeden obiekt
  497. var formData = {
  498. boxData: boxData,
  499. productData: productData
  500. };
  501.  
  502. // Wyślij dane na serwer za pomocą AJAX
  503. $.ajax({
  504. url: '/shop',
  505. type: 'POST',
  506. data: { _token: '{{ csrf_token() }}', formData: formData },
  507. success: function(response) {
  508. console.log('Wysłane')
  509. console.log(response.aa);
  510. },
  511. error: function(error) {
  512. console.log('nie wyslane')
  513. // console.error(error);
  514. }
  515. });
  516. });
  517. });
  518. </script>
  519.  
  520.  
  521.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement