<template>
  <div class="medical-document">
    <!-- 标题区域 -->
    <div class="doc-header">
      <h1 class="doc-title">{{ hospitalName }} 住院患者入院沟通记录单</h1>
      <div class="doc-subtitle">住院号: {{ formData.hospitalNo || '待填写' }}</div>
    </div>

    <!-- 内容区域 -->
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="100px"
      label-align="left"
      class="doc-content"
    >
      <!-- 患者基础信息 -->
      <section class="doc-section">
        <h2 class="section-title">一、患者基础信息</h2>
        <div class="adaptive-grid">
          <el-form-item label="姓名" prop="patientName" class="grid-item required">
            <el-input v-model="formData.patientName" placeholder="请输入患者姓名" clearable />
          </el-form-item>
          
          <el-form-item label="性别" prop="gender" class="grid-item required">
            <el-select v-model="formData.gender" placeholder="请选择性别">
              <el-option label="男" value="男" />
              <el-option label="女" value="女" />
            </el-select>
          </el-form-item>
          
          <el-form-item label="年龄" prop="age" class="grid-item required">
            <div class="input-with-unit">
              <el-input v-model.number="formData.age" type="number" placeholder="请输入年龄" />
              <span class="unit">岁</span>
            </div>
          </el-form-item>
          
          <el-form-item label="科室/病区" prop="department" class="grid-item required">
            <el-input v-model="formData.department" placeholder="如：内科疗区" clearable />
          </el-form-item>
          
          <el-form-item label="病房/床号" prop="bedNo" class="grid-item required">
            <el-input v-model="formData.bedNo" placeholder="如：307-12" clearable />
          </el-form-item>
          
          <el-form-item label="入院日期" prop="admissionDate" class="grid-item required">
            <el-date-picker
              v-model="formData.admissionDate"
              type="date"
              placeholder="选择入院日期"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </div>
      </section>

      <!-- 医疗团队信息 -->
      <section class="doc-section">
        <h2 class="section-title">二、医疗团队信息</h2>
        <div class="adaptive-grid">
          <el-form-item label="经治医师" prop="treatingDoctor" class="grid-item required">
            <el-input v-model="formData.treatingDoctor" placeholder="请输入医师姓名" clearable />
          </el-form-item>
          
          <el-form-item label="主治医师" prop="attendingDoctor" class="grid-item required">
            <el-input v-model="formData.attendingDoctor" placeholder="请输入医师姓名" clearable />
          </el-form-item>
          
          <el-form-item label="科主任" prop="departmentHead" class="grid-item required">
            <el-input v-model="formData.departmentHead" placeholder="请输入主任姓名" clearable />
          </el-form-item>
        </div>
      </section>

      <!-- 病情与诊断 -->
      <section class="doc-section">
        <h2 class="section-title">三、病情与诊断</h2>
        
        <el-form-item label="病情状况" prop="condition" class="full-width-item required">
          <el-input
            v-model="formData.condition"
            type="textarea"
            placeholder="详细描述患者病情状况"
            autosize
            maxlength="1000"
            show-word-limit
          />
        </el-form-item>
        
        <div class="diagnosis-container">
          <el-form-item label="中医诊断" prop="tcmDiagnosis" class="diagnosis-item">
            <el-input
              v-model="formData.tcmDiagnosis"
              type="textarea"
              placeholder="如：胸痹心痛（气阴两虚证）"
              autosize
              maxlength="500"
              show-word-limit
            />
          </el-form-item>
          
          <el-form-item label="西医诊断" prop="westernDiagnosis" class="diagnosis-item">
            <el-input
              v-model="formData.westernDiagnosis"
              type="textarea"
              placeholder="如：1.冠状动脉粥样硬化性心脏病..."
              autosize
              maxlength="800"
              show-word-limit
            />
          </el-form-item>
        </div>
      </section>

      <!-- 治疗与检查计划 -->
      <section class="doc-section">
        <h2 class="section-title">四、治疗与检查计划</h2>
        
        <el-form-item label="治疗方案" prop="treatmentPlan" class="full-width-item required">
          <el-input
            v-model="formData.treatmentPlan"
            type="textarea"
            placeholder="详细描述治疗方案"
            autosize
            maxlength="1000"
            show-word-limit
          />
        </el-form-item>
        
        <el-form-item label="进一步检查项目" prop="examinationItems" class="full-width-item required">
          <el-input
            v-model="formData.examinationItems"
            type="textarea"
            placeholder="列出需要进行的检查项目"
            autosize
            maxlength="1000"
            show-word-limit
          />
        </el-form-item>
      </section>

      <!-- 风险告知 -->
      <section class="doc-section">
        <h2 class="section-title">五、风险告知</h2>
        <el-form-item label="告知内容" prop="riskNotification" class="full-width-item required">
          <el-input
            v-model="formData.riskNotification"
            type="textarea"
            placeholder="告知患者可能存在的风险"
            autosize
            maxlength="800"
            show-word-limit
          />
        </el-form-item>
      </section>

      <!-- 签署区域（优化后：三列自适应+细节样式） -->
      <section class="doc-section">
        <h2 class="section-title">六、签署确认</h2>
        <div class="adaptive-grid signature-area" style="grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));">
          <el-form-item label="患者或家属签字" prop="patientSignature" class="grid-item required">
            <el-input v-model="formData.patientSignature" placeholder="请签字" clearable />
            <div class="signature-tip">请填写患者或家属签字</div>
          </el-form-item>
          
          <el-form-item label="与患者关系" prop="relationship" class="grid-item">
            <el-input v-model="formData.relationship" placeholder="如：本人、配偶、子女" clearable />
          </el-form-item>
          
          <el-form-item label="签字日期" prop="signatureDate" class="grid-item required">
            <el-date-picker
              v-model="formData.signatureDate"
              type="date"
              placeholder="选择签字日期"
              value-format="YYYY-MM-DD"
              style="width: 100%;"
            />
          </el-form-item>
          
          <el-form-item label="沟通医师签字" prop="doctorSignature" class="grid-item required">
            <el-input v-model="formData.doctorSignature" placeholder="请签字" clearable />
            <div class="signature-tip">请填写沟通医师签字</div>
          </el-form-item>
          
          <el-form-item label="沟通日期" prop="communicationDate" class="grid-item required">
            <el-date-picker
              v-model="formData.communicationDate"
              type="datetime"
              placeholder="选择沟通日期时间"
              value-format="YYYY-MM-DD HH:mm"
              style="width: 100%;"
            />
          </el-form-item>
        </div>
      </section>
    </el-form>

    <!-- 操作按钮 -->
    <div class="btn-group">
      <el-button type="primary" @click="submit">保存记录</el-button>
      <el-button type="success" @click="handlePrint">打印记录</el-button>
      <el-button type="warning" @click="handleReset">重置表单</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox, ElForm, ElFormItem, ElInput, ElSelect, ElOption, ElDatePicker, ElButton } from 'element-plus';

