<template>
  <DefaultLayout>
    <!-- banner -->
    <section class="banner-section">
      <img src="@assets/contact-banner.png" alt="联系我们" />
    </section>

    <!-- 带背景的内容区域 -->
    <div class="content-with-bg">
      <!-- 联系信息模块 -->
      <section class="contact-info-section">
        <div class="container">
          <div class="contact-info-card">
            <!-- 上半部分：联系信息 -->
            <div class="contact-details">
              <!-- 左侧信息 -->
              <div class="contact-left">
                <div class="contact-item">
                  <img src="@assets/contact-addr.png" alt="地址" class="contact-icon" />
                  <div class="contact-text">
                    <h4>公司地址：</h4>
                    <p>{{ companyInfo.address }}</p>
                  </div>
                </div>

                <div class="contact-item">
                  <img src="@assets/contact-phone.png" alt="电话" class="contact-icon" />
                  <div class="contact-text">
                    <h4>联系电话：</h4>
                    <p v-for="item in companyInfo.contactsList" :key="item.id">
                      {{ item.contactName }}：{{ item.contactMobile }}</p>
                  </div>
                </div>

                <div class="contact-item">
                  <img src="@assets/contact-mail.png" alt="邮箱" class="contact-icon" />
                  <div class="contact-text">
                    <h4>公司电话：</h4>
                    <p>{{ companyInfo.mobile }}</p>
                  </div>
                </div>
              </div>

              <!-- 右侧信息 -->
              <div class="contact-right">
                <div class="qr-codes">
                  <div class="qr-item" v-for="contact in companyInfo.contactsList" :key="contact.id">
                    <img :src="imgHost + contact.contactWxQrcode" :alt="getContactTypeLabel(contact.contactType)" />
                    <p>{{ getContactTypeLabel(contact.contactType) }} <br /> <b>{{ contact.contactName }}</b></p>
                  </div>
                </div>

                <div class="website-info">
                  <img src="@assets/contact-mail.png" alt="网站" class="contact-icon" />
                  <div class="contact-text">
                    <h4>邮箱</h4>
                    <p>{{ companyInfo.email }}</p>
                  </div>
                  <div class="contact-text" v-if="businessCooperationEmail">
                    <h4>商务合作</h4>
                    <p>{{ businessCooperationEmail }}</p>
                  </div>
                </div>
              </div>
            </div>

            <!-- 下半部分：地图 -->
            <div class="map-section">
              <img :src="imgHost + companyInfo.addressImg" alt="公司位置地图" class="map-image" @click="openMap" />
              <!-- <iframe style="width: 100%;;height: 350px;border-radius: 10px;" src="https://j.map.baidu.com/ef/0c9k" frameborder="0"></iframe> -->
            </div>
          </div>
        </div>
      </section>

      <!-- 留言模块 -->
      <section class="message-section">
        <div class="container">
          <div class="message-card">
            <h2 class="section-title">在线留言</h2>

            <form @submit.prevent="submitMessage" class="message-form">
              <div class="form-row">
                <div class="form-group">
                  <label>联系人</label>
                  <input type="text" v-model="messageForm.name" placeholder="请输入联系人" required />
                </div>
                <div class="form-group">
                  <label>联系电话</label>
                  <input type="tel" v-model="messageForm.phone" placeholder="请输入联系电话" required />
                </div>
              </div>

              <div class="form-row">
                <div class="form-group">
                  <label>联系邮箱</label>
                  <input type="email" v-model="messageForm.email" placeholder="请输入联系邮箱" />
                </div>
                <div class="form-group">
                  <label>联系地址</label>
                  <input type="text" v-model="messageForm.address" placeholder="请输入联系地址" />
                </div>
              </div>

              <div class="form-group full-width">
                <label>留言内容</label>
                <textarea v-model="messageForm.content" placeholder="请输入留言内容" rows="6" required></textarea>
              </div>

              <div class="form-actions">
                <button type="submit" class="submit-btn" :disabled="isSubmitting">
                  {{ isSubmitting ? '提交中...' : '提交留言' }}
                </button>
              </div>
            </form>
          </div>
        </div>
      </section>

      <!-- 底部间距，确保背景延伸到footer下方 -->
      <div class="bottom-spacer"></div>
    </div>
  </DefaultLayout>
