<template>
  <div class="home">
    <!-- 导航栏 -->
     <nav-bar />
    
    <!-- 蓝色背景区域 -->
    <div class="blue-bg"></div>
    
    <!-- 医院信息区域 -->
    <div class="hos-info">
      <div class="pay-to">付款给</div>
      <div class="hos-name">{{ payInfo.hospitalName || '中国人民武装警察部队宁夏回族自治区总队医院' }}</div>
      <div class="hos-icon">
        <img src="@/assets/医院图标@2x.png" alt="医院图标" />
      </div>
    </div>
    
    <!-- 费用明细卡 -->
    <div class="fee-card">
      <div class="fee-item total-fee">
   <span style="font-size: 0.52rem; font-family: 'PingFang SC', 'Microsoft YaHei', '黑体', sans-serif; font-weight: 500;">费用总额</span>
        <span class="fee-value">¥{{ payInfo.feeSumamt || '0.00' }}</span>
      </div>
      
      <div class="divider"></div>
      
      <div class="fee-item">
        <span>医保基金支付</span>
        <span class="fee-value">¥{{ payInfo.fundPay || '0.00' }}</span>
      </div>
      
      <div class="fee-item">
        <span>个人账户支付</span>
        <span class="fee-value">¥{{ payInfo.psnAcctPay || '0.00' }}</span>
      </div>
      <div class="fee-item">
        <span>其他抵扣金额</span>
        <span class="fee-value">¥{{ payInfo.othFeeAmt || '0.00' }}</span>
      </div>
      <div class="fee-item cash-pay">
        <span>现金支付</span>
        <span class="fee-value">¥{{ payInfo.ownPayAmt || '0.00' }}</span>
      </div>
      
      <div class="divider"></div>
      
      <!-- 医保支付信息 -->
      <div class="medical-payment-info">
        <div class="pay-icon-bottom">
          <img src="@/assets/医保混合支付@2x.png" class="medical-icon" alt="医保移动支付图标" />
          <span class="medical-text">医保移动支付</span>
        </div>
      </div>
    </div>
    
    <!-- 底部支付栏 -->
    <div class="bottom-pay-bar">
      <div class="amount-to-pay">
        <span>您还需支付：</span>
        <span class="amount-value">¥{{ payInfo.ownPayAmt || '0.00' }}</span>
      </div>
      <van-button type="primary" round @click="onConfirm" class="pay-btn" :loading="loading">去支付</van-button>
    </div>
  </div>
</template>

<script>
import { ybOrdering } from '@/request/api.js'
import { Icon, Button } from 'vant'

export default {
  components: {
    [Icon.name]: Icon,
    [Button.name]: Button
  },
  data() {
    return {
      payInfo: {},
      YbInfo:{},
      loading: false
    }
  },
  mounted() {
     this.YbInfo = JSON.parse(sessionStorage.getItem('YbInfo'));
    // 从路由参数获取支付信息
    const options = this.$route.query
    if (options.data) {
      const decodeData = JSON.parse(decodeURIComponent(options.data))
      this.payInfo = decodeData.data || decodeData
      console.log("payInfo",this.payInfo);
    }
  },
  methods: {
    onNavBack() {
      this.$router.back()
    },
    onConfirm() {
      if (this.loading) return; // 防止重复点击
      
      console.log('触发去支付操作')
      this.loading = true;
      
      let payType = "3"
      if (this.payInfo.ownPayAmt == 0) {
        payType = "2"
      }
      
      const request_content = {
        "payAuthNo": this.YbInfo.payAuthNo,
        "payOrdId": this.payInfo.payOrdId,
        "setlLatlnt": this.YbInfo.userLongitudeLatitude.latitude + "," +this.YbInfo.userLongitudeLatitude.longitude
      }
         // 保存支付信息到全局状态
      this.$store.commit('setYbPayInfo', {
        hospitalName: this.payInfo.hospitalName,
        feeSumamt: this.payInfo.feeSumamt,
        fundPay: this.payInfo.fundPay,
        psnAcctPay: this.payInfo.psnAcctPay,
        othFeeAmt: this.payInfo.othFeeAmt,
        ownPayAmt: this.payInfo.ownPayAmt,
      })
      this.$store.commit('setHospOutTradeNo', this.payInfo.hosp_out_trade_no)
      
      const req = {
        "total_fee": this.payInfo.feeSumamt,
        "cash_fee": this.payInfo.ownPayAmt,
        "ybzf": this.payInfo.fundPay + "",
        "zhzf": this.payInfo.psnAcctPay + "",
        "hosp_out_trade_no": this.payInfo.hosp_out_trade_no,
        "pay_type": payType,
        "user_card_no": this.YbInfo.userCardNo,
        "user_name": this.YbInfo.userName,
        "serial_no": this.YbInfo.jlid,
        "gmt_out_create": this.YbInfo.kdsj,
        "request_content": JSON.stringify(request_content),
        "ysje": this.YbInfo.ysje,
        "patid": this.YbInfo.jzrid,
        "payOrdId": this.payInfo.payOrdId,
        "payToken": this.payInfo.payAuthCode,
      }
      
      ybOrdering(req).then(res => {
        if (res.code === 200) {
         window.location.href = res.data.pay_url;
        }
        console.log(res)
      }).catch(err => {
        console.error('支付请求失败:', err)
      }).finally(() => {
        this.loading = false;
      })
    }
  }
}
</script>

