<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="hiddName">
        <el-input v-model="queryParams.hiddName" 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 hidd_grad" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="隐患状态" prop="hiddStat">
        <el-select v-model="queryParams.hiddStat" placeholder="请选择隐患状态" clearable style="width: 240px">
          <el-option v-for="dict in hidd_stat" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属标段" prop="tendGuid" v-if="showTendFilter">
        <el-input v-model="queryParams.tendGuid" placeholder="请输入标段" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </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="['hidd:objhidd:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['hidd:objhidd:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['hidd:objhidd:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Merge" @click="handleMergeQuery" v-hasPermi="['hidd:objhidd:query']">合并清单</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="Connection" @click="handleTendQuery" v-hasPermi="['hidd:objhidd:query']">标段查询</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Promotion" @click="handleSafeSeSave" v-hasPermi="['hidd:objhidd:edit']">安全检查保存</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="hidd-collect-index"></right-toolbar>
    </el-row>

    <div class="table-wrapper">
    <el-table :height="tableHeight" v-loading="loading" :data="objHiddList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="隐患名称" align="center" prop="hiddName" v-if="columns.hiddName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="隐患等级" align="center" prop="hiddGrad" v-if="columns.hiddGrad.visible">
        <template #default="scope">
          <dict-tag :options="hidd_grad" :value="scope.row.hiddGrad" />
        </template>
      </el-table-column>
      <el-table-column label="隐患类别" align="center" prop="hiddClas" v-if="columns.hiddClas.visible">
        <template #default="scope">
          <dict-tag :options="hidd_clas" :value="scope.row.hiddClas" />
        </template>
      </el-table-column>
      <el-table-column label="隐患状态" align="center" prop="hiddStat" v-if="columns.hiddStat.visible">
        <template #default="scope">
          <dict-tag :options="hidd_stat" :value="scope.row.hiddStat" />
        </template>
      </el-table-column>
      <el-table-column label="所属标段" align="center" prop="tendGuid" v-if="columns.tendGuid.visible" :show-overflow-tooltip="true" />
      <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" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['hidd:objhidd:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="详情" placement="top">
            <el-button link type="primary" icon="View" @click="handleDetail(scope.row)"></el-button>
          </el-tooltip>
          <el-tooltip content="销号" placement="top">
            <el-button link type="primary" icon="CircleCheck" @click="handleCancel(scope.row)" v-hasPermi="['hidd:objhidd:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['hidd:objhidd: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="objHiddRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="隐患名称" prop="hiddName">
              <el-input v-model="form.hiddName" 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 hidd_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="hiddClas">
              <el-select v-model="form.hiddClas" placeholder="请选择隐患类别">
                <el-option v-for="dict in hidd_clas" :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="tendGuid">
              <el-input v-model="form.tendGuid" placeholder="请输入所属标段" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="关联危险源" prop="hazGuid">
              <el-select v-model="form.hazGuid" placeholder="请选择关联危险源" filterable clearable>
                <el-option v-for="item in hazOptions" :key="item.guid" :label="item.hazName" :value="item.guid" />
              </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-col :span="12">
            <el-form-item label="隐患状态" prop="hiddStat">
              <el-select v-model="form.hiddStat" placeholder="请选择隐患状态">
                <el-option v-for="dict in hidd_stat" :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="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="24">
            <el-form-item label="隐患描述" prop="otlcolQiffFnvu">
              <el-input v-model="form.otlcolQiffFnvu" 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>

    <!-- 合并清单对话框 -->
    <el-dialog title="隐患合并清单" v-model="mergeOpen" width="800px" append-to-body>
      <el-form :model="mergeForm" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="合并条件">
              <el-select v-model="mergeForm.mergeType" placeholder="请选择合并条件">
                <el-option label="按部位合并" value="proPart" />
                <el-option label="按等级合并" value="hiddGrad" />
                <el-option label="按类别合并" value="hiddClas" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table v-loading="mergeLoading" :data="mergeList">
        <el-table-column label="隐患名称" align="center" prop="hiddName" :show-overflow-tooltip="true" />
        <el-table-column label="隐患等级" align="center" prop="hiddGrad">
          <template #default="scope">
            <dict-tag :options="hidd_grad" :value="scope.row.hiddGrad" />
          </template>
        </el-table-column>
        <el-table-column label="隐患状态" align="center" prop="hiddStat">
          <template #default="scope">
            <dict-tag :options="hidd_stat" :value="scope.row.hiddStat" />
          </template>
        </el-table-column>
        <el-table-column label="涉及部位" align="center" prop="proPart" :show-overflow-tooltip="true" />
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitMerge">查 询</el-button>
          <el-button @click="mergeOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 标段联合查询对话框 -->
    <el-dialog title="隐患+标段联合查询" v-model="tendOpen" width="800px" append-to-body>
      <el-form :model="tendForm" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="标段名称">
              <el-input v-model="tendForm.tendName" placeholder="请输入标段名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="隐患等级">
              <el-select v-model="tendForm.hiddGrad" placeholder="请选择隐患等级" clearable>
                <el-option v-for="dict in hidd_grad" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table v-loading="tendLoading" :data="tendList">
        <el-table-column label="隐患名称" align="center" prop="hiddName" :show-overflow-tooltip="true" />
        <el-table-column label="隐患等级" align="center" prop="hiddGrad">
          <template #default="scope">
            <dict-tag :options="hidd_grad" :value="scope.row.hiddGrad" />
          </template>
        </el-table-column>
        <el-table-column label="所属标段" align="center" prop="tendGuid" :show-overflow-tooltip="true" />
        <el-table-column label="隐患状态" align="center" prop="hiddStat">
          <template #default="scope">
            <dict-tag :options="hidd_stat" :value="scope.row.hiddStat" />
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitTend">查 询</el-button>
          <el-button @click="tendOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 安全检查隐患保存对话框 -->
    <el-dialog title="安全检查隐患保存" v-model="safeSeOpen" width="800px" append-to-body>
      <el-form :model="safeSeForm" :rules="safeSeRules" ref="safeSeRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="隐患名称" prop="hiddName">
              <el-input v-model="safeSeForm.hiddName" placeholder="请输入隐患名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="隐患等级" prop="hiddGrad">
              <el-select v-model="safeSeForm.hiddGrad" placeholder="请选择隐患等级">
                <el-option v-for="dict in hidd_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="hiddClas">
              <el-select v-model="safeSeForm.hiddClas" placeholder="请选择隐患类别">
                <el-option v-for="dict in hidd_clas" :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="safeSeForm.proPart" placeholder="请输入涉及部位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="隐患描述" prop="otlcolQiffFnvu">
              <el-input v-model="safeSeForm.otlcolQiffFnvu" type="textarea" placeholder="请输入隐患描述" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitSafeSe">确 定</el-button>
          <el-button @click="safeSeOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="HiddCollect">
