index.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <!DOCTYPE html>
  2. <html lang="fr">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Clients - dirCops</title>
  7. <script src="https://cdn.tailwindcss.com"></script>
  8. </head>
  9. <body class="bg-gray-50 min-h-screen">
  10. <div class="max-w-4xl mx-auto px-4 py-8">
  11. <header class="flex justify-between items-center mb-8">
  12. <h1 class="text-2xl font-semibold text-gray-800">dirCops</h1>
  13. <nav class="space-x-4">
  14. <a href="index.html" class="text-blue-600 font-medium">Clients</a>
  15. <a href="dashboard.html" class="text-gray-600 hover:text-blue-600">Tableau de bord</a>
  16. <a href="top-clients.html" class="text-gray-600 hover:text-blue-600">Top clients</a>
  17. <a href="quantites-clients.html" class="text-gray-600 hover:text-blue-600">Quantités</a>
  18. </nav>
  19. </header>
  20. <div id="error-message" class="hidden mb-4 p-3 bg-red-100 text-red-700 rounded"></div>
  21. <section class="bg-white rounded-lg shadow p-6 mb-6">
  22. <h2 class="text-lg font-medium text-gray-800 mb-4">Ajouter un client</h2>
  23. <form id="form-client" class="flex gap-4 items-end">
  24. <div class="flex-1">
  25. <label for="nom_client" class="block text-sm text-gray-600 mb-1">Nom du client</label>
  26. <input type="text" id="nom_client" name="nom" class="w-full border border-gray-300 rounded px-3 py-2" placeholder="Ex: AQUANARIA" required>
  27. </div>
  28. <button type="submit" class="px-4 py-2 bg-green-600 text-white rounded hover:bg-green-700">Ajouter</button>
  29. </form>
  30. </section>
  31. <div class="mb-6">
  32. <label for="search" class="block text-sm font-medium text-gray-700 mb-1">Rechercher un client</label>
  33. <input type="text" id="search" placeholder="Nom du client..." class="w-full max-w-md border border-gray-300 rounded-lg px-3 py-2 focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
  34. </div>
  35. <div id="client-list" class="bg-white rounded-lg shadow overflow-hidden">
  36. <table class="min-w-full divide-y divide-gray-200">
  37. <thead class="bg-gray-100">
  38. <tr>
  39. <th class="px-4 py-3 text-left text-xs font-medium text-gray-600 uppercase">Client</th>
  40. <th class="px-4 py-3 text-right text-xs font-medium text-gray-600 uppercase">Actions</th>
  41. </tr>
  42. </thead>
  43. <tbody id="client-list-body" class="divide-y divide-gray-200">
  44. <tr><td colspan="2" class="px-4 py-6 text-center text-gray-500">Chargement...</td></tr>
  45. </tbody>
  46. </table>
  47. </div>
  48. </div>
  49. <script src="js/utils.js"></script>
  50. <script>
  51. const listBody = document.getElementById('client-list-body');
  52. const searchInput = document.getElementById('search');
  53. async function loadClients(q = '') {
  54. hideError();
  55. try {
  56. const clients = await apiGet('/clients.php', q ? { q } : {});
  57. if (clients.length === 0) {
  58. listBody.innerHTML = '<tr><td colspan="2" class="px-4 py-6 text-center text-gray-500">Aucun client trouvé.</td></tr>';
  59. return;
  60. }
  61. listBody.innerHTML = clients.map(c => {
  62. const nomEscaped = escapeHtml(c.nom).replace(/'/g, "\\'").replace(/"/g, '&quot;');
  63. return `
  64. <tr class="hover:bg-gray-50">
  65. <td class="px-4 py-3 font-medium text-gray-900">${escapeHtml(c.nom)}</td>
  66. <td class="px-4 py-3 text-right">
  67. <a href="client.html?id=${c.id}" class="text-blue-600 hover:underline mr-4">Voir le détail</a>
  68. <button onclick="deleteClient(${c.id}, ${JSON.stringify(c.nom)})" class="text-red-600 hover:text-red-800" title="Supprimer">
  69. <svg class="w-4 h-4 inline" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
  70. </button>
  71. </td>
  72. </tr>
  73. `;
  74. }).join('');
  75. } catch (e) {
  76. showError(e.message);
  77. listBody.innerHTML = '<tr><td colspan="2" class="px-4 py-6 text-center text-red-500">Erreur de chargement.</td></tr>';
  78. }
  79. }
  80. function escapeHtml(text) {
  81. const div = document.createElement('div');
  82. div.textContent = text;
  83. return div.innerHTML;
  84. }
  85. searchInput.addEventListener('input', () => {
  86. const q = searchInput.value.trim();
  87. loadClients(q);
  88. });
  89. const formClient = document.getElementById('form-client');
  90. formClient.addEventListener('submit', async (e) => {
  91. e.preventDefault();
  92. hideError();
  93. const nom = document.getElementById('nom_client').value.trim();
  94. if (!nom) return;
  95. try {
  96. await apiPost('/clients.php', { nom });
  97. document.getElementById('nom_client').value = '';
  98. loadClients(searchInput.value.trim());
  99. } catch (err) {
  100. showError(err.message);
  101. }
  102. });
  103. window.deleteClient = async function(clientId, nomClient) {
  104. if (!confirm('Êtes-vous sûr de vouloir supprimer le client "' + nomClient + '" ? Cette action supprimera également toutes les factures, paiements et écheanciers associés.')) {
  105. return;
  106. }
  107. try {
  108. await apiDelete(`/clients.php?id=${clientId}`);
  109. loadClients(searchInput.value.trim());
  110. } catch (err) {
  111. showError(err.message);
  112. }
  113. };
  114. loadClients();
  115. </script>
  116. </body>
  117. </html>