const fs = require('fs');
let c = fs.readFileSync('app_final.js', 'utf8');

const newRender = `window.renderAuditLogs = function() {
  const tbody = document.getElementById('audit-logs-tbody');
  if(!tbody) return;
  tbody.innerHTML = '';
  
  let logs = (window.currentAuditLogs || []).slice().reverse();
  
  const searchTerm = (document.getElementById('audit-search').value || '').toLowerCase();
  const dateFilter = document.getElementById('audit-date').value;
  
  logs.forEach(log => {
    if(searchTerm && (!String(log.username || '').toLowerCase().includes(searchTerm) && !String(log.action || '').toLowerCase().includes(searchTerm) && !String(log.details || '').toLowerCase().includes(searchTerm))) return;
    if(dateFilter && !String(log.timestamp || '').startsWith(dateFilter)) return;
    
    tbody.innerHTML += \`<tr>
      <td>\${log.timestamp || ''}</td>
      <td>\${log.username || ''}</td>
      <td><span class="badge \${log.action === 'حذف' ? 'bg-danger' : 'bg-primary'}">\${log.action || ''}</span></td>
      <td>\${log.module || log.store || ''}</td>
      <td>\${log.record_id || ''}</td>
      <td>\${log.details || ''}</td>
    </tr>\`;
  });
};`;

const originalRender = `window.renderAuditLogs = function() {
  const tbody = document.getElementById('audit-logs-tbody');
  if(!tbody) return;
  tbody.innerHTML = '';
  
  let logs = (window.currentAuditLogs || []).slice().reverse();
  
  const searchTerm = (document.getElementById('audit-search').value || '').toLowerCase();
  const dateFilter = document.getElementById('audit-date').value;
  
  logs.forEach(log => {
    if(searchTerm && (!log.username.toLowerCase().includes(searchTerm) && !log.action.toLowerCase().includes(searchTerm) && !log.details.toLowerCase().includes(searchTerm))) return;
    if(dateFilter && !log.timestamp.startsWith(dateFilter)) return;
    
    tbody.innerHTML += \`<tr>
      <td>\${log.timestamp}</td>
      <td>\${log.username}</td>
      <td><span class="badge \${log.action === 'حذف' ? 'bg-danger' : 'bg-primary'}">\${log.action}</span></td>
      <td>\${log.store}</td>
      <td>\${log.record_id}</td>
      <td>\${log.details}</td>
    </tr>\`;
  });
};`;

c = c.replace(newRender, originalRender);
fs.writeFileSync('app_final.js', c);
console.log('Restored original renderAuditLogs');