</template>

<script>
import { ref, reactive, onMounted, computed } from 'vue'
import DefaultLayout from '@/layouts/DefaultLayout.vue'
import { getContacts, addDzNotes } from '@/api/modules/home'

export default {
  name: 'ContactPage',
  components: {
    DefaultLayout
  },
  setup() {
    // 环境变量
    const imgHost = import.meta.env.VITE_APP_IMG_HOST
    // 留言表单数据
    const messageForm = reactive({
      name: '',
      phone: '',
      email: '',
      address: '',
      content: ''
    })

    const companyInfo = ref({});

    const handleGetCompanyInfo = async () => {
      try {
        const result = await getContacts()
        const data = result.data;
        companyInfo.value = data;
        // console.log('公司信息:', companyInfo)

      } catch (error) {
        console.error('加载首页数据失败:', error)
      }
    }

    const isSubmitting = ref(false)

    // 计算商务合作邮箱
    const businessCooperationEmail = computed(() => {
      if (!companyInfo.value.contactsList) return null
      const businessContact = companyInfo.value.contactsList.find(contact => contact.contactType === 0)
      return businessContact ? businessContact.contactEmail : null
    })

    // 获取联系类型标签
    const getContactTypeLabel = (contactType) => {
      const typeMap = {
        0: '商务合作',
        1: '产品咨询',
        2: '研发咨询'
      }
      return typeMap[contactType] || '联系我们'
    }

    // 提交留言
    const submitMessage = async () => {
      isSubmitting.value = true

      try {
        // 构建API请求参数
        const requestData = {
          noteName: messageForm.name,
          noteMobile: messageForm.phone,
          noteEmail: messageForm.email,
          noteAddress: messageForm.address,
          noteContent: messageForm.content
        }

        // console.log('提交留言:', requestData)

        // 调用API提交留言
        const result = await addDzNotes(requestData)

        if (result.code === 200 || result.success) {
          alert('留言提交成功！我们会尽快与您联系。')

          // 重置表单
          Object.keys(messageForm).forEach(key => {
            messageForm[key] = ''
          })
        } else {
          throw new Error(result.message || '提交失败')
        }
      } catch (error) {
        console.error('提交留言失败:', error)
        alert('提交失败，请稍后重试。')
      } finally {
        isSubmitting.value = false
      }
    }

    const openMap = () =>{
      window.open('https://j.map.baidu.com/ef/0c9k','_blank')
    }

    onMounted(async () => {
      handleGetCompanyInfo()
    })

    return {
      imgHost,
      messageForm,
      isSubmitting,
      submitMessage,
      handleGetCompanyInfo,
      companyInfo,
      getContactTypeLabel,
      businessCooperationEmail,
      openMap
    }
  }
}
</script>

<style lang="scss" scoped>
// banner区域
.banner-section {
  position: relative;

  img {
    width: 100%;
    display: block;
  }
}

// 带背景的内容区域
.content-with-bg {
  background: url('@assets/contact-bg.png') no-repeat center 185px;
  background-size: cover;
  position: relative;
  // 确保背景延伸到footer下方
  margin-bottom: -137px; // 负边距让背景延伸到footer下方
  padding-bottom: 137px; // 对应的内边距保持内容位置
}

