<template>
  <view class="container">
    <view class="card">
      <view class="confirm-title">预约信息确认</view>
      <view class="card-row"><text class="card-label">检查项目</text><text class="card-value">{{ appName }}</text></view>
      <view class="card-row"><text class="card-label">预约日期</text><text class="card-value highlight">{{ date }}</text></view>
    </view>

    <view class="card" v-if="tips">
      <view class="card-title">检查须知</view>
      <view class="tips-text">{{ tips }}</view>
    </view>

    <view class="confirm-area">
      <view class="btn-primary" @tap="handleConfirm" :class="{ disabled: submitting }">
        {{ submitting ? '提交中...' : '确认预约' }}
      </view>
    </view>
  </view>
</template>

<script>
import { getApplicationDetail } from '../../services/application'
import { createAppointment } from '../../services/appointment'
import { formatTime } from '../../utils/format'

export default {
  data() {
    return {
      applicationIds: '',
      planId: '',
      date: '',
      appName: '',
      tips: '',
      submitting: false
    }
  },
  onLoad(options) {
    this.applicationIds = options.application_ids || ''
    this.planId = options.plan_id || ''
    this.date = options.date || ''
    this.loadInfo()
  },
  methods: {
    async loadInfo() {
      try {
        const ids = this.applicationIds.split(',')
        const res = await getApplicationDetail({ application_id: ids[0] })
        this.appName = res.data.examination_item_name || ''
        if (res.data.exam_item_detail) {
          this.tips = res.data.exam_item_detail.check_tips || ''
        }
      } catch (e) {}
    },
    async handleConfirm() {
      if (this.submitting) return
      this.submitting = true
      try {
        const ids = this.applicationIds.split(',').map(Number)
        await createAppointment({
          application_ids: ids,
          channel_id: 3,
          plan_id: Number(this.planId)
        })
        uni.redirectTo({ url: `/pages/appointment/result?success=1&date=${this.date}&name=${encodeURIComponent(this.appName)}` })
      } catch (e) {
        uni.redirectTo({ url: `/pages/appointment/result?success=0&msg=${encodeURIComponent(e.message || '预约失败')}` })
      }
      this.submitting = false
    }
  }
}
</script>

<style scoped>
.confirm-title { font-size: 36rpx; font-weight: 700; color: #333; margin-bottom: 24rpx; }
.tips-text { font-size: 26rpx; color: #666; line-height: 1.8; }
.highlight { color: #2B7FD2; font-weight: 600; }
.confirm-area { padding: 40rpx 0; }
.confirm-area .btn-primary { margin-top: 20rpx; }
.confirm-area .btn-primary.disabled { opacity: 0.6; }
</style>
