AdminModal.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <template>
  2. <div class="admin--modal-overlay" @click.self="close">
  3. <div class="admin--modal">
  4. <div class="admin--modal-header">
  5. <h4>{{ isEdit ? '관리자 수정' : '관리자 추가' }}</h4>
  6. <button @click="close" class="admin--modal-close">&times;</button>
  7. </div>
  8. <form @submit.prevent="save" class="admin--modal-form">
  9. <div class="admin--modal-body">
  10. <div class="admin--form-row">
  11. <div class="admin--form-group">
  12. <label class="admin--form-label">아이디 *</label>
  13. <div v-if="!isEdit" class="admin--input-with-button">
  14. <input
  15. v-model="formData.username"
  16. type="text"
  17. class="admin--form-input"
  18. required
  19. placeholder="영문, 숫자 조합"
  20. @input="handleUsernameInput"
  21. />
  22. <button
  23. type="button"
  24. class="admin--btn-check"
  25. @click="checkDuplicateUsername"
  26. :disabled="!formData.username || isCheckingUsername"
  27. >
  28. {{ isCheckingUsername ? '확인 중...' : '중복 체크' }}
  29. </button>
  30. </div>
  31. <input
  32. v-else
  33. v-model="formData.username"
  34. type="text"
  35. class="admin--form-input"
  36. required
  37. :disabled="true"
  38. placeholder="영문, 숫자 조합"
  39. />
  40. <p v-if="!isEdit && usernameCheckResult === 'available'" class="admin--form-help admin--text-success">
  41. 사용 가능한 아이디입니다.
  42. </p>
  43. <p v-if="!isEdit && usernameCheckResult === 'duplicate'" class="admin--form-help admin--text-error">
  44. 이미 사용 중인 아이디입니다.
  45. </p>
  46. </div>
  47. <div class="admin--form-group">
  48. <label class="admin--form-label">이름 *</label>
  49. <input
  50. v-model="formData.name"
  51. type="text"
  52. class="admin--form-input"
  53. required
  54. placeholder="관리자 이름"
  55. />
  56. </div>
  57. </div>
  58. <div class="admin--form-row">
  59. <div class="admin--form-group">
  60. <label class="admin--form-label">이메일 *</label>
  61. <input
  62. v-model="formData.email"
  63. type="email"
  64. class="admin--form-input"
  65. required
  66. placeholder="example@email.com"
  67. />
  68. </div>
  69. <div class="admin--form-group">
  70. <label class="admin--form-label">부서명</label>
  71. <input
  72. v-model="formData.department"
  73. type="text"
  74. class="admin--form-input"
  75. placeholder="예: 개발팀, 영업팀"
  76. />
  77. </div>
  78. </div>
  79. <div class="admin--form-row">
  80. <div class="admin--form-group">
  81. <label class="admin--form-label">역할 *</label>
  82. <select v-model="formData.role" class="admin--form-select" required>
  83. <option value="admin">일반 관리자</option>
  84. <option value="super_admin">슈퍼 관리자</option>
  85. </select>
  86. </div>
  87. <div class="admin--form-group">
  88. <label class="admin--form-label">상태 *</label>
  89. <select v-model="formData.status" class="admin--form-select" required>
  90. <option value="active">활성</option>
  91. <option value="inactive">비활성</option>
  92. </select>
  93. </div>
  94. </div>
  95. <div class="admin--form-row" v-if="!isEdit">
  96. <div class="admin--form-group">
  97. <label class="admin--form-label">비밀번호 *</label>
  98. <input
  99. v-model="formData.password"
  100. type="password"
  101. class="admin--form-input"
  102. :required="!isEdit"
  103. placeholder="최소 8자 이상"
  104. minlength="8"
  105. />
  106. </div>
  107. <div class="admin--form-group">
  108. <label class="admin--form-label">비밀번호 확인 *</label>
  109. <input
  110. v-model="formData.password_confirm"
  111. type="password"
  112. class="admin--form-input"
  113. :required="!isEdit"
  114. placeholder="비밀번호 재입력"
  115. minlength="8"
  116. />
  117. </div>
  118. </div>
  119. </div>
  120. <div class="admin--modal-footer">
  121. <button type="button" @click="close" class="admin--btn-small admin--btn-small-secondary">
  122. 취소
  123. </button>
  124. <button type="submit" class="admin--btn-small admin--btn-small-primary" :disabled="saving">
  125. {{ saving ? '저장 중...' : '저장' }}
  126. </button>
  127. </div>
  128. </form>
  129. </div>
  130. </div>
  131. </template>
  132. <script setup>
  133. import { ref, computed, watch } from 'vue'
  134. const props = defineProps({
  135. admin: {
  136. type: Object,
  137. default: null
  138. }
  139. })
  140. const emit = defineEmits(['close', 'saved'])
  141. const { get, post, put } = useApi()
  142. const isEdit = computed(() => !!props.admin)
  143. const saving = ref(false)
  144. const isCheckingUsername = ref(false)
  145. const usernameCheckResult = ref('') // 'available', 'duplicate', ''
  146. const formData = ref({
  147. username: '',
  148. name: '',
  149. email: '',
  150. department: '',
  151. password: '',
  152. password_confirm: '',
  153. role: 'admin',
  154. status: 'active'
  155. })
  156. // props.admin이 변경되면 formData 업데이트
  157. watch(() => props.admin, (newAdmin) => {
  158. if (newAdmin) {
  159. formData.value = {
  160. username: newAdmin.username || '',
  161. name: newAdmin.name || '',
  162. email: newAdmin.email || '',
  163. department: newAdmin.department || '',
  164. password: '',
  165. password_confirm: '',
  166. role: newAdmin.role || 'admin',
  167. status: newAdmin.status || 'active'
  168. }
  169. }
  170. }, { immediate: true })
  171. // 아이디 입력 시 중복 체크 결과 초기화
  172. const handleUsernameInput = () => {
  173. usernameCheckResult.value = ''
  174. }
  175. // 아이디 중복 체크
  176. const checkDuplicateUsername = async () => {
  177. if (!formData.value.username) {
  178. alert('아이디를 입력하세요.')
  179. return
  180. }
  181. isCheckingUsername.value = true
  182. usernameCheckResult.value = ''
  183. try {
  184. const { data, error } = await get('/admin/check-username', {
  185. params: {
  186. username: formData.value.username
  187. }
  188. })
  189. console.log('[AdminModal] 중복 체크 응답:', { data, error })
  190. if (error) {
  191. alert('중복 체크에 실패했습니다.')
  192. return
  193. }
  194. if (data?.success) {
  195. if (data?.data?.available) {
  196. usernameCheckResult.value = 'available'
  197. } else {
  198. usernameCheckResult.value = 'duplicate'
  199. }
  200. }
  201. } catch (error) {
  202. console.error('중복 체크 오류:', error)
  203. alert('중복 체크 중 오류가 발생했습니다.')
  204. } finally {
  205. isCheckingUsername.value = false
  206. }
  207. }
  208. const close = () => {
  209. emit('close')
  210. }
  211. const save = async () => {
  212. // 신규 생성 시 유효성 검사
  213. if (!isEdit.value) {
  214. // 아이디 중복 체크 확인
  215. if (usernameCheckResult.value !== 'available') {
  216. alert('아이디 중복 체크를 해주세요.')
  217. return
  218. }
  219. // 비밀번호 확인
  220. if (formData.value.password !== formData.value.password_confirm) {
  221. alert('비밀번호가 일치하지 않습니다.')
  222. return
  223. }
  224. if (formData.value.password.length < 8) {
  225. alert('비밀번호는 최소 8자 이상이어야 합니다.')
  226. return
  227. }
  228. }
  229. saving.value = true
  230. try {
  231. let result
  232. if (isEdit.value) {
  233. // 수정
  234. const updateData = {
  235. name: formData.value.name,
  236. email: formData.value.email,
  237. department: formData.value.department,
  238. role: formData.value.role,
  239. status: formData.value.status
  240. }
  241. result = await put(`/admin/${props.admin.id}`, updateData)
  242. } else {
  243. // 생성
  244. const createData = {
  245. username: formData.value.username,
  246. name: formData.value.name,
  247. email: formData.value.email,
  248. department: formData.value.department,
  249. password: formData.value.password,
  250. role: formData.value.role,
  251. status: formData.value.status
  252. }
  253. result = await post('/admin', createData)
  254. }
  255. const { data, error } = result
  256. if (error) {
  257. console.error('[AdminModal] 저장 실패:', error)
  258. const errorMessage = error.response?.data?.message || '저장에 실패했습니다.'
  259. emit('saved', errorMessage)
  260. return
  261. }
  262. if (data?.success) {
  263. emit('saved', data.message || '저장되었습니다.')
  264. }
  265. } finally {
  266. saving.value = false
  267. }
  268. }
  269. </script>
  270. <style scoped>
  271. .admin--modal-overlay {
  272. position: fixed;
  273. top: 0;
  274. left: 0;
  275. right: 0;
  276. bottom: 0;
  277. background: rgba(0, 0, 0, 0.5);
  278. z-index: 9999;
  279. display: flex;
  280. align-items: center;
  281. justify-content: center;
  282. }
  283. .admin--modal {
  284. background: #ffffff;
  285. padding: 0;
  286. border-radius: 8px;
  287. min-width: 700px;
  288. max-width: 800px;
  289. width: 90%;
  290. box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  291. max-height: 90vh;
  292. display: flex;
  293. flex-direction: column;
  294. }
  295. .admin--modal-form {
  296. display: flex;
  297. flex-direction: column;
  298. flex: 1;
  299. min-height: 0;
  300. }
  301. .admin--modal-header {
  302. padding: 20px;
  303. border-bottom: 1px solid #e0e0e0;
  304. display: flex;
  305. justify-content: space-between;
  306. align-items: center;
  307. flex-shrink: 0;
  308. }
  309. .admin--modal-header h4 {
  310. margin: 0;
  311. font-size: 18px;
  312. font-weight: 600;
  313. color: #1a1a1a;
  314. }
  315. .admin--modal-close {
  316. background: none;
  317. border: none;
  318. font-size: 28px;
  319. cursor: pointer;
  320. color: #999;
  321. line-height: 1;
  322. transition: color 0.2s;
  323. padding: 0;
  324. width: 30px;
  325. height: 30px;
  326. display: flex;
  327. align-items: center;
  328. justify-content: center;
  329. }
  330. .admin--modal-close:hover {
  331. color: #333;
  332. }
  333. .admin--modal-body {
  334. padding: 24px;
  335. overflow-y: auto;
  336. flex: 1;
  337. min-height: 0;
  338. }
  339. .admin--form-row {
  340. display: grid;
  341. grid-template-columns: 1fr 1fr;
  342. gap: 16px;
  343. margin-bottom: 20px;
  344. }
  345. .admin--form-row:last-child {
  346. margin-bottom: 0;
  347. }
  348. .admin--form-group {
  349. display: flex;
  350. flex-direction: column;
  351. }
  352. .admin--form-label {
  353. display: block;
  354. margin-bottom: 8px;
  355. font-size: 14px;
  356. font-weight: 500;
  357. color: #333333;
  358. }
  359. .admin--form-input,
  360. .admin--form-select {
  361. width: 100%;
  362. padding: 10px 14px;
  363. border: 1px solid #e0e0e0;
  364. border-radius: 4px;
  365. background: #ffffff;
  366. color: #333333;
  367. font-size: 14px;
  368. transition: border-color 0.2s;
  369. }
  370. .admin--form-input:focus,
  371. .admin--form-select:focus {
  372. outline: none;
  373. border-color: var(--admin-accent-primary, #217346);
  374. }
  375. .admin--form-input:disabled {
  376. background: #f5f5f5;
  377. color: #999;
  378. cursor: not-allowed;
  379. }
  380. .admin--form-input::placeholder {
  381. color: #999;
  382. }
  383. .admin--modal-footer {
  384. padding: 20px 24px;
  385. border-top: 1px solid #e0e0e0;
  386. display: flex;
  387. gap: 10px;
  388. justify-content: flex-end;
  389. flex-shrink: 0;
  390. }
  391. .admin--btn-small {
  392. padding: 6px 14px;
  393. border: none;
  394. border-radius: 4px;
  395. font-size: 13px;
  396. font-weight: 500;
  397. cursor: pointer;
  398. transition: all 0.3s ease;
  399. font-family: 'FORDKOREAType', sans-serif;
  400. white-space: nowrap;
  401. }
  402. .admin--btn-small:disabled {
  403. opacity: 0.5;
  404. cursor: not-allowed;
  405. }
  406. .admin--btn-small-secondary {
  407. background: #f5f5f5;
  408. color: #333333;
  409. border: 1px solid #e0e0e0;
  410. }
  411. .admin--btn-small-secondary:hover:not(:disabled) {
  412. background: #eeeeee;
  413. border-color: var(--admin-accent-primary, #217346);
  414. color: #333333;
  415. }
  416. .admin--btn-small-primary {
  417. background: var(--admin-accent-primary, #217346);
  418. color: #ffffff;
  419. }
  420. .admin--btn-small-primary:hover:not(:disabled) {
  421. background: var(--admin-accent-hover, #1a5c37);
  422. }
  423. </style>