<template>
  <div>
    <div class="banner">
      <!-- 介绍 -->
      <div class="introduce">
        <div class="title">
          <span>{{ productList.title }}</span>
        </div>
        <!-- 详情 -->
        <div class="details">
          <span>{{ productList.description }}</span>
        </div>
        <div class="btn" @click="openConsultDialog(productList, 'banner')">
          立即咨询
        </div>
      </div>
    </div>
    <!-- 规格 -->
    <div class="specification">
      <div class="title">
        产品<span class="color">规格</span>
      </div>
      <!-- 产品列表 -->
      <div class="product-list">
        <!-- 列表项 -->
        <div class="list-box" v-for="(item, index) in productList.products" :key="index">
          <div class="box-title">
            {{ item.title }}
          </div>
          <!-- 详情 -->
          <div class="box-details">
            <div class="details-item" v-for="(items, index) in item.list" :key="index">
              <span>
                <span class="item-title">
                  <span>{{ items.name }}</span>
                </span>
                <span class="item-details">
                  <span>{{ items.content }}</span>
                </span>
              </span>
            </div>
          </div>
          <!-- 价格 -->
          <div class="price">
            ￥<span class="price-dig">{{ item.price }}</span><span class="price-color">{{ item.price_unit }}(可短租)</span>
          </div>
          <!-- 咨询 -->
          <div class="box-btn" @click="openConsultDialog(item, 'spec')">
            立即咨询
          </div>
        </div>
      </div>
    </div>

    <!-- 产品咨询弹窗 -->
    <ProductConsultDialog
      :visible.sync="showConsultDialog"
      :platform-name="platformName"
      :qr-code="qrCode"
      :default-form-data="consultFormData"
      :submit-api="submitConsultApi"
      @success="handleConsultSuccess"
      @close="handleDialogClose"
    />

    <Footer></Footer>
  </div>
</template>

<script>
import { reqProductDetail, reqProductConsult } from '@/api/H5/index'
import Footer from '../components/footer/footer.vue';
import ProductConsultDialog from '../components/H5_dialog/index.vue'

