File size: 8,243 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Question System Admin</title>
<style>
  * { box-sizing: border-box; }
  body { font-family: system-ui, sans-serif; margin: 0; background: #f6f8fa; color: #1f2328; }
  header { background: #24292f; color: #fff; padding: 16px 24px; }
  header h1 { margin: 0; font-size: 20px; }
  main { max-width: 960px; margin: 0 auto; padding: 24px; display: grid; gap: 20px; }
  section { background: #fff; border: 1px solid #d0d7de; border-radius: 10px; padding: 16px 20px; }
  h2 { margin: 0 0 12px; font-size: 16px; }
  .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
  input, select, button { padding: 6px 10px; border: 1px solid #c7ced6; border-radius: 6px; font-size: 14px; }
  button { background: #2563eb; color: #fff; border: none; cursor: pointer; }
  button.secondary { background: #6e7781; }
  button.danger { background: #cf222e; }
  ul { list-style: none; padding: 0; margin: 12px 0 0; }
  .q-item { display: flex; gap: 8px; align-items: center; padding: 8px; border: 1px solid #eaeef2; border-radius: 6px; margin-bottom: 8px; }
  .q-item .q-text { flex: 1; }
  .q-item[data-status="offline"] .q-text { color: #999; text-decoration: line-through; }
  .status-tag { font-size: 12px; color: #57606a; }
  .user-card { padding: 8px; border: 1px solid #eaeef2; border-radius: 6px; margin-bottom: 8px; }
  .hint { color: #57606a; font-size: 13px; }
</style>
</head>
<body>
  <header><h1>Question System Admin</h1></header>
  <main data-testid="admin-dashboard">

    <!-- ===== Question bank: upload / edit / offline (down-shelf) / delete ===== -->
    <section data-testid="question-bank">
      <h2>Question Bank</h2>
      <div class="row">
        <input data-testid="question-input" placeholder="Enter question text" aria-label="question text">
        <button data-testid="upload-question">upload question</button>
      </div>
      <ul data-testid="question-list"></ul>
      <p class="hint" data-testid="question-count">Questions: 0</p>
    </section>

    <!-- ===== User management: basic info + answer records ===== -->
    <section data-testid="user-management">
      <h2>User Management</h2>
      <div id="user-list"></div>
    </section>

    <!-- ===== Permission management: admin vs normal user ===== -->
    <section data-testid="permission-management">
      <h2>Permission Management</h2>
      <div class="row">
        <label for="role">current role:</label>
        <select data-testid="role-select" id="role">
          <option value="admin" data-role="admin" selected>admin</option>
          <option value="normal user" data-role="normal user">normal user</option>
        </select>
        <span class="status-tag" data-testid="role-status" data-role="admin">role = admin</span>
      </div>
      <p class="hint">Admins can manage questions; normal users can only browse, so management controls are hidden.</p>
    </section>
  </main>

<script>
  (function () {
    // ---- in-memory state ----
    var questions = [];     // {id, text, status, edited}
    var seq = 1;
    var users = [
      { name: 'Alice', email: 'alice@example.com', records: ['Q1 ✓', 'Q2 ✗'] },
      { name: 'Bob', email: 'bob@example.com', records: ['Q1 ✓'] }
    ];
    var role = 'admin';

    var listEl = document.querySelector('[data-testid="question-list"]');
    var countEl = document.querySelector('[data-testid="question-count"]');
    var inputEl = document.querySelector('[data-testid="question-input"]');
    var uploadBtn = document.querySelector('[data-testid="upload-question"]');
    var roleSelect = document.querySelector('[data-testid="role-select"]');
    var roleStatus = document.querySelector('[data-testid="role-status"]');

    function isAdmin() { return role === 'admin'; }

    function renderQuestions() {
      listEl.innerHTML = '';
      questions.forEach(function (q) {
        var li = document.createElement('li');
        li.className = 'q-item';
        li.setAttribute('data-testid', 'question-item');
        li.setAttribute('data-id', q.id);
        li.setAttribute('data-status', q.status);
        if (q.edited) li.setAttribute('data-edited', 'true');

        var text = document.createElement('span');
        text.className = 'q-text';
        text.setAttribute('data-testid', 'question-text');
        text.textContent = q.text + (q.edited ? ' [edited]' : '');

        var tag = document.createElement('span');
        tag.className = 'status-tag';
        tag.textContent = q.status;

        var editBtn = document.createElement('button');
        editBtn.className = 'secondary';
        editBtn.setAttribute('data-testid', 'edit-question');
        editBtn.textContent = 'edit';
        editBtn.onclick = function () {
          if (!isAdmin()) return;
          var next = inputEl.value.trim();
          q.text = next || q.text;
          q.edited = true;
          renderQuestions();
        };

        var offlineBtn = document.createElement('button');
        offlineBtn.className = 'secondary';
        offlineBtn.setAttribute('data-testid', 'offline-question');
        offlineBtn.textContent = q.status === 'offline' ? 'online' : 'offline';
        offlineBtn.onclick = function () {
          if (!isAdmin()) return;
          q.status = q.status === 'offline' ? 'online' : 'offline';
          renderQuestions();
        };

        li.appendChild(text);
        li.appendChild(tag);
        li.appendChild(editBtn);
        li.appendChild(offlineBtn);

        // delete is an admin-only control
        var delBtn = document.createElement('button');
        delBtn.className = 'danger';
        delBtn.setAttribute('data-testid', 'delete-question');
        delBtn.textContent = 'delete';
        delBtn.onclick = function () {
          if (!isAdmin()) return;
          questions = questions.filter(function (x) { return x.id !== q.id; });
          renderQuestions();
        };
        delBtn.style.display = isAdmin() ? '' : 'none';
        li.appendChild(delBtn);

        editBtn.style.display = isAdmin() ? '' : 'none';
        offlineBtn.style.display = isAdmin() ? '' : 'none';

        listEl.appendChild(li);
      });
      countEl.textContent = 'Questions: ' + questions.length;
      uploadBtn.style.display = isAdmin() ? '' : 'none';
      inputEl.disabled = !isAdmin();
    }

    function renderUsers() {
      var c = document.getElementById('user-list');
      c.innerHTML = '';
      users.forEach(function (u, index) {
        var card = document.createElement('div');
        card.className = 'user-card';
        card.setAttribute('data-testid', 'user-card');
        card.setAttribute('data-user-id', String(u.id ?? index));
        var info = document.createElement('div');
        info.textContent = u.name + ' · ' + u.email;
        card.appendChild(info);
        u.records.forEach(function (r) {
          var rec = document.createElement('div');
          rec.className = 'status-tag';
          rec.setAttribute('data-testid', 'answer-record');
          rec.setAttribute('data-answer-record', 'true');
          rec.textContent = 'answer record: ' + r;
          card.appendChild(rec);
        });
        c.appendChild(card);
      });
    }

    document.querySelector('[data-testid="upload-question"]').onclick = function () {
      if (!isAdmin()) return;
      var text = inputEl.value.trim();
      if (!text) return;
      questions.push({ id: seq++, text: text, status: 'online', edited: false });
      inputEl.value = '';
      renderQuestions();
    };

    roleSelect.onchange = function () {
      role = roleSelect.value;
      roleStatus.textContent = 'role = ' + role;
      roleStatus.setAttribute('data-role', role);
      renderQuestions(); // re-render so admin-only controls reflect the role
    };

    window.__setUsers = function (nextUsers) {
      users = structuredClone(nextUsers);
      renderUsers();
    };

    // seed one question so the dashboard is non-empty on load
    questions.push({ id: seq++, text: 'Sample question', status: 'online', edited: false });
    renderQuestions();
    renderUsers();
  })();
</script>
</body>
</html>