<template>
  <div ref="containerRef" class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="备案编号" prop="regCode">
        <el-input v-model="queryParams.regCode" placeholder="请输入备案编号" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="核销编号" prop="writeCode">
        <el-input v-model="queryParams.writeCode" placeholder="请输入核销编号" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="备案时间" style="width: 308px">
        <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['hazard:bishazmajregwrit:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="hazard-bishazmajregwrit-record"></right-toolbar>
    </el-row>

    <div class="table-wrapper">
    <el-table :height="tableHeight" v-loading="loading" :data="recordList">
      <el-table-column label="备案编号" align="center" prop="regCode" v-if="columns.regCode.visible" :show-overflow-tooltip="true" />
      <el-table-column label="备案时间" align="center" prop="regTime" v-if="columns.regTime.visible" width="180" />
      <el-table-column label="核销编号" align="center" prop="writeCode" v-if="columns.writeCode.visible" :show-overflow-tooltip="true" />
      <el-table-column label="核销时间" align="center" prop="writeOffTime" v-if="columns.writeOffTime.visible" width="180" />
      <el-table-column label="状态" align="center" prop="hazStat" v-if="columns.hazStat.visible">
        <template #default="scope">
          <dict-tag :options="haz_reg_stat" :value="scope.row.hazStat" />
        </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="reviOpin" v-if="columns.reviOpin.visible" :show-overflow-tooltip="true" />
      <el-table-column label="记录人员" align="center" prop="recPers" v-if="columns.recPers.visible" />
      <el-table-column label="采集时间" align="center" prop="collTime" v-if="columns.collTime.visible" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template #default="scope">
          <el-tooltip content="详情" placement="top">
            <el-button link type="primary" icon="View" @click="handleDetail(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    </div>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 备案记录详情对话框 -->
    <el-dialog title="备案记录详情" v-model="detailOpen" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="备案编号">{{ detailRow.regCode }}</el-descriptions-item>
        <el-descriptions-item label="备案时间">{{ detailRow.regTime }}</el-descriptions-item>
        <el-descriptions-item label="核销编号">{{ detailRow.writeCode }}</el-descriptions-item>
        <el-descriptions-item label="核销时间">{{ detailRow.writeOffTime }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ detailRow.hazStat }}</el-descriptions-item>
        <el-descriptions-item label="记录人员">{{ detailRow.recPers }}</el-descriptions-item>
        <el-descriptions-item label="采集时间">{{ detailRow.collTime }}</el-descriptions-item>
        <el-descriptions-item label="审核意见" :span="2">{{ detailRow.reviOpin }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button @click="detailOpen = false">关 闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="HazardRecord">
import { listBisHazMajRegWrit } from "@/api/slaj/hazard"
import { useTableHeight } from '@/hooks/useTableHeight'

const { tableHeight, containerRef } = useTableHeight()

const { proxy } = getCurrentInstance()
const { haz_reg_stat } = proxy.useDict("haz_reg_stat")

const recordList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const dateRange = ref([])

const detailOpen = ref(false)
const detailRow = ref({})

const columns = ref({
  regCode: { label: '备案编号', visible: true },
  regTime: { label: '备案时间', visible: true },
  writeCode: { label: '核销编号', visible: true },
  writeOffTime: { label: '核销时间', visible: true },
  hazStat: { label: '状态', visible: true },
  reviOpin: { label: '审核意见', visible: true },
  recPers: { label: '记录人员', visible: true },
  collTime: { label: '采集时间', visible: true }
})

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    regCode: undefined,
    writeCode: undefined
  }
})

const { queryParams } = toRefs(data)

/** 查询备案记录列表 */
function getList() {
  loading.value = true
  listBisHazMajRegWrit(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false
    recordList.value = res.rows
    total.value = res.total
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 详情按钮操作 */
function handleDetail(row) {
  detailRow.value = row
  detailOpen.value = true
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("hazard/bishazmajregwrit/export", {
    ...queryParams.value
  }, `hazardRecord_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList()
})
</script>
