File size: 12,866 Bytes
987557e
4db60e4
cc0748d
 
 
4c817bd
 
 
 
 
 
 
 
 
 
987557e
11361a8
cc0748d
 
 
 
 
 
 
 
 
 
4c817bd
11361a8
4db60e4
 
25874a8
cc0748d
 
4db60e4
 
11361a8
 
4db60e4
 
 
 
 
25874a8
cc0748d
 
 
 
4db60e4
4c817bd
4db60e4
 
 
 
 
 
 
 
 
 
 
11361a8
 
 
4db60e4
 
 
 
11361a8
 
 
 
 
 
4db60e4
11361a8
 
987557e
11361a8
 
4c817bd
 
 
11361a8
 
 
 
987557e
770297a
4c817bd
4db60e4
 
 
 
4c817bd
 
 
 
4db60e4
987557e
 
11361a8
 
25874a8
cc0748d
 
4db60e4
11361a8
987557e
 
11361a8
 
987557e
cc0748d
 
 
 
 
 
 
 
 
 
987557e
11361a8
4db60e4
4c817bd
 
 
 
 
 
 
11361a8
4db60e4
 
 
11361a8
 
 
987557e
 
 
4c817bd
 
 
 
 
 
 
11361a8
 
4c817bd
 
 
 
11361a8
987557e
 
 
 
cc0748d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25874a8
cc0748d
25874a8
cc0748d
 
25874a8
cc0748d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25874a8
 
11361a8
987557e
4c817bd
 
 
 
 
11361a8
 
4c817bd
11361a8
 
 
4c817bd
987557e
 
11361a8
 
4c817bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11361a8
4c817bd
11361a8
4c817bd
11361a8
4c817bd
 
11361a8
4c817bd
 
4db60e4
4c817bd
 
 
 
4db60e4
4c817bd
4db60e4
 
 
 
 
 
 
4c817bd
 
 
 
4db60e4
4c817bd
4db60e4
 
 
 
 
 
11361a8
 
4c817bd
11361a8
4c817bd
11361a8
 
4c817bd
11361a8
 
4c817bd
11361a8
 
4c817bd
11361a8
987557e
 
 
11361a8
 
4c817bd
987557e
 
4c817bd
987557e
11361a8
 
 
 
 
 
4c817bd
 
 
987557e
 
11361a8
4c817bd
987557e
4c817bd
987557e
4c817bd
987557e
 
 
 
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
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
// frontend/src/components/admin/ClassForm.jsx
import React, { useState, useEffect } from "react";
import { Plus, Trash2, Loader2, Save, X, MapPin } from "lucide-react";
import { useQuery } from "@tanstack/react-query";
import api from "../../api/client";

const DAYS = [
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday",
  "Sunday",
];

