<template>
  <div>
    <el-row :gutter="24">
      <el-col :span="4" :xs="24">
        <el-input v-model="diagnosis" placeholder="诊断名称" clearable style="width: 100%; margin-bottom: 10px"
          @keyup.enter="queryDiagnosisUse">
          <template #append>
            <el-button icon="Search" @click="queryDiagnosisUse" />
          </template>
        </el-input>
        <el-tree
          ref="treeRef"
          :data="tree"
          node-key="id"
          :props="{ label: 'name', children: 'children' }"
          highlight-current
          default-expand-all
          :filter-node-method="filterNode"
          class="tree-with-scrollbar"
          @node-click="handleNodeClick"
          max-height="650"
        >
          <template #default="{ node, data }">
            <div class="custom-tree-node">
              <span>{{ node.label }}</span>
              <span class="tree-node-actions">
                <template v-if="node.level === 1 && data.name != '常用' && data.name != '历史'">
                  <el-button
                    style="color: #000000"
                    type="text"
                    size="small"
                    @click.stop="addChild(data)"
                  >
                    <el-icon>
                      <Plus />
                    </el-icon>
                  </el-button>
                </template>
                <el-popconfirm width="200" :hide-after="10" title="确认删除此常用诊断吗" placement="top-start"
                  @confirm="deleteChild(data)">
                  <template #reference>
                    <el-button
                      style="color: #000000"
                      v-if="
                        node.level === 2 &&
                        node.parent.data.name != '常用' &&
                        node.parent.data.name != '历史'
                      "
                      type="text"
                      size="small"
                      @click.stop=""
                    >
                      <el-icon>
                        <Minus />
                      </el-icon>
                    </el-button>
                  </template>
                </el-popconfirm>
              </span>
            </div>
          </template>
        </el-tree>
      </el-col>
      <el-col :span="20" :xs="24">
        <div style="margin-bottom: 10px">
          <el-button type="primary" plain @click="handleAddDiagnosis()"> 新增诊断 </el-button>
          <el-button type="primary" plain @click="handleSaveDiagnosis()" :loading="saveLoading"> 保存诊断 </el-button>
          <el-button type="primary" plain @click="handleAddTcmDiagonsis()"> 中医诊断 </el-button>
          <el-button type="primary" plain @click="handleImport()"> 导入慢性病诊断 </el-button>
          <span style="font-size: 12px; margin-left: 10px"
            >注意 : 若使用电子处方，请不要导入慢性病诊断</span
          >
        </div>

        <el-form :model="form" :rules="rules" ref="formRef">
          <el-table ref="diagnosisTableRef" :data="form.diagnosisList" height="650">
            <el-table-column label="序号" type="index" width="50" />
            <el-table-column label="分类" align="center" prop="classification" width="120">
              <template #default="scope">
                <el-form-item :prop="`diagnosisList.${scope.$index}.classification`">
