<template>
  <ContentWrap>
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="80px"
    >
      <el-form-item label="长者姓名" prop="elderName">
        <el-input
          v-model="queryParams.elderName"
          class="!w-240px"
          clearable
          placeholder="请输入长者姓名"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请类型" prop="type">
        <el-select
          v-model="queryParams.type"
          class="!w-240px"
          clearable
          placeholder="请选择申请类型"
          @change="handleQuery"
        >
          <el-option
            v-for="option in approvalTypeOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
      </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>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column align="center" label="审批编号" prop="id" />
      <el-table-column align="center" label="长者姓名" prop="elderName">
        <template #default="scope">
          <el-link :underline="false" type="primary" @click="handleDetail(scope.row)">
            {{ scope.row.elderName || '-' }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column align="center" label="申请类型" prop="type">
        <template #default="scope">
          {{ getApprovalTypeLabel(scope.row.type) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="审批状态" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.APPROVAL_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="创建时间"
        prop="createTime"
        :formatter="dateFormatter"
      />
      <el-table-column align="center" fixed="right" label="操作">
        <template #default="scope">
          <el-button link type="primary" @click="handleDetail(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script lang="ts" setup>
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import * as NotifyMessageApi from '@/api/system/notify/message'
import { getTenantId } from '@/utils/auth'

defineOptions({ name: 'BpmAllProcessList' })

const { push } = useRouter()

const loading = ref(false)
const total = ref(0)
const list = ref<NotifyMessageApi.NotifyInstitutionApprovalRespVO[]>([])
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  elderName: undefined as string | undefined,
  type: undefined as number | undefined,
  tenantId:getTenantId()
})
const queryFormRef = ref()

const approvalTypeOptions = [
  { label: '申请入住', value: 1 },
  { label: '申请退住', value: 2 },
  { label: '外出登记', value: 3 },
  { label: '床位变更', value: 4 },
  { label: '护理变更', value: 5 },
  { label: '餐饮变更', value: 6 },
  { label: '价格变更', value: 7 },
  { label: '转院变更', value: 8 },
  { label: '入住办理', value: 9 },
  { label: '轮候', value: 10 }
]

const getApprovalTypeLabel = (value?: number) => {
  if (value === undefined || value === null) return '-'
  return approvalTypeOptions.find((o) => o.value === value)?.label || String(value)
}

const getList = async () => {
  loading.value = true
  try {
    const data = await NotifyMessageApi.getInstitutionApprovalPage(queryParams)
    list.value = data.list || []
    total.value = data.total || 0
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

const handleDetail = (row: NotifyMessageApi.NotifyInstitutionApprovalRespVO) => {
  if (!row?.processInstanceId) return
  push({ name: 'BpmProcessInstanceDetail', query: { id: row.processInstanceId } })
}

onMounted(() => {
  getList()
})
</script>
