<template>
  <view class="add-schedule-page">
    <!-- 导航栏 -->
    <uni-nav-bar
      left-icon="left"
      title="增加门诊安排"
      @clickLeft="goBack"
      fixed
      color="#8B2316"
      height="180rpx"
      :border="false"
      backgroundColor="#eee"
    />

    <!-- 表单内容 -->
    <view class="form-container">
      <view class="form-item">
        <text class="form-label">选择日期</text>
        <picker mode="date" :value="formData.date" @change="onDateChange">
          <view class="picker-input">
            <text class="picker-text">{{ formData.date || '请选择日期' }}</text>
            <text class="picker-arrow">›</text>
          </view>
        </picker>
      </view>

      <view class="form-item">
        <text class="form-label">时间段</text>
        <picker mode="selector" :range="timePeriods" :value="timePeriodIndex" @change="onTimePeriodChange">
          <view class="picker-input">
            <text class="picker-text">{{ timePeriods[timePeriodIndex] || '请选择时间段' }}</text>
            <text class="picker-arrow">›</text>
          </view>
        </picker>
      </view>

      <view class="form-item">
        <text class="form-label">医院名称</text>
        <input class="form-input" placeholder="请输入医院名称" v-model="formData.hospital_name" />
      </view>

      <view class="form-item">
        <text class="form-label">诊室号</text>
        <input class="form-input" placeholder="请输入诊室号" v-model="formData.room_number" />
      </view>

      <view class="form-item">
        <text class="form-label">价格</text>
        <input class="form-input" placeholder="请输入价格" v-model="formData.price" type="number" />
      </view>

      <view class="form-item">
        <text class="form-label">门诊类型</text>
        <view class="checkbox-group">
          <view class="checkbox-item" @click="toggleSpecial">
            <view class="checkbox" :class="{ checked: formData.is_special }">
              <text class="checkbox-icon" v-if="formData.is_special">✓</text>
            </view>
            <text class="checkbox-text">特需门诊</text>
          </view>
        </view>
      </view>

      <view class="form-item">
        <text class="form-label">备注信息</text>
        <textarea class="form-textarea" placeholder="请输入备注信息" v-model="formData.remarks" :adjust-position="false"/>
      </view>
    </view>

    <!-- 底部按钮 -->
    <view class="bottom-actions">
      <view class="save-btn" @click="saveSchedule">
        <text class="btn-text">保存</text>
      </view>
      <view class="publish-btn" @click="publishSchedule">
        <text class="btn-text">发布</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { reactive, ref } from 'vue'
import api from '@/api/api'

// 响应式数据
const formData = reactive({
  date: '',
  time_period: '',
  hospital_name: '',
  room_number: '',
  price: '',
  is_special: false,
  remarks: ''
})

const timePeriods = ['上午', '下午', '晚上']
const timePeriodIndex = ref(0)

// 方法
const goBack = () => {
  uni.navigateBack()
}

const onDateChange = (e) => {
  formData.date = e.detail.value
}

const onTimePeriodChange = (e) => {
  timePeriodIndex.value = e.detail.value
  formData.time_period = timePeriods[e.detail.value]
}

const toggleSpecial = () => {
  formData.is_special = !formData.is_special
}

const saveSchedule = async () => {
  if (!formData.date || !formData.hospital_name) {
    uni.showToast({
      title: '请填写必要信息',
      icon: 'none'
    })
    return
  }

  try {
    const res = await api.addOutPatient(formData)
    if (res.code === 200) {
      uni.showToast({
        title: '保存成功',
        icon: 'none'
      })
      setTimeout(() => {
        uni.navigateBack()
      }, 1500)
    } else {
      uni.showToast({
        title: res.msg || '保存失败',
        icon: 'none'
      })
    }
  } catch (error) {
    uni.showToast({
      title: '保存失败',
      icon: 'none'
    })
  }
}

const publishSchedule = async () => {
  if (!formData.date || !formData.hospital_name) {
    uni.showToast({
      title: '请填写必要信息',
      icon: 'none'
    })
    return
  }

  uni.showModal({
    title: '确认发布',
    content: '确定要发布这条门诊安排吗？',
    success: async (res) => {
      if (res.confirm) {
        try {
          const publishRes = await api.publishOutPatient(formData)
          if (publishRes.code === 200) {
            uni.showToast({
              title: '发布成功',
              icon: 'none'
            })
            setTimeout(() => {
              uni.navigateBack()
            }, 1500)
          } else {
            uni.showToast({
              title: publishRes.msg || '发布失败',
              icon: 'none'
            })
          }
        } catch (error) {
          uni.showToast({
            title: '发布失败',
            icon: 'none'
          })
        }
      }
    }
  })
}
</script>

<style lang="scss" scoped>
.add-schedule-page {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 120rpx;
}

/* 表单容器 */
.form-container {
  padding: 30rpx;
}

.form-item {
  background-color: #fff;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
}

.form-label {
  display: block;
  font-size: 28rpx;
  color: #333;
  margin-bottom: 20rpx;
}

.form-input {
  width: 100%;
  font-size: 28rpx;
  color: #333;
  border: none;
  outline: none;
}

.picker-input {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx 0;
}

.picker-text {
  font-size: 28rpx;
  color: #333;
}

.picker-arrow {
  font-size: 24rpx;
  color: #999;
}

.checkbox-group {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.checkbox-item {
  display: flex;
  align-items: center;
  gap: 15rpx;
}

.checkbox {
  width: 40rpx;
  height: 40rpx;
  border: 2rpx solid #ddd;
  border-radius: 8rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff;
}

.checkbox.checked {
  background-color: #8B2316;
  border-color: #8B2316;
}

.checkbox-icon {
  color: #fff;
  font-size: 24rpx;
}

.checkbox-text {
  font-size: 28rpx;
  color: #333;
}

.form-textarea {
  width: 100%;
  min-height: 120rpx;
  font-size: 28rpx;
  color: #333;
  border: none;
  outline: none;
  line-height: 1.5;
}

/* 底部按钮 */
.bottom-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 30rpx;
  background-color: #fff;
  border-top: 1rpx solid #eee;
  display: flex;
  gap: 20rpx;
}

.save-btn {
  flex: 1;
  background-color: #6c757d;
  border-radius: 12rpx;
  padding: 25rpx;
  text-align: center;
}

.publish-btn {
  flex: 1;
  background-color: #8B2316;
  border-radius: 12rpx;
  padding: 25rpx;
  text-align: center;
}

.btn-text {
  color: #fff;
  font-size: 32rpx;
}
</style>
