<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
    <el-form-item label="登记来源" prop="regSource">
        <el-select
          v-model="queryParams.regSource"
          placeholder="请选择来源"
          clearable
          class="!w-200px"
        >
        <el-option label="门诊" value="门诊" />
        <el-option label="住院" value="住院" />
        <el-option label="体检" value="体检" />
        </el-select>
      </el-form-item>

      <el-form-item label="患者姓名" prop="pName">
        <el-input
          v-model="queryParams.pname"
          placeholder="请输入患者姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-150px"
        />
      </el-form-item>

      <el-form-item label="登记时间" prop="regDate">
        <el-date-picker
          v-model="queryParams.regDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="分检时间" prop="sortDate">
        <el-date-picker
          v-model="queryParams.sortDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-200px"
        />
      </el-form-item>

      <el-form-item label="登记单号" prop="regId">
        <el-input
          v-model="queryParams.regId"
          placeholder="请输入登记单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-150px"
        />
      </el-form-item>
    
      <el-form-item label="检查单号" prop="examId">
        <el-input
          v-model="queryParams.examId"
          placeholder="请输入检查单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-150px"
        />
      </el-form-item>
      
     
     
     <!--  <el-form-item label="检查日期" prop="examDate">
        <el-date-picker
          v-model="queryParams.examDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item> -->
     <!--  <el-form-item label="检查项目名称" prop="examItemName">
        <el-input
          v-model="queryParams.examItemName"
          placeholder="请输入检查项目名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="检查状态" prop="examStatus">
        <el-select
          v-model="queryParams.examStatus"
          placeholder="请选择检查状态"
          clearable
          class="!w-200px"
        >
          <el-option label="未分检" value="未分检" /> 
          <el-option label="未检查" value="未检查" /> 
          <el-option label="已检查" value="已检查" /> 
          <el-option label="已放弃" value="已放弃" /> 
        </el-select>
      </el-form-item>

      <el-form-item label="执行科室" prop="deviceDepartment">
        <el-select
          v-model="queryParams.deviceDepartment"
          placeholder="请选择执行科室"
          clearable
          class="!w-200px"
        >
          <el-option label="影像科" 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-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['applyregistration:applyform:create']"
          v-if="false"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['applyregistration:applyform:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['applyregistration:applyform:export']"
        >
          <Icon icon="ep:plus" 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 type="selection" width="55" />
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="登记单号" align="center" prop="regId" />
      <el-table-column label="登记来源" align="center" prop="regSource"  width="100px"/>
      <el-table-column label="检查单号" align="center" prop="examId" />
      <el-table-column label="患者姓名" align="center" prop="pname" width="100px"/>
      <el-table-column label="性别" align="center" prop="gender" width="100px"/>
      <el-table-column
        label="出生日期"
        align="center"
        prop="birthday"
        :formatter="dateFormatter"
        width="160px"
        v-if="false"
      />
      <el-table-column
        label="检查日期"
        align="center"
        prop="examDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="检查项目名称" align="center" prop="examItemName" />
      <el-table-column label="检查设备ID" align="center" prop="deviceId" v-if="false"/>
      <el-table-column label="检查设备" align="center" prop="deviceName" />
      <el-table-column label="联系电话" align="center" prop="contactTel"  />
      <el-table-column
        label="登记时间"
        align="center"
        prop="regDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="分检时间"
        align="center"
        prop="sortDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="开单医生" align="center" prop="billgDoctor" width="100px"/>
      <el-table-column label="检查状态" align="center" prop="examStatus" width="100px" />
      <el-table-column label="开单医生科室" align="center" prop="billDoctorDepartment" :label-class-name="'no-wrap'" v-if="false" />
      <el-table-column
        label="此条记录的创建时间"
        align="center"
        prop="createDate"
        :formatter="dateFormatter"
        width="180px"
        v-if="false"
      />
      <el-table-column label="检查项目代码" align="center" prop="examItemCode" v-if="false"/>
      <el-table-column label="机构ID" align="center" prop="orgId" v-if="false" />
      <el-table-column label="分检医生" align="center" prop="sortDoctor" width="100px"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['applyregistration:applyform:update']"
          >
            分检
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['applyregistration:applyform:delete']"
          >
            作废
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['applyregistration:applyform:delete']"
          >
            导引单
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ApplyformForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ApplyformApi, ApplyformVO } from '@/api/applyregistration/applyform'
import ApplyformForm from './ApplyformForm.vue'
import { fa } from 'element-plus/es/locale';

/** 申请登记记录 列表 */
defineOptions({ name: 'Applyform' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ApplyformVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  regId: undefined,
  regSource: undefined,
  examId: undefined,
  pname: undefined,
  gender: undefined,
  birthday: undefined,
  examDate: [],
  examItemName: undefined,
  deviceId: undefined,
  deviceName: undefined,
  contactTel: undefined,
  regDate: [],
  sortDate: [],
  billgDoctor: undefined,
  examStatus: undefined,
  billDoctorDepartment: undefined,
  createDate: [],
  examItemCode: undefined,
  orgId: undefined,
  sortDoctor: undefined,
  deviceDepartment:undefined,
  departmentCode:undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ApplyformApi.getApplyformPage(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 = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ApplyformApi.deleteApplyform(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ApplyformApi.exportApplyform(queryParams)
    download.excel(data, '申请登记记录.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>