import { listObjHidd, getObjHidd, delObjHidd, addObjHidd, updateObjHidd } from "@/api/slaj/hidd"
import { useTableHeight } from '@/hooks/useTableHeight'
import { delBisHiddRectProg } from "@/api/slaj/hidd"
import { listObjHaz } from "@/api/slaj/hazard"

const { tableHeight, containerRef } = useTableHeight()

const { proxy } = getCurrentInstance()
const { hidd_grad, hidd_stat, hidd_clas } = proxy.useDict("hidd_grad", "hidd_stat", "hidd_clas")

const hazOptions = ref([])

/** 查询危险源下拉选项 */
function getHazOptions() {
  listObjHaz({ pageNum: 1, pageSize: 1000 }).then(res => {
    hazOptions.value = res.rows
  })
}

const objHiddList = 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 showTendFilter = ref(false)

// 合并清单
const mergeOpen = ref(false)
const mergeLoading = ref(false)
const mergeList = ref([])
const mergeForm = ref({ mergeType: 'proPart' })

// 标段联合查询
const tendOpen = ref(false)
const tendLoading = ref(false)
const tendList = ref([])
const tendForm = ref({})

// 安全检查隐患保存
const safeSeOpen = ref(false)
const safeSeForm = ref({})
const safeSeRules = {
  hiddName: [{ required: true, message: "隐患名称不能为空", trigger: "blur" }],
  hiddGrad: [{ required: true, message: "隐患等级不能为空", trigger: "change" }],
  hiddStat: [{ required: true, message: "隐患状态不能为空", trigger: "change" }]
}

