<template>
  <scroll-view scroll-y class="page-scroll">
    <view class="page-content">
      <text class="page-title">新建预约</text>

      <view class="form-group">
        <text class="form-label">选择医生</text>
        <picker :range="doctorNames" @change="onDoctorChange">
          <view class="form-picker">
            {{ selectedDoctorName || '请选择医生' }}
            <text class="picker-arrow">▼</text>
          </view>
        </picker>
      </view>

      <view class="form-group">
        <text class="form-label">预约日期</text>
        <picker mode="date" :value="date" @change="(e: any) => date = e.detail.value">
          <view class="form-picker">
            {{ date || '请选择日期' }}
            <text class="picker-arrow">▼</text>
          </view>
        </picker>
      </view>

      <view class="form-group">
        <text class="form-label">预约时间</text>
        <picker mode="time" :value="time" @change="(e: any) => time = e.detail.value">
          <view class="form-picker">
            {{ time || '请选择时间' }}
            <text class="picker-arrow">▼</text>
          </view>
        </picker>
      </view>

      <view class="form-group">
        <text class="form-label">备注</text>
        <textarea v-model="notes" class="form-textarea" placeholder="请输入备注信息" />
      </view>

      <view class="submit-btn" @tap="handleSubmit">
        {{ submitting ? '提交中...' : '提交预约' }}
      </view>
    </view>
  </scroll-view>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { api } from '@/services/request'

const doctors = ref<any[]>([])
const selectedDoctorIdx = ref(-1)
const date = ref('')
const time = ref('')
const notes = ref('')
const submitting = ref(false)

const doctorNames = computed(() => doctors.value.map(d => d.name || d.display_name || '医生'))
const selectedDoctorName = computed(() => selectedDoctorIdx.value >= 0 ? doctorNames.value[selectedDoctorIdx.value] : '')

function onDoctorChange(e: any) {
  selectedDoctorIdx.value = e.detail.value
}

async function fetchDoctors() {
  try { doctors.value = await api.get<any[]>('/health/doctors') || [] }
  catch { doctors.value = [] }
}

async function handleSubmit() {
  if (selectedDoctorIdx.value < 0) {
    uni.showToast({ title: '请选择医生', icon: 'none' })
    return
  }
  if (!date.value || !time.value) {
    uni.showToast({ title: '请选择日期和时间', icon: 'none' })
    return
  }
  submitting.value = true
  try {
    await api.post('/health/appointments', {
      doctor_id: doctors.value[selectedDoctorIdx.value].id,
      appointment_time: `${date.value} ${time.value}`,
      notes: notes.value,
    })
    uni.showToast({ title: '预约成功', icon: 'success' })
    setTimeout(() => uni.navigateBack(), 1500)
  } catch (err: any) {
    uni.showToast({ title: err.message || '预约失败', icon: 'none' })
  }
  submitting.value = false
}

onMounted(fetchDoctors)
</script>

<style lang="scss" scoped>
.page-scroll { height: 100vh; background: $bg; }
.page-content { padding: 28px 24px 120px; }
.page-title { @include section-title; }

.form-group {
  margin-bottom: 28px;
}

.form-label {
  display: block;
  font-size: var(--tk-font-body);
  color: $tx2;
  margin-bottom: 12px;
}

.form-picker {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: $card;
  border-radius: $r-sm;
  padding: 18px 20px;
  font-size: var(--tk-font-body);
  color: $tx;
  box-shadow: $shadow-sm;
}

.picker-arrow {
  font-size: var(--tk-font-cap);
  color: $tx3;
}

.form-textarea {
  width: 100%;
  min-height: 160px;
  background: $card;
  border-radius: $r-sm;
  padding: 18px 20px;
  font-size: var(--tk-font-body);
  color: $tx;
  box-sizing: border-box;
}

.submit-btn {
  @include btn-primary;
  margin-top: 16px;
}
</style>
