<template>
  <section id="contact" class="contact-section">
    <div class="container">
      <h2 class="section-title">{{ t('contact.title') }}</h2>
      <div class="contact-container">
        <div class="contact-form">
          <form @submit.prevent="submitForm">
            <div class="form-group">
              <label for="name">{{ t('contact.name') }}</label>
              <input
                type="text"
                id="name"
                v-model="formData.name"
                required
                :placeholder="t('contact.name')"
              />
            </div>
            <div class="form-group">
              <label for="email">{{ t('contact.email') }}</label>
              <input
                type="email"
                id="email"
                v-model="formData.email"
                required
                :placeholder="t('contact.email')"
              />
            </div>
            <div class="form-group">
              <label for="subject">{{ t('contact.subject') }}</label>
              <input
                type="text"
                id="subject"
                v-model="formData.subject"
                required
                :placeholder="t('contact.subject')"
              />
            </div>
            <div class="form-group">
              <label for="message">{{ t('contact.message') }}</label>
              <textarea
                id="message"
                v-model="formData.message"
                required
                rows="5"
                :placeholder="t('contact.message')"
              ></textarea>
            </div>
            <button type="submit" class="submit-btn">{{ t('contact.send') }}</button>
          </form>
          <div v-if="showSuccessMessage" class="success-message">{{ t('contact.success') }}</div>
          <div v-if="showErrorMessage" class="error-message">{{ t('contact.error') }}</div>
        </div>
        <div class="contact-info">
          <div class="info-item">
            <div class="info-icon">
              <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"></path>
                <polyline points="22,6 12,13 2,6"></polyline>
              </svg>
            </div>
            <div class="info-text">
              <h3>Email</h3>
              <p>2187434671@qq.com</p>
            </div>
          </div>
          <div class="info-item">
            <div class="info-icon">
              <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"></path>
              </svg>
            </div>
            <div class="info-text">
              <h3>Phone</h3>
              <p>(86)15668601400</p>
            </div>
          </div>
          <div class="info-item">
            <div class="info-icon">
              <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <rect x="3" y="9" width="18" height="13" rx="2" ry="2"></rect>
                <path d="M7 10v3"></path>
                <path d="M17 10v3"></path>
                <path d="M9 22V12h6v10"></path>
              </svg>
            </div>
            <div class="info-text">
              <h3>Address</h3>
              <p>No. 28, Dayou Tianyuan, Hi-tech Industrial Park, Dalian, Liaoning Province, China</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </section>
</template>

<script setup lang="ts">
import { ref, inject } from 'vue'
import { EmailService } from '../../services/emailService'


export interface FormData {
  name: string
  email: string
  subject: string
  message: string
}

const formData = ref<FormData>({
  name: '',
  email: '',
  subject: '',
  message: '',
})

const showSuccessMessage = ref(false)
const showErrorMessage = ref(false)
const isSubmitting = ref(false)

// 注入翻译函数
const t = inject<(key: string) => string>('t') || ((key) => key)

const submitForm = async () => {
  isSubmitting.value = true
  showSuccessMessage.value = false
  showErrorMessage.value = false

  try {
    const response = await EmailService.sendEmail(formData.value)

    if (response.success) {
      showSuccessMessage.value = true
      // 重置表单
      formData.value = {
        name: '',
        email: '',
        subject: '',
        message: '',
      }
      // 5秒后隐藏成功消息
      setTimeout(() => {
        showSuccessMessage.value = false
      }, 5000)
    } else {
      showErrorMessage.value = true
    }
  } catch (error) {
    console.error('Error submitting form:', error)
    showErrorMessage.value = true
  } finally {
    isSubmitting.value = false
  }
}
</script>

<style scoped>
.contact-section {
  padding: 5rem 0;
  background-color: white;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1.5rem;
}

.section-title {
  text-align: center;
  font-size: 2.5rem;
  margin-bottom: 3rem;
  color: #1a1a2e;
}

.contact-container {
  display: flex;
  gap: 3rem;
  flex-wrap: wrap;
}

.contact-form {
  flex: 1;
  min-width: 300px;
  background-color: #f8f9fa;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: #1a1a2e;
  font-weight: bold;
  text-align: left; /*label文字居左 */
  display: block; /* 确保标签独占一行，更好地控制对齐 */
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 0.8rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

.submit-btn {
  background-color: #4cc9f0;
  color: white;
  border: none;
  padding: 0.8rem 1.5rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  transition: background-color 0.3s;
}

.submit-btn:hover {
  background-color: #4361ee;
}

.success-message {
  margin-top: 1rem;
  padding: 1rem;
  background-color: #d4edda;
  color: #155724;
  border-radius: 4px;
}

.error-message {
  margin-top: 1rem;
  padding: 1rem;
  background-color: #f8d7da;
  color: #721c24;
  border-radius: 4px;
}

.contact-info {
  flex: 1;
  min-width: 300px;
}

.info-item {
  display: flex;
  margin-bottom: 2rem;
  align-items: flex-start;
}
/*email,phone,adress左对齐 */
.info-item .info-text {
  text-align: left;
}
/* email,phone,adress的图标和内容整体向下移动  */
.contact-info {
  margin-top: 30px; 
}
.info-icon {
  color: #4cc9f0;
  margin-right: 1rem;
  padding-top: 0.3rem;
}

.info-text h3 {
  color: #1a1a2e;
  margin-bottom: 0.5rem;
}

.info-text p {
  color: #666;
}


.form-group input,
.form-group textarea {
  width: calc(100% - 1.6rem);

}

@media (max-width: 768px) {
  .section-title {
    font-size: 2rem;
  }

  .contact-container {
    flex-direction: column;
  }



}
</style>