<script setup>
import { ref } from "vue";
import { useI18n } from "vue-i18n";

import axios from "axios";
import { message } from "@/utils/message.js";

const { t } = useI18n();
const runtimeConfig = useRuntimeConfig();
const form = ref({
  firstName: "",
  lastName: "",
  email: "",
  company: "",
  phone: "",
});
const submitted = ref(false);

async function handleSubmit(e) {
  e.preventDefault();
  if (
    Object.values(form.value).some((value) => value === "" || value === null)
  ) {
    message.warning(t("email_alerts.validation.complete_info"));
    return;
  }
  if (Object.values(form.value).some((value) => value.length > 50)) {
    message.warning(t("email_alerts.validation.field_length"));
    return;
  }
  let url = `${runtimeConfig.public.apiBase}/fiee/emailalerts/submit`;
  // let url = 'https://erpapi-out.szjixun.cn/api/fiee/emailalerts/submit'
  const res = await axios.post(url, form.value);
  if (res.data.status === 0) {
    submitted.value = true;
  }
}
</script>

<template>
  <div class="alerts-container">
    <!-- Title Section -->
    <!-- 未提交 -->
    <div v-if="!submitted" class="title-section">
      <div class="title-decoration"></div>
      <div class="title">{{ t("email_alerts.title") }}</div>
      <div class="subtitle">{{ t("email_alerts.required_fields") }}</div>
    </div>
    <!-- 已提交 -->
    <div v-else class="title-section">
      <div class="title-decoration"></div>
      <div class="title">{{ t("email_alerts.submitted_successfully") }}</div>
      <div class="subtitle">{{ t("email_alerts.submitted_info") }}</div>
    </div>
    <!-- Form Card -->
    <div
      class="form-card relative"
      :style="{
        width: '100%',
        height: submitted ? '593px' : 'auto',
      }"
    >
      <template v-if="!submitted">
        <form class="form-content" @submit="handleSubmit">
          <div class="form-group mt-[36px]">
            <label for="firstName">{{
              t("email_alerts.form.first_name")
            }}</label>
            <input
              id="firstName"
              v-no-space
              v-model="form.firstName"
              type="text"
              required
            />
          </div>
          <div class="form-group">
            <label for="lastName">{{ t("email_alerts.form.last_name") }}</label>
            <input
              id="lastName"
              v-no-space
              v-model="form.lastName"
              type="text"
              required
            />
          </div>
          <div class="form-group">
            <label for="email">{{ t("email_alerts.form.email") }}</label>
            <input
              id="email"
              v-no-space
              v-model="form.email"
              type="email"
              required
            />
          </div>
          <div class="form-group">
            <label for="company">{{ t("email_alerts.form.company") }}</label>
            <input
              id="company"
              v-no-space
              v-model="form.company"
              type="text"
              required
            />
          </div>
          <div class="form-group">
            <label for="phone">{{ t("email_alerts.form.phone") }}</label>
            <input id="phone" v-no-space v-model="form.phone" type="tel" />
          </div>
          <button type="submit" class="submit-btn">
            {{ t("email_alerts.form.submit") }}
          </button>
        </form>
      </template>
      <template v-else>
        <div class="submitted-data">
          <div class="submitted-data-content">
            <div class="submitted-row">
              <span class="label">{{
                t("email_alerts.submitted_data.first_name")
              }}</span>
              <span class="value">{{
                form.firstName || t("email_alerts.submitted_data.not_filled")
              }}</span>
            </div>
            <div class="submitted-row">
              <span class="label">{{
                t("email_alerts.submitted_data.last_name")
              }}</span>
              <span class="value">{{
                form.lastName || t("email_alerts.submitted_data.not_filled")
              }}</span>
            </div>
            <div class="submitted-row">
              <span class="label">{{
                t("email_alerts.submitted_data.email")
              }}</span>
              <span class="value">{{
                form.email || t("email_alerts.submitted_data.not_filled")
              }}</span>
            </div>
            <div class="submitted-row">
              <span class="label">{{
                t("email_alerts.submitted_data.company")
              }}</span>
              <span class="value">{{
                form.company || t("email_alerts.submitted_data.not_filled")
              }}</span>
            </div>
            <div class="submitted-row">
              <span class="label">{{
                t("email_alerts.submitted_data.phone")
              }}</span>
              <span class="value">{{
                form.phone || t("email_alerts.submitted_data.not_filled")
              }}</span>
            </div>
          </div>
        </div>
        <div class="submitted-bg"></div>
      </template>
    </div>
  </div>
