AdminModal.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. <input
  14. v-model="formData.username"
  15. type="text"
  16. class="admin--form-input"
  17. required
  18. :disabled="isEdit"
  19. placeholder="영문, 숫자 조합"
  20. />
  21. </div>
  22. <div class="admin--form-group">
  23. <label class="admin--form-label">이름 *</label>
  24. <input
  25. v-model="formData.name"
  26. type="text"
  27. class="admin--form-input"
  28. required
  29. placeholder="관리자 이름"
  30. />
  31. </div>
  32. </div>
  33. <div class="admin--form-row">
  34. <div class="admin--form-group">
  35. <label class="admin--form-label">이메일 *</label>
  36. <input
  37. v-model="formData.email"
  38. type="email"
  39. class="admin--form-input"
  40. required
  41. placeholder="example@email.com"
  42. />
  43. </div>
  44. <div class="admin--form-group">
  45. <label class="admin--form-label">역할 *</label>
  46. <select v-model="formData.role" class="admin--form-select" required>
  47. <option value="admin">일반 관리자</option>
  48. <option value="super_admin">슈퍼 관리자</option>
  49. </select>
  50. </div>
  51. </div>
  52. <div class="admin--form-row" v-if="!isEdit">
  53. <div class="admin--form-group">
  54. <label class="admin--form-label">비밀번호 *</label>
  55. <input
  56. v-model="formData.password"
  57. type="password"
  58. class="admin--form-input"
  59. :required="!isEdit"
  60. placeholder="최소 8자 이상"
  61. minlength="8"
  62. />
  63. </div>
  64. <div class="admin--form-group">
  65. <label class="admin--form-label">비밀번호 확인 *</label>
  66. <input
  67. v-model="formData.password_confirm"
  68. type="password"
  69. class="admin--form-input"
  70. :required="!isEdit"
  71. placeholder="비밀번호 재입력"
  72. minlength="8"
  73. />
  74. </div>
  75. </div>
  76. <div class="admin--form-row">
  77. <div class="admin--form-group">
  78. <label class="admin--form-label">상태 *</label>
  79. <select v-model="formData.status" class="admin--form-select" required>
  80. <option value="active">활성</option>
  81. <option value="inactive">비활성</option>
  82. </select>
  83. </div>
  84. </div>
  85. </div>
  86. <div class="admin--modal-footer">
  87. <button type="button" @click="close" class="admin--btn-small admin--btn-small-secondary">
  88. 취소
  89. </button>
  90. <button type="submit" class="admin--btn-small admin--btn-small-primary" :disabled="saving">
  91. {{ saving ? '저장 중...' : '저장' }}
  92. </button>
  93. </div>
  94. </form>
  95. </div>
  96. </div>
  97. </template>
  98. <script setup>
  99. import { ref, computed, watch } from 'vue'
  100. const props = defineProps({
  101. admin: {
  102. type: Object,
  103. default: null
  104. }
  105. })
  106. const emit = defineEmits(['close', 'saved'])
  107. const { post, put } = useApi()
  108. const isEdit = computed(() => !!props.admin)
  109. const saving = ref(false)
  110. const formData = ref({
  111. username: '',
  112. name: '',
  113. email: '',
  114. password: '',
  115. password_confirm: '',
  116. role: 'admin',
  117. status: 'active'
  118. })
  119. // props.admin이 변경되면 formData 업데이트
  120. watch(() => props.admin, (newAdmin) => {
  121. if (newAdmin) {
  122. formData.value = {
  123. username: newAdmin.username || '',
  124. name: newAdmin.name || '',
  125. email: newAdmin.email || '',
  126. password: '',
  127. password_confirm: '',
  128. role: newAdmin.role || 'admin',
  129. status: newAdmin.status || 'active'
  130. }
  131. }
  132. }, { immediate: true })
  133. const close = () => {
  134. emit('close')
  135. }
  136. const save = async () => {
  137. // 비밀번호 확인 (신규 생성 시)
  138. if (!isEdit.value) {
  139. if (formData.value.password !== formData.value.password_confirm) {
  140. emit('saved', '비밀번호가 일치하지 않습니다.')
  141. return
  142. }
  143. if (formData.value.password.length < 8) {
  144. emit('saved', '비밀번호는 최소 8자 이상이어야 합니다.')
  145. return
  146. }
  147. }
  148. saving.value = true
  149. try {
  150. let result
  151. if (isEdit.value) {
  152. // 수정
  153. const updateData = {
  154. name: formData.value.name,
  155. email: formData.value.email,
  156. role: formData.value.role,
  157. status: formData.value.status
  158. }
  159. result = await put(`/admin/${props.admin.id}`, updateData)
  160. } else {
  161. // 생성
  162. const createData = {
  163. username: formData.value.username,
  164. name: formData.value.name,
  165. email: formData.value.email,
  166. password: formData.value.password,
  167. role: formData.value.role,
  168. status: formData.value.status
  169. }
  170. result = await post('/admin', createData)
  171. }
  172. const { data, error } = result
  173. if (error) {
  174. console.error('[AdminModal] 저장 실패:', error)
  175. const errorMessage = error.response?.data?.message || '저장에 실패했습니다.'
  176. emit('saved', errorMessage)
  177. return
  178. }
  179. if (data?.success) {
  180. emit('saved', data.message || '저장되었습니다.')
  181. }
  182. } finally {
  183. saving.value = false
  184. }
  185. }
  186. </script>
  187. <style scoped>
  188. .admin--modal-overlay {
  189. position: fixed;
  190. top: 0;
  191. left: 0;
  192. right: 0;
  193. bottom: 0;
  194. background: rgba(0, 0, 0, 0.7);
  195. z-index: 9999;
  196. display: flex;
  197. align-items: center;
  198. justify-content: center;
  199. }
  200. .admin--modal {
  201. background: #2d2d2d;
  202. padding: 0;
  203. border-radius: 8px;
  204. min-width: 700px;
  205. max-width: 800px;
  206. width: 90%;
  207. box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  208. max-height: 90vh;
  209. display: flex;
  210. flex-direction: column;
  211. }
  212. .admin--modal-form {
  213. display: flex;
  214. flex-direction: column;
  215. flex: 1;
  216. min-height: 0;
  217. }
  218. .admin--modal-header {
  219. padding: 20px;
  220. border-bottom: 1px solid #404040;
  221. display: flex;
  222. justify-content: space-between;
  223. align-items: center;
  224. flex-shrink: 0;
  225. }
  226. .admin--modal-header h4 {
  227. margin: 0;
  228. font-size: 18px;
  229. font-weight: 600;
  230. color: #ffffff;
  231. }
  232. .admin--modal-close {
  233. background: none;
  234. border: none;
  235. font-size: 28px;
  236. cursor: pointer;
  237. color: #999;
  238. line-height: 1;
  239. transition: color 0.2s;
  240. padding: 0;
  241. width: 30px;
  242. height: 30px;
  243. display: flex;
  244. align-items: center;
  245. justify-content: center;
  246. }
  247. .admin--modal-close:hover {
  248. color: #fff;
  249. }
  250. .admin--modal-body {
  251. padding: 24px;
  252. overflow-y: auto;
  253. flex: 1;
  254. min-height: 0;
  255. }
  256. .admin--form-row {
  257. display: grid;
  258. grid-template-columns: 1fr 1fr;
  259. gap: 16px;
  260. margin-bottom: 20px;
  261. }
  262. .admin--form-row:last-child {
  263. margin-bottom: 0;
  264. }
  265. .admin--form-group {
  266. display: flex;
  267. flex-direction: column;
  268. }
  269. .admin--form-label {
  270. display: block;
  271. margin-bottom: 8px;
  272. font-size: 14px;
  273. font-weight: 500;
  274. color: #e0e0e0;
  275. }
  276. .admin--form-input,
  277. .admin--form-select {
  278. width: 100%;
  279. padding: 10px 14px;
  280. border: 1px solid #404040;
  281. border-radius: 4px;
  282. background: #1a1a1a;
  283. color: #ffffff;
  284. font-size: 14px;
  285. transition: border-color 0.2s;
  286. }
  287. .admin--form-input:focus,
  288. .admin--form-select:focus {
  289. outline: none;
  290. border-color: var(--admin-accent-primary, #217346);
  291. }
  292. .admin--form-input:disabled {
  293. background: #252525;
  294. color: #666;
  295. cursor: not-allowed;
  296. }
  297. .admin--form-input::placeholder {
  298. color: #666;
  299. }
  300. .admin--modal-footer {
  301. padding: 20px 24px;
  302. border-top: 1px solid #404040;
  303. display: flex;
  304. gap: 10px;
  305. justify-content: flex-end;
  306. flex-shrink: 0;
  307. }
  308. .admin--btn-small {
  309. padding: 10px 20px;
  310. border-radius: 4px;
  311. cursor: pointer;
  312. font-size: 14px;
  313. transition: all 0.2s;
  314. border: none;
  315. font-weight: 500;
  316. }
  317. .admin--btn-small:disabled {
  318. opacity: 0.5;
  319. cursor: not-allowed;
  320. }
  321. .admin--btn-small-secondary {
  322. background: #252525;
  323. color: #e0e0e0;
  324. border: 1px solid #404040;
  325. }
  326. .admin--btn-small-secondary:hover:not(:disabled) {
  327. background: #2d2d2d;
  328. }
  329. .admin--btn-small-primary {
  330. background: var(--admin-accent-primary, #217346);
  331. color: white;
  332. }
  333. .admin--btn-small-primary:hover:not(:disabled) {
  334. background: var(--admin-accent-hover, #1a5c37);
  335. }
  336. </style>