document.addEventListener('DOMContentLoaded', function () { const formChoices = new Choices('#products', { searchEnabled: true, searchChoices: false, // Prevent Choices.js from filtering the local list searchResultLimit: 10, // Optional: Limit visible results placeholder: true, placeholderValue: formipay_access_page.filter_form.products.placeholder, noResultsText: formipay_access_page.filter_form.products.noresult_text, itemSelectText: '', }); const searchInput = document.querySelector('.choices__input--cloned'); let typingTimer; searchInput.addEventListener('input', function () { const query = searchInput.value; if (query.length >= 3) { clearTimeout(typingTimer); typingTimer = setTimeout(() => { fetchChoices(query); }, 300); // Add a debounce delay } }); function fetchChoices(query) { fetch(formipay_access_page.ajax_url, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', }, body: new URLSearchParams({ action: 'formipay_access_items_get_products', search: query, nonce: formipay_access_page.nonce }), }) .then((response) => response.json()) .then((data) => { formChoices.clearChoices(); formChoices.setChoices( data, 'value', 'label', true); }) .catch((error) => console.error('Error:', error)); } document.getElementById('reset-filter').addEventListener('click', function(){ formChoices.removeActiveItems(); const event = new Event('change', { bubbles: true }); document.getElementById('orderby').value = 'ID'; document.getElementById('sort_by').value = 'desc'; document.getElementById('keyword').value = ''; document.getElementById('keyword').dispatchEvent(event); }); }); jQuery(function($){ let formipay_table_grid = new gridjs.Grid({ server: { url: formipay_access_page.ajax_url+'?action=formipay-tabledata-access-items&post_status='+document.getElementById('post_status').value+'&product='+document.getElementById('products').value+'&orderby='+document.getElementById('orderby').value+'&sort='+document.getElementById('sort_by').value+'&search='+document.getElementById('keyword').value+'&_wpnonce='+formipay_access_page.nonce, then: data => { if(data.posts_report){ processPostsReport(data.posts_report); } return data.results.map( access => [access.ID, access.ID, access.title, access.type, access.products, access.status] ); }, total: data => data.total }, columns: [ { id: 'checkbox', name: gridjs.html(``), width: '50px', formatter: (_, row) => gridjs.html( `` ) }, { name: formipay_access_page.columns.id, width: '75px' }, { name: formipay_access_page.columns.title, formatter: (_, row) => { var html = ` ${_}
`; return gridjs.html(html) } }, { name: formipay_access_page.columns.type, formatter: (type, row) => { var html; if(type == 'Redirect'){ html = ` ${type}`; }else if(type == 'Download') { html = ` ${type}`; }else if(type == 'Document') { html = ` ${type}`; } return gridjs.html(html) } }, { name: formipay_access_page.columns.products, formatter: (products, row) => { let html = ''; // Loop through each product in the products array if(products.length > 0){ products.forEach(product => { if (product) { html += ` ${product} `; } }); } return gridjs.html(html); } }, { name: formipay_access_page.columns.status, formatter: (_, row) => gridjs.html(` ${_} `) } ], pagination: { limit: 10, server: { url: (prev, page, limit) => `${prev}&limit=${limit}&offset=${page * limit}` }, summary: false }, className: { table: 'formipay-grid-table' } }).render(document.getElementById('formipay-access-items')); var $tableContainer = $('.formipay-grid-table'); var $deleteBtn = $('#formipay-delete-selected'); function updateDeleteButtonVisibility() { if ($tableContainer.find('.formipay-row-checkbox:checked').length > 0) { $deleteBtn.show(); } else { $deleteBtn.hide(); } } // Handle row and "select all" checkbox changes $tableContainer.on('change', '.formipay-row-checkbox', function() { updateDeleteButtonVisibility(); }); // Handle row and "select all" checkbox changes $tableContainer.on('change', '#select-all-rows', function() { const is_checked = $(this).is(':checked'); $tableContainer.find('.formipay-row-checkbox').prop('checked', is_checked); updateDeleteButtonVisibility(); }); // Handle delete button click $deleteBtn.on('click', function() { var selectedIds = $tableContainer.find('.formipay-row-checkbox:checked').map(function() { return $(this).data('id'); }).get(); if (selectedIds.length > 0) { Swal.fire({ icon: 'info', html: formipay_access_page.modal.bulk_delete.question, showCancelButton: true, confirmButtonText: formipay_access_page.modal.bulk_delete.confirmButton, cancelButtonText: formipay_access_page.modal.bulk_delete.cancelButton }).then((result) => { /* Read more about isConfirmed, isDenied below */ if (result.isConfirmed) { $.ajax({ type: 'post', url: formipay_access_page.ajax_url, data: { action: 'formipay-bulk-delete-access-item', ids: selectedIds, _wpnonce: formipay_access_page.nonce }, success: function (res) { Swal.fire({ title: res.data.title, html: res.data.message, icon: res.data.icon }); formipay_table_grid.forceRender(); $tableContainer.find('.formipay-row-checkbox').prop('checked', false); updateDeleteButtonVisibility(); refresh_table_with_filter(); } }); } }); } }); function refresh_table_with_filter() { formipay_table_grid.updateConfig({ server: { url: formipay_access_page.ajax_url+'?action=formipay-tabledata-access-items&post_status='+document.getElementById('post_status').value+'&product='+document.getElementById('products').value+'&orderby='+document.getElementById('orderby').value+'&sort='+document.getElementById('sort_by').value+'&search='+document.getElementById('keyword').value, then: data => data.results.map( access => [access.ID, access.ID, access.title, access.type, access.products, access.status] ), total: data => data.total } }).forceRender(); } $('.form-tool, #post_status').on('change', function(){ refresh_table_with_filter(); }); $(document).on('mouseover', 'td[data-column-id=title]', function(){ $(this).find('.post-action').css('visibility', 'visible'); }); $(document).on('mouseleave', 'td[data-column-id=title]', function(){ $(this).find('.post-action').css('visibility', 'hidden'); }); $(document).on('click', '#add-new-item', async function(e){ e.preventDefault(); const { value: coupon_code } = await Swal.fire({ input: "text", inputLabel: formipay_access_page.modal.add.title, showCancelButton: true, confirmButtonText: formipay_access_page.modal.add.confirmButton, cancelButtonText: formipay_access_page.modal.add.cancelButton, reverseButtons: true, inputValidator: (value) => { if (!value) { return formipay_access_page.modal.add.validation; } } }).then((result) => { /* Read more about isConfirmed, isDenied below, input value is ${coupon_code} */ if (result.isConfirmed && result.value) { $.ajax({ type: 'post', url: formipay_access_page.ajax_url, data: { action: 'formipay-create-access-item-post', title: result.value, nonce: formipay_access_page.nonce }, success: function (res) { if(res.success){ window.location.href = res.data.edit_post_url; }else{ Swal.fire({ html: res.data.message, icon: 'error' }); } } }); } }); }); $(document).on('click', '.delete-access', function(e){ e.preventDefault(); var data_id = $(this).attr('data-id'); Swal.fire({ icon: 'info', html: formipay_access_page.modal.delete.question, showCancelButton: true, confirmButtonText: formipay_access_page.modal.delete.confirmButton, cancelButtonText: formipay_access_page.modal.delete.cancelButton, }).then((result) => { /* Read more about isConfirmed, isDenied below */ if (result.isConfirmed) { $.ajax({ type: 'post', url: formipay_access_page.ajax_url, data: { action: 'formipay-delete-access-item', id: data_id, nonce: formipay_access_page.nonce }, success: function (res) { Swal.fire({ title: res.data.title, html: res.data.message, icon: res.data.icon }); formipay_table_grid.forceRender(); } }); } }); }); $(document).on('click', '.duplicate-access', function(e){ e.preventDefault(); var data_id = $(this).attr('data-id'); Swal.fire({ icon: 'info', html: formipay_access_page.modal.duplicate.question, showCancelButton: true, confirmButtonText: formipay_access_page.modal.duplicate.confirmButton, cancelButtonText: formipay_access_page.modal.duplicate.cancelButton, }).then((result) => { /* Read more about isConfirmed, isDenied below */ if (result.isConfirmed) { $.ajax({ type: 'post', url: formipay_access_page.ajax_url, data: { action: 'formipay-duplicate-access-item', id: data_id, nonce: formipay_access_page.nonce }, success: function (res) { Swal.fire({ title: res.data.title, html: res.data.message, icon: res.data.icon }); formipay_table_grid.forceRender(); } }); } }); }); });