<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="120px"
    >
      <el-form-item label="持有人" prop="detectorId">
        <el-select
          v-model="queryParams.detectorId"
          placeholder="请选择持有人"
          clearable
          filterable
          @keyup.enter="handleQuery"
          class="!w-240px"
        >
          <el-option
            v-for="item in handDetectorStore.getHandDetectorList"
            :key="item.id"
            :label="item.name"
            :value="String(item.id)"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="持有人name" prop="name">
        <el-select
          v-model="queryParams.name"
          placeholder="请选择持有人"
          clearable
          filterable
          @keyup.enter="handleQuery"
          class="!w-240px"
        >
          <el-option
            v-for="item in handDetectorStore.getHandDetectorList"
            :key="item.id"
            :label="item.name"
            :value="String(item.name)"
          />
        </el-select>
      </el-form-item> -->

      <el-form-item label="设备编号" prop="sn">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <!-- 气体类型 -->
      <el-form-item label="气体类型" prop="gasType">
        <el-select
          v-model="queryParams.gasType"
          placeholder="请选择气体类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="item in handDetectorStore.getGasTypes"
            :key="item.id"
            :label="item.chemical"
            :value="String(item.chemical)"
          />
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="开始时间" prop="tAlarmStart">
        <el-date-picker
          v-model="queryParams.tAlarmStart"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="选择开始时间"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="结束时间" prop="tAlarmEnd">
        <el-date-picker
          v-model="queryParams.tAlarmEnd"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="选择结束时间"
          clearable
          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="item in getIntDictOptions(DICT_TYPE.HAND_DETECTOR_HANDLE_STATUS)"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          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>
        <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="['gas:hand-alarm:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button> -->
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['gas:hand-alarm:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="isEmpty(checkedIds)"
          @click="handleDeleteBatch"
          v-hasPermi="['gas:hand-alarm:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="持有人" align="center" prop="name">
        <!-- <template #default="scope">
          {{
            handDetectorStore.getHandDetectorList.find((item) => item.id === scope.row.detectorId)
              ?.name
          }}
        </template> -->
      </el-table-column>
      <el-table-column label="设备编号" align="center" prop="sn" />
      <el-table-column label="报警类型" align="center" prop="alarmLevel" width="90px">
        <template #default="">
          低电量预警
        </template>
      </el-table-column>
      <el-table-column
        label="气体类型"
        align="center"
        prop="gasType"
        width="80px"
      ></el-table-column>
 
      <el-table-column
        label="开始时间"
        align="center"
        prop="talarmStart"
        :formatter="dateFormatter"
        width="160px"
      />
      <el-table-column
        label="结束时间"
        align="center"
        prop="talarmEnd"
        :formatter="dateFormatter"
        width="160px"
      />
      <el-table-column label="状态" align="center" prop="status" width="80px">
        <template #default="scope">
          {{ getDictLabel(DICT_TYPE.HAND_DETECTOR_HANDLE_STATUS, scope.row.status) }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <!-- <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      /> -->
      <el-table-column label="操作" align="center" width="140px">
        <template #default="scope">

          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['gas:hand-alarm:update']"
          >
            编辑
          </el-button> 
          <!-- <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['gas:hand-alarm: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>

  <!-- 表单弹窗：添加/修改 -->
  <HandAlarmForm
    ref="formRef"
    @success="getList"
    :handDetector="handDetectorStore.getHandDetectorList"
    :gasTypes="handDetectorStore.getGasTypes"
    :alarmTypes="handDetectorStore.getAlarmTypes"
  />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { HandAlarmApi, HandAlarm } from '@/api/gas/handalarm'
import HandAlarmForm from './HandAlarmForm.vue'
import { useHandDetectorStore } from '@/store/modules/handDetector'
import { DICT_TYPE, getIntDictOptions, getDictLabel } from '@/utils/dict'
/** 手持探测器警报 列表 */
defineOptions({ name: 'HandAlarm' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const handDetectorStore = useHandDetectorStore()
const loading = ref(true) // 列表的加载中
const list = ref<HandAlarm[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  alarmType:2,// 报警类型 1：气体报警 2：电量报警 3：离线报警
  detectorId: undefined,
  name: undefined,
  sn: undefined,

  alarmLevel: undefined,
  gasType: undefined,
  unit: undefined,
  location: undefined,
  picX: undefined,
  picY: undefined,
  vAlarmFirst: undefined,
  vAlarmMaximum: undefined,
  tAlarmStart: undefined,
  tAlarmEnd: undefined,
  status: undefined,
  remark: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await HandAlarmApi.getHandAlarmPage(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)
}

/** 批量删除GAS手持探测器警报 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await HandAlarmApi.deleteHandAlarmList(checkedIds.value)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: HandAlarm[]) => {
  checkedIds.value = records.map((item) => item.id)
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await HandAlarmApi.exportHandAlarm(queryParams)
    download.excel(data, '手持探测器警报.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
  handDetectorStore.getAllHandDetector()
  handDetectorStore.getAllAlarmTypes()
  handDetectorStore.getAllGasTypes()
})
</script>
