File size: 14,866 Bytes
33d9e63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee88768
33d9e63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
/**
 * @license
 * SPDX-License-Identifier: Apache-2.0
 */

import React from 'react';
import { Project } from '../../types';
import { Button } from '../common/Button';
import { Badge } from '../common/Badge';
import { projectService } from '../../services/projectService';
import { Modal } from '../common/Modal';
import {
  LayoutDashboard,
  Settings,
  Share2,
  Home,
  ExternalLink,
  LogOut,
  Menu,
  Users,
  HelpCircle,
  Play,
  Pause,
} from 'lucide-react';

interface OpsLayoutProps {
  project?: Project | null;
  onUpdateProject?: (updated: Project) => void;
  activeTab?: 'dashboard' | 'basic' | 'organization' | 'questions' | 'distribution';
  onNavigateTab?: (tab: 'dashboard' | 'basic' | 'organization' | 'questions' | 'distribution') => void;
  onNavigateHome?: () => void;
  onLogout: () => void;
  children: React.ReactNode;
}

export const OpsLayout: React.FC<OpsLayoutProps> = ({
  project,
  onUpdateProject,
  activeTab,
  onNavigateTab,
  onNavigateHome,
  onLogout,
  children,
}) => {
  const [mobileMenuOpen, setMobileMenuOpen] = React.useState(false);
  const isProjectPage = !!project;
  const [adminId, setAdminId] = React.useState<string>('Admin');

  // Modal states
  const [modalOpen, setModalOpen] = React.useState(false);
  const [modalTitle, setModalTitle] = React.useState('');
  const [modalMessage, setModalMessage] = React.useState('');
  const [modalConfirmAction, setModalConfirmAction] = React.useState<(() => void) | null>(null);
  const [isAlert, setIsAlert] = React.useState(false);

  React.useEffect(() => {
    if (typeof window !== 'undefined') {
      const storedId = localStorage.getItem('stateless_ops_user_id');
      if (storedId) {
        setAdminId(storedId);
      }
    }
  }, []);

  // Status badge styling
  const getStatusBadge = (status: Project['status']) => {
    switch (status) {
      case 'draft':
        return (
          <Badge variant="default">
            초안 (Draft)
          </Badge>
        );
      case 'collecting':
        return (
          <Badge variant="primary" animate>
            수집 중 (Collecting)
          </Badge>
        );
      case 'closed':
        return (
          <Badge variant="slate">
            마감됨 (Closed)
          </Badge>
        );
      default:
        return null;
    }
  };

  // Response rates calculations
  const totalEmployees = project?.employees.length || 0;
  const submittedCount = project?.employees.filter((e) => e.response_status === 'submitted').length || 0;
  const responseRate = totalEmployees > 0 ? Math.round((submittedCount / totalEmployees) * 100) : 0;

  const handleToggleStatus = () => {
    if (!project) return;
    const nextStatus: Project['status'] = project.status === 'collecting' ? 'closed' : 'collecting';
    const msg =
      nextStatus === 'closed'
        ? '설문을 마감하시겠습니까? 마감 시 임직원들은 더 이상 설문 페이지에 접속할 수 없습니다.'
        : '설문 수집을 재개하시겠습니까?';

    setModalTitle('프로젝트 상태 변경');
    setModalMessage(msg);
    setIsAlert(false);
    setModalConfirmAction(() => async () => {
      setModalOpen(false);
      try {
        const updated = await projectService.updateProjectStatus(project.id, nextStatus);
        if (updated) {
          onUpdateProject?.(updated);
          // Show success modal
          setModalTitle('알림');
          setModalMessage('프로젝트 수집 상태가 변경되었습니다.');
          setIsAlert(true);
          setModalConfirmAction(null);
          setModalOpen(true);
        }
      } catch (err: any) {
        // Show error modal
        setModalTitle('오류');
        setModalMessage(`수집 상태 변경 실패: ${err.message}`);
        setIsAlert(true);
        setModalConfirmAction(null);
        setModalOpen(true);
      }
    });
    setModalOpen(true);
  };

  return (
    <div className="min-h-screen bg-white flex flex-col font-sans antialiased text-slate-800" id="ops-layout-wrapper">
      {/* Top Navbar */}
      <header
        className="sticky top-0 z-40 bg-white border-b border-slate-100 h-16 flex items-center justify-between px-4 md:px-6 shrink-0"
        id="ops-top-nav"
      >
        <div className="flex items-center gap-1">
          {project && (
            <button
              onClick={() => setMobileMenuOpen(!mobileMenuOpen)}
              className="p-1.5 text-slate-500 hover:text-slate-800 border-0 bg-transparent lg:hidden cursor-pointer"
              id="ops-mobile-menu-toggle"
            >
              <Menu className="w-5 h-5" />
            </button>
          )}
          <div
            onClick={onNavigateHome}
            className={`flex items-center gap-2 ${onNavigateHome ? 'cursor-pointer hover:opacity-90 active:scale-98 transition-all' : ''}`}
            id="ops-home-logo"
          >
            <div className="w-6 h-6 md:w-7 md:h-7 bg-orange-500 rounded-sm flex items-center justify-center text-white font-bold shadow-xs">
              <Home className="w-3.5 h-3.5 md:w-4.5 md:h-4.5" strokeWidth={2.5} />
            </div>

            <div className="flex items-baseline tracking-tight">
              <h1 className={`text-xl font-semibold text-slate-900 ${isProjectPage ? 'hidden' : 'block'}`}>
                조직 네트워크 진단
                <span className="pl-2 text-sm font-medium text-slate-500">설문 관리 시스템</span>
              </h1>

              {project && (
                <div
                  onClick={(e) => {
                    e.stopPropagation();
                    onNavigateTab?.('dashboard');
                  }}
                  className="flex items-center cursor-pointer hover:text-orange-500 transition-colors"
                  title="데이터 관리 탭으로 이동"
                  id="ops-project-title-link"
                >
                  <span className="text-lg md:text-xl font-semibold tracking-tight leading-none">
                    {project.company_info.name}<span className="hidden md:inline"> : {project.title}</span>
                  </span>
                </div>
              )}
            </div>
          </div>
        </div>

        <div className="flex items-center gap-3">
          <div className="hidden md:flex flex-col items-end text-right">
            <span className="text-sm font-semibold text-slate-700">{adminId}</span>
          </div>
          <Button
            onClick={onLogout}
            variant="outline"
            size="md"
            leftIcon={<LogOut className="w-3.5 h-3.5" />}
            id="ops-logout-btn"
            className="!hidden md:!inline-flex"
          >
            로그아웃
          </Button>
        </div>
      </header>

      {/* Main Body */}
      <div className="flex-1 flex" id="ops-body-container">
        {/* Sidebar Left */}
        {project && (
          <aside
            className={`fixed inset-y-16 left-0 z-30 w-80 shrink-0 bg-slate-50 border-r border-slate-100 flex flex-col justify-between transition-transform duration-300 lg:sticky lg:transform-none lg:z-0 ${mobileMenuOpen ? 'translate-x-0' : '-translate-x-full lg:translate-x-0'
              }`}
            id="ops-sidebar"
          >
            <div className="p-4 space-y-6">
              <div className="space-y-4">
                <div className="px-3">
                  <div className="p-4 bg-white rounded-xl border border-slate-200/60 shadow-xs space-y-4" id='active-project-card'>
                    {/* Project status, duration and actions */}
                    <div className="space-y-4">
                      <div className="flex flex-col justify-center items-center gap-1.5 space-y-1">
                        <span className="text-sm font-bold text-slate-800 uppercase tracking-wider">
                          Project Status
                        </span>
                        {getStatusBadge(project.status)}
                        <p className="text-sm text-center text-slate-800 font-semibold tracking-wider">
                          {project.start_date} ~ {project.end_date}
                        </p>
                      </div>

                      <div className="space-y-2.5 pt-3 border-t border-slate-100">
                        <Button
                          href={project.status === 'collecting' ? project.common_survey_url : `${project.common_survey_url}&preview=true`}
                          target="_blank"
                          rel="noopener noreferrer"
                          variant="outline"
                          size="md"
                          leftIcon={<ExternalLink className="w-3.5 h-3.5 shrink-0" />}
                          id="ops-open-survey-page-link"
                          className="w-full"
                        >
                          {project.status === 'collecting' ? '설문 페이지 바로가기' : '설문 미리보기'}
                        </Button>

                        <Button
                          onClick={handleToggleStatus}
                          variant={project.status === 'collecting' ? 'amber' : 'emerald'}
                          size="md"
                          leftIcon={
                            project.status === 'collecting' ? (
                              <Pause className="w-3.5 h-3.5" />
                            ) : (
                              <Play className="w-3.5 h-3.5" />
                            )
                          }
                          className="w-full"
                        >
                          {project.status === 'collecting' ? '설문 마감하기' : '설문 수집 개시'}
                        </Button>
                      </div>
                    </div>
                  </div>
                </div>

                <nav className="space-y-1 px-1">
                  {onNavigateTab && activeTab && (
                    <>
                      <Button
                        onClick={() => {
                          onNavigateTab('dashboard');
                          setMobileMenuOpen(false);
                        }}
                        variant={activeTab === 'dashboard' ? 'menuActive' : 'menu'}
                        className="px-4 py-3 font-semibold"
                        id="ops-tab-dashboard"
                        leftIcon={<LayoutDashboard className="w-4.5 h-4.5" />}
                      >
                        데이터 관리
                      </Button>

                      <Button
                        onClick={() => {
                          onNavigateTab('organization');
                          setMobileMenuOpen(false);
                        }}
                        variant={activeTab === 'organization' ? 'menuActive' : 'menu'}
                        className="px-4 py-3 font-semibold"
                        id="ops-tab-organization"
                        leftIcon={<Users className="w-4.5 h-4.5" />}
                      >
                        직원 명단 관리
                      </Button>

                      <Button
                        onClick={() => {
                          onNavigateTab('questions');
                          setMobileMenuOpen(false);
                        }}
                        variant={activeTab === 'questions' ? 'menuActive' : 'menu'}
                        className="px-4 py-3 font-semibold"
                        id="ops-tab-questions"
                        leftIcon={<HelpCircle className="w-4.5 h-4.5" />}
                      >
                        문항 관리
                      </Button>

                      <Button
                        onClick={() => {
                          onNavigateTab('distribution');
                          setMobileMenuOpen(false);
                        }}
                        variant={activeTab === 'distribution' ? 'menuActive' : 'menu'}
                        className="px-4 py-3 font-semibold"
                        id="ops-tab-distribution"
                        leftIcon={<Share2 className="w-4.5 h-4.5" />}
                      >
                        설문 링크 관리
                      </Button>

                      <Button
                        onClick={() => {
                          onNavigateTab('basic');
                          setMobileMenuOpen(false);
                        }}
                        variant={activeTab === 'basic' ? 'menuActive' : 'menu'}
                        className="px-4 py-3 font-semibold"
                        id="ops-tab-basic"
                        leftIcon={<Settings className="w-4.5 h-4.5" />}
                      >
                        기본 설정 관리
                      </Button>
                    </>
                  )}
                </nav>
              </div>
            </div>
          </aside>
        )}

        {/* Main Content Pane */}
        <main className="flex-1 p-6 md:py-8 md:px-12 lg:px-16 overflow-y-auto max-w-full bg-slate-50" id="ops-main-pane">
          <div className="max-w-4xl mx-auto w-full px-0">
            {children}
          </div>
        </main>
      </div>

      {/* Footer */}
      <footer className="h-14 bg-white border-t border-slate-100 px-6 flex items-center justify-center text-xs md:text-sm font-medium text-slate-400 shrink-0" id="ops-layout-footer ">
        <div>
          &copy; 2026 시앤피컨설팅 주식회사. All rights reserved.
        </div>
      </footer>

      {/* Backdrop for mobile sidebar drawer */}
      {mobileMenuOpen && (
        <div
          onClick={() => setMobileMenuOpen(false)}
          className="fixed inset-0 z-20 bg-slate-900/10 backdrop-blur-xs lg:hidden"
          id="ops-sidebar-mobile-backdrop"
        />
      )}

      {/* Common Modal for dialogs */}
      <Modal
        isOpen={modalOpen}
        onClose={() => setModalOpen(false)}
        title={modalTitle}
        showCloseButton={true}
        footerActions={
          isAlert ? (
            <Button onClick={() => setModalOpen(false)} className="w-full">
              확인
            </Button>
          ) : (
            <div className="flex gap-2 w-full">
              <Button onClick={() => setModalOpen(false)} variant="outline" className="flex-1">
                취소
              </Button>
              <Button
                onClick={() => {
                  if (modalConfirmAction) {
                    modalConfirmAction();
                  }
                }}
                variant="primary"
                className="flex-1"
              >
                확인
              </Button>
            </div>
          )
        }
      >
        <p className="text-center py-2 text-slate-700">{modalMessage}</p>
      </Modal>
    </div>
  );
};