// 联系信息模块
.contact-info-section {
  position: relative;
  margin-top: -185px; // 向上覆盖banner
  z-index: 10;
  padding: 0 20px 60px;

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

// 底部间距
.bottom-spacer {
  height: 100px; // 额外的间距确保背景覆盖footer下方
}

.contact-info-card {
  background: linear-gradient(0deg, #fff 0%, #fff 50%, rgba(255, 255, 255, .0) 100%);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.contact-details {
  display: flex;
  gap: 60px;
  margin-bottom: 40px;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 30px;
  }
}

.contact-left {
  flex: 1;

  .contact-item {
    display: flex;
    align-items: flex-start;
    gap: 15px;
    margin-bottom: 30px;

    &:last-child {
      margin-bottom: 0;
    }
  }

  .contact-icon {
    width: 24px;
    height: 24px;
    margin-top: 2px;
    flex-shrink: 0;
  }

  .contact-text {
    h4 {
      color: #333;
      font-size: 16px;
      font-weight: 600;
      margin: 0 0 8px 0;
    }

    p {
      color: #666;
      font-size: 14px;
      line-height: 1.5;
      margin: 0 0 4px 0;

      &:last-child {
        margin-bottom: 0;
      }
    }
  }
}

.contact-right {
  flex: 1;

  .qr-codes {
    display: flex;
    gap: 60px;
    margin-bottom: 30px;
    justify-content: flex-start;

    .qr-item {
      text-align: left;

      img {
        width: 174px;
        height: 174px;
        border-radius: 8px;
        margin-bottom: 8px;
      }

      p {
        color: #666;
        font-size: 16px;
        margin: 0;

        b {
          margin-top: 12px;
          display: block;
        }
      }
    }
  }

  .website-info {
    display: flex;
    align-items: flex-start;
    gap: 15px;

    .contact-icon {
      width: 24px;
      height: 24px;
      margin-top: 2px;
      flex-shrink: 0;
    }

    .contact-text {
      flex: 1;

      h4 {
        color: #333;
        font-size: 16px;
        font-weight: 600;
        margin: 0 0 8px 0;
      }

      p {
        color: #666;
        font-size: 14px;
        line-height: 1.5;
        margin: 0 0 4px 0;

        &:last-child {
          margin-bottom: 0;
        }
      }

      &+.contact-text {
        margin-left: 30px;
      }
    }
  }
}

.map-section {
  border-top: 1px solid #eee;
  padding-top: 30px;

  .map-image {
    width: 100%;
    height: 300px;
    object-fit: cover;
    border-radius: 8px;
    cursor: pointer;
  }
}

// 留言模块
.message-section {
  padding: 0 20px 60px;

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

.message-card {
  background: white;
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
}

.section-title {
  text-align: center;
  font-size: 28px;
  font-weight: 600;
  color: #333;
  margin: 0 0 40px 0;
  position: relative;

  &::after {
    content: '';
    position: absolute;
    bottom: -10px;
    left: 50%;
    transform: translateX(-50%);
    width: 60px;
    height: 3px;
    background: linear-gradient(90deg, #4A90E2, #7B68EE);
    border-radius: 2px;
  }
}

.message-form {
  .form-row {
    display: flex;
    gap: 20px;
    margin-bottom: 20px;

    @media (max-width: 768px) {
      flex-direction: column;
      gap: 0;
    }
  }

  .form-group {
    flex: 1;
    margin-bottom: 20px;

    &.full-width {
      width: 100%;
    }

    label {
      display: block;
      color: #333;
      font-size: 14px;
      font-weight: 500;
      margin-bottom: 8px;
    }

    input,
    textarea {
      width: 100%;
      padding: 12px 16px;
      border: 1px solid #ddd;
      border-radius: 6px;
      font-size: 14px;
      transition: all 0.3s ease;
      box-sizing: border-box;

      &:focus {
        outline: none;
        border-color: #4A90E2;
        box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);
      }

      &::placeholder {
        color: #999;
      }
    }

    textarea {
      resize: vertical;
      min-height: 120px;
      font-family: inherit;
    }
  }

  .form-actions {
    text-align: center;
    margin-top: 30px;
  }

  .submit-btn {
    background: linear-gradient(90deg, #4A90E2, #7B68EE);
    color: white;
    border: none;
    padding: 14px 40px;
    border-radius: 25px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
    min-width: 140px;

    &:hover:not(:disabled) {
      transform: translateY(-2px);
      box-shadow: 0 8px 20px rgba(74, 144, 226, 0.3);
    }

    &:disabled {
      opacity: 0.6;
      cursor: not-allowed;
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .content-with-bg {
    // 移动端可能需要调整背景图位置
    background-position: center top;
    margin-bottom: -30px;
    padding-bottom: 30px;
  }

  .contact-info-section {
    margin-top: -80px;
    padding: 0 15px 40px;
  }

  .contact-info-card {
    padding: 30px 20px;
  }

  .contact-right .qr-codes {
    gap: 20px;

    .qr-item img {
      width: 60px;
      height: 60px;
    }
  }

  .message-section {
    padding: 40px 15px;
  }

  .message-card {
    padding: 30px 20px;
  }

  .section-title {
    font-size: 24px;
  }

  .bottom-spacer {
    height: 50px; // 移动端减少间距
  }
}
</style>