</template>

<style scoped lang="scss">
.alerts-container {
  width: 650 * 2.5px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.title-section {
  margin-top: 38 * 2.5px;
  display: flex;
  flex-direction: column;
  gap: 8 * 2.5px;
  padding: 0 16 * 2.5px;
  margin-bottom: 32 * 2.5px;
  align-self: center;
}

.title-decoration {
  width: 58 * 2.5px;
  height: 7 * 2.5px;
  background: #ff7bac;
  margin: auto 0;
  align-self: center;
}

.title {
  font-family: "PingFang SC", sans-serif;
  font-weight: 500;
  font-size: 32 * 2.5px;
  line-height: 1.4em;
  color: #000000;
  letter-spacing: 0.03em;
}

.subtitle {
  font-family: "PingFang SC", sans-serif;
  font-size: 14 * 2.5px;
  line-height: 1.375em;
  color: #455363;
  letter-spacing: 0.48 * 2.5px;
  align-self: center;
}

.form-card {
  width: 100%;
  background-color: white;
  border-radius: 16 * 2.5px;
  padding: 24 * 2.5px;
  box-shadow: 0px 3 * 2.5px 14 * 2.5px 0px rgba(0, 0, 0, 0.16);
  animation: fade-in 0.8s cubic-bezier(0.23, 1, 0.32, 1) both;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-content {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 16 * 2.5px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8 * 2.5px;

  label {
    font-family: "PingFang SC", sans-serif;
    font-weight: 500;
    font-size: 14 * 2.5px;
    line-height: 1.375em;
    color: #000000;
    letter-spacing: 0.48 * 2.5px;
  }

  input {
    height: 38 * 2.5px;
    border: 1 * 2.5px solid #e0e0e6;
    border-radius: 8 * 2.5px;
    padding: 0 12 * 2.5px;
    font-size: 14 * 2.5px;
    outline: none;
    transition: border-color 0.3s;

    &:focus {
      border-color: #ff7bac;
    }
  }
}
.submit-btn {
  height: 60 * 2.5px;
  background: #ff7bac;
  border-radius: 8 * 2.5px;
  border: none;
  font-family: "PingFang SC", sans-serif;
  font-weight: 500;
  font-size: 24 * 2.5px;
  line-height: 32 * 2.5px;
  color: white;
  letter-spacing: 1.2 * 2.5px;
  cursor: pointer;
  transition: opacity 0.3s;
  margin-top: 8 * 2.5px; // 16px (from figma form-group gap) + 8px = 24px
  margin-bottom: 16 * 2.5px;

  &:hover {
    opacity: 0.9;
  }
}

.success-title {
  font-size: 24px;
  font-weight: bold;
  color: #ff7bac;
  margin-bottom: 16px;
}

.success-info {
  margin-bottom: 24px;
  color: #455363;
}

.submitted-data {
  padding: 36 * 2.5px 24 * 2.5px 24 * 2.5px;
  border-radius: 16 * 2.5px;
  width: 678 * 2.5px;
  height: 428 * 2.5px;
  flex-shrink: 0;
}
.submitted-bg {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url("@/assets/image/768/email-alerts-submit.png");
  background-repeat: no-repeat;
  background-position: bottom;
  background-size: 100%;
}

.submitted-data-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16 * 2.5px;
  padding: 16 * 2.5px 0;
}

.submitted-row {
  display: flex;
  font-family: "PingFang SC", sans-serif;
  font-size: 14 * 2.5px;
  line-height: 1.375em;
  letter-spacing: 0.48 * 2.5px;
  width: 280 * 2.5px;
}

.label {
  font-weight: 500;
  color: #000000;
  width: 110 * 2.5px;
  flex-shrink: 0;
}

.value {
  font-weight: 400;
  color: #455363;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@keyframes fade-in {
  0% {
    opacity: 0;
    transform: translateY(20px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
