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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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import { useState } from "react";
import { useSelector } from "react-redux";
import { FiHash, FiAlignLeft, FiLock, FiGlobe, FiX, FiArrowLeft } from "react-icons/fi";

function CreateRoomView({ onCancel, onCreate }) {
  const { isDark } = useSelector((state) => state.theme);
  const [roomName, setRoomName] = useState("");
  const [roomTopic, setRoomTopic] = useState("");
  const [roomType, setRoomType] = useState("public");
  const [nameError, setNameError] = useState("");
  const [apiError, setApiError] = useState("");
  const [isSubmitting, setIsSubmitting] = useState(false);

  const handleSubmit = async () => {
    const trimmedName = roomName.trim();
    if (!trimmedName) {
      setNameError("Vui lòng nhập tên room");
      return;
    }
    if (trimmedName.length < 2) {
      setNameError("Tên room phải có ít nhất 2 ký tự");
      return;
    }
    setNameError("");
    setApiError("");
    setIsSubmitting(true);

    try {
      await onCreate({
        name: trimmedName,
        displayName: trimmedName,
        topic: roomTopic.trim(),
        type: roomType,
      });
    } catch (err) {
      setApiError(err?.message || "Không thể tạo room. Vui lòng thử lại.");
    } finally {
      setIsSubmitting(false);
    }
  };

  const handleKeyDown = (e) => {
    if (e.key === "Enter" && !e.shiftKey) {
      e.preventDefault();
      handleSubmit();
    }
    if (e.key === "Escape") {
      onCancel();
    }
  };

  return (
    <div
      className="flex-1 flex flex-col min-w-0"
      style={{ background: "var(--bg-surface)" }}
    >
      {/* Header */}
      <div
        className="px-4 py-3 border-b shrink-0 flex items-center gap-3"
        style={{
          borderColor: "var(--border-primary)",
          background: "var(--bg-surface-secondary)",
        }}
      >
        <button
          onClick={onCancel}
          className="p-1.5 rounded-md hover:opacity-70 transition-opacity cursor-pointer"
          style={{ color: "var(--text-secondary)" }}
          title="Quay lại"
        >
          <FiArrowLeft size={18} />
        </button>
        <div className="flex-1 min-w-0">
          <div
            className="text-[15px] font-semibold"
            style={{ color: "var(--text-primary)" }}
          >
            Tạo room mới
          </div>
          <div
            className="text-xs mt-0.5"
            style={{ color: "var(--text-secondary)" }}
          >
            Thêm kênh thảo luận mới vào space
          </div>
        </div>
      </div>

      {/* Content */}
      <div className="flex-1 overflow-y-auto p-6">
        <div className="max-w-lg mx-auto space-y-6">
          {/* Room Info Card */}
          <div>
            <h3
              className="text-sm font-semibold mb-3"
              style={{ color: "var(--text-primary)" }}
            >
              Thông tin room
            </h3>
            <div
              className="p-4 rounded-lg space-y-4"
              style={{ background: "var(--card-bg-secondary)" }}
            >
              {/* Room Name */}
              <div>
                <label
                  className="text-xs font-medium mb-1 block"
                  style={{ color: "var(--text-secondary)" }}
                >
                  Tên room <span style={{ color: "var(--danger)" }}>*</span>
                </label>
                <div className="relative">
                  <FiHash
                    size={16}
                    className="absolute left-3 top-1/2 -translate-y-1/2"
                    style={{ color: "var(--text-muted)" }}
                  />
                  <input
                    type="text"
                    value={roomName}
                    onChange={(e) => {
                      setRoomName(e.target.value);
                      if (nameError) setNameError("");
                    }}
                    onKeyDown={handleKeyDown}
                    placeholder="ví-dụ: thao-luan-chung"
                    className="w-full pl-9 pr-3 py-2 rounded-md text-sm border outline-none"
                    style={{
                      background: "var(--input-bg)",
                      borderColor: nameError
                        ? "var(--danger)"
                        : "var(--input-border)",
                      color: "var(--input-text)",
                    }}
                    onFocus={(e) =>
                      (e.currentTarget.style.borderColor = "var(--primary)")
                    }
                    onBlur={(e) =>
                      (e.currentTarget.style.borderColor = nameError
                        ? "var(--danger)"
                        : "var(--input-border)")
                    }
                    autoFocus
                  />
                </div>
                {nameError && (
                  <div
                    className="text-xs mt-1"
                    style={{ color: "var(--danger)" }}
                  >
                    {nameError}
                  </div>
                )}
              </div>

              {/* Room Topic */}
              <div>
                <label
                  className="text-xs font-medium mb-1 block"
                  style={{ color: "var(--text-secondary)" }}
                >
                  Chủ đề{" "}
                  <span style={{ color: "var(--text-muted)" }}>(tùy chọn)</span>
                </label>
                <div className="relative">
                  <FiAlignLeft
                    size={16}
                    className="absolute left-3 top-2.5"
                    style={{ color: "var(--text-muted)" }}
                  />
                  <input
                    type="text"
                    value={roomTopic}
                    onChange={(e) => setRoomTopic(e.target.value)}
                    onKeyDown={handleKeyDown}
                    placeholder="Mô tả ngắn về nội dung thảo luận..."
                    className="w-full pl-9 pr-3 py-2 rounded-md text-sm border outline-none"
                    style={{
                      background: "var(--input-bg)",
                      borderColor: "var(--input-border)",
                      color: "var(--input-text)",
                    }}
                    onFocus={(e) =>
                      (e.currentTarget.style.borderColor = "var(--primary)")
                    }
                    onBlur={(e) =>
                      (e.currentTarget.style.borderColor = "var(--input-border)")
                    }
                  />
                </div>
              </div>
            </div>
          </div>

          {/* Room Type Card */}
          <div>
            <h3
              className="text-sm font-semibold mb-3"
              style={{ color: "var(--text-primary)" }}
            >
              Quyền riêng tư
            </h3>
            <div className="space-y-2">
              {/* Public option */}
              <button
                onClick={() => setRoomType("public")}
                className="w-full p-4 rounded-lg flex items-start gap-3 text-left cursor-pointer transition-colors border"
                style={{
                  background:
                    roomType === "public"
                      ? "var(--primary-active)"
                      : "var(--card-bg-secondary)",
                  borderColor:
                    roomType === "public"
                      ? "var(--primary)"
                      : "transparent",
                }}
                onMouseEnter={(e) => {
                  if (roomType !== "public") {
                    e.currentTarget.style.background = "var(--hover-primary)";
                  }
                }}
                onMouseLeave={(e) => {
                  if (roomType !== "public") {
                    e.currentTarget.style.background = "var(--card-bg-secondary)";
                  }
                }}
              >
                <div
                  className="mt-0.5 flex-shrink-0"
                  style={{
                    color:
                      roomType === "public"
                        ? "var(--primary)"
                        : "var(--text-muted)",
                  }}
                >
                  <FiGlobe size={18} />
                </div>
                <div className="flex-1 min-w-0">
                  <div
                    className="text-sm font-medium"
                    style={{
                      color:
                        roomType === "public"
                          ? "var(--primary)"
                          : "var(--text-primary)",
                    }}
                  >
                    Công khai
                  </div>
                  <div
                    className="text-xs mt-0.5"
                    style={{ color: "var(--text-secondary)" }}
                  >
                    Mọi thành viên trong space đều có thể tham gia và xem tin
                    nhắn
                  </div>
                </div>
                <div
                  className="w-4 h-4 rounded-full border-2 flex-shrink-0 mt-0.5 flex items-center justify-center"
                  style={{
                    borderColor:
                      roomType === "public"
                        ? "var(--primary)"
                        : "var(--text-muted)",
                  }}
                >
                  {roomType === "public" && (
                    <div
                      className="w-2 h-2 rounded-full"
                      style={{ background: "var(--primary)" }}
                    />
                  )}
                </div>
              </button>

              {/* Private option */}
              <button
                onClick={() => setRoomType("private")}
                className="w-full p-4 rounded-lg flex items-start gap-3 text-left cursor-pointer transition-colors border"
                style={{
                  background:
                    roomType === "private"
                      ? "var(--primary-active)"
                      : "var(--card-bg-secondary)",
                  borderColor:
                    roomType === "private"
                      ? "var(--primary)"
                      : "transparent",
                }}
                onMouseEnter={(e) => {
                  if (roomType !== "private") {
                    e.currentTarget.style.background = "var(--hover-primary)";
                  }
                }}
                onMouseLeave={(e) => {
                  if (roomType !== "private") {
                    e.currentTarget.style.background = "var(--card-bg-secondary)";
                  }
                }}
              >
                <div
                  className="mt-0.5 flex-shrink-0"
                  style={{
                    color:
                      roomType === "private"
                        ? "var(--primary)"
                        : "var(--text-muted)",
                  }}
                >
                  <FiLock size={18} />
                </div>
                <div className="flex-1 min-w-0">
                  <div
                    className="text-sm font-medium"
                    style={{
                      color:
                        roomType === "private"
                          ? "var(--primary)"
                          : "var(--text-primary)",
                    }}
                  >
                    Riêng tư
                  </div>
                  <div
                    className="text-xs mt-0.5"
                    style={{ color: "var(--text-secondary)" }}
                  >
                    Chỉ thành viên được mời mới có thể tham gia và xem tin nhắn
                  </div>
                </div>
                <div
                  className="w-4 h-4 rounded-full border-2 flex-shrink-0 mt-0.5 flex items-center justify-center"
                  style={{
                    borderColor:
                      roomType === "private"
                        ? "var(--primary)"
                        : "var(--text-muted)",
                  }}
                >
                  {roomType === "private" && (
                    <div
                      className="w-2 h-2 rounded-full"
                      style={{ background: "var(--primary)" }}
                    />
                  )}
                </div>
              </button>
            </div>
          </div>
        </div>
      </div>

      {/* API Error */}
      {apiError && (
        <div
          className="px-6 py-3 border-t"
          style={{
            borderColor: "var(--danger)",
            background: "rgba(239, 68, 68, 0.1)",
            color: "var(--danger)",
          }}
        >
          <div className="text-sm">{apiError}</div>
        </div>
      )}

      {/* Footer */}
      <div
        className="px-6 py-4 border-t flex justify-end gap-3"
        style={{
          borderColor: "var(--border-primary)",
          background: "var(--bg-surface-secondary)",
        }}
      >
        <button
          onClick={onCancel}
          disabled={isSubmitting}
          className="px-4 py-2 rounded-md text-sm font-medium cursor-pointer"
          style={{
            background: "transparent",
            color: "var(--text-primary)",
            border: "1px solid var(--border-primary)",
          }}
          onMouseEnter={(e) =>
            (e.currentTarget.style.background = "var(--hover-primary)")
          }
          onMouseLeave={(e) => (e.currentTarget.style.background = "transparent")}
        >
          Hủy
        </button>
        <button
          onClick={handleSubmit}
          disabled={isSubmitting}
          className="px-4 py-2 rounded-md text-sm font-medium cursor-pointer"
          style={{
            background: isSubmitting ? "var(--text-muted)" : "var(--primary)",
            color: isDark ? "var(--bg-surface)" : "#fff",
            opacity: isSubmitting ? 0.7 : 1,
          }}
          onMouseEnter={(e) => {
            if (!isSubmitting)
              e.currentTarget.style.background = "var(--primary-hover)";
          }}
          onMouseLeave={(e) => {
            if (!isSubmitting)
              e.currentTarget.style.background = "var(--primary)";
          }}
        >
          {isSubmitting ? "Đang tạo..." : "Tạo room"}
        </button>
      </div>
    </div>
  );
}

export default CreateRoomView;