// 医院名称
const hospitalName = '长春市朝阳区中医院';
defineOptions({
  name: 'InHospitalCommunicate'
});
// 表单引用
const formRef = ref(null);

// 表单数据
const formData = reactive({
  // 基础信息
  hospitalNo: '',
  patientName: '',
  gender: '',
  age: '',
  department: '',
  bedNo: '',
  admissionDate: '',
  
  // 医疗团队
  treatingDoctor: '',
  attendingDoctor: '',
  departmentHead: '',
  
  // 病情诊断
  condition: '',
  tcmDiagnosis: '',
  westernDiagnosis: '',
  
  // 治疗检查
  treatmentPlan: '',
  examinationItems: '',
  
  // 风险告知
  riskNotification: '',
  
  // 签署信息
  patientSignature: '',
  relationship: '',
  signatureDate: '',
  doctorSignature: '',
  communicationDate: ''
});

// 表单验证规则
const rules = reactive({
  hospitalNo: [
    { required: true, message: '请填写住院号', trigger: ['blur', 'submit'] }
  ],
  patientName: [
    { required: true, message: '请填写患者姓名', trigger: ['blur', 'submit'] }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: ['change', 'submit'] }
  ],
  age: [
    { required: true, message: '请填写年龄', trigger: ['blur', 'submit'] },
    { type: 'number', min: 0, max: 150, message: '年龄需在0-150之间', trigger: ['blur', 'submit'] }
  ],
  department: [
    { required: true, message: '请填写科室/病区', trigger: ['blur', 'submit'] }
  ],
  bedNo: [
    { required: true, message: '请填写病房/床号', trigger: ['blur', 'submit'] }
  ],
  admissionDate: [
    { required: true, message: '请选择入院日期', trigger: ['change', 'submit'] }
  ],
  treatingDoctor: [
    { required: true, message: '请填写经治医师', trigger: ['blur', 'submit'] }
  ],
  attendingDoctor: [
    { required: true, message: '请填写主治医师', trigger: ['blur', 'submit'] }
  ],
  departmentHead: [
    { required: true, message: '请填写科主任', trigger: ['blur', 'submit'] }
  ],
  condition: [
    { required: true, message: '请描述病情状况', trigger: ['blur', 'submit'] }
  ],
  treatmentPlan: [
    { required: true, message: '请填写治疗方案', trigger: ['blur', 'submit'] }
  ],
  examinationItems: [
    { required: true, message: '请填写检查项目', trigger: ['blur', 'submit'] }
  ],
  riskNotification: [
    { required: true, message: '请填写风险告知内容', trigger: ['blur', 'submit'] }
  ],
  patientSignature: [
    { required: true, message: '请填写患者或家属签字', trigger: ['blur', 'submit'] }
  ],
  signatureDate: [
    { required: true, message: '请选择签字日期', trigger: ['change', 'submit'] }
  ],
  doctorSignature: [
    { required: true, message: '请填写医师签字', trigger: ['blur', 'submit'] }
  ],
  communicationDate: [
    { required: true, message: '请选择沟通日期', trigger: ['change', 'submit'] }
  ]
});

