<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="hazName">
        <el-input v-model="queryParams.hazName" placeholder="请输入危险源名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="危险源等级" prop="hiddGrad">
        <el-select v-model="queryParams.hiddGrad" placeholder="请选择危险源等级" clearable style="width: 240px">
          <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="acciCause">
        <el-input v-model="queryParams.acciCause" 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="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['hazard:objhaz:add']">事故隐患登记</el-button>
      </el-col>
      <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="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['hazard:objhaz:remove']">删除</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-accihazard-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="acciCause" v-if="columns.acciCause.visible" :show-overflow-tooltip="true" />
      <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-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['hazard:objhaz:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="上报" placement="top">
            <el-button link type="primary" icon="Top" @click="handleReport(scope.row)"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['hazard:objhaz:remove']"></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="title" v-model="open" width="800px" append-to-body>
      <el-form :model="form" :rules="rules" ref="hazRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="危险源名称" prop="hazName">
              <el-input v-model="form.hazName" placeholder="请输入危险源名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="危险源等级" prop="hiddGrad">
              <el-select v-model="form.hiddGrad" placeholder="请选择危险源等级">
                <el-option v-for="dict in haz_grad" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="风险等级" prop="riskGrad">
              <el-select v-model="form.riskGrad" placeholder="请选择风险等级">
                <el-option v-for="dict in risk_grad" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="涉及部位" prop="proPart">
              <el-input v-model="form.proPart" placeholder="请输入涉及部位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="责任人" prop="supPers">
              <el-input v-model="form.supPers" placeholder="请输入责任人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="巡查人员" prop="patPers">
              <el-input v-model="form.patPers" placeholder="请输入巡查人员" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="部位经度" prop="partLong">
              <el-input v-model="form.partLong" placeholder="请输入部位经度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部位纬度" prop="partLat">
              <el-input v-model="form.partLat" placeholder="请输入部位纬度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否管控" prop="ifControl">
              <el-select v-model="form.ifControl" placeholder="请选择是否管控">
                <el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-input v-model="form.type" placeholder="请输入类型" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="事故原因" prop="acciCause">
              <el-input v-model="form.acciCause" type="textarea" placeholder="请输入事故原因" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="危害风险" prop="harmRisk">
              <el-input v-model="form.harmRisk" type="textarea" placeholder="请输入危害风险" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="note">
              <el-input v-model="form.note" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AcciHazard">
import { listObjHaz, getObjHaz, delObjHaz, addObjHaz, updateObjHaz } from "@/api/slaj/hazard"
import { useTableHeight } from '@/hooks/useTableHeight'
import { addTBusiHazardReport } from "@/api/slaj/hazard"

const { tableHeight, containerRef } = useTableHeight()

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

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

const columns = ref({
  hazName: { label: '事故隐患名称', visible: true },
  hiddGrad: { label: '危险源等级', visible: true },
  riskGrad: { label: '风险等级', visible: true },
  proPart: { label: '涉及部位', visible: true },
  acciCause: { label: '事故原因', visible: true },
  supPers: { label: '责任人', visible: true },
  collTime: { label: '采集时间', visible: true }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    hazName: undefined,
    hiddGrad: undefined,
    acciCause: undefined
  },
  rules: {
    hazName: [{ required: true, message: "危险源名称不能为空", trigger: "blur" }],
    hiddGrad: [{ required: true, message: "危险源等级不能为空", trigger: "change" }],
    acciCause: [{ required: true, message: "事故原因不能为空", trigger: "blur" }]
  }
})

const { queryParams, form, rules } = 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 handleAdd() {
  reset()
  open.value = true
  title.value = "事故隐患登记"
}

function handleUpdate(row) {
  reset()
  const guid = row.guid || ids.value
  getObjHaz(guid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改事故隐患"
  })
}

function handleReport(row) {
  proxy.$confirm('确认上报事故隐患"' + row.hazName + '"?', '提示', {
    type: 'warning'
  }).then(() => {
    addTBusiHazardReport({ hazIdenGuid: row.guid }).then(() => {
      proxy.$modal.msgSuccess("上报成功")
    })
  }).catch(() => {})
}

function submitForm() {
  proxy.$refs["hazRef"].validate(valid => {
    if (valid) {
      form.value.regOrgGuid = undefined
      if (form.value.guid != undefined) {
        updateObjHaz(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addObjHaz(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

function handleDelete(row) {
  const guids = row.guid || ids.value
  proxy.$modal.confirm('是否确认删除事故隐患编号为"' + guids + '"的数据项？').then(function () {
    return delObjHaz(guids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

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

function reset() {
  form.value = {
    guid: undefined,
    hazName: undefined,
    hiddGrad: undefined,
    riskGrad: undefined,
    proPart: undefined,
    partLong: undefined,
    partLat: undefined,
    supPers: undefined,
    patPers: undefined,
    acciCause: undefined,
    harmRisk: undefined,
    safePrec: undefined,
    emerPrec: undefined,
    ifControl: undefined,
    type: undefined,
    note: undefined,
    collTime: undefined,
    recPers: undefined
  }
  proxy.resetForm("hazRef")
}

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