<template>
  <my-layout>
    <layout-item class="bg-white">
      <div class="p-15px flex items-center" @keyup.enter="search()">
        <input-filter v-model="name" label="姓名" placeholder="请输入姓名" :width="240" />
        <input-filter v-model="mobile" label="手机号" placeholder="请输入手机号" :width="240" />
        <el-button type="primary" class="ml-10px" @click="search">查询</el-button>
        <el-button class="ml-10px" @click="reset">重置</el-button>
      </div>
    </layout-item>
    <layout-main :scroll="false">
      <el-table v-loading="loading" border ref="tableRef" height="100%" :data="list">
        <el-common-column prop="blhno" label="病历号" />
        <el-common-column prop="name" label="姓名">
          <template #default="{ row }">
           <el-text type="primary" class="cursor-pointer" @click="showDetail(row)">{{ row.name }}</el-text>
          </template>
        </el-common-column>
        <el-common-column prop="genderAge" label="性别/年龄">
          <template #default="{ row: {age ,sex} }">
            {{ sex }} {{ age && sex ?' / ' : '' }} {{ age }}
          </template>
        </el-common-column>
        <el-common-column prop="mobile" label="手机号" />
        <el-common-column prop="idType" label="证件类型">
          <template #default>身份证</template>
        </el-common-column>
        <el-common-column prop="idCard" label="证件号" />
        <el-common-column prop="address" label="地址" />
      </el-table>
    </layout-main>
    <layout-item>
      <pagination :totalRow="total" :pageSize="pageSize" :currentPage="page" @handle-size-change="changeSize"
        @handle-current-change="changePage" />
    </layout-item>
  </my-layout>
  <archive-drawer :patient="patient || {}" :visible="archiveVisible" @close="archiveVisible = false" />

</template>

<script setup>
import { onMounted, ref, computed } from "vue";
import { storeToRefs } from "pinia";
import { ElMessage } from "element-plus";
import { useHlw } from "@/api/hlw";
import useElPagination from "@/hooks/useElPagination";
import { memberStore } from "@/store/member";
import { useImStore } from "@/store/im";
import { InputFilter } from "@/components/filter-bar";
import MyLayout, { LayoutMain, LayoutItem } from "@/components/layout";
import pagination from "@/components/pagination/pagination.vue";
import elCommonColumn from "@/components/el-common-column/el-common-column.vue";
import archiveDrawer from "@/components/archive-drawer/archive-drawer.vue";

const { list, page, pageSize, total, changePage, changeSize } = useElPagination(getList);
const props = defineProps({
  doctorView: {
    type: Boolean,
    default: false,
  },
});
const name = ref('');
const mobile = ref('');
const loading = ref(false);
const params = ref({});
const { memberInfo } = storeToRefs(memberStore());
const archiveVisible = ref(false);
const patient = ref({});

function showDetail(row) {
  patient.value = {...row};
  archiveVisible.value = true
}

function search() {
  params.value = {
    name: name.value.trim(),
    mobile: mobile.value.trim(),
  };
  if (props.doctorView && !memberInfo.value.doctorNo) {
    list.value = [];
    total.value = 0;
  } else if (props.doctorView) {
    params.value.doctorCode = memberInfo.value.doctorNo;
  }
  page.value = 1;
  getList();
}

function reset() {
  name.value = '';
  mobile.value = '';
  search();
}

async function getList() {
  loading.value = true;
  const { data, success, message } = await useHlw({ type: 'getOrderPatientList', page: page.value, pageSize: pageSize.value, ...params.value });
  list.value = data && Array.isArray(data.list) ? data.list : [];
  total.value = data && data.total >= 0 ? data.total : 0;
  if (!success) {
    ElMessage.error(message || '获取数据失败，请稍后重试！');
  }
  loading.value = false;
}

onMounted(() => {
  search();
});
</script>
