<template>
  <div ref="containerRef" class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="下级单位" prop="orgGuid">
        <el-input v-model="queryParams.orgGuid" placeholder="请输入下级单位名称" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="危险源等级" prop="hiddGrad">
        <el-select v-model="queryParams.hiddGrad" placeholder="请选择危险源等级" clearable style="width: 150px">
          <el-option v-for="dict in haz_grad" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="备案状态" prop="hazStat">
        <el-select v-model="queryParams.hazStat" placeholder="请选择备案状态" clearable style="width: 150px">
          <el-option v-for="dict in haz_reg_stat" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </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="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['hazard:objhaz:edit']">审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['hazard:objhaz:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="hazard-nextdep-index"></right-toolbar>
    </el-row>

    <div class="table-wrapper">
    <el-table :height="tableHeight" v-loading="loading" :data="hazList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="危险源名称" align="center" prop="hazName" v-if="columns.hazName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="所属单位" align="center" prop="orgGuid" v-if="columns.orgGuid.visible" :show-overflow-tooltip="true" />
      <el-table-column label="所属工程" align="center" prop="engGuid" v-if="columns.engGuid.visible" :show-overflow-tooltip="true" />
      <el-table-column label="危险源等级" align="center" prop="hiddGrad" v-if="columns.hiddGrad.visible">
        <template #default="scope">
          <dict-tag :options="haz_grad" :value="scope.row.hiddGrad" />
        </template>
      </el-table-column>
      <el-table-column label="风险等级" align="center" prop="riskGrad" v-if="columns.riskGrad.visible">
        <template #default="scope">
          <dict-tag :options="risk_grad" :value="scope.row.riskGrad" />
        </template>
      </el-table-column>
      <el-table-column label="危险源状态" align="center" prop="hazStat" v-if="columns.hazStat.visible">
        <template #default="scope">
          <dict-tag :options="haz_stat" :value="scope.row.hazStat" />
        </template>
      </el-table-column>
      <el-table-column label="备案状态" align="center" prop="regStat" v-if="columns.regStat.visible">
        <template #default="scope">
          <dict-tag :options="haz_reg_stat" :value="scope.row.regStat" />
        </template>
      </el-table-column>
      <el-table-column label="监管责任人" align="center" prop="supPers" v-if="columns.supPers.visible" />
      <el-table-column label="采集时间" align="center" prop="collTime" v-if="columns.collTime.visible" width="180" />
    </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="auditOpen" width="600px" append-to-body>
      <el-form :model="auditForm" :rules="auditRules" ref="auditRef" label-width="100px">
        <el-descriptions :column="1" border style="margin-bottom: 16px">
          <el-descriptions-item label="危险源名称">{{ currentRow.hazName }}</el-descriptions-item>
          <el-descriptions-item label="所属单位">{{ currentRow.orgGuid }}</el-descriptions-item>
          <el-descriptions-item label="危险源等级">
            <dict-tag :options="haz_grad" :value="currentRow.hiddGrad" />
          </el-descriptions-item>
          <el-descriptions-item label="当前状态">
            <dict-tag :options="haz_stat" :value="currentRow.hazStat" />
          </el-descriptions-item>
        </el-descriptions>
        <el-row>
          <el-col :span="12">
            <el-form-item label="审核结果" prop="auditResult">
              <el-select v-model="auditForm.auditResult" placeholder="请选择审核结果">
                <el-option label="备案通过" value="approved" />
                <el-option label="退回修改" value="rejected" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="监管责任人" prop="supPers">
              <el-input v-model="auditForm.supPers" placeholder="设置监管责任人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="审核意见" prop="opinion">
              <el-input v-model="auditForm.opinion" type="textarea" placeholder="请输入审核意见" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitAudit">确认审核</el-button>
          <el-button @click="auditOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="HazardNextDep">
import { listObjHaz, updateObjHaz } from "@/api/slaj/hazard"
import { useTableHeight } from '@/hooks/useTableHeight'

const { tableHeight, containerRef } = useTableHeight()

const { proxy } = getCurrentInstance()
const { haz_grad, haz_stat, risk_grad, haz_reg_stat } = proxy.useDict("haz_grad", "haz_stat", "risk_grad", "haz_reg_stat")

const hazList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const dateRange = ref([])

const auditOpen = ref(false)
const currentRow = ref({})
const auditForm = ref({})
const auditRules = {
  auditResult: [{ required: true, message: "请选择审核结果", trigger: "change" }]
}

const columns = ref({
  hazName: { label: '危险源名称', visible: true },
  orgGuid: { label: '所属单位', visible: true },
  engGuid: { label: '所属工程', visible: true },
  hiddGrad: { label: '危险源等级', visible: true },
  riskGrad: { label: '风险等级', visible: true },
  hazStat: { label: '危险源状态', visible: true },
  regStat: { label: '备案状态', visible: true },
  supPers: { label: '监管责任人', visible: true },
  collTime: { label: '采集时间', visible: true }
})

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orgGuid: undefined,
    hiddGrad: undefined,
    hazStat: undefined
  }
})

const { queryParams } = toRefs(data)

function getList() {
  loading.value = true
  listObjHaz(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false
    hazList.value = res.rows
    total.value = res.total
  })
}

function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery() {
  dateRange.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.guid)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

function handleUpdate(row) {
  currentRow.value = row
  auditForm.value = {
    auditResult: undefined,
    supPers: row.supPers || '',
    opinion: ''
  }
  auditOpen.value = true
}

function submitAudit() {
  proxy.$refs["auditRef"].validate(valid => {
    if (!valid) return
    const data = { guid: currentRow.value.guid }
    if (auditForm.value.auditResult === 'approved') {
      data.hazStat = '2'
      data.supPers = auditForm.value.supPers
    } else {
      data.hazStat = '0'
    }
    data.remark = auditForm.value.opinion
    updateObjHaz(data).then(res => {
      proxy.$modal.msgSuccess(auditForm.value.auditResult === 'approved' ? "备案审核通过" : "已退回")
      auditOpen.value = false
      getList()
    })
  })
}

function handleExport() {
  proxy.download("hazard/objhaz/export", {
    ...queryParams.value
  }, `nextDep_${new Date().getTime()}.xlsx`)
}

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