<template>
  <div>
  <el-skeleton :loading="loading" animated>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px">
      <el-form-item label="标本ID" prop="infoId">
        <el-input
          v-model="queryParams.infoId"
          placeholder="标本ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="中文名称" prop="chineseName">
        <el-input
          v-model="queryParams.chineseName"
          placeholder="请输入中文名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="标本编号" prop="specimenNumber">
        <el-input
          v-model="queryParams.specimenNumber"
          placeholder="请输入申请出库的标本编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="申请日期" prop="applicationDate">
        <el-date-picker
          v-model="queryParams.applicationDate"
          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-220px"
        />
      </el-form-item>
      <el-form-item label="审批状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择审批状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE. MUSEUMS_APPROAL_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="标本状态" prop="sampleStatus">-->
<!--        <el-select-->
<!--          v-model="queryParams.sampleStatus"-->
<!--          placeholder="请选择标本状态"-->
<!--          clearable-->
<!--          class="!w-240px"-->
<!--        >-->
<!--          <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="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['museums:specimen-outbound:export']"
        >
          <Icon icon="ep:download" 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 label="序号" align="center" prop="id" />
      <el-table-column label="标本ID" align="center" prop="infoId" />
      <el-table-column label="中文名称" align="center" prop="chineseName" />
      <el-table-column label="标本编号" align="center" prop="specimenNumber" />
      <el-table-column label="申请人(单位)" align="center" width="120px" prop="applicantName" />
      <el-table-column
        label="申请日期"
        align="center"
        prop="applicationDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="用途" align="center" prop="applicationUsage" />
      <el-table-column label="审批状态" align="center" prop="status"  >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MUSEUMS_APPROAL_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="出库时间"
        align="center"
        prop="outgoingTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="退还日期"
        align="center"
        prop="returnDate"
        :formatter="dateFormatter"
        width="180px"
      />
<!--      <el-table-column label="标本情况" align="center" prop="specimenCondition" />-->
<!--      <el-table-column label="标本状态" align="center" prop="sampleStatus" />-->
      <el-table-column label="操作" align="center" min-width="150px">
        <template #default="scope" >
          <el-button
            link
            type="primary"
            @click="detail(scope.row.id)"
          >
          查看
          </el-button>
          <el-button
            v-if=" scope.row.status !== 2 && scope.row.status !== 3  && scope.row.status !== 4"
            link
            type="primary"
            @click="handleOutbound(scope.row.id)"
          >
            出库
          </el-button>
          <el-button
            v-if="scope.row.status !== 2 && scope.row.status !== 4"
            link
            type="success"
            @click=" openForm(scope.row.id)"
          >
            回库
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：回库 -->
  <specimenReturnForm ref="formRef" @success="getList" />
  </el-skeleton>
  </div>
</template>

<script setup lang="ts">
import download from '@/utils/download'
import { dateFormatter } from '@/utils/formatTime'
import {defineComponent, ref,reactive} from "vue";
import { SpecimenOutboundApi, SpecimenOutboundVO } from '@/api/museums/specimenoutbound'
import specimenReturnForm from '@/views/museums/specimenoutbound/OutboundForm/specimenReturnForm.vue'
 defineComponent({name:'OutboundManagement'})
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SpecimenOutboundVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  infoId: undefined,
  chineseName: undefined,
  specimenNumber: undefined,
  applicantName: undefined,
  applicationDate: [],
  applicationUsage: undefined,
  attachments: undefined,
  status: undefined,
  remarks: undefined,
  processInstanceId: undefined,
  operator: undefined,
  outgoingTime: [],
  returner: undefined,
  receiver: undefined,
  returnDate: [],
  specimenCondition: undefined,
  createTime: [],
  sampleStatus: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SpecimenOutboundApi.getSpecimenOutboundPage(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 handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SpecimenOutboundApi.exportSpecimenOutbound(queryParams)
    download.excel(data, '标本出库回库信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}



/** 查看详情页面 **/
import { useRouter } from 'vue-router';
import {DICT_TYPE, getIntDictOptions} from "@/utils/dict";
const router = useRouter();
const detail=(dataId: number) => {
  router.push({
    name: 'OutboundDetail',
    query: {
      dataId: dataId,
    }
  })
}

/** 出库操作 */
const handleOutbound = async (id) => {
  try {
    // 出库的二次确认
    await message.OutboundConfirm()
    // 发起出库
    await  SpecimenOutboundApi.ConfirmSpecimenOutbound(id)
    message.success(t('出库成功'))
    // 刷新列表
    await getList()
  } catch (error) {
    message.error(t('出库失败'));
  }
};

/** 回库 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

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

</script>
