<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-230px"
        >
          <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="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-230px"
        >
          <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="结束日期"
          @clear="handleClearTime(createTime)"
          class="!w-290px"
        />
      </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="结束日期"
          @clear="handleClearTime2(handleTime)"
          class="!w-290px"
        />
      </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="id" />
      <el-table-column label="物资柜" align="center" prop="exceptionCategoryName" />
      <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="raiseTime" />
      <el-table-column label="异常解除时间" align="center" prop="handleTime" />
    </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,getDateRange } from '@/utils/formatTime'
import * as doorExceptionApi from '@/api/material/doorException'
import * as CabinetApi from '@/api/material/information'
import {doorExceptionPage} from "@/api/material/doorException";

defineOptions({ name: 'MaterialDoorException' })

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 dateShortcuts = [
//   {
//     text: '最近一周',
//     value: () => {
//       const end = new Date()
//       const start = new Date()
//       start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
//       return [start, end]
//     }
//   },
//   {
//     text: '最近一个月',
//     value: () => {
//       const end = new Date()
//       const start = new Date()
//       start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
//       return [start, end]
//     }
//   },
//   {
//     text: '最近三个月',
//     value: () => {
//       const end = new Date()
//       const start = new Date()
//       start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
//       return [start, end]
//     }
//   }
// ]

// 查询参数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  exceptionType: '1',
  loanFromId: undefined,
  status: undefined,
  startRaiseTime: undefined,
  endRaiseTime: undefined,
  startHandleTime: undefined,
  endHandleTime: undefined
})

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

/** 查询异常列表 */
const getList = async () => {
  loading.value = true
  try {
    // 处理时间范围
    if (Array.isArray(createTime.value) && createTime.value?.length == 2) {
      const [startTime, endTime] = getDateRange(createTime.value[0], createTime.value[1])
      queryParams.startRaiseTime = startTime
      queryParams.endRaiseTime = endTime
    }
    if (Array.isArray(handleTime.value) && handleTime.value?.length == 2) {
      const [startTime, endTime] = getDateRange(handleTime.value[0], handleTime.value[1])
      queryParams.startHandleTime = startTime
      queryParams.endHandleTime = endTime
    }
    const data = await doorExceptionApi.doorExceptionPage(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
  queryParams.startRaiseTime = undefined
  queryParams.endRaiseTime = undefined
  queryParams.startHandleTime = undefined
  queryParams.endHandleTime = undefined
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 清空时间范围 */
const handleClearTime = (value: [Date, Date] | null) => {
  if (!value) {
    queryParams.startRaiseTime = undefined
    queryParams.endRaiseTime = undefined
  }
}

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

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