const columns = ref({
  hiddName: { label: '隐患名称', visible: true },
  hiddGrad: { label: '隐患等级', visible: true },
  hiddClas: { label: '隐患类别', visible: true },
  hiddStat: { label: '隐患状态', visible: true },
  tendGuid: { label: '所属标段', visible: false },
  proPart: { label: '涉及部位', visible: true },
  collTime: { label: '采集时间', visible: true }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    hiddName: undefined,
    hiddGrad: undefined,
    hiddStat: undefined,
    tendGuid: undefined
  },
  rules: {
    hiddName: [{ required: true, message: "隐患名称不能为空", trigger: "blur" }],
    hiddGrad: [{ required: true, message: "隐患等级不能为空", trigger: "change" }],
    hiddStat: [{ required: true, message: "隐患状态不能为空", trigger: "change" }]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询隐患列表 */
function getList() {
  loading.value = true
  listObjHidd(queryParams.value).then(res => {
    loading.value = false
    objHiddList.value = res.rows
    total.value = res.total
  })
}

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  queryParams.value.tendGuid = undefined
  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
  getObjHidd(guid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改隐患"
  })
}

/** 详情按钮操作 */
function handleDetail(row) {
  const guid = row.guid || ids.value
  proxy.$router.push({ path: '/slaj/hidd/detail', query: { guid: guid } })
}

/** 销号按钮 */
function handleCancel(row) {
  proxy.$modal.confirm('是否确认销号隐患"' + row.hiddName + '"？').then(function () {
    const data = {
      guid: row.guid,
      hiddStat: '2'
    }
    return updateObjHidd(data)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("销号成功")
  }).catch(() => {})
}

/** 合并清单查询 */
function handleMergeQuery() {
  mergeForm.value = { mergeType: 'proPart' }
  mergeList.value = []
  mergeOpen.value = true
}

/** 提交合并查询 */
function submitMerge() {
  mergeLoading.value = true
  listObjHidd({ mergeType: mergeForm.value.mergeType }).then(res => {
    mergeLoading.value = false
    mergeList.value = res.rows
  })
}

/** 标段联合查询 */
function handleTendQuery() {
  tendForm.value = {}
  tendList.value = []
  tendOpen.value = true
}

/** 提交标段查询 */
function submitTend() {
  tendLoading.value = true
  listObjHidd({ tendName: tendForm.value.tendName, hiddGrad: tendForm.value.hiddGrad }).then(res => {
    tendLoading.value = false
    tendList.value = res.rows
  })
}

/** 安全检查隐患保存 */
function handleSafeSeSave() {
  safeSeForm.value = {
    hiddName: undefined,
    hiddGrad: undefined,
    hiddClas: undefined,
    proPart: undefined,
    otlcolQiffFnvu: undefined
  }
  safeSeOpen.value = true
}

/** 提交安全检查保存 */
function submitSafeSe() {
  proxy.$refs["safeSeRef"].validate(valid => {
    if (valid) {
      const data = {
        ...safeSeForm.value,
        hiddStat: '0',
        hiddSour: '3'
      }
      addObjHidd(data).then(response => {
        proxy.$modal.msgSuccess("保存成功")
        safeSeOpen.value = false
        getList()
      })
    }
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["objHiddRef"].validate(valid => {
    if (valid) {
      if (form.value.guid != undefined) {
        updateObjHidd(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addObjHidd(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 delObjHidd(guids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 重置表单 */
function reset() {
  form.value = {
    guid: undefined,
    hiddName: undefined,
    engGuid: undefined,
    tendGuid: undefined,
    seGuid: undefined,
    orgGuid: undefined,
    hiddSour: undefined,
    hiddGrad: undefined,
    hiddClas: undefined,
    ifFound: undefined,
    proPart: undefined,
    partLong: undefined,
    partLat: undefined,
    otlcolQiffFnvu: undefined,
    inspRecGuid: undefined,
    seCheckItemGuid: undefined,
    hiddStat: undefined,
    hiddMergGuid: undefined,
    recOrgGuid: undefined,
    note: undefined,
    collTime: undefined,
    updTime: undefined,
    recPers: undefined,
    hiddsGuid: undefined,
    occuNum: undefined,
    sinsScheGuid: undefined,
    groupGuid: undefined,
    temp: undefined,
    toDate: undefined,
    dictGuid: undefined,
    hazGuid: undefined
  }
  proxy.resetForm("objHiddRef")
}

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