<template>
  <el-select v-model="currentWard"
             :clearable="wardsClearable"
             style="width: 110px"
             @change="fetchOverviews">
    <el-option v-for="item in allWards"
               :key="item.code"
               :label="item.name"
               :value="item.code"/>
  </el-select>

  <el-input v-model="queryPatNo" clearable style="width: 90px"  placeholder="住院号"
            @keyup.enter="queryPatientInfoClick"></el-input>
  <el-button icon="Search" type="primary"  @click="queryPatientInfoClick">查询</el-button>
  <br>
  <el-table :data="cptOverviews"
            height="800"
            @row-click="handleClickOverview"
            ref="elTableRef"
            highlight-current-row
            class="remove_hover"
            @selection-change="handleSelectionChange"
           >
    <el-table-column fixed v-if="props.selectFlag" type="selection" width="35"></el-table-column>
    <el-table-column label="床" prop="bedNo" width="30"/>
    <el-table-column label="姓名" width="70" prop="name">
      <template #default="{row}">
        <span>
          {{ row.name }}
        </span>
      </template>
    </el-table-column>
    <el-table-column label="住院号" prop="inpatientNo" width="65">
      <template #default="{row}">
        <div :style="{color: row.dismissOrder ===1 ? 'red' : '#19e63c'}">
          {{ row.inpatientNo }}
        </div>
      </template>
    </el-table-column>
    <el-table-column label="性别" prop="sex" width="65">
      <template #default="scope">
        {{ cptSex(scope.row.sex) }}
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup name='PatientBaseList'>
import {computed, onMounted, ref} from "vue";
import {getOverView} from "@/api/inpatient/patient";
import {allWardsVisible} from "@/utils/permission";
import {cptSex} from "@/utils/computed";
import {cqYzPrint, lsYzPrint,queryPatientInfo,queryYz,getPatientBaseInfo} from "@/api/medical-advice/medical-advice-management";
import {getAllWards} from "@/api/zhu-yuan-yi-sheng/resident-doctor";
import sleep from "@/utils/sleep";
const selections = ref([])
const myPatient = ref(false)
const queryPatNo = ref('')
const wardsClearable = allWardsVisible()
const allWards = ref([])
const currentWard = ref()
const overviews = ref([])
const emit = defineEmits(['selectPatientInfo','allPatientList']);
const props = defineProps({
  yzType: {
    type: String
  },
  selectFlag : {
    type: Boolean
  }
})
const fetchOverviews = () => {
  getOverView(currentWard.value).then((res) => {
    overviews.value = res
  })
}

const queryPatientInfoClick=()=>{
  queryPatientInfo(currentWard.value,queryPatNo.value).then((res) => {
    overviews.value = res
  })
}
const cptOverviews = computed(() => {
  if(props.selectFlag){
    emit('allPatientList',overviews.value)
  }
  return overviews.value
})

const elTableRef = ref(null)
//行点击事件
const handleClickOverview = (row) => {
  if(props.yzType=='cq'){
    cqYzPrint(row.inpatientNo,row.admissTimes).then((res) => {
      let val = {patientInfo:res.patientInfo,yzPrintVOList:res.yzPrintVOList}
      emit('selectPatientInfo',val)
    })
  }else if(props.yzType=='ls'){
    lsYzPrint(row.inpatientNo,row.admissTimes).then((res) => {
      let val = {patientInfo:res.patientInfo,yzPrintVOList:res.yzPrintVOList}
      emit('selectPatientInfo',val)
    })
  }else if(props.yzType=='cxyz'){
    let queryData ={
      patNo:row.inpatientNo,
      times:row.admissTimes,
      queryRange:'0',
      statusFlag:'3'
    }

    queryYz(queryData).then((res) => {
      let val = {patientInfo:res.patientInfo,
        yzDataList:res.yzDataList,
        queryRange:queryData.queryRange,
        statusFlag:queryData.statusFlag
      }
      emit('selectPatientInfo',val)
    })
  }else {
    let params = {inpatientNo:row.inpatientNo,
      admissTimes:row.admissTimes}
    getPatientBaseInfo(params).then((res) => {
      let  val = {patientInfo:res}
      emit('selectPatientInfo',val)
    })

  }
}


const handleSelectionChange = (val) => {
  if(props.selectFlag){
    selections.value = val
    emit('selectPatientInfo',val)
  }
}






onMounted(() => {
  getAllWards().then((res) => {
    if (res.length > 0) {
      allWards.value = res
      currentWard.value = wardsClearable ? '' : res[0].code
      fetchOverviews()
    }
  })
})
</script>

<style scoped lang="scss">

.el_table__current {
  background-color: #a7d3ff;
}

</style>
