<script setup>
// Optional final step: email the new merchant their login credentials.
// Shown after a successful onboarding run. Credentials are displayed read-only for
// reference and sent via /api/admin/merchant-onboarding/send-credentials.
const props = defineProps({
  modelValue: { type: Boolean, default: false },
  toEmail: { type: String, default: '' },
  username: { type: String, default: '' },
  password: { type: String, default: '' },
  isGerman: { type: Boolean, default: false }
})
const emit = defineEmits(['update:modelValue'])

const LOGIN_URL = 'https://app.logship.de'

const email = ref(props.toEmail)
watch(() => props.toEmail, (v) => { email.value = v })

const sending = ref(false)
const result = ref({ status: '', message: '' })

const close = () => {
  if (sending.value) return
  emit('update:modelValue', false)
}

const send = async () => {
  result.value = { status: '', message: '' }
  if (!email.value) {
    result.value = { status: 400, message: props.isGerman ? 'E-Mail-Adresse fehlt.' : 'Recipient e-mail is required.' }
    return
  }
  sending.value = true
  try {
    const res = await $fetch('/api/admin/merchant-onboarding/send-credentials', {
      method: 'POST',
      headers: useRequestHeaders(['cookie']),
      body: { toEmail: email.value, username: props.username, password: props.password }
    })
    if (Number(res.status) === 200) {
      result.value = { status: 200, message: props.isGerman ? 'Zugangsdaten gesendet.' : 'Credentials sent successfully.' }
    } else {
      result.value = { status: res.status || 500, message: res.message || 'Failed to send' }
    }
  } catch (e) {
    result.value = { status: 500, message: e?.data?.message || e?.message || 'Failed to send' }
  } finally {
    sending.value = false
  }
}
</script>

<template>
  <div class="modal" :class="{ active: modelValue }" :style="{ display: modelValue ? 'block' : 'none' }">
    <div class="modalDialog modalCenter" @click.self="close">
      <div class="modalContent" style="max-width: 480px;">
        <div class="modalHeader">
          <h3 class="modalTitle">{{ isGerman ? 'Zugangsdaten senden' : 'Send login credentials' }}</h3>
          <div class="cursor-pointer" @click="close"><span class="tf-icon ti ti-x ti-sm"></span></div>
        </div>
        <div class="modalBody">
          <div class="mx-5 my-4">
            <p class="mb-3" style="font-size: 0.88rem; color: #555;">
              {{ isGerman
                ? 'Sendet Benutzername, Passwort und Login-Link an die E-Mail-Adresse des Händlers.'
                : 'Emails the username, password and login link to the merchant.' }}
            </p>

            <div class="creds-box">
              <div><span class="creds-k">{{ isGerman ? 'Benutzername' : 'Username' }}</span><span class="creds-v">{{ username }}</span></div>
              <div><span class="creds-k">{{ isGerman ? 'Passwort' : 'Password' }}</span><span class="creds-v mono">{{ password }}</span></div>
              <div><span class="creds-k">Login</span><span class="creds-v">{{ LOGIN_URL }}</span></div>
            </div>

            <label class="label d-block mt-3">{{ isGerman ? 'Empfänger E-Mail' : 'Recipient e-mail' }} *</label>
            <input type="email" v-model="email" class="input" placeholder="merchant@example.com" />

            <div v-if="result.message" class="mt-3 result-line" :class="Number(result.status) === 200 ? 'is-ok' : 'is-err'">
              {{ result.message }}
            </div>
          </div>
        </div>
        <div class="modalFooter">
          <button type="button" class="button" @click="close">{{ isGerman ? 'Schließen' : 'Close' }}</button>
          <button type="button" class="button is-primary" @click="send"
                  :class="{ 'pointerEvents-none opacity-0.6': sending || !email }">
            <span v-if="sending" class="ti ti-loader spin me-1"></span>
            {{ sending ? (isGerman ? 'Senden…' : 'Sending…') : (isGerman ? 'Senden' : 'Send') }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.creds-box {
  background: var(--theme-bg-secondary, #f8fafc);
  border: 1px solid var(--theme-border, #e2e8f0);
  border-radius: 8px;
  padding: 10px 12px;
}
.creds-box > div { display: flex; justify-content: space-between; gap: 10px; padding: 4px 0; }
.creds-k { color: #6b7280; font-size: 0.82rem; }
.creds-v { font-weight: 600; font-size: 0.86rem; text-align: right; word-break: break-all; }
.mono { font-family: monospace; }
.result-line { font-size: 0.85rem; padding: 8px 10px; border-radius: 6px; }
.is-ok { background: #ecfdf5; color: #065f46; border: 1px solid #a7f3d0; }
.is-err { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }
.spin { animation: wzspin 1s linear infinite; display: inline-block; }
@keyframes wzspin { to { transform: rotate(360deg); } }
</style>
