<template>
  <el-dialog
    v-model="visible"
    title="假期流向登记"
    width="600px"
    destroy-on-close
    :close-on-click-modal="false"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="假期类型" prop="type">
        <el-select v-model="form.type" style="width: 100%">
          <el-option label="离校 (回家/外出)" value="leave" />
          <el-option label="留校" value="stay" />
        </el-select>
      </el-form-item>

      <!-- 离校表单 -->
      <template v-if="form.type === 'leave'">
        <el-form-item label="离校时间" prop="leaveTime">
          <el-date-picker v-model="form.leaveTime" type="datetime" style="width: 100%" />
        </el-form-item>
        <el-form-item label="目的地" prop="destination">
          <el-input v-model="form.destination" placeholder="省/市/区" />
        </el-form-item>
        <el-form-item label="交通方式" prop="transport">
          <el-select v-model="form.transport" style="width: 100%">
            <el-option label="高铁/火车" value="train" />
            <el-option label="飞机" value="plane" />
            <el-option label="大巴/汽车" value="bus" />
            <el-option label="自驾/家长接送" value="car" />
          </el-select>
        </el-form-item>
        <el-form-item label="车次/航班" prop="trainNo">
          <el-input v-model="form.trainNo" placeholder="如：G1234" />
        </el-form-item>
        <el-form-item label="预计返校" prop="returnTime">
          <el-date-picker v-model="form.returnTime" type="datetime" style="width: 100%" />
        </el-form-item>
      </template>

      <!-- 留校表单 -->
      <template v-else>
        <el-form-item label="留校时间" prop="stayRange">
          <el-date-picker
            v-model="form.stayRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="留校原因" prop="reason">
          <el-select v-model="form.reason" style="width: 100%">
            <el-option label="科研项目" value="research" />
            <el-option label="竞赛集训" value="competition" />
            <el-option label="勤工助学" value="work" />
            <el-option label="实习" value="internship" />
            <el-option label="其他" value="other" />
          </el-select>
        </el-form-item>
        <el-form-item label="详细说明" prop="description">
          <el-input v-model="form.description" type="textarea" rows="3" />
        </el-form-item>
      </template>

      <el-form-item label="紧急联系人" prop="contactName">
        <el-input v-model="form.contactName" />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input v-model="form.contactPhone" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">提交申请</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['success'])

const visible = ref(false)
const formRef = ref()

const form = reactive({
  type: 'leave',
  leaveTime: '',
  destination: '',
  transport: '',
  trainNo: '',
  returnTime: '',
  stayRange: [],
  reason: '',
  description: '',
  contactName: '',
  contactPhone: ''
})

const rules = {
  type: [{ required: true, message: '请选择类型', trigger: 'change' }],
  leaveTime: [{ required: true, message: '请选择离校时间', trigger: 'change' }],
  destination: [{ required: true, message: '请输入目的地', trigger: 'blur' }],
  contactName: [{ required: true, message: '请输入紧急联系人', trigger: 'blur' }],
  contactPhone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
  stayRange: [{ required: true, message: '请选择留校时间', trigger: 'change' }],
  reason: [{ required: true, message: '请选择留校原因', trigger: 'change' }],
}

const open = () => {
  // Reset form
  form.type = 'leave'
  form.leaveTime = ''
  form.destination = ''
  form.transport = ''
  form.trainNo = ''
  form.returnTime = ''
  form.stayRange = []
  form.reason = ''
  form.description = ''
  form.contactName = ''
  form.contactPhone = ''
  
  visible.value = true
}

const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success('申请提交成功')
      emit('success')
      visible.value = false
    }
  })
}

defineExpose({
  open
})
</script>