<!--                  <el-select v-model="scope.row.classification" placeholder=" " style="width: 100px">
                    <el-option label="西医" value="西医" />
                    <el-option label="中医" value="中医" />
                    <el-option label="证型" value="证型" />
                    <el-option label="其他" value="其他" />
                  </el-select>-->
                  <el-select v-model="scope.row.classification" placeholder=" " style="width: 100px">
                    <el-option
                        v-for="item in diagnosis_classification"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="诊断类型" align="center" prop="medTypeCode" width="180">
              <template #default="scope">
                <el-form-item :prop="`diagnosisList.${scope.$index}.medTypeCode`" :rules="rules.medTypeCode">
                  <el-select v-model="scope.row.medTypeCode" placeholder=" " style="width: 150px">
                    <el-option
                      v-for="item in med_type"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="诊断" align="center" prop="name" >
              <template #default="scope">
                <el-form-item :prop="`diagnosisList.${scope.$index}.name`" :rules="rules.name">
                  <el-popover
                    :popper-style="{ padding: '0' }"
                    placement="bottom-start"
                    :visible="scope.row.showPopover"
                    trigger="manual"
                    :width="800"
                  >
                    <template #default>
                      <diagnosislist
                        :diagnosisSearchkey="diagnosisSearchkey"
                        @selectDiagnosis="handleSelsectDiagnosis"
                      />
                    </template>
                    <template #reference>
                      <el-input v-model="scope.row.name" placeholder="请选择诊断" @input="handleChange"
                        @focus="handleFocus(scope.row, scope.$index)" @blur="handleBlur(scope.row)" />
                    </template>
                  </el-popover>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="主诊断" align="center" prop="maindiseFlag" width="100">
              <template #default="scope">
                <el-form-item style="display: flex;justify-content: center;margin-bottom: 0;">
                  <el-select
                    v-model="scope.row.maindiseFlag"
                    placeholder=" "
                    style="width: 80px"
                    @change="(value) => handleMaindise(value, scope.$index)"
                  >
                    <el-option label="是" :value="1" />
                    <el-option label="否" :value="0" />
                  </el-select>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="诊断备注" align="center" prop="diagnosisDesc" width="180">
              <template #default="scope">
                <el-form-item :prop="`diagnosisList.${scope.$index}.diagnosisDesc`">
                  <el-input v-model="scope.row.diagnosisDesc" placeholder="请输入备注" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="ICD代码" align="center" prop="ybNo" width="180" />
            <el-table-column label="诊断状态" align="center" prop="verificationStatusEnum" width="120">
              <template #default="scope">
                <el-form-item :prop="`diagnosisList.${scope.$index}.verificationStatusEnum`" style="margin-bottom: 0;">
                  <el-select
                    v-model="scope.row.verificationStatusEnum"
                    placeholder=" "
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in diagnosisOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="发病日期" align="center" prop="onsetDate" width="160">
              <template #default="scope">
                <el-form-item :prop="`diagnosisList.${scope.$index}.onsetDate`">
                  <el-date-picker
                    v-model="scope.row.onsetDate"
                    type="date"
                    placeholder=" "
                    style="width: 100%"
                    value-format="YYYY-MM-DD"
                  />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="诊断日期" align="center" prop="diagnosisTime" width="160">
              <template #default="scope">
                <el-form-item :prop="`diagnosisList.${scope.$index}.diagnosisTime`">
                  <el-date-picker
                    v-model="scope.row.diagnosisTime"
                    type="date"
                    placeholder=" "
                    style="width: 100%"
                    value-format="YYYY-MM-DD"
                  />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="医生" align="center" prop="diagnosisDoctor" width="120" />
            <el-table-column label="长效诊断标识" align="center" prop="longTermFlag" width="140">
              <template #default="scope">
                <el-form-item :prop="`diagnosisList.${scope.$index}.longTermFlag`">
                  <el-select v-model="scope.row.longTermFlag" placeholder=" " style="width: 100%">
                    <el-option
                        v-for="item in long_term_flag"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="130">
              <template #default="scope">
                <el-button link type="primary" @click="handleDeleteDiagnosis(scope.row, scope.$index)">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </el-col>
    </el-row>
    <diagnosisdialog
      :openDiagnosis="openDiagnosis"
      @close="closeDiagnosisDialog"
      :radio="orgOrUser"
    />
    <AddDiagnosisDialog
      :openAddDiagnosisDialog="openAddDiagnosisDialog"
      :updateZy="updateZy"
      :patientInfo="props.patientInfo"
      @close="closeDiagnosisDialog"
    />
    <InfectiousDiseaseReportDialog
      ref="infectiousDiseaseReportRef"
      :patientInfo="props.patientInfo"
      :dept-id="userStore.orgId"
      :doctor-id="userStore.practitionerId"
      @close="handleInfectiousReportClose"
      @success="handleInfectiousReportSuccess"
    />
  </div>
</template>