export default {
  components: {
    Footer,
    ProductConsultDialog
  },
  data() {
    return {
      id: null,
      productList: [],

      // 咨询弹窗相关
      showConsultDialog: false,
      platformName: '产品详情页',
      qrCode: '',
      consultFormData: {
        content: '',
        custom_type: '1',
        name: '',
        phone: '',
        company: '',
        email: '',
        checked: false
      },
      currentProduct: null,
      consultSource: '' // 记录咨询来源：banner 或 spec
    }
  },

  created() {
    // 获取id
    this.id = this.$route.query.id
    console.log('ID:', this.id)
    this.getProductList()
  },

  methods: {
    // 获取产品列表
    async getProductList() {
      const res = await reqProductDetail({ id: this.id })
      console.log('产品详情是', res)
      if (res.status === true) {
        this.productList = res.data
      }
    },

    // 打开咨询弹窗
    openConsultDialog(product, source) {
      this.currentProduct = product
      this.consultSource = source

      // 根据来源设置不同的咨询内容
      let content = ''
      if (source === 'banner') {
        // 从banner咨询
        content = `我想咨询关于【${product.title || '产品'}】的详细信息`
      } else if (source === 'spec') {
        // 从规格列表咨询
        content = `我想咨询【${product.title || '产品规格'}】的价格和租赁详情，价格：${product.price}${product.price_unit || ''}`
      }

      // 设置咨询表单的预填内容
      this.consultFormData = {
        ...this.consultFormData,
        content: content
      }

      this.showConsultDialog = true
    },

    // 提交咨询API
    async submitConsultApi(data) {
      // 构建提交数据
      const submitData = {
        ...data,
        product_id: this.id,
        product_title: this.productList.title || '',
        product_name: this.currentProduct?.title || '',
        product_price: this.currentProduct?.price || '',
        product_unit: this.currentProduct?.price_unit || '',
        consult_source: this.consultSource || '',
        page_type: 'product_detail',
        page_url: window.location.href
      }

      return await reqProductConsult(submitData)
    },

    // 咨询成功回调
    handleConsultSuccess(response) {
      console.log('咨询提交成功:', response)
      this.$message.success('咨询提交成功，我们将尽快联系您！')
    },

    // 弹窗关闭回调
    handleDialogClose() {
      this.currentProduct = null
      this.consultSource = ''
      // 重置表单数据
      this.consultFormData = {
        content: '',
        custom_type: '1',
        name: '',
        phone: '',
        company: '',
        email: '',
        checked: false
      }
    }
  }
}
</script>
<style lang="less" scoped>
.color {
  background: linear-gradient(to right, #0066FF, #66CCFF);
  /* 蓝到浅蓝渐变 */
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  display: inline-block;
}

.banner {
  height: 4.2rem;
  background: url(../images/calculateBanner.jpg) no-repeat;
  background-size: 100% 100%;
  position: relative;

  .introduce {
    position: absolute;
    left: 50%;
    top: 14%;
    transform: translate(-90%);
  }

  .title {
    font-size: 0.32rem;
    /* 修改为0.32rem */
    color: #000;
    font-weight: 700;
  }

  .details {
    font-size: 0.18rem;
    /* 修改为0.18rem */
    color: #8890ab;
    line-height: 2 !important;
    margin-top: .1rem;
  }

  .btn {
    margin-top: .1rem;
    width: 2rem;
    height: .4rem;
    border-radius: .1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: .2rem;
    background: linear-gradient(90deg, #275AFF 0%, #2EBDFA 100%);
    color: #fff;
    cursor: pointer; /* 添加手型光标 */
  }
}

// 规格
.specification {
  height: 100%;
  background: linear-gradient(to bottom, #fff, #f2f6fb, #fff);

  .title {
    text-align: center;
    font-size: 0.32rem;
    /* 修改为0.32rem */
    font-weight: 600;
  }
}

// 产品列表
.product-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  padding: 0 1%;

  // 列表项
  .list-box {
    position: relative;
    margin: .2rem 2%;
    width: 46%;
    box-shadow: 0 .02rem .08rem rgba(39, 90, 255, 0.08);
    border-radius: .3rem;
    padding: .2rem .2rem;
    background-color: #fff;

    // 标题
    .box-title {
      font-size: 0.24rem;
      /* 修改为0.24rem */
      font-weight: 600;
      margin-bottom: .2rem;
    }

    // 详情
    .box-details {
      .details-item {
        display: flex;
        justify-content: space-between;
        line-height: 1;

        .item-title {
          font-size: .16rem;
          color: #000;
          margin-right: 0.07rem;
          font-weight: 500;
        }

        .item-details {
          font-size: .16rem;
          color: #8890ab;
        }
      }
    }

    // 价格
    .price {
      font-size: .16rem;
      color: #F62424;
      margin-top: .2rem;
      padding-top: .2rem;
      // 虚线
      border-top: .02rem dashed #e0e0e0;
      margin-bottom: .8rem;
      .price-dig {
        font-size: 0.28rem;
        /* 修改为0.28rem */
        font-weight: 600;
      }

      .price-color {
        color: #8890ab;
      }
    }

     .box-btn {
      width: calc(100% - 0.4rem); /* 减去左右 padding */
      margin-top: .2rem; /* 增加上边距 */
      padding: .1rem .2rem;
      text-align: center;
      border-radius: .1rem;
      font-size: .2rem;
      background: linear-gradient(90deg, #275AFF 0%, #2EBDFA 100%);
      color: #fff;
      position: absolute;
      left: 0.2rem;
      right: 0.2rem;
      bottom: 0.2rem;
      cursor: pointer;
    }
  }
}
</style>
