<template>
  <div class="admin--modal-overlay">
    <div class="admin--modal password--recommend-modal">
      <div class="admin--modal-header">
        <h4>비밀번호 변경 권장</h4>
      </div>

      <div class="admin--modal-body">
        <div class="admin--warning-box">
          <svg width="48" height="48" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M12 2L2 20h20L12 2zm0 5.5l6.5 11h-13L12 7.5z" fill="#ff9800"/>
            <path d="M11 10h2v5h-2v-5zm0 6h2v2h-2v-2z" fill="#fff"/>
          </svg>
          <h3>비밀번호 변경이 필요합니다</h3>
          <p>마지막 비밀번호 변경 후 6개월이 경과하였습니다.</p>
          <p>보안을 위해 비밀번호를 변경해주세요.</p>
        </div>

        <form @submit.prevent="changePassword" v-if="showPasswordForm">
          <div class="admin--form-group">
            <label class="admin--form-label">현재 비밀번호 *</label>
            <input
              v-model="formData.current_password"
              type="password"
              class="admin--form-input"
              required
              placeholder="현재 비밀번호 입력"
            />
          </div>

          <div class="admin--form-group">
            <label class="admin--form-label">새 비밀번호 *</label>
            <input
              v-model="formData.new_password"
              type="password"
              class="admin--form-input"
              required
              placeholder="최소 8자 이상"
              minlength="8"
            />
          </div>

          <div class="admin--form-group">
            <label class="admin--form-label">새 비밀번호 확인 *</label>
            <input
              v-model="formData.new_password_confirm"
              type="password"
              class="admin--form-input"
              required
              placeholder="새 비밀번호 재입력"
              minlength="8"
            />
          </div>

          <div v-if="errorMessage" class="admin--error-message">
            {{ errorMessage }}
          </div>
        </form>
      </div>

      <div class="admin--modal-footer">
        <button
          type="button"
          @click="skipAndContinue"
          class="admin--btn-small admin--btn-small-secondary"
        >
          나중에 변경
        </button>
        <button
          v-if="!showPasswordForm"
          type="button"
          @click="showPasswordForm = true"
          class="admin--btn-small admin--btn-small-primary"
        >
          지금 변경
        </button>
        <button
          v-else
          type="button"
          @click="changePassword"
          class="admin--btn-small admin--btn-small-primary"
          :disabled="saving"
        >
          {{ saving ? '변경 중...' : '변경' }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const props = defineProps({
  adminId: {
    type: Number,
    required: true
  }
})

const emit = defineEmits(['close', 'changed'])

const { post } = useApi()

const showPasswordForm = ref(false)
const saving = ref(false)
const errorMessage = ref('')

const formData = ref({
  current_password: '',
  new_password: '',
  new_password_confirm: ''
})

const skipAndContinue = () => {
  emit('close')
}

const changePassword = async () => {
  errorMessage.value = ''

  // 비밀번호 확인
  if (formData.value.new_password !== formData.value.new_password_confirm) {
    errorMessage.value = '새 비밀번호가 일치하지 않습니다.'
    return
  }

  if (formData.value.new_password.length < 8) {
    errorMessage.value = '비밀번호는 최소 8자 이상이어야 합니다.'
    return
  }

  if (!formData.value.current_password) {
    errorMessage.value = '현재 비밀번호를 입력해주세요.'
    return
  }

  saving.value = true

  try {
    const requestData = {
      current_password: formData.value.current_password,
      new_password: formData.value.new_password
    }

    const { data, error } = await post(`/admin/${props.adminId}/password`, requestData)

    if (error) {
      console.error('[PasswordChangeRecommendModal] 변경 실패:', error)
      errorMessage.value = error.response?.data?.message || '비밀번호 변경에 실패했습니다.'
      return
    }

    if (data?.success) {
      emit('changed', '비밀번호가 변경되었습니다.')
    }
  } finally {
    saving.value = false
  }
}
</script>

<style>
.admin--modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}

.admin--modal {
  background: #ffffff;
  padding: 0;
  border-radius: 8px;
  min-width: 500px;
  max-width: 600px;
  width: 90%;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}

.password--recommend-modal {
  border: 2px solid #ff9800;
}

.admin--modal-header {
  padding: 20px;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
  background: #fafafa;
}

.admin--modal-header h4 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #1a1a1a;
}

.admin--modal-body {
  padding: 24px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.admin--warning-box {
  text-align: center;
  padding: 20px;
  margin-bottom: 24px;
}

.admin--warning-box svg {
  margin: 0 auto 16px;
  display: block;
}

.admin--warning-box h3 {
  margin: 0 0 12px 0;
  font-size: 20px;
  font-weight: 600;
  color: #ff9800;
}

.admin--warning-box p {
  margin: 6px 0;
  color: #333333;
  font-size: 14px;
  line-height: 1.6;
}

.admin--form-group {
  display: flex;
  flex-direction: column;
  margin-bottom: 20px;
}

.admin--form-group:last-child {
  margin-bottom: 0;
}

.admin--form-label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 500;
  color: #333333;
}

.admin--form-input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  background: #ffffff;
  color: #333333;
  font-size: 14px;
  transition: border-color 0.2s;
}

.admin--form-input:focus {
  outline: none;
  border-color: var(--admin-accent-primary, #217346);
}

.admin--form-input::placeholder {
  color: #999;
}

.admin--error-message {
  margin-top: 12px;
  padding: 12px;
  background: rgba(244, 67, 54, 0.1);
  border: 1px solid rgba(244, 67, 54, 0.3);
  border-radius: 4px;
  color: #f44336;
  font-size: 13px;
}

.admin--modal-footer {
  padding: 20px 24px;
  border-top: 1px solid #e0e0e0;
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  flex-shrink: 0;
}

.admin--btn-small {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  font-family: 'FORDKOREAType', sans-serif;
  white-space: nowrap;
}

.admin--btn-small:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.admin--btn-small-secondary {
  background: #f5f5f5;
  color: #333333;
  border: 1px solid #e0e0e0;
}

.admin--btn-small-secondary:hover:not(:disabled) {
  background: #eeeeee;
  border-color: var(--admin-accent-primary, #217346);
  color: #333333;
}

.admin--btn-small-primary {
  background: var(--admin-accent-primary, #217346);
  color: #ffffff;
}

.admin--btn-small-primary:hover:not(:disabled) {
  background: var(--admin-accent-hover, #1a5c37);
}
</style>