<script setup>
import {getCurrentInstance} from 'vue';
import useUserStore from '@/store/modules/user';
import {
  delEncounterDiagnosis,
  deleteDiagnosisBind,
  deleteTcmDiagnosis,
  diagnosisInit,
  getChronicDisease,
  getConditionDefinitionInfo,
  getEmrDetail,
  getEncounterDiagnosis,
  getTcmDiagnosis,
  isFoodDiseasesNew,
  saveDiagnosis,
} from '../api';
import diagnosisdialog from '../diagnosis/diagnosisdialog.vue';
import AddDiagnosisDialog from './addDiagnosisDialog.vue';
import diagnosislist from '../diagnosis/diagnosislist.vue';
import InfectiousDiseaseReportDialog from './infectiousDiseaseReportDialog.vue';
// const diagnosisList = ref([]);
const allowAdd = ref(false);
const tree = ref([]);
const openDiagnosis = ref(false);
const updateZy = ref([]);
const openAddDiagnosisDialog = ref(false);
const diagnosisSearchkey = ref('');
const diagnosisOptions = ref([]);
const rowIndex = ref();
const diagnosis = ref();
const orgOrUser = ref();
const saveLoading = ref(false);
const form = ref({
  diagnosisList: [],
  isDataLoaded: false,
});
const props = defineProps({
  patientInfo: {
    type: Object,
    required: true,
  },
});
const emits = defineEmits(['diagnosisSave']);
const { proxy } = getCurrentInstance();
const userStore = useUserStore();
const { med_type } = proxy.useDict('med_type');
const { diagnosis_classification } = proxy.useDict('diagnosis_classification');
const { long_term_flag } = proxy.useDict('long_term_flag');

const rules = ref({
  name: [{ required: true, message: '请选择诊断', trigger: 'change' }],
  medTypeCode: [{ required: true, message: '请选择诊断类型', trigger: 'change' }],
  diagSrtNo: [{ required: true, message: '请输入诊断序号', trigger: 'change' }],
});

watch(
  () => form.value.diagnosisList,
  () => {
    emits('diagnosisSave', false);
  },
  { deep: true }
);

function getDetail(encounterId) {
  if (!encounterId) {
    console.warn('未提供有效的就诊ID，无法获取病历详情');
    return;
  }
  
  getEmrDetail(encounterId).then((res) => {
    allowAdd.value = res.data ? true : false;
  });
}

function refreshData() {
  getList();
}
let maxNo = 99;
async function getList() {
  try {
    const res = await getEncounterDiagnosis(props.patientInfo.encounterId);
    if (res.code == 200) {
      // 过滤掉中医诊断，只保留西医诊断
      form.value.diagnosisList = res.data.filter(item => item.typeName !== '中医诊断');
      // 为旧数据添加默认分类
      form.value.diagnosisList.forEach(item => {
        if (!item.classification) {
          item.classification = '西医';
        }
      });
      emits('diagnosisSave', false);
    }
    maxNo = form.value.diagnosisList.length;

    const tcmRes = await getTcmDiagnosis({ encounterId: props.patientInfo.encounterId });
    if (tcmRes.code == 200) {
      if (tcmRes.data.illness.length > 0) {
        tcmRes.data.illness.forEach((item, index) => {
          if (item.diagSrtNo <= maxNo) {
            return;
          }
          form.value.diagnosisList.push({
            name: item.name + '-' + tcmRes.data.symptom[index].name,
            diagSrtNo: item.diagSrtNo,
            ybNo: item.ybNo,
            medTypeCode: item.medTypeCode,
            syndromeGroupNo: item.syndromeGroupNo,
            typeName: '中医诊断',
            classification: '中医', // 中医诊断默认分类
            onsetDate: item.onsetDate,
            updateId:item.encounterDiagnosisId+'-'+tcmRes.data.symptom[index].encounterDiagnosisId,
            illnessDefinitionId : item.definitionId,
            symptomDefinitionId : tcmRes.data.symptom[index].definitionId,
            symptomYbNo: tcmRes.data.symptom[index].ybNo,
          });
          maxNo = item.diagSrtNo;
        });
      }
      emits('diagnosisSave', false);
    }
    getTree();
  } catch (error) {
    console.error('获取诊断列表失败:', error);
  }
}

init();
function init() {
  diagnosisInit().then((res) => {
    if (res.code == 200) {
      diagnosisOptions.value = res.data.verificationStatusOptions;
    }
  });
}

