<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="80px"
    >
      <el-form-item label="长者姓名" prop="elderName">
        <el-input @keyup.enter="handleQuery" v-model="queryParams.elderName" placeholder="请输入长者姓名" class="!w-240px" />
      </el-form-item>
      <el-form-item label="床位" prop="bedId">
        <SelectRoom v-model="queryParams.bedId" class="!w-240px" :tId="queryParams.tenantIds" />
      </el-form-item>
      <el-form-item label="护理等级" prop="nurseLevelId">
        <TgSelect v-model="queryParams.nurseLevelId" class="!w-240px">
          <el-option
            v-for="(n, i) in nurseLevelList"
            :key="i"
            :label="n.nurseLevelName"
            :value="n.id"
          />
        </TgSelect>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <TabBarBtn @add="openForm()" />
    <Table2
      v-loading="loading"
      :data="list"
      :columns="LifeCarePlanColumns"
      :queryParams="queryParams"
      @row-click="handleRowClick"
      @edit="openForm"
      @detail="(arg) => openForm(arg, true)"
    />

    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <Form ref="formRef" @success="getList" />
</template>
<script lang="ts" setup>
import { getnursingPlanPage, getNursingLevelPage } from '@/api/elderly/nursing'
import { LifeCarePlanColumns } from '../column'
import {useUserStore} from "@/store/modules/user";
import Form from './Form.vue'

defineOptions({ name: 'LifeCarePlan' })

const userStore = useUserStore()
const route = useRoute()

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  nurseLevelId: undefined,
  bedId: undefined,
  elderName: "",
  tenantIds: userStore.orgTenantId,
  type: route.path.indexOf('medical-care-plan') > -1 ? 2 : 1, // 2：医疗护理 1 生活护理
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await getnursingPlanPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (row: any = {}, isDetail: boolean = false) => {
  formRef.value.open(row.id, isDetail, false)
}

// 复制
const currItem = ref({})
const handleRowClick = (val) => {
  currItem.value = val
}

const nurseLevelList = ref<{ nurseLevelName: string; id: string | number }[]>([])
const getNurseLevelList = async () => {
  try {
    const data = await getNursingLevelPage({
      pageNum: 1,
      pageSize: 200
    })
    nurseLevelList.value = data.list
  } finally {
  }
}

/** 初始化 **/
onMounted(() => {
  if(route.query && route.query.elderName){
    queryParams.elderName = route.query.elderName as string
  }
  getList()
  getNurseLevelList()
})
</script>
