<template>
  <Mask :is-show="is_show" :width="450" :height="250" :show-footer="true" @close="close" title="诊断">
    <template #default>
      <div class="wrapper">
        <span>诊断：</span>
        <DiagnosisSearchInput
            ref="diagnosisSearchRef"
            :request-api="diagnosisSearchApi"
            :show-config="diagnosisShowConfig"
            @selectedCallBack="diagnosisSelect"
            style="height: 100%;width: 100%"
        >
        </DiagnosisSearchInput>
      </div>
    </template>
    <template #footer>
      <div class="footer">
        <div class="default-btn" @click="close">取消</div>
        <div class="default-btn" @click="save">确定</div>
      </div>
    </template>
  </Mask>
</template>

<script setup lang="ts">
import {ref} from "vue";
import Mask from "@/components/common/Mask.vue";
import DiagnosisSearchInput from "@/components/outpatient/DiagnosisSearchInput.vue";
import {apiConfig} from "@/assets/config/apiConfig.ts";
import {getEndOfDay} from "@/utils/dateUtils.ts";
import {post} from "@/utils/request.ts";
import {ElMessage} from "element-plus";

const is_show = ref(false);
const init = async () => {
  is_show.value = true
  try {
    let data: any = await post(apiConfig.RegistrationList, {
      query: {
        status: 1,
        beginTime: new Date(),
        endTime: getEndOfDay(new Date())
      }
    }, {catch_error: true});
    post('registration/changeStatus', {id: data.list[0].id, status: 2}).then((res: any) => {
      registerId.value = res.id
      patientId.value = res.patientInfoId
    })
  } catch (e) {
    is_show.value = false
  }
}
const diagnosisSearchApi = "social/diagnose/search"

interface ShowConfig {
  label: string;
  prop: string;
}

const diagnosisShowConfig: ShowConfig[] = [
  {
    label: "诊断名称",
    prop: "name",
  },
  {
    label: "诊断编码",
    prop: "code",
  }
]
const medicalRecord = ref<any>({})
const registerId = ref<any>(null)
const patientId = ref<any>(null)
const diagnosisSelect = (list: any) => {
  const diagnosisNames = list.map((item: any) => item.name).join(',')
  medicalRecord.value.diagnosisDetail = JSON.stringify(list)
  medicalRecord.value.diagnosisSummary = diagnosisNames
}
const emit = defineEmits(['close'])
const save = () => {
  const data = {
    registrationId: registerId.value,
    patientId: patientId.value,
    diagnosisMedicalRecord: medicalRecord.value,
    itemList: [],
    goodsList: [],
  }
  post('medical/record/save', {data: data}).then((res: any) => {
    ElMessage.success("快速接诊成功")
    is_show.value = false
    emit('close')
  })
}
const close = () => {
  medicalRecord.value = {}
  registerId.value = null
  patientId.value = null
  is_show.value = false
}
defineExpose({init})
</script>

<style scoped lang="scss">
.wrapper {
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 24px;
  span{
    width: 60px;
  }
}

:deep(.el-input__wrapper) {
  height: 42px;
  flex: 1;
  min-width: 0;
}

.footer {
  height: 100%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 0 24px;

  .default-btn {
    margin-left: 24px;

    &:first-child {
      margin-left: 0;
    }
  }
}
</style>