function handleImport() {
  if (!props.patientInfo || !props.patientInfo.encounterId) {
    console.warn('患者就诊信息不完整，无法导入慢性病信息');
    return;
  }
  
  if (props.patientInfo.contractName != '自费') {
    // 获取患者慢性病信息
    getChronicDisease({ encounterId: props.patientInfo.encounterId }).then((res) => {
      if (res.data && res.data.length > 0) {
        // 计算现有最大排序号
        const maxSortNo = form.value.diagnosisList.length > 0 
          ? Math.max(...form.value.diagnosisList.map(item => item.diagSrtNo || 0)) 
          : 0;
          
        res.data.forEach((item, index) => {
          form.value.diagnosisList.push({
        ...item,
        ...{
          medTypeCode: '140104',
          verificationStatusEnum: 4,
          definitionId: item.id,
          diagSrtNo: maxSortNo + index + 1,
          iptDiseTypeCode: 2,
          diagnosisDesc: '',
          classification: '西医', // 导入的慢性病默认为西医
          onsetDate: getCurrentDate(),
              diagnosisDoctor: props.patientInfo.practitionerName || props.patientInfo.doctorName || props.patientInfo.physicianName || userStore.name,
              diagnosisTime: getCurrentDate()
        },
          });
        });
        
        // 导入完成后按排序号排序
        form.value.diagnosisList.sort((a, b) => (a.diagSrtNo || 0) - (b.diagSrtNo || 0));
        emits('diagnosisSave', false);
      }
    });
  }
}

/**
 * 添加子节点
 */
function addChild(data) {
  orgOrUser.value = data.name;
  openDiagnosis.value = true;
}

/**
 * 删除子节点
 */
function deleteChild(data) {
  deleteDiagnosisBind(data.id).then((res) => {
    if (res.code == 200) {
      proxy.$modal.msgSuccess('删除成功');
      getTree();
    }
  });
}
watch(diagnosis, (val) => {
  proxy.$refs['treeRef'].filter(val);
});

/** 通过条件过滤节点  */
const filterNode = (value, data) => {
  console.log('filterNode', value, data);

  if (!value) return true;
  return data.name.indexOf(value) !== -1;
};

/**
 * 获取诊断树列表
 */
function getTree() {
  const patientId = props.patientInfo?.patientId || '';
  getConditionDefinitionInfo(patientId).then((res) => {
    if (res.code == 200) {
      // 确保数据结构正确，避免直接修改数组对象
      const patientHistoryList = Array.isArray(res.data.patientHistoryList) ? res.data.patientHistoryList : [];
      const doctorCommonUseList = Array.isArray(res.data.doctorCommonUseList) ? res.data.doctorCommonUseList : [];
      const userPersonalList = Array.isArray(res.data.userPersonalList) ? res.data.userPersonalList : [];
      const organizationList = Array.isArray(res.data.organizationList) ? res.data.organizationList : [];

      // 手动构造树列表;
      tree.value[0] = {
        id: '1',
        name: '历史',
        children: patientHistoryList,
      };
      tree.value[1] = {
        id: '2',
        name: '常用',
        children: doctorCommonUseList,
      };
      tree.value[2] = {
        id: '3',
        name: '个人',
        children: userPersonalList,
      };
      tree.value[3] = {
        id: '4',
        name: '科室',
        children: organizationList,
      };
      console.log(tree.value);
    }
  });
}

/**
 * 添加西医诊断
 */
function handleAddDiagnosis() {
  proxy.$refs.formRef.validate((valid) => {
    if (valid) {
      const maxSortNo = form.value.diagnosisList.length > 0 
        ? Math.max(...form.value.diagnosisList.map(item => item.diagSrtNo || 0)) 
        : 0;
      
      form.value.diagnosisList.push({
        showPopover: false,
        name: undefined,
        verificationStatusEnum: 4,
        medTypeCode: '11',
        diagSrtNo: maxSortNo + 1,
        iptDiseTypeCode: 2,
        diagnosisDesc: '',
        classification: '西医', // 默认为西医
        onsetDate: getCurrentDate(),
        diagnosisDoctor: props.patientInfo.practitionerName || props.patientInfo.doctorName || props.patientInfo.physicianName || userStore.name,
        diagnosisTime: getCurrentDate()
      });
      
      // 添加后按排序号排序
      form.value.diagnosisList.sort((a, b) => (a.diagSrtNo || 0) - (b.diagSrtNo || 0));
      
      if (form.value.diagnosisList.length == 1) {
        form.value.diagnosisList[0].maindiseFlag = 1;
      }
    }
  });
}

// 添加中医诊断
function handleAddTcmDiagonsis() {
  updateZy.value = [];
  openAddDiagnosisDialog.value = true;
}

/**
 * 删除诊断
 */
