<template>
  <div class="awaitList-container">
    <div class="operate">
      <el-space>
        <el-input
          v-model="queryParams.searchKey"
          style="max-width: 600px"
          placeholder="请输入内容"
          class="input-with-select"
          @keydown.enter="handleQuery"
        >
        </el-input>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleQuery">查询</el-button>
      </el-space>
      <el-space>
        <!-- <el-button>读卡</el-button> -->
        <el-button type="primary" @click="doRegistering('no-file')">无档登记</el-button>
      </el-space>
    </div>
    <div class="table-container">
      <el-table
        :data="treatHospitalizedData"
        style="width: 100%"
        height="100%"
        show-overflow-tooltip
      >
        <el-table-column type="index" width="54" align="center" label="序号" />
        <el-table-column prop="patientName" align="center" label="患者姓名" />
        <el-table-column prop="genderEnum_enumText" label="性别" align="center" />
        <el-table-column prop="age" label="年龄" align="center" />
        <el-table-column prop="requestTime" align="center" label="申请时间" />
        <el-table-column prop="sourceName" align="center" label="申请来源" />
        <el-table-column prop="wardName" align="center" label="入院病区" />
        <el-table-column fixed="right" align="center" label="操作" width="88">
          <template #default="scope">
            <el-button type="primary" link @click="doRegistering(scope.row)"> 登记 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <PatientRegister
      v-model="patientRegisterVisible"
      :patientInfo="patient"
      :inHospitalInfo="inHospitalInfo"
      title="登记"
      :registrationType="registrationType"
      @okAct="patientRegisterOK"
      @cancelAct="cancelAct"
      :noFile="noFile"
    />
  </div>
</template>
<script setup>
import PatientRegister from './patientRegister.vue';
import { getAdmissionPage, getPatientBasicInfo, getInHospitalInfo } from './api';
const emits = defineEmits(['okList']);

const total = ref();
const patient = ref({});
const inHospitalInfo = ref({});
const noFile = ref(false);
const registrationType = ref(true);
const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  searchKey: undefined,
  registeredFlag: '0',
  searchKey: '',
});

const treatHospitalizedData = ref([]);
const doRegistering = (row) => {
  if (row == 'no-file') {
    patientRegisterVisible.value = true;
    noFile.value = true;
    inHospitalInfo.value = {};
  } else {
    getPatientBasicInfo(row.patientId).then((res) => {
      patient.value = res.data;
    });
    getInHospitalInfo(row.encounterId).then((res) => {
      inHospitalInfo.value = res.data;
      patientRegisterVisible.value = true;
      noFile.value = false;
    });
  }
};

const getList = () => {
  getAdmissionPage(queryParams.value).then((res) => {
    treatHospitalizedData.value = res.data.records;
    total.value = res.data.total;
  });
};

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}

function resetQuery() {
  queryParams.value = {
    pageNo: 1,
    pageSize: 10,
    searchKey: undefined,
    registeredFlag: '0',
    searchKey: '',
  };
  getList();
}
const patientRegisterVisible = ref(false);
const patientYbRegisterVisible = ref(false);

const patientRegisterOK = () => {
  patientRegisterVisible.value = false;
  queryParams.value.searchKey = '';
  emits('okList');
};

const cancelAct = () => {
  patientRegisterVisible.value = false;
};

getList();
</script>
<style lang="scss" scoped>
.awaitList-container {
  width: 100%;
  height: 100%;
  .operate {
    display: flex;
    justify-content: space-between;
    height: 40px;
    padding: 0 16px;
    align-items: center;
  }
  .table-container {
    padding: 8px 16px;
  }
}
</style>
