<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="异常分类" prop="exceptionCategory">
        <el-select
          v-model="queryParams.exceptionCategory"
          placeholder="请选择"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CLASSIFICATION_OF_EXCEPTIONS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="异常来源" prop="sourceName">
        <el-input
          v-model="queryParams.sourceName"
          placeholder="请输入异常来源"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="异常类型" prop="exceptionType">
        <el-select
          v-model="queryParams.exceptionType"
          placeholder="请选择"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.TYPE_OF_EXCEPTION)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="严重等级" prop="exceptionLevel">
        <el-select
          v-model="queryParams.exceptionLevel"
          placeholder="请选择"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.SEVERITY_LEVEL)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上报人" prop="raiserName">
        <el-input
          v-model="queryParams.raiserName"
          placeholder="请输入上报人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="异常发生时间">
        <el-date-picker
          v-model="raiseTime"
          type="datetimerange"
          value-format="YYYY-MM-DD HH:mm:ss"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="!w-240px"
          @clear="handleClearTime(raiseTime)"
        />
      </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="结束日期"
          class="!w-240px"
          @clear="handleClearTime2(handleTime)"
        />
      </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-radio-group v-model="tabPosition" class="mb-15px">
      <el-radio-button label="first">全部</el-radio-button>
      <el-radio-button label="second">待处理</el-radio-button>
      <el-radio-button label="third">已处理</el-radio-button>
    </el-radio-group>

    <el-table v-loading="loading" :data="exceptionList">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="异常分类" align="center" prop="exceptionCategory">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.CLASSIFICATION_OF_EXCEPTIONS"
            :value="scope.row.exceptionCategory"
          />
        </template>
      </el-table-column>
      <el-table-column label="异常来源" align="center" prop="sourceName" />
      <el-table-column label="异常类型" align="center" prop="exceptionTypeName" />
      <el-table-column label="严重等级" align="center" prop="exceptionLevelName" />
      <el-table-column label="上报人" align="center" prop="raiserName" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MANUAL_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-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="handleCheck(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      v-model:total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 详情弹窗 -->
  <ExceptionDetail ref="detailRef" @success="getList" />
</template>

<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter,getDateRange } from '@/utils/formatTime'
import * as ExceptionApi from '@/api/material/manualException'
import ExceptionDetail from './ExceptionDetail.vue'
import { listManualException } from '@/api/material/manualException'

defineOptions({ name: 'MaterialManualException' })

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

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const exceptionList = ref([]) // 列表的数据
const tabPosition = ref('first') // 当前选中的标签页
const raiseTime = 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,
  exceptionCategory: undefined,
  exceptionDescription: undefined,
  exceptionLevel: undefined,
  exceptionLevelName: undefined,
  exceptionType: undefined,
  exceptionTypeName: undefined,
  raiseTime: undefined,
  raiser: undefined,
  raiserName: undefined,
  sourceId: undefined,
  sourceName: undefined,
  startHandleTime: undefined,
  endHandleTime: undefined,
  startRaiseTime: undefined,
  endRaiseTime: undefined,
  status: undefined,
  isUpload:1 //区分人工异常喝物资柜门异常，1是人工
})

const queryFormRef = ref() // 搜索的表单
const detailRef = ref() // 详情弹窗

// 监听标签页切换
watch(tabPosition, (val) => {
  queryParams.status = val === 'second' ? '0' : val === 'third' ? '1' : undefined
  getList()
})

/** 查询异常列表 */
const getList = async () => {
  loading.value = true
  try {
    // 处理时间范围
    if (Array.isArray(raiseTime.value) && raiseTime.value?.length == 2) {
      const [startTime, endTime] = getDateRange(raiseTime.value[0], raiseTime.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 ExceptionApi.listManualException(queryParams)
    exceptionList.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

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

/** 重置按钮操作 */
const resetQuery = () => {
  raiseTime.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
  }
}

/** 查看详情 */
const handleCheck = (row: any) => {
  detailRef.value?.open(row.id)
}

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