<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="物资柜" prop="loanFromId">
        <el-select
          v-model="queryParams.loanFromId"
          placeholder="请选择物资柜"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in cabinets"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物资名称" prop="materialsName">
        <el-input
          v-model="queryParams.materialsName"
          placeholder="请输入物资名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="归还人" prop="restitutionUserName">
        <el-input
          v-model="queryParams.restitutionUserName"
          placeholder="请输入归还人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </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.MATERIAL_EXCEPTION_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="异常发生时间">
        <el-date-picker
          v-model="createTime"
          type="datetimerange"
          value-format="YYYY-MM-DD HH:mm:ss"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />

      </el-form-item>
      <el-form-item label="异常解除时间">
        <el-date-picker
          v-model="handleTime"
          type="datetimerange"
          value-format="YYYY-MM-DD HH:mm:ss"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </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="exceptionList"
    >
      <el-table-column label="编号" align="center" prop="misplaceId" />
      <el-table-column label="物资柜" align="center" prop="loanFromName" />
      <el-table-column label="物资名称" align="center" prop="materialsName" />
      <el-table-column label="归还柜" align="center" prop="restitutionToName" />
      <el-table-column label="归还人" align="center" prop="restitutionUserName" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MATERIAL_EXCEPTION_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="异常发生时间" align="center" prop="occurTime" :formatter="dateFormatter" />
      <el-table-column label="异常解除时间" align="center" prop="handleTime" :formatter="dateFormatter" />
    </el-table>
    <Pagination
      v-model:total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import * as ExceptionApi from '@/api/material/exception'
import * as CabinetApi from '@/api/material/information'
import {returnExceptionPage} from "@/api/material/exception";

defineOptions({ name: 'MaterialException' })

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

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const exceptionList = ref([]) // 列表的数据
const cabinets = ref([]) // 物资柜选项
const createTime = ref<[Date, Date] | null>(null) // 异常发生时间
const handleTime = ref<[Date, Date] | null>(null) // 异常解除时间

// 查询参数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  exceptionType: '0',
  loanFromId: undefined,
  materialsName: undefined,
  restitutionUserName: undefined,
  status: undefined,
  startTime: undefined,
  endTime: undefined,
  handleStartTime: undefined,
  handleEndTime: undefined
})

const queryFormRef = ref() // 搜索的表单

/** 查询异常列表 */
const getList = async () => {
  loading.value = true
  try {
    // 处理时间范围
    if (createTime.value?.length === 2) {
      queryParams.startTime = dateFormatter(createTime.value[0])
      queryParams.endTime = dateFormatter(createTime.value[1])
    }
    if (handleTime.value?.length === 2) {
      queryParams.handleStartTime = dateFormatter(handleTime.value[0])
      queryParams.handleEndTime = dateFormatter(handleTime.value[1])
    }
    const data = await ExceptionApi.returnExceptionPage(queryParams)
    exceptionList.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 查询物资柜列表 */
const getCabinetList = async () => {
  const data = await CabinetApi.getMaterialsCabinets({ pageNo: 1, pageSize: -1 })
  if (data?.list) {
    cabinets.value = data.list.map(item => ({
      value: item.id,
      label: item.cabinetName
    }))
  }
}

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

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

// /** 清空时间范围 */
// const handleClearTime = (value: [Date, Date] | null) => {
//   if (!value) {
//     queryParams.startTime = undefined
//     queryParams.endTime = undefined
//   }
// }
//
// /** 清空处理时间范围 */
// const handleClearTime2 = (value: [Date, Date] | null) => {
//   if (!value) {
//     queryParams.handleStartTime = undefined
//     queryParams.handleEndTime = undefined
//   }
// }

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