<template>
  <el-container>
    <el-aside style="width: 190px;">
      <el-select v-model="currentWard" filterable style="width: 120px" @change="huoQuHuanZheLieBiaoClick">
        <el-option v-for="item in userWards" :key="item.code" :label="item.name" :value="item.code"></el-option>
      </el-select>
      <el-select v-model="inpatientNo" :filter-method="filterMethod" clearable filterable style="width: 120px;">
        <el-option v-for="item in cptHuanZheLieBiaoData" :key="item.inpatientNo" :label="item.name"
                   :value="item.inpatientNo"></el-option>
      </el-select>
      <el-checkbox v-model="woDeBingRen" style="margin-left: 10px" @change="woDeBingRenClick">我的病人</el-checkbox>
      <el-table
          :data="cptHuanZheLieBiaoData.slice((huanZheLieBiaoCurrentPage - 1) * 20,huanZheLieBiaoCurrentPage * 20 )"
          :height="tableHeight / 1.15" :row-style="tableBackColor" @row-click="tableClick">
        <el-table-column label="床位" prop="bedNo" width="40"></el-table-column>
        <el-table-column label="姓名" prop="name">
          <template #default="scope">
            <div :title="scope.row.zkWardName">
              <img :src="scope.row.sex === 1 ? maleIcon : femaleIcon"
                   class="sex-icon"/>
              {{ scope.row.name }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="住院号" prop="inpatientNo"></el-table-column>
        <el-table-column label="小科室" prop="zkWardName"></el-table-column>
      </el-table>
      <el-pagination
          :page-size="20"
          :total="huanZheLieBiaoData.length"
          layout="prev,pager,next,total"
          small
          @current-change="huanZheLeiBiaoCurrentChange"
      >
      </el-pagination>
    </el-aside>
  </el-container>
</template>

<script>

import {computed, onMounted, ref, watch} from "vue";
import store from "@/store";
import {huoQuGeRenPinLv, huoQuHuanZheLieBiao, huoQuHuanZheXinXi} from "@/api/zhu-yuan-yi-sheng/yi-zhu-lu-ru";

import {
  huanZhePinLvData,
  huanZheXinXi,
  selectWardCode
} from "@/views/hospitalization/zhu-yuan-yi-sheng/public-js/zhu-yuan-yi-sheng"
import maleIcon from '@/assets/male-icon.png'
import femaleIcon from '@/assets/female-icon.png'
import {getUserWards} from "@/api/case-front-sheet";
import {listNotBlank, stringIsBlank, stringNotBlank} from "@/utils/blank-utils";
import {clone} from "@/utils/clone";

export default {
  name: "HuanZheLieBiao",
  props: ['inpatientNo'],
  setup(props, cxt) {
    const tableHeight = computed(() => {
      return store.state.app.windowSize.h
    })
    // 选择的病房
    const currentWard = ref('')
    // 用户可以选择的 病房信息
    const userWards = ref([])
    // 住院号
    const inpatientNo = ref('')

    const cptHuanZheLieBiaoData = ref([])
    const huanZheLieBiaoData = ref([])
    const huanZheLieBiaoCurrentPage = ref(1)
    const woDeBingRen = ref(false)

    const huoQuHuanZheLieBiaoClick = () => {
      huoQuHuanZheLieBiao(currentWard.value).then((res) => {
        huanZheLieBiaoData.value = res
        cptHuanZheLieBiaoData.value = clone(res)
        selectWardCode.value = currentWard.value
      }).catch((e) => {
        huanZheLieBiaoData.value = []
        cptHuanZheLieBiaoData.value = []
      })
    }

    const huanZheLeiBiaoCurrentChange = (val) => {
      huanZheLieBiaoCurrentPage.value = val
    }

    const tableClick = (val) => {
      inpatientNo.value = val.inpatientNo
    }

    watch(() => inpatientNo.value, () => {
      if (stringIsBlank(inpatientNo.value)) {
        cptHuanZheLieBiaoData.value = huanZheLieBiaoData.value
      } else {
        huoQuHuanZheXinXi(inpatientNo.value).then((res) => {
          huanZheXinXi.value = res
          // 加载患者的频率
          huoQuGeRenPinLv(res.inpatientNo, res.admissTimes).then((res) => {
            huanZhePinLvData.value = res
          })
        })
      }
    })

    const woDeBingRenClick = () => {
      woDeBingRen.value ? filterMethod(store.state.user.info.code) : cptHuanZheLieBiaoData.value = huanZheLieBiaoData.value
    }


    const filterMethod = (val) => {
      cptHuanZheLieBiaoData.value = huanZheLieBiaoData.value.filter(item => {
        return item.inpatientNo.indexOf(val) > -1 || item.name.indexOf(val) > -1
            || item.consultPhysician.indexOf(val) > -1 || (stringNotBlank(item.referPhysician) && item.referPhysician.indexOf(val) > -1)
      })
    }

    const tableBackColor = ({row, column, rowIndex, columnIndex}) => {
      if (row.inpatientNo === inpatientNo.value) {
        return {
          backgroundColor: '#a7d3ff!important'
        }
      }
    }

    onMounted(() => {
      getUserWards().then((res) => {
        userWards.value = res
        if (listNotBlank(userWards.value)) {
          currentWard.value = userWards.value[0].code
          selectWardCode.value = currentWard.value
          huoQuHuanZheLieBiaoClick()
        }
      })
    })

    return {
      tableHeight,
      currentWard,
      userWards,
      huanZheLieBiaoData,
      huoQuHuanZheLieBiaoClick,
      huanZheLieBiaoCurrentPage,
      huanZheLeiBiaoCurrentChange,
      maleIcon,
      femaleIcon,
      woDeBingRen,
      cptHuanZheLieBiaoData,
      tableClick,
      inpatientNo,
      filterMethod,
      tableBackColor,
      woDeBingRenClick
    }
  }
}
</script>

<style scoped>

</style>
