<template>
  <div class="advice-form-container">
    <el-form :model="adviceData" label-width="80px" class="main-form">
      <el-row :gutter="16">
        <el-col :span="6">
          <el-form-item label="类型">
            <el-select v-model="adviceData.orderType" class="order-type-select" @change="onTypeChange">
              <el-option label="西药" value="WESTERN_MED" />
              <el-option label="中成药" value="CHINESE_PATENT" />
              <el-option label="诊疗" value="TREATMENT" />
              <el-option label="手术" value="SURGERY" />
              <el-option label="文字医嘱" value="TEXT" />
              <el-option label="出院带药" value="DISCHARGE_MED" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="长期/临时">
            <el-radio-group v-model="adviceData.frequencyType" :disabled="isDischargeMed || isTextAdvice">
              <el-radio label="长期">长期</el-radio>
              <el-radio label="临时">临时</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <!-- Bug #587: 新增开始时间字段 -->
        <el-col :span="8">
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker
              v-model="adviceData.startTime"
              type="datetime"
              placeholder="选择开始时间"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD HH:mm:ss"
              name="startTime"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- Bug #589: 联动专属面板 -->
    <DischargeMedPanel 
      v-if="adviceData.orderType === 'DISCHARGE_MED'" 
      :visible="true" 
      @confirm="onPanelConfirm" 
      @cancel="onPanelCancel" 
    />

    <!-- Bug #588: 联动文字医嘱专属面板 -->
    <TextAdvicePanel 
      v-if="adviceData.orderType === 'TEXT'" 
      :visible="true" 
      :current-dept="currentDept"
      @confirm="onTextPanelConfirm" 
      @cancel="onPanelCancel" 
    />
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'
import DischargeMedPanel from './DischargeMedPanel.vue'
import TextAdvicePanel from './TextAdvicePanel.vue'

const adviceData = reactive({
  orderType: '',
  frequencyType: '临时',
  startTime: '' // Bug #587
})

const isDischargeMed = ref(false)
const isTextAdvice = ref(false)
const currentDept = ref('呼吸内科病房') // 实际应从患者上下文动态获取

// Bug #587: 初始化默认开始时间为当前服务器时间
onMounted(() => {
  const now = new Date()
  const pad = n => n.toString().padStart(2, '0')
  adviceData.startTime = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`
})

const onTypeChange = (val) => {
  isDischargeMed.value = val === 'DISCHARGE_MED'
  isTextAdvice.value = val === 'TEXT'
}

const onPanelConfirm = (data) => {
  // 合并子面板数据
  Object.assign(adviceData, data)
}

const onPanelCancel = () => {
  // 取消逻辑
}

const onTextPanelConfirm = (data) => {
  Object.assign(adviceData, data)
}
</script>
