<script setup lang="ts">
import { computed, ref } from 'vue'
import { type CampaignLabel } from '~/stores/pages'

interface Props {
  campaigns: CampaignLabel[]
}

interface Emits {
  (e: 'close'): void
  (e: 'submit', payload: { name: string; campaignId: string; domain: string; routePath: string }): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const form = ref({
  name: '메인 랜딩 페이지',
  campaignId: '',
  domain: 'ad-camp-temp.test',
  routePath: '/'
})

const campaignChoices = computed(() =>
  props.campaigns.map((campaign) => ({
    value: campaign.id,
    label: campaign.name
  }))
)

if (campaignChoices.value.length > 0) {
  form.value.campaignId = campaignChoices.value[0]?.value || ''
}

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

const onSubmit = () => {
  const payload = {
    name: form.value.name.trim(),
    campaignId: form.value.campaignId,
    domain: form.value.domain.trim(),
    routePath: form.value.routePath.trim()
  }

  if (!payload.name || !payload.campaignId || !payload.domain || !payload.routePath) {
    return
  }

  emit('submit', payload)
  form.value = {
    name: '메인 랜딩 페이지',
    campaignId: props.campaigns[0]?.id || '',
    domain: 'ad-camp-temp.test',
    routePath: '/'
  }
}

const onBackdrop = (evt: MouseEvent) => {
  if (evt.target === evt.currentTarget) {
    onClose()
  }
}
</script>

<template>
  <div
    class="fixed inset-0 z-50 flex items-center justify-center bg-slate-950/70 p-4"
    role="dialog"
    aria-modal="true"
    @click="onBackdrop"
  >
    <div class="w-full max-w-lg rounded-2xl border border-indigo-400/20 bg-slate-950 p-6 shadow-2xl shadow-black/60">
      <div class="flex items-center justify-between">
        <h2 class="text-2xl font-bold text-white">새 페이지 만들기</h2>
        <button
          type="button"
          @click="onClose"
          class="rounded-md border border-slate-700/70 px-2 py-1 text-xs font-semibold text-slate-300"
        >
          닫기
        </button>
      </div>

      <p class="mt-2 text-sm text-slate-400">페이지명/도메인/경로를 입력하고 생성하면 즉시 페이지가 등록됩니다.</p>

      <div class="mt-5 space-y-4">
        <label class="block space-y-2">
          <span class="text-sm text-slate-300">페이지명</span>
          <input
            v-model="form.name"
            type="text"
            placeholder="예: 메인 랜딩 페이지"
            class="w-full rounded-xl border border-indigo-500/25 bg-slate-900 px-4 py-3 text-slate-100 outline-none focus:border-cyan-300"
          />
        </label>

        <label class="block space-y-2">
          <span class="text-sm text-slate-300">프로젝트</span>
          <select
            v-model="form.campaignId"
            class="w-full rounded-xl border border-indigo-500/25 bg-slate-900 px-4 py-3 text-slate-100 outline-none focus:border-cyan-300"
          >
            <option value="">프로젝트 선택</option>
            <option v-for="campaign in campaignChoices" :key="campaign.value" :value="campaign.value">
              {{ campaign.label }}
            </option>
          </select>
        </label>

        <label class="block space-y-2">
          <span class="text-sm text-slate-300">도메인</span>
          <input
            v-model="form.domain"
            type="text"
            placeholder="예: summer.example.com"
            class="w-full rounded-xl border border-indigo-500/25 bg-slate-900 px-4 py-3 text-slate-100 outline-none focus:border-cyan-300"
          />
        </label>

        <label class="block space-y-2">
          <span class="text-sm text-slate-300">페이지 경로</span>
          <input
            v-model="form.routePath"
            type="text"
            placeholder="예: /, /offer, /google"
            class="w-full rounded-xl border border-indigo-500/25 bg-slate-900 px-4 py-3 text-slate-100 outline-none focus:border-cyan-300"
          />
        </label>
      </div>

      <div class="mt-6 flex justify-end gap-2">
        <button
          type="button"
          @click="onClose"
          class="rounded-md border border-slate-700 px-4 py-2 text-sm text-slate-300"
        >
          취소
        </button>
        <button
          type="button"
          @click="onSubmit"
          class="rounded-md bg-gradient-to-r from-indigo-500 to-cyan-500 px-4 py-2 text-sm font-semibold text-white"
        >
          생성
        </button>
      </div>
    </div>
  </div>
</template>