export default function ClassForm({ classData, onSave, onCancel, isLoading }) {
  // Fetch membership plans if not provided
  const { data: plans = [], isLoading: plansLoading } = useQuery({
    queryKey: ["membership-plans"],
    queryFn: async () => {
      const res = await api.get("/admin/plans");
      return Array.isArray(res.data) ? res.data : [];
    },
    initialData: [],
  });

  // schedule: [{ day, start_time, end_time }]
  const [formData, setFormData] = useState({
    name: "",
    description: "",
    coach_email: "",
    location: "",
    membership_plan_ids: [],
    schedule: [{ day: "Monday", start_time: "", end_time: "" }],
    is_active: true,
  });

  useEffect(() => {
    if (!classData) return;
    setFormData({
      name: classData.name || "",
      description: classData.description || "",
      coach_email: classData.coach_email || "",
      location: classData.location || "",
      membership_plan_ids: Array.isArray(classData.membership_plan_ids) 
        ? classData.membership_plan_ids 
        : (classData.membership_plans?.map(p => p.id) || []),
      schedule:
        Array.isArray(classData.schedule) && classData.schedule.length > 0
          ? classData.schedule.map((slot) => ({
              day: slot.day || "Monday",
              start_time: slot.start_time || "",
              end_time: slot.end_time || "",
            }))
          : [{ day: "Monday", start_time: "", end_time: "" }],
      is_active:
        classData.is_active !== undefined ? classData.is_active : true,
    });
  }, [classData]);

  const addScheduleSlot = () => {
    setFormData((prev) => ({
      ...prev,
      schedule: [
        ...prev.schedule,
        { day: "Monday", start_time: "", end_time: "" },
      ],
    }));
  };

  const removeScheduleSlot = (index) => {
    setFormData((prev) => ({
      ...prev,
      schedule: prev.schedule.filter((_, i) => i !== index),
    }));
  };

  const updateScheduleSlot = (index, field, value) => {
    setFormData((prev) => {
      const schedule = [...prev.schedule];
      schedule[index] = { ...schedule[index], [field]: value };
      return { ...prev, schedule };
    });
  };

  const handleSubmit = (e) => {
    e.preventDefault();

    // Transform schedule to a simple structure that backend can later map
    const transformedSchedule = formData.schedule.map((slot) => ({
      day: slot.day,
      start_time: slot.start_time,
      end_time: slot.end_time,
      time:
        slot.start_time && slot.end_time
          ? `${slot.start_time}-${slot.end_time}`
          : "",
    }));

    const payload = {
      name: formData.name,
      description: formData.description,
      coach_email: formData.coach_email || null,
      location: formData.location || null,
      membership_plan_ids: formData.membership_plan_ids || [],
      schedule: transformedSchedule,
      is_active: formData.is_active,
    };

    onSave(payload);
  };

  const handlePlanToggle = (planId) => {
    setFormData((prev) => {
      const currentIds = prev.membership_plan_ids || [];
      const newIds = currentIds.includes(planId)
        ? currentIds.filter((id) => id !== planId)
        : [...currentIds, planId];
      return { ...prev, membership_plan_ids: newIds };
    });
  };

  return (
    <form onSubmit={handleSubmit} className="space-y-6">
      {/* Class Name */}
      <div className="space-y-1">
        <label className="block text-sm font-medium text-stone-800">
          Class Name
        </label>
        <input
          type="text"
          className="w-full rounded-md border border-stone-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-red-600"
          value={formData.name}
          onChange={(e) =>
            setFormData((prev) => ({ ...prev, name: e.target.value }))
          }
          placeholder="e.g., Beginner Karate"
          required
        />
      </div>

      {/* Description */}
      <div className="space-y-1">
        <label className="block text-sm font-medium text-stone-800">
          Description
        </label>
        <textarea
          className="w-full rounded-md border border-stone-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-red-600"
          rows={2}
          value={formData.description}
          onChange={(e) =>
            setFormData((prev) => ({
              ...prev,
              description: e.target.value,
            }))
          }
          placeholder="Class description..."
        />
      </div>

      {/* Coach Email and Location - Side by Side */}
      <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
        {/* Coach Email */}
        <div className="space-y-1">
          <label className="block text-sm font-medium text-stone-800">
            Coach Email (Optional)
          </label>
          <input
            type="email"
            className="w-full rounded-md border border-stone-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-red-600"
            value={formData.coach_email}
            onChange={(e) =>
              setFormData((prev) => ({ ...prev, coach_email: e.target.value }))
            }
            placeholder="coach@dojo.com"
          />
          <p className="text-xs text-stone-500 mt-1">
            Assign a coach to manage attendance.
          </p>
        </div>

        {/* Location */}
        <div className="space-y-1">
          <label className="block text-sm font-medium text-stone-800">
            Location
          </label>
          <div className="relative">
            <MapPin className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-stone-400" />
            <input
              type="text"
              className="w-full rounded-md border border-stone-300 pl-10 pr-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-red-600"
              value={formData.location}
              onChange={(e) =>
                setFormData((prev) => ({ ...prev, location: e.target.value }))
              }
              placeholder="Address or Zoom meeting link"
            />
          </div>
          <p className="text-xs text-stone-500 mt-1">
            Physical address or online meeting details.
          </p>
        </div>
      </div>

      {/* Membership Plans */}
      <div className="space-y-2">
        <label className="block text-sm font-medium text-stone-800">
          Membership Plans
        </label>
        <p className="text-xs text-stone-500 mb-3">
          Select which membership plans can access this class. Members under these plans will be able to see and enroll in this class.
        </p>
        {plansLoading ? (
          <div className="text-sm text-stone-500">Loading plans...</div>
        ) : plans.length === 0 ? (
          <div className="text-sm text-stone-500">No membership plans available.</div>
        ) : (
          <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3">
            {plans
              .filter((plan) => plan.is_active)
              .map((plan) => (
                <label
                  key={plan.id}
                  className="flex items-start gap-3 p-3 border border-stone-200 rounded-lg hover:bg-stone-50 cursor-pointer transition-colors"
                >
                  <input
                    type="checkbox"
                    checked={formData.membership_plan_ids?.includes(plan.id) || false}
                    onChange={() => handlePlanToggle(plan.id)}
                    className="mt-0.5 h-4 w-4 rounded border-stone-300 text-red-600 focus:ring-red-600"
                  />
                  <div className="flex-1 min-w-0">
                    <div className="text-sm font-medium text-stone-900">
                      {plan.name}
                    </div>
                    <div className="text-xs text-stone-500 mt-0.5">
                      ${plan.price} / {plan.billing_period}
                    </div>
                  </div>
                </label>
              ))}
          </div>
        )}
      </div>

      {/* Schedule */}
      <div>
        <div className="flex items-center justify-between mb-3">
          <span className="text-sm font-medium text-stone-800">
            Class Schedule
          </span>
          <button
            type="button"
            onClick={addScheduleSlot}
            className="inline-flex items-center gap-2 rounded-md border border-stone-300 px-2.5 py-1 text-xs font-medium text-stone-800 hover:bg-stone-50"
          >
            <Plus className="w-4 h-4" />
            Add Time Slot
          </button>
        </div>

        <div className="space-y-3">
          {formData.schedule.map((slot, index) => (
            <div
              key={index}
              className="rounded-lg border border-stone-200 bg-stone-50 px-3 py-3"
            >
              <div className="flex flex-col gap-3 md:flex-row md:items-center">
                {/* Day */}
                <div className="w-full md:w-40">
                  <label className="block text-xs font-medium text-stone-700 mb-1">
                    Day
                  </label>
                  <select
                    className="w-full rounded-md border border-stone-300 px-2 py-1.5 text-sm focus:outline-none focus:ring-2 focus:ring-red-600"
                    value={slot.day}
                    onChange={(e) =>
                      updateScheduleSlot(index, "day", e.target.value)
                    }
                  >
                    {DAYS.map((day) => (
                      <option key={day} value={day}>
                        {day}
                      </option>
                    ))}
                  </select>
                </div>

                {/* From / To */}
                <div className="flex flex-1 flex-col gap-3 md:flex-row">
                  <div className="flex-1">
                    <label className="block text-xs font-medium text-stone-700 mb-1">
                      From
                    </label>
                    <input
                      type="time"
                      className="w-full rounded-md border border-stone-300 px-2 py-1.5 text-sm focus:outline-none focus:ring-2 focus:ring-red-600"
                      value={slot.start_time}
                      onChange={(e) =>
                        updateScheduleSlot(index, "start_time", e.target.value)
                      }
                    />
                  </div>
                  <div className="flex-1">
                    <label className="block text-xs font-medium text-stone-700 mb-1">
                      To
                    </label>
                    <input
                      type="time"
                      className="w-full rounded-md border border-stone-300 px-2 py-1.5 text-sm focus:outline-none focus:ring-2 focus:ring-red-600"
                      value={slot.end_time}
                      onChange={(e) =>
                        updateScheduleSlot(index, "end_time", e.target.value)
                      }
                    />
                  </div>
                </div>

                {/* Remove */}
                {formData.schedule.length > 1 && (
                  <button
                    type="button"
                    onClick={() => removeScheduleSlot(index)}
                    className="self-start rounded-md p-2 text-red-600 hover:bg-red-50"
                  >
                    <Trash2 className="w-4 h-4" />
                  </button>
                )}
              </div>
            </div>
          ))}
        </div>
      </div>

      {/* Actions */}
      <div className="flex gap-3 pt-2">
        <button
          type="submit"
          disabled={isLoading}
          className="inline-flex items-center gap-2 rounded-md bg-red-600 px-4 py-2 text-sm font-medium text-white hover:bg-red-700 disabled:opacity-60"
        >
          {isLoading ? (
            <Loader2 className="w-4 h-4 animate-spin" />
          ) : (
            <Save className="w-4 h-4" />
          )}
          {classData ? "Update Class" : "Create Class"}
        </button>

        <button
          type="button"
          onClick={onCancel}
          disabled={isLoading}
          className="inline-flex items-center gap-2 rounded-md border border-stone-300 px-4 py-2 text-sm font-medium text-stone-800 hover:bg-stone-50 disabled:opacity-60"
        >
          <X className="w-4 h-4" />
          Cancel
        </button>
      </div>
    </form>
  );
}