<style scoped lang="scss">
.home {
  position: relative;
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-top: 0;
}

// 导航栏样式
.nav-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 1.1733rem; // 44px
  padding: 0 0.4rem; // 15px → 0.4rem
  background-color: #3B71E8;
  color: white;
  position: relative;
  z-index: 10;

  .back-btn,
  .more-btn {
    width: 1.1733rem;
    height: 1.1733rem;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .nav-title {
    font-size: 0.42rem; // 18px → 0.48rem? 但限制最大 0.42rem
    font-weight: 500;
  }
}

// 蓝色背景区域
.blue-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 7rem; // 200px
  background-color: #3B71E8;
  border-radius: 0 0 2.1333rem 2.1333rem; // 80px ≈ 40% of 200px
  z-index: 1;
}

// 医院信息区域
.hos-info {
  position: relative;
  padding: 0.5333rem 0.4rem 0; // 20px, 15px
  color: white;
  z-index: 2;

  .pay-to {
    font-size: 0.39rem; // 14px → ~0.37rem, but cap at 0.3 for subtitle
    opacity: 0.8;
    margin-bottom: 0.1333rem; // 5px
    color: #a6c1ff;
   font-weight: normal;
  }

  .hos-name {
    font-size: 0.42rem; // 18px → 0.48rem → clamp to 0.42rem
    font-weight: 500;
    width: 59%;
    color: #ffffff;
  }

    .hos-icon {
    position: absolute;
    top: 0.6667rem; // 25px
    right: 0.4rem; // 15px
    width: 1.5rem; // 40px
    height: 1.5rem;

    img {
      width: 60%;
      height: 60%;
      object-fit: contain;
    }
  }
}

// 费用明细卡片
.fee-card {
  position: relative;
  z-index: 2;
  background-color: white;
  border-radius: 0.32rem; // 12px
  margin: 0.4rem; // 15px
  padding: 0.5333rem; // 20px
  box-shadow: 0 0.0533rem 0.2667rem rgba(0, 0, 0, 0.05); // 2px, 10px

  .fee-item {
    display: flex;
    justify-content: space-between;
    padding: 0.32rem 0; // 12px
    font-size: 0.4rem; // 16px → 0.4267rem → clamp to 0.3733 (~14px) or 0.4?

    span:first-child {
      color: #666;
      font-weight: normal;
    }

    .fee-value {
      color: #333;
      font-weight: 500;
    }

  }

  .total-fee {
    font-weight: bold;

    span:first-child {
      color: #333;
    }

    .fee-value {
      font-size: 0.52rem; // 强调总额
      font-weight: bold;
    }
  }

  .cash-pay {
    span,
    .fee-value {
      color: #3B71E8 !important;
      font-weight: bold;
      font-size: 0.42rem;
    }
  }

  .divider {
    height: 0.0267rem; // 1px
    background-color: #f5f5f5;
    margin: 0.2667rem 0; // 10px
  }

  .medical-payment-info {
    padding: 0.4rem 0 0.1333rem; // 15px, 5px

    .pay-icon-bottom {
      display: flex;
      justify-content: center;
      align-items: center;

      .medical-icon {
        height: 0.4267rem; // 16px
        margin-right: 0.2133rem; // 8px
      }

      .medical-text {
        font-size: 0.3467rem; // 14px → 0.37rem, but use 0.3467 for subtlety
        color: #999;
      }
    }
  }
}

// 底部支付栏
.bottom-pay-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 1.6rem; // 60px
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 0.4rem; // 15px
  box-shadow: 0 -0.0533rem 0.2667rem rgba(0, 0, 0, 0.05);
  z-index: 10;

  .amount-to-pay {
    font-size: 0.3467rem; // 14px
    color: #666;

    .amount-value {
      font-size: 0.42rem; // 18px → 0.48 → clamp to 0.42
      color: #3B71E8;
      font-weight: bold;
      margin-left: 0.1333rem; // 5px
    }
  }

  .pay-btn {
    width: 3.2rem; // 120px
    height: 1.0667rem; // 40px
    background: linear-gradient(135deg, #3B71E8 0%, #2A57C4 100%);
    color: white;
    font-size: 0.4rem; // 16px → 0.4267 → use 0.4
    border-radius: 0.5333rem; // 20px
    border: none;
    box-shadow: none;
    margin-right: 0.65rem;
  }
}
</style>