function handleDeleteDiagnosis(row, index) {
  if (row.conditionId) {
    delEncounterDiagnosis(row.conditionId).then((res) => {
      if (res.code == 200) {
        proxy.$modal.msgSuccess('删除成功');
        getList();
        getTree();
      }
    });
  } else if (row.syndromeGroupNo) {
    deleteTcmDiagnosis(row.syndromeGroupNo).then(() => {
      getList();
      getTree();
    });
  } else {
    form.value.diagnosisList.splice(index, 1);
    // 删除后不重新计算排序号，保持用户设置的排序不变
    emits('diagnosisSave', false);
  }
}

function handleMaindise(value, index) {
  if (value == 1) {
    let flag = 0;
    form.value.diagnosisList.forEach((item) => {
      if (item.maindiseFlag == 1) {
        flag++;
      }
    });
    if (flag > 1) {
      form.value.diagnosisList[index].maindiseFlag = 0;
      proxy.$modal.msgWarning('只能有一条主诊断');
    }
  }
}

/**
 * 保存诊断
 * 使用 async/await 重构，优化错误处理和加载状态
 */
async function handleSaveDiagnosis() {
  try {
    // 表单验证
    const valid = await proxy.$refs.formRef.validate().catch(() => false);
    if (!valid) {
      return;
    }

    // 检查诊断列表是否为空
    if (form.value.diagnosisList.length === 0) {
      proxy.$modal.msgWarning('诊断不能为空');
      return;
    }

    // 检查是否至少有一条主诊断
    const hasMainDiagnosis = form.value.diagnosisList.some((diagnosis) => diagnosis.maindiseFlag === 1);
    if (!hasMainDiagnosis) {
      proxy.$modal.msgWarning('至少添加一条主诊断');
      return;
    }

    // 防护性检查：确保患者信息完整
    if (!props.patientInfo?.patientId || !props.patientInfo?.encounterId) {
      proxy.$modal.msgWarning('患者信息不完整，无法保存诊断');
      return;
    }

    // 开始加载状态，防止重复提交
    saveLoading.value = true;

    // 保存前按排序号排序
    form.value.diagnosisList.sort((a, b) => (a.diagSrtNo || 0) - (b.diagSrtNo || 0));

    // 调用保存诊断接口
    const res = await saveDiagnosis({
      patientId: props.patientInfo.patientId,
      encounterId: props.patientInfo.encounterId,
      diagnosisChildList: form.value.diagnosisList,
    });

    if (res.code === 200) {
      // 刷新树和列表数据（等待列表数据加载完成，确保获取到reportTypeCode）
      await getList();
      getTree();
      emits('diagnosisSave', false);
      proxy.$modal.msgSuccess('诊断已保存并按排序号排序');

      // 食源性疾病病例数据智能采集
      await handleFoodDiseasesCheck();

      // 传染病报告卡：检查诊断的reportTypeCode，弹出对应的报告卡界面
      handleInfectiousDiseaseReport();
    }
  } catch (error) {
    console.error('保存诊断失败:', error);
    proxy.$modal.msgError('保存诊断失败，请稍后重试');
  } finally {
    // 结束加载状态
    saveLoading.value = false;
  }
}

/**
 * 食源性疾病病例数据智能采集
 */
async function handleFoodDiseasesCheck() {
  try {
    const res = await isFoodDiseasesNew({
      encounterId: props.patientInfo.encounterId,
    });
    if (res.code === 200 && res.data) {
      window.open(res.data, '_blank');
    }
  } catch (error) {
    console.error('食源性疾病检查失败:', error);
  }
}

/**
 * 传染病报告卡处理
 * 通过诊断目录维护的'报卡类型'字段自动识别是否有需要填写的传染病报告卡
 * 如果有则弹出诊断对应需登记的报告卡界面
 */
function handleInfectiousDiseaseReport() {
  // 查找所有有报卡类型的诊断（reportTypeCode不为空）
  const diagnosesWithReportType = form.value.diagnosisList.filter(d => d.reportTypeCode);

  if (diagnosesWithReportType.length === 0) {
    return;
  }

  // 优先使用主诊断，如果没有主诊断有报卡类型则使用第一个有报卡类型的诊断
  const mainDiagnosisWithReport = diagnosesWithReportType.find(d => d.maindiseFlag === 1);
  const targetDiagnosis = mainDiagnosisWithReport || diagnosesWithReportType[0];

  // 弹出传染病报告卡弹窗
  proxy.$refs.infectiousDiseaseReportRef?.show(targetDiagnosis);
}