// 生命周期
onMounted(() => {
  // 初始化日期为当前日期
  const today = new Date();
  formData.admissionDate = formatDate(today);
  formData.signatureDate = formatDate(today);
  formData.communicationDate = formatDateTime(today);
});

// 提交表单
const submit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success('记录保存成功');
      console.log('表单数据:', formData);
    }
  });
};

// 打印功能
const handlePrint = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      window.print();
    } else {
      ElMessageBox.warning('请先完善表单信息再打印');
    }
  });
};

// 重置表单
const handleReset = () => {
  ElMessageBox.confirm(
    '确定要重置表单吗？所有已填写内容将被清空',
    '确认重置',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    formRef.value.resetFields();
    const today = new Date();
    formData.admissionDate = formatDate(today);
    formData.signatureDate = formatDate(today);
    formData.communicationDate = formatDateTime(today);
    ElMessage.success('表单已重置');
  });
};

// 日期格式化工具
const formatDate = (date) => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
};

const formatDateTime = (date) => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hour = String(date.getHours()).padStart(2, '0');
  const minute = String(date.getMinutes()).padStart(2, '0');
  return `${year}-${month}-${day} ${hour}:${minute}`;
};
</script>

<style scoped>
.medical-document {
  max-width: 1200px;
  margin: 20px auto;
  padding: 30px;
  background: #fff;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  font-family: 'SimSun', '宋体', serif;
}

/* 标题样式 */
.doc-header {
  text-align: center;
  margin-bottom: 30px;
}

.doc-title {
  font-size: 22px;
  margin: 0 0 10px;
  font-weight: bold;
}

.doc-subtitle {
  font-size: 16px;
  color: #666;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #333;
}

/* 内容区域 */
.doc-content {
  width: 100%;
}

.doc-section {
  margin-bottom: 25px;
  padding-bottom: 15px;
  border-bottom: 1px dashed #ccc;
}

.section-title {
  font-size: 18px;
  margin: 0 0 15px;
  color: #333;
  font-weight: bold;
}

/* 自适应网格布局 */
.adaptive-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 15px 20px;
  margin-bottom: 15px;
}

.grid-item {
  margin-bottom: 0;
  display: flex;
  flex-direction: column;
}
.grid-item .el-form-item__content {
  flex: 1;
  min-width: 0; /* 确保输入框可收缩适配列宽 */
}

/* 全宽项目 */
.full-width-item {
  width: 100%;
  margin-bottom: 15px;
}

/* 诊断区域布局 */
.diagnosis-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 15px;
}

.diagnosis-item {
  margin-bottom: 0;
}

/* 带单位输入框 */
.input-with-unit {
  display: flex;
  align-items: center;
  gap: 8px;
}

.unit {
  white-space: nowrap;
  color: #666;
}

/* 签名区域优化 */
.signature-area {
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
}

.signature-tip {
  font-size: 12px;
  color: #f56c6c;
  margin-top: 4px;
}

/* 按钮组 */
.btn-group {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 2px solid #333;
}

/* 必填项样式 */
.required .el-form-item__label::before {
  content: '*';
  color: #ff4d4f;
  margin-right: 4px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .medical-document {
    padding: 15px;
  }
  
  .diagnosis-container {
    grid-template-columns: 1fr;
  }
  
  .adaptive-grid {
    grid-template-columns: 1fr;
  }
  
  .doc-title {
    font-size: 18px;
  }
  
  .section-title {
    font-size: 16px;
  }
}

/* 打印样式优化 */
@media print {
  .btn-group {
    display: none;
  }
  
  .medical-document {
    box-shadow: none;
    margin: 0;
    padding: 0;
  }
  
  .el-input__inner, .el-select__input, .el-textarea__inner {
    border: none !important;
    box-shadow: none !important;
    background: transparent !important;
  }
  
  .el-form-item__label {
    font-weight: bold !important;
  }
}
</style>