script.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // 主JavaScript文件
  2. document.addEventListener('DOMContentLoaded', function() {
  3. // 刷新页面按钮
  4. const refreshBtn = document.getElementById('refresh-btn');
  5. if (refreshBtn) {
  6. refreshBtn.addEventListener('click', function(e) {
  7. e.preventDefault();
  8. window.location.reload();
  9. });
  10. }
  11. // 加载统计数据
  12. loadStats();
  13. // 删除按钮事件处理
  14. setupDeleteButtons();
  15. // 模态框事件处理
  16. setupModal();
  17. });
  18. // 加载统计数据
  19. function loadStats() {
  20. const statsContainer = document.getElementById('stats-container');
  21. if (!statsContainer) return;
  22. fetch('/stats')
  23. .then(response => response.json())
  24. .then(data => {
  25. if (data.success) {
  26. let html = `<p><strong>总申请数:</strong> ${data.total}</p>`;
  27. if (data.stats && data.stats.length > 0) {
  28. html += '<div class="stats-list">';
  29. data.stats.forEach(stat => {
  30. html += `<p><span class="status-badge status-${stat.state}">${stat.state}</span>: ${stat.count}</p>`;
  31. });
  32. html += '</div>';
  33. }
  34. statsContainer.innerHTML = html;
  35. } else {
  36. statsContainer.innerHTML = '<p>无法加载统计数据</p>';
  37. }
  38. })
  39. .catch(error => {
  40. console.error('加载统计数据失败:', error);
  41. statsContainer.innerHTML = '<p>加载统计数据失败</p>';
  42. });
  43. }
  44. // 设置删除按钮事件
  45. function setupDeleteButtons() {
  46. const deleteButtons = document.querySelectorAll('.delete-btn');
  47. deleteButtons.forEach(button => {
  48. button.addEventListener('click', function() {
  49. const appId = this.getAttribute('data-id');
  50. const appName = this.getAttribute('data-name');
  51. // 显示确认对话框
  52. showDeleteModal(appId, appName);
  53. });
  54. });
  55. }
  56. // 设置模态框
  57. function setupModal() {
  58. const deleteModal = document.getElementById('delete-modal');
  59. const cancelDeleteBtn = document.getElementById('cancel-delete');
  60. const confirmDeleteBtn = document.getElementById('confirm-delete');
  61. if (!deleteModal) return;
  62. // 取消删除
  63. if (cancelDeleteBtn) {
  64. cancelDeleteBtn.addEventListener('click', function() {
  65. deleteModal.style.display = 'none';
  66. });
  67. }
  68. // 确认删除
  69. if (confirmDeleteBtn) {
  70. confirmDeleteBtn.addEventListener('click', function() {
  71. const appId = this.getAttribute('data-id');
  72. if (appId) {
  73. deleteApplication(appId);
  74. }
  75. });
  76. }
  77. // 点击模态框外部关闭
  78. window.addEventListener('click', function(e) {
  79. if (e.target === deleteModal) {
  80. deleteModal.style.display = 'none';
  81. }
  82. });
  83. }
  84. // 显示删除确认模态框
  85. function showDeleteModal(appId, appName) {
  86. const deleteModal = document.getElementById('delete-modal');
  87. const deleteName = document.getElementById('delete-name');
  88. const confirmDeleteBtn = document.getElementById('confirm-delete');
  89. if (!deleteModal) return;
  90. deleteName.textContent = appName;
  91. confirmDeleteBtn.setAttribute('data-id', appId);
  92. deleteModal.style.display = 'flex';
  93. }
  94. // 删除申请记录
  95. function deleteApplication(appId) {
  96. fetch(`/delete/${appId}`, {
  97. method: 'POST',
  98. headers: {
  99. 'Content-Type': 'application/json',
  100. }
  101. })
  102. .then(response => response.json())
  103. .then(result => {
  104. const deleteModal = document.getElementById('delete-modal');
  105. if (deleteModal) {
  106. deleteModal.style.display = 'none';
  107. }
  108. if (result.success) {
  109. // 显示成功消息
  110. showNotification('申请记录已成功删除', 'success');
  111. // 重新加载页面
  112. setTimeout(() => {
  113. window.location.reload();
  114. }, 1000);
  115. } else {
  116. showNotification('删除失败: ' + result.message, 'error');
  117. }
  118. })
  119. .catch(error => {
  120. console.error('删除失败:', error);
  121. showNotification('删除失败: ' + error, 'error');
  122. });
  123. }
  124. // 显示通知消息
  125. function showNotification(message, type = 'info') {
  126. // 移除现有的通知
  127. const existingNotification = document.querySelector('.notification');
  128. if (existingNotification) {
  129. existingNotification.remove();
  130. }
  131. // 创建新通知
  132. const notification = document.createElement('div');
  133. notification.className = `notification notification-${type}`;
  134. notification.textContent = message;
  135. // 添加样式
  136. notification.style.cssText = `
  137. position: fixed;
  138. top: 20px;
  139. right: 20px;
  140. padding: 15px 20px;
  141. border-radius: 4px;
  142. color: white;
  143. font-weight: 500;
  144. z-index: 10000;
  145. box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  146. animation: slideIn 0.3s ease-out;
  147. `;
  148. if (type === 'success') {
  149. notification.style.backgroundColor = '#27ae60';
  150. } else if (type === 'error') {
  151. notification.style.backgroundColor = '#e74c3c';
  152. } else {
  153. notification.style.backgroundColor = '#3498db';
  154. }
  155. // 添加到页面
  156. document.body.appendChild(notification);
  157. // 3秒后自动移除
  158. setTimeout(() => {
  159. notification.style.animation = 'slideOut 0.3s ease-out';
  160. setTimeout(() => {
  161. if (notification.parentNode) {
  162. notification.parentNode.removeChild(notification);
  163. }
  164. }, 300);
  165. }, 3000);
  166. }
  167. // 添加CSS动画
  168. if (!document.querySelector('#notification-styles')) {
  169. const style = document.createElement('style');
  170. style.id = 'notification-styles';
  171. style.textContent = `
  172. @keyframes slideIn {
  173. from {
  174. transform: translateX(100%);
  175. opacity: 0;
  176. }
  177. to {
  178. transform: translateX(0);
  179. opacity: 1;
  180. }
  181. }
  182. @keyframes slideOut {
  183. from {
  184. transform: translateX(0);
  185. opacity: 1;
  186. }
  187. to {
  188. transform: translateX(100%);
  189. opacity: 0;
  190. }
  191. }
  192. `;
  193. document.head.appendChild(style);
  194. }