<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="hazName">
        <el-input v-model="queryParams.hazName" placeholder="请输入危险源名称" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <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>
        <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:objhaz:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="hazard-supervise-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="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="proPart" v-if="columns.proPart.visible" :show-overflow-tooltip="true" />
      <el-table-column label="报送时间" align="center" prop="collTime" v-if="columns.collTime.visible" width="180" />
      <el-table-column label="监管单位" align="center" prop="supOrg" v-if="columns.supOrg.visible" />
      <el-table-column label="监管责任人" align="center" prop="supPers" v-if="columns.supPers.visible" />
      <el-table-column label="责任人电话" align="center" prop="offiTel" v-if="columns.offiTel.visible" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-tooltip content="设置监管责任人" placement="top" v-if="!scope.row.supPers">
            <el-button link type="warning" icon="User" @click="handleSupervise(scope.row)">设置监管</el-button>
          </el-tooltip>
          <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="superviseOpen" width="600px" append-to-body>
      <el-form :model="superviseForm" :rules="superviseRules" ref="superviseRef" label-width="120px">
        <el-alert title="正在对以下危险源设置监管责任人" type="info" :closable="false" show-icon style="margin-bottom: 16px" />
        <el-descriptions :column="1" border style="margin-bottom: 16px">
          <el-descriptions-item label="危险源名称">{{ currentHaz.hazName }}</el-descriptions-item>
          <el-descriptions-item label="危险源等级">
            <dict-tag :options="haz_grad" :value="currentHaz.hiddGrad" />
          </el-descriptions-item>
        </el-descriptions>
        <el-row>
          <el-col :span="12">
            <el-form-item label="监管责任人" prop="supPers">
              <el-input v-model="superviseForm.supPers" placeholder="请输入监管责任人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="责任人电话" prop="offiTel">
              <el-input v-model="superviseForm.offiTel" placeholder="请输入责任人电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="监管单位" prop="supOrg">
              <el-input v-model="superviseForm.supOrg" placeholder="请输入监管单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="监管时间" prop="supTime">
              <el-date-picker v-model="superviseForm.supTime" type="date" placeholder="请选择监管时间" value-format="YYYY-MM-DD" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitSupervise">确认监管</el-button>
          <el-button @click="superviseOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

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

const { tableHeight, containerRef } = useTableHeight()

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

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

const superviseOpen = ref(false)
const currentHaz = ref({})
const superviseForm = ref({})
const superviseRules = {
  supPers: [{ required: true, message: "监管责任人不能为空", trigger: "blur" }],
  offiTel: [{ required: true, message: "责任人电话不能为空", trigger: "blur" }]
}

const columns = ref({
  hazName: { label: '危险源名称', visible: true },
  hiddGrad: { label: '危险源等级', visible: true },
  riskGrad: { label: '风险等级', visible: true },
  proPart: { label: '涉及部位', visible: true },
  collTime: { label: '报送时间', visible: true },
  supOrg: { label: '监管单位', visible: true },
  supPers: { label: '监管责任人', visible: true },
  offiTel: { label: '责任人电话', visible: true }
})

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

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)
}

/** 打开监管对话框 */
function handleSupervise(row) {
  currentHaz.value = row
  superviseForm.value = {
    supPers: row.supPers || '',
    offiTel: row.offiTel || '',
    supOrg: row.supOrg || '',
    supTime: ''
  }
  superviseOpen.value = true
}

/** 提交监管 */
function submitSupervise() {
  proxy.$refs["superviseRef"].validate(valid => {
    if (!valid) return
    const data = {
      guid: currentHaz.value.guid,
      supPers: superviseForm.value.supPers,
      offiTel: superviseForm.value.offiTel,
      supOrg: superviseForm.value.supOrg,
      supTime: superviseForm.value.supTime,
      hazStat: '2'
    }
    updateObjHaz(data).then(res => {
      proxy.$modal.msgSuccess("监管设置成功")
      superviseOpen.value = false
      getList()
    })
  })
}

function handleDetail(row) {
  proxy.$router.push({ path: '/slaj/hazard/detail', query: { guid: row.guid } })
}

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

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