/**
 * 关闭诊断弹窗
 */
function closeDiagnosisDialog(str) {
  if (str === 'success') {
    proxy.$modal.msgSuccess('操作成功');
  }
  openAddDiagnosisDialog.value = false;
  openDiagnosis.value = false;
  getList();
  getTree();
}

/**
 * 传染病报告卡关闭
 */
function handleInfectiousReportClose() {
  // 关闭传染病报告卡弹窗
}

/**
 * 传染病报告卡保存成功
 */
function handleInfectiousReportSuccess() {
}

function queryDiagnosisUse(value) { }

function handleChange(value) {
  diagnosisSearchkey.value = value;
}
/**
 * 选择诊断并赋值到列表
 */
function handleSelsectDiagnosis(row) {
  console.log(row);
  form.value.diagnosisList[rowIndex.value].ybNo = row.ybNo;
  form.value.diagnosisList[rowIndex.value].name = row.name;
  form.value.diagnosisList[rowIndex.value].definitionId = row.id;
}
/**获取焦点时 打开列表 */
function handleFocus(row, index) {
  if(row.typeName==='中医诊断'){
    updateZy.value = [];
    updateZy.value.push({
      illnessDefinitionId: row.illnessDefinitionId,
      symptomDefinitionId: row.symptomDefinitionId,
      syndromeGroupNo: row.syndromeGroupNo,
      symptomYbNo:row.symptomYbNo,
      ybNo:row.ybNo,
      updateId: row.updateId,
      diagSrtNo: row.diagSrtNo,
      name:row.name,
    });
    openAddDiagnosisDialog.value = true;
  }else{
    rowIndex.value = index;
    row.showPopover = true;
  }
  
}
/**失去焦点时 关闭列表 */
function handleBlur(row) {
  row.showPopover = false;
}

function handleNodeClick(data) {
  console.log(data.children);
  // 检查节点是否为根节点
  if (data.children != undefined) {
    // 如果是根节点，不执行任何操作
    return;
  }
  // if (!allowAdd.value) {
  //   proxy.$modal.msgWarning('请先填写病历');
  //   return;
  // }
  const isDuplicate = form.value.diagnosisList.some(
    (diagnosis) => diagnosis.ybNo === data.ybNo || diagnosis.name === data.name
  );
  if (isDuplicate) {
    proxy.$modal.msgWarning('该诊断项已存在');
    return;
  }
  
  // 计算现有最大排序号
  const maxSortNo = form.value.diagnosisList.length > 0 
    ? Math.max(...form.value.diagnosisList.map(item => item.diagSrtNo || 0)) 
    : 0;
    
form.value.diagnosisList.push({
    ybNo: data.ybNo,
    name: data.name,
    verificationStatusEnum: 4,
    medTypeCode: '11',
    diagSrtNo: maxSortNo + 1,
    definitionId: data.definitionId,
    classification: '西医', // 默认为西医
    onsetDate: getCurrentDate(),
    diagnosisDoctor: props.patientInfo.practitionerName || props.patientInfo.doctorName || props.patientInfo.physicianName || userStore.name,
    diagnosisTime: getCurrentDate()
  });
  
  // 添加后按排序号排序
  form.value.diagnosisList.sort((a, b) => (a.diagSrtNo || 0) - (b.diagSrtNo || 0));
  
  if (form.value.diagnosisList.length == 1) {
    form.value.diagnosisList[0].maindiseFlag = 1;
  }
}

function getCurrentDate() {
  const date = new Date();
  const year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  month = month < 10 ? "0" + month : month;
  day = day < 10 ? "0" + day : day;
  return `${year}-${month}-${day}`;
}

defineExpose({ getList, getDetail, handleSaveDiagnosis });
</script>

<style lang="scss" scoped>
.el-checkbox.is-bordered.el-checkbox--small {
  background-color: #ffffff;
}
.tree-with-scrollbar {
  max-height: 650px;
  overflow-y: auto;
}
.custom-tree-node {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.tree-node-actions {
  display: flex;
  align-items: center;
}
</style>
