File size: 6,255 Bytes
1ddf755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d514191
1ddf755
 
 
 
d514191
1ddf755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df718f6
 
1ddf755
 
 
 
 
 
 
17cb949
1ddf755
 
 
 
 
 
 
 
 
 
 
 
d514191
 
 
1ddf755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df718f6
 
1ddf755
 
 
 
 
 
 
 
 
 
 
 
 
 
d514191
 
 
 
 
 
 
1ddf755
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Doctor & Patient Management</title>
  <style>
    body {
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      margin: 0;
      padding: 20px;
      background-color: #f8f9fa;
    }
    h1 {
      text-align: center;
      color: #2c3e50;
    }
    .tabs {
      display: flex;
      justify-content: center;
      margin: 20px 0;
      border-bottom: 2px solid #ddd;
    }
    .tab-button {
      padding: 12px 30px;
      font-size: 16px;
      cursor: pointer;
      background: none;
      border: none;
      border-bottom: 3px solid transparent;
      margin: 0 10px;
      transition: all 0.3s;
    }
    .tab-button.active {
      border-bottom: 3px solid #3498db;
      color: #3498db;
      font-weight: bold;
    }
    .table-container {
      max-width: 1200px;
      margin: 20px auto;
      background: white;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    }
    table {
      width: 100%;
      border-collapse: collapse;
    }
    th, td {
      padding: 12px;
      text-align: left;
      border-bottom: 1px solid #ddd;
    }
    th {
      background-color: #3498db;
      color: white;
    }
    tr:hover {
      background-color: #f1f1f1;
    }
    .loading {
      text-align: center;
      padding: 20px;
      color: #666;
    }
    .error {
      color: red;
      text-align: center;
    }
  </style>
</head>
<body>

  <h1>Doctor & Patient Management System</h1>

  <div class="tabs">
    <button class="tab-button" onclick="showTab(0)">Doctors</button>
    <button class="tab-button" onclick="showTab(1)">Patients</button>
  </div>

  <!-- Doctors Tab -->
  <div id="doctors-tab" class="table-container" style="display:none;">
    <h2>Doctors List</h2>
    <div id="doctors-loading" class="loading">Loading doctors...</div>
    <table id="doctors-table" style="display:none;">
      <thead>
        <tr>
          <th>ID</th>
          <th>Doctor Name</th>
          <th>Category</th>
          <th>Visiting Days</th>
          <th>Visiting Time</th>
          <th>Fee (BDT)</th>
        </tr>
      </thead>
      <tbody></tbody>
    </table>
  </div>

  <!-- Patients Tab -->
  <div id="patients-tab" class="table-container" style="display:none;">
    <h2>Patients List</h2>
    <div id="patients-loading" class="loading">Loading patients...</div>
    <table id="patients-table" style="display:none;">
      <thead>
        <tr>
          <th>ID</th>
          <th>Patient Name</th>
          <th>Age</th>
          <th>Phone</th>
          <th>Email</th>
          <th>Doctor</th>
          <th>Category</th>
          <th>Visiting Date</th>
          <th>Visiting Day</th>
          <th>Visiting Time</th>
        </tr>
      </thead>
      <tbody></tbody>
    </table>
  </div>

  <script>
    const API_BASE = new URL('/dbapi', window.location.origin).toString().replace(/\/$/, '');

    function showTab(tabIndex) {
      const doctorsTab = document.getElementById('doctors-tab');
      const patientsTab = document.getElementById('patients-tab');
      const buttons = document.querySelectorAll('.tab-button');

      buttons.forEach((btn, index) => {
        btn.classList.toggle('active', index === tabIndex);
      });

      doctorsTab.style.display = tabIndex === 0 ? 'block' : 'none';
      patientsTab.style.display = tabIndex === 1 ? 'block' : 'none';

      // Remember last selected tab (0=Doctors, 1=Patients)
      try { localStorage.setItem('db_view_tab', String(tabIndex)); } catch {}
    }

    async function fetchDoctors() {
      const loading = document.getElementById('doctors-loading');
      const table = document.getElementById('doctors-table');
      const tbody = table.querySelector('tbody');

      try {
        const res = await fetch(`${API_BASE}/api/doctors`);
        if (!res.ok) throw new Error(`HTTP Error: ${res.status}`);

        const doctors = await res.json();

        tbody.innerHTML = doctors.map(doc => `
          <tr>
            <td>${doc.id}</td>
            <td>${doc.doctor_name}</td>
            <td>${doc.category}</td>
            <td>${doc.visiting_days}</td>
            <td>${doc.visiting_time}</td>
            <td>${doc.visiting_money}</td>
          </tr>`).join('');

        loading.style.display = 'none';
        table.style.display = 'table';
      } catch (err) {
        console.error(err);
        loading.innerHTML = `<span class="error">Failed to fetch doctors.<br>${err.message}</span>`;
      }
    }

    async function fetchPatients() {
      const loading = document.getElementById('patients-loading');
      const table = document.getElementById('patients-table');
      const tbody = table.querySelector('tbody');

      try {
        const res = await fetch(`${API_BASE}/api/patients`);
        if (!res.ok) throw new Error(`HTTP Error: ${res.status}`);

        const patients = await res.json();

        tbody.innerHTML = patients.map(patient => `
          <tr>
            <td>${patient.id}</td>
            <td>${patient.patient_name}</td>
            <td>${patient.patient_age}</td>
            <td>${patient.patient_num}</td>
            <td>${patient.patient_mail}</td>
            <td>${patient.doctor_name}</td>
            <td>${patient.doctor_category}</td>
            <td>${patient.visiting_date}</td>
            <td>${patient.visiting_day || ''}</td>
            <td>${patient.visiting_time || ''}</td>
          </tr>`).join('');

        loading.style.display = 'none';
        table.style.display = 'table';
      } catch (err) {
        console.error(err);
        loading.innerHTML = `<span class="error">Failed to fetch patients.<br>${err.message}</span>`;
      }
    }

    // Load data when page loads
    window.onload = () => {
      fetchDoctors();
      fetchPatients();   // Preload both for better UX
      // Default to Patients tab on load, but allow user preference
      let tab = 1;
      try {
        const saved = Number(localStorage.getItem('db_view_tab'));
        if (saved === 0 || saved === 1) tab = saved;
      } catch {}
      showTab(tab);
    };
  </script>
</body>
</html>