<template>
  <div class="app-container">
    <el-tabs v-model="activeTab" type="border-card" @tab-change="handleTabChange">
      <el-tab-pane v-if="hasTabPermi2('eng')" label="工程危险源管理" name="eng">
        <el-form :model="queryParams" :inline="true" v-show="showSearch" label-width="80px">
          <el-form-item label="危险源名称" prop="hazName">
            <el-input v-model="queryParams.hazName" placeholder="请输入" clearable style="width: 180px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="所属工程" prop="engName">
            <el-input v-model="queryParams.engName" placeholder="请输入" clearable style="width: 180px" @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="riskGrad">
            <el-select v-model="queryParams.riskGrad" placeholder="请选择" clearable style="width: 150px">
              <el-option v-for="dict in risk_grad" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="管控状态" prop="ifControl">
            <el-select v-model="queryParams.ifControl" placeholder="请选择" clearable style="width: 120px">
              <el-option v-for="dict in sys_yes_no" :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-form-item>
          <el-form-item style="margin-left:auto;">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['hazar:eng:add']">新建</el-button>
            <el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['hazar:eng:remove']">删除</el-button>
          </el-form-item>
        </el-form>

        <div class="table-wrapper">
        <el-table :height="tableHeight" v-loading="loading" :data="list" @selection-change="handleSelectionChange" border style="width:100%" :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" type="index" width="60" />
          <el-table-column label="危险源名称" align="center" prop="hazName" min-width="160" :show-overflow-tooltip="true" />
          <el-table-column label="所属工程" align="center" prop="engName" min-width="150" :show-overflow-tooltip="true" />
          <el-table-column label="危险源等级" align="center" width="90">
            <template #default="scope">
              <dict-tag :options="haz_grad" :value="scope.row.hiddGrad" />
            </template>
          </el-table-column>
          <el-table-column label="风险等级" align="center" width="90">
            <template #default="scope">
              <dict-tag :options="risk_grad" :value="scope.row.riskGrad" />
            </template>
          </el-table-column>
          <el-table-column label="监督负责人" align="center" prop="supPers" width="110" />
          <el-table-column label="责任人联系电话" align="center" prop="offiTel" width="130" />
          <el-table-column label="是否立牌公告" align="center" width="110">
            <template #default="scope">
              <dict-tag :options="sys_yes_no" :value="scope.row.ifLiceNoti" />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="220" fixed="right">
            <template #default="scope">
              <el-button link type="info" @click="handleRecord(scope.row)" v-hasPermi="['hazar:eng:record']">填报记录</el-button>
              <el-button v-if="scope.row.hazStat === '0'" link type="warning" v-hasPermi="['hazar:eng:report']" @click="handleSubmit(scope.row)">报送</el-button>
              <el-tag v-else-if="scope.row.hazStat === '1'" type="warning" size="small">已报送</el-tag>
              <el-tag v-else-if="scope.row.hazStat === '2'" type="success" size="small">已监管</el-tag>
              <el-button link type="primary" icon="Edit" v-hasPermi="['hazar:eng:edit']" @click="handleUpdate(scope.row)" />
              <el-button link type="danger" icon="Delete" v-if="canDelete(scope.row)" @click="handleDelete(scope.row)" />
            </template>
          </el-table-column>
        </el-table>

        <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
        </div>
      </el-tab-pane>

      <el-tab-pane v-if="hasTabPermi2('tend')" label="标段危险源管理" name="tend">
        <el-form :model="queryParams2" :inline="true" v-show="showSearch" label-width="80px">
          <el-form-item label="危险源名称" prop="hazName">
            <el-input v-model="queryParams2.hazName" placeholder="请输入" clearable style="width: 180px" @keyup.enter="handleQuery2" />
          </el-form-item>
          <el-form-item label="危险源等级" prop="hiddGrad">
            <el-select v-model="queryParams2.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="riskGrad">
            <el-select v-model="queryParams2.riskGrad" placeholder="请选择" clearable style="width: 150px">
              <el-option v-for="dict in risk_grad" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="选择参建单位" prop="orgName">
            <el-input v-model="queryParams2.orgName" placeholder="请输入" clearable style="width: 180px" @keyup.enter="handleQuery2" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery2">搜索</el-button>
          </el-form-item>
          <el-form-item style="margin-left:auto;">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['hazar:tend:add']">新建</el-button>
            <el-button type="warning" plain icon="Download" @click="handleExport2">导出</el-button>
            <el-button type="danger" plain icon="Delete" :disabled="multiple2" @click="handleDelete2()" v-hasPermi="['hazar:tend:remove']">删除</el-button>
          </el-form-item>
        </el-form>

        <div class="table-wrapper">
        <el-table :height="tableHeight" v-loading="loading2" :data="list2" @selection-change="handleSelectionChange2" border style="width:100%" :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" type="index" width="60" />
          <el-table-column label="危险源名称" align="center" prop="hazName" min-width="160" :show-overflow-tooltip="true" />
          <el-table-column label="编码" align="center" prop="hazCode" width="160" :show-overflow-tooltip="true" />
          <el-table-column label="所属工程" align="center" prop="engName" min-width="140" :show-overflow-tooltip="true" />
          <el-table-column label="所属标段" align="center" prop="tendName" min-width="140" :show-overflow-tooltip="true" />
          <el-table-column label="危险源等级" align="center" width="90">
            <template #default="scope">
              <dict-tag :options="haz_grad" :value="scope.row.hiddGrad" />
            </template>
          </el-table-column>
          <el-table-column label="风险等级" align="center" width="90">
            <template #default="scope">
              <dict-tag :options="risk_grad" :value="scope.row.riskGrad" />
            </template>
          </el-table-column>
          <el-table-column label="监管负责人" align="center" prop="supPers" width="110" />
          <el-table-column label="责任人联系电话" align="center" prop="offiTel" width="130" />
          <el-table-column label="是否立牌公告" align="center" width="110">
            <template #default="scope">
              <dict-tag :options="sys_yes_no" :value="scope.row.ifLiceNoti" />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="220" fixed="right">
            <template #default="scope">
              <el-button link type="info" @click="handleRecord(scope.row)" v-hasPermi="['hazar:tend:record']">填报记录</el-button>
              <el-button link type="primary" icon="Edit" v-hasPermi="['hazar:tend:edit']" @click="handleUpdate2(scope.row)" />
              <el-button link type="danger" icon="Delete" v-if="canDelete(scope.row)" @click="handleDelete2(scope.row)" />
              <el-button v-if="scope.row.hazStat === '0'" link type="warning" v-hasPermi="['hazar:tend:report']" @click="handleSubmit(scope.row)">报送</el-button>
              <el-tag v-else-if="scope.row.hazStat === '1'" type="warning" size="small">已报送</el-tag>
              <el-tag v-else-if="scope.row.hazStat === '2'" type="success" size="small">已监管</el-tag>
            </template>
          </el-table-column>
        </el-table>
        </div>

        <pagination :total="total2" v-model:page="queryParams2.pageNum" v-model:limit="queryParams2.pageSize" @pagination="getList2" />
      </el-tab-pane>

      <el-tab-pane v-if="hasTabPermi2('chem')" label="危险物品登记" name="chem">
        <el-form :model="queryParams3" :inline="true" v-show="showSearch" label-width="90px">
          <el-form-item label="危险物品名称" prop="hachName">
            <el-input v-model="queryParams3.hachName" placeholder="请输入" clearable style="width: 200px" @keyup.enter="handleQuery3" />
          </el-form-item>
          <el-form-item label="选择参建单位" prop="orgName">
            <el-input v-model="queryParams3.orgName" placeholder="请输入" clearable style="width: 180px" @keyup.enter="handleQuery3" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery3">搜索</el-button>
          </el-form-item>
          <el-form-item style="margin-left:auto;">
            <el-button type="primary" plain icon="Plus" @click="handleChemAdd" v-hasPermi="['hazar:chem:add']">新建</el-button>
            <el-button type="warning" plain icon="Download" @click="handleExport3">导出</el-button>
            <el-button type="danger" plain icon="Delete" :disabled="multiple3" @click="handleDelete3()" v-hasPermi="['hazar:chem:remove']">删除</el-button>
          </el-form-item>
        </el-form>

        <div class="table-wrapper">
        <el-table :height="tableHeight" v-loading="loading3" :data="list3" @selection-change="handleSelectionChange3" border style="width:100%" :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" type="index" width="60" />
          <el-table-column label="危险物品名称" align="center" prop="hachName" min-width="140" :show-overflow-tooltip="true" />
          <el-table-column label="化学名" align="center" prop="chemName" width="120" :show-overflow-tooltip="true" />
          <el-table-column label="俗名" align="center" prop="locName" width="120" :show-overflow-tooltip="true" />
          <el-table-column label="剧毒物品编号" align="center" prop="toxicSubsNum" width="120" />
          <el-table-column label="危规号" align="center" prop="dangNum" width="100" />
          <el-table-column label="当前储量" align="center" prop="currRese" width="90" />
          <el-table-column label="存储包装方式" align="center" prop="storPack" width="110" />
          <el-table-column label="过期处理方式" align="center" prop="ovduProcMode" width="110" :show-overflow-tooltip="true" />
          <el-table-column label="操作" align="center" width="180" fixed="right">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate3(scope.row)" v-hasPermi="['hazar:chem:edit']" />
              <el-button link type="danger" icon="Delete" @click="handleDelete3(scope.row)" v-hasPermi="['hazar:chem:remove']" />
              <el-button link type="info" @click="handleChemRecord(scope.row)" v-hasPermi="['hazar:chem:record']">出入库记录</el-button>
            </template>
          </el-table-column>
        </el-table>
        </div>

        <pagination :total="total3" v-model:page="queryParams3.pageNum" v-model:limit="queryParams3.pageSize" @pagination="getList3" />
      </el-tab-pane>
    </el-tabs>

    <el-dialog :title="formTitle" v-model="formOpen" width="650px" append-to-body>
      <el-form :model="form" ref="formRef" label-width="100px">
        <el-form-item label="危险源名称" prop="hazName" :rules="[{ required: true, message: '请输入危险源名称', trigger: 'blur' }]">
          <el-input v-model="form.hazName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="所属工程" prop="engGuid" :rules="[{ required: true, message: '请选择所属工程', trigger: 'change' }]">
          <el-select v-model="form.engGuid" placeholder="请选择" style="width:100%" @change="onEngChange">
            <el-option v-for="item in engList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属标段" prop="tendName">
          <el-input v-model="form.tendName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="危险源等级" prop="hiddGrad" :rules="[{ required: true, message: '请选择危险源等级', trigger: 'change' }]">
          <el-select v-model="form.hiddGrad" placeholder="请选择" style="width:100%">
            <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="riskGrad">
          <el-select v-model="form.riskGrad" placeholder="请选择" style="width:100%">
            <el-option v-for="dict in risk_grad" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input v-model="form.type" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="监督负责人" prop="supPers">
          <el-input v-model="form.supPers" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="责任人电话" prop="offiTel">
          <el-input v-model="form.offiTel" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="是否立牌公告" prop="ifLiceNoti">
          <el-select v-model="form.ifLiceNoti" placeholder="请选择" style="width:100%">
            <el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="涉及部位" prop="proPart">
          <el-input v-model="form.proPart" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" type="textarea" :rows="2" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm" :loading="formLoading">保 存</el-button>
        <el-button @click="formOpen = false">取 消</el-button>
      </template>
    </el-dialog>

    <el-dialog title="危险物品登记" v-model="chemFormOpen" width="900px" append-to-body>
      <el-form :model="chemForm" ref="chemFormRef" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="危险物品名称" prop="hachName" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
              <el-input v-model="chemForm.hachName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="化学名" prop="chemName" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
              <el-input v-model="chemForm.chemName" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="俗名" prop="locName" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
              <el-input v-model="chemForm.locName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="剧毒物品编号" prop="toxicSubsNum" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
              <el-input v-model="chemForm.toxicSubsNum" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="危规号" prop="dangNum" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
              <el-input v-model="chemForm.dangNum" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当前储量" prop="currRese" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
              <el-input v-model="chemForm.currRese" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="存储包装方式" prop="storPack" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
              <el-input v-model="chemForm.storPack" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="过期处理方式" prop="ovduProcMode" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
              <el-input v-model="chemForm.ovduProcMode" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitChemForm">保 存</el-button>
        <el-button @click="chemFormOpen = false">取 消</el-button>
      </template>
    </el-dialog>

    <el-dialog title="出入库记录" v-model="storageOpen" width="950px" append-to-body>
      <el-tabs v-model="storageTab" @tab-change="loadStorList">
        <el-tab-pane label="入库记录" name="in">
          <el-form :model="storQuery" :inline="true" label-width="70px" class="mb8">
            <el-form-item label="供货商">
              <el-input v-model="storQuery.supplier" placeholder="请输入" clearable style="width: 160px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="loadStorList">搜索</el-button>
            </el-form-item>
            <el-form-item style="margin-left:auto;">
              <el-button type="primary" plain icon="Plus" @click="handleStorAdd">新建</el-button>
            </el-form-item>
          </el-form>
          <el-table :data="storList" border size="small" style="width:100%" height="300">
            <el-table-column label="序号" align="center" type="index" width="50" />
            <el-table-column label="入库日期" align="center" prop="storDate" width="100" />
            <el-table-column label="入库类型" align="center" prop="storType" min-width="90" />
            <el-table-column label="供货商" align="center" prop="supplier" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="数量" align="center" prop="quantity" width="70" />
            <el-table-column label="经办人" align="center" prop="handle" min-width="90" />
            <el-table-column label="验收人" align="center" prop="acceptor" min-width="90" />
            <el-table-column label="存放地点" align="center" prop="storLoc" min-width="130" :show-overflow-tooltip="true" />
            <el-table-column label="操作" align="center" width="80">
              <template #default="scope">
                <el-button link type="primary" icon="Edit" @click="handleStorUpdate(scope.row)" />
                <el-button link type="danger" icon="Delete" @click="handleStorDelete(scope.row)" />
              </template>
            </el-table-column>
          </el-table>
          <pagination :total="storTotal" v-model:page="storQuery.pageNum" v-model:limit="storQuery.pageSize" @pagination="loadStorList" layout="total, prev, pager, next" />
        </el-tab-pane>
        <el-tab-pane label="出库记录" name="out">
          <el-form :model="delQuery" :inline="true" label-width="70px" class="mb8">
            <el-form-item label="出库类型">
              <el-input v-model="delQuery.delType" placeholder="请输入" clearable style="width: 160px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="loadDelList">搜索</el-button>
            </el-form-item>
            <el-form-item style="margin-left:auto;">
              <el-button type="primary" plain icon="Plus" @click="handleDelAdd">新建</el-button>
            </el-form-item>
          </el-form>
          <el-table :data="delList" border size="small" style="width:100%" height="300">
            <el-table-column label="序号" align="center" type="index" width="50" />
            <el-table-column label="出库日期" align="center" prop="delTime" width="100" />
            <el-table-column label="出库类型" align="center" prop="delType" min-width="90" />
            <el-table-column label="数量" align="center" prop="quantity" width="70" />
            <el-table-column label="经办人" align="center" prop="handle" min-width="100" />
            <el-table-column label="审核人" align="center" prop="auditor" min-width="100" />
            <el-table-column label="操作" align="center" width="80">
              <template #default="scope">
                <el-button link type="primary" icon="Edit" @click="handleDelUpdate(scope.row)" />
                <el-button link type="danger" icon="Delete" @click="handleDelSingleDelete(scope.row)" />
              </template>
            </el-table-column>
          </el-table>
          <pagination :total="delTotal" v-model:page="delQuery.pageNum" v-model:limit="delQuery.pageSize" @pagination="loadDelList" layout="total, prev, pager, next" />
        </el-tab-pane>
      </el-tabs>

      <el-dialog title="入库记录" v-model="storFormOpen" width="500px" append-to-body>
        <el-form :model="storForm" ref="storFormRef" label-width="90px">
          <el-form-item label="入库日期" prop="storDate" :rules="[{ required: true, message: '请选择', trigger: 'change' }]">
            <el-date-picker v-model="storForm.storDate" type="date" placeholder="请选择" value-format="YYYY-MM-DD" style="width:100%" />
          </el-form-item>
          <el-form-item label="入库类型" prop="storType">
            <el-input v-model="storForm.storType" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="供货商" prop="supplier" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
            <el-input v-model="storForm.supplier" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="数量" prop="quantity">
            <el-input v-model="storForm.quantity" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="经办人" prop="handle">
            <el-input v-model="storForm.handle" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="验收人" prop="acceptor">
            <el-input v-model="storForm.acceptor" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="存放地点" prop="storLoc">
            <el-input v-model="storForm.storLoc" placeholder="请输入" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button type="primary" @click="submitStorForm">保 存</el-button>
          <el-button @click="storFormOpen = false">取 消</el-button>
        </template>
      </el-dialog>

      <el-dialog title="出库记录" v-model="delFormOpen" width="500px" append-to-body>
        <el-form :model="delForm" ref="delFormRef" label-width="80px">
          <el-form-item label="出库日期" prop="delTime" :rules="[{ required: true, message: '请选择', trigger: 'change' }]">
            <el-date-picker v-model="delForm.delTime" type="date" placeholder="请选择" value-format="YYYY-MM-DD" style="width:100%" />
          </el-form-item>
          <el-form-item label="出库类型" prop="delType">
            <el-input v-model="delForm.delType" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="数量" prop="quantity">
            <el-input v-model="delForm.quantity" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="经办人" prop="handle">
            <el-input v-model="delForm.handle" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="审核人" prop="auditor">
            <el-input v-model="delForm.auditor" placeholder="请输入" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button type="primary" @click="submitDelForm">保 存</el-button>
          <el-button @click="delFormOpen = false">取 消</el-button>
        </template>
      </el-dialog>
    </el-dialog>

    <el-dialog title="日常监控记录" v-model="recordOpen" width="850px" append-to-body>
      <el-table :data="recList" @selection-change="handleRecSelectionChange" border size="small">
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column label="巡视日期" align="center" prop="patTime" width="120" />
        <el-table-column label="监控巡视人" align="center" prop="patPers" width="120" />
        <el-table-column label="巡视发现问题" align="center" prop="probFound" min-width="160" :show-overflow-tooltip="true" />
        <el-table-column label="监控防范措施" align="center" prop="treaMeas" min-width="160" :show-overflow-tooltip="true" />
        <el-table-column label="操作" align="center" width="80">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleRecUpdate(scope.row)" />
            <el-button link type="danger" icon="Delete" @click="handleRecSingleDelete(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
      <pagination :total="recTotal" v-model:page="recQuery.pageNum" v-model:limit="recQuery.pageSize" @pagination="loadRecList" layout="total, prev, pager, next" />

      <el-dialog title="监控记录" v-model="recFormOpen" width="500px" append-to-body>
        <el-form :model="recForm" ref="recFormRef" label-width="100px">
          <el-form-item label="巡视日期" prop="patTime" :rules="[{ required: true, message: '请选择巡视日期', trigger: 'change' }]">
            <el-date-picker v-model="recForm.patTime" type="date" placeholder="请选择" value-format="YYYY-MM-DD" style="width:100%" />
          </el-form-item>
          <el-form-item label="监控巡视人" prop="patPers" :rules="[{ required: true, message: '请输入监控巡视人', trigger: 'blur' }]">
            <el-input v-model="recForm.patPers" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="巡视发现问题" prop="probFound">
            <el-input v-model="recForm.probFound" type="textarea" :rows="2" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="监控防范措施" prop="treaMeas">
            <el-input v-model="recForm.treaMeas" type="textarea" :rows="2" placeholder="请输入" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button type="primary" @click="submitRecForm">保 存</el-button>
          <el-button @click="recFormOpen = false">取 消</el-button>
        </template>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script setup name="HazardManage">
import { listObjHaz, delObjHaz, addObjHaz, updateObjHaz } from "@/api/slaj/hazard"
import { listObjHach, addObjHach, updateObjHach, delObjHach } from "@/api/slaj/hazard"
import { listAttEngBase } from "@/api/slaj/hazard"
import { listBisHazPatRec, addBisHazPatRec, updateBisHazPatRec, delBisHazPatRec } from "@/api/slaj/hazard"
import { listBisHachStor, addBisHachStor, updateBisHachStor, delBisHachStor } from "@/api/slaj/hazard"
import { listBisHachDel, addBisHachDel, updateBisHachDel, delBisHachDel } from "@/api/slaj/hazard"
import useUserStore from '@/store/modules/user'

const userStore = useUserStore()

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

const tabPermiMap2 = { eng: 'hazar:tab:manageEng', tend: 'hazar:tab:manageTend', chem: 'hazar:tab:manageChem' }
function hasTabPermi2(name) {
  const p = tabPermiMap2[name]
  return !p || proxy.$auth.hasPermi(p)
}
const activeTab = ref(Object.keys(tabPermiMap2).find(k => hasTabPermi2(k)) || 'eng')

function canDelete(row) {
  if (proxy.$auth.hasPermi('hazar:objhaz:remove')) return true
  if (proxy.$auth.hasPermi('hazar:objhaz:removeDraft') && row.hazStat === '0') return true
  return false
}
const showSearch = ref(true)
const tableHeight = 'calc(100vh - 240px)'

const list = ref([]); const loading = ref(false); const total = ref(0)
const ids = ref([]); const single = ref(true); const multiple = ref(true)
const formOpen = ref(false); const formTitle = ref(''); const formLoading = ref(false)
const form = ref({}); const formRef = ref(null); const engList = ref([])

const recordOpen = ref(false); const recHazGuid = ref('')
const recList = ref([]); const recIds = ref([]); const recSingle = ref(true); const recMultiple = ref(true)
const recTotal = ref(0); const recQuery = reactive({ pageNum: 1, pageSize: 10 })
const recFormOpen = ref(false); const recForm = ref({}); const recFormRef = ref(null)

const chemFormOpen = ref(false); const chemForm = ref({}); const chemFormRef = ref(null)

const storageOpen = ref(false); const storageHachGuid = ref('')
const storList = ref([]); const delList = ref([]); const storageTab = ref('in')
const storQuery = reactive({ pageNum: 1, pageSize: 10, supplier: undefined })
const storTotal = ref(0); const delTotal = ref(0); const delQuery = reactive({ pageNum: 1, pageSize: 10 })
const storFormOpen = ref(false); const storForm = ref({}); const storFormRef = ref(null)
const delFormOpen = ref(false); const delForm = ref({}); const delFormRef = ref(null)
const list2 = ref([]); const loading2 = ref(false); const total2 = ref(0)
const ids2 = ref([]); const single2 = ref(true); const multiple2 = ref(true)
const list3 = ref([]); const loading3 = ref(false); const total3 = ref(0)
const ids3 = ref([]); const single3 = ref(true); const multiple3 = ref(true)

const queryParams = reactive({ pageNum: 1, pageSize: 10, hazName: undefined, engName: undefined, hiddGrad: undefined, riskGrad: undefined, ifControl: undefined })
const queryParams2 = reactive({ pageNum: 1, pageSize: 10, hazName: undefined, hiddGrad: undefined, riskGrad: undefined, orgName: undefined })
const queryParams3 = reactive({ pageNum: 1, pageSize: 10, hachName: undefined, orgName: undefined })

function getList() {
  loading.value = true
  listObjHaz({ ...queryParams }).then(res => {
    list.value = res.rows || []; total.value = res.total || 0; loading.value = false
  }).catch(() => { loading.value = false })
}
function getList2() {
  loading2.value = true
  listObjHaz({ ...queryParams2 }).then(res => {
    list2.value = res.rows || []; total2.value = res.total || 0; loading2.value = false
  }).catch(() => { loading2.value = false })
}
function handleSelectionChange2(selection) { ids2.value = selection.map(item => item.guid); single2.value = selection.length !== 1; multiple2.value = !selection.length }
function getList3() {
  loading3.value = true
  listObjHach({ ...queryParams3 }).then(res => {
    list3.value = res.rows || []; total3.value = res.total || 0; loading3.value = false
  }).catch(() => { loading3.value = false })
}
function handleSelectionChange3(selection) { ids3.value = selection.map(item => item.guid); single3.value = selection.length !== 1; multiple3.value = !selection.length }
function handleChemAdd() {
  chemForm.value = { hachName: '', chemName: '', locName: '', toxicSubsNum: '', dangNum: '', currRese: '', storPack: '', ovduProcMode: '', orgGuid: userStore.orgGuid || '' }
  chemFormOpen.value = true
}
function handleUpdate3(row) {
  const data = row || (ids3.value.length ? list3.value.find(d => d.guid === ids3.value[0]) : null)
  if (!data) { proxy.$modal.msgWarning('请先选择一条数据'); return }
  chemForm.value = { ...data }
  chemFormOpen.value = true
}
function submitChemForm() {
  chemFormRef.value.validate(valid => {
    if (!valid) return
    const api = chemForm.value.guid ? updateObjHach : addObjHach
    api(chemForm.value).then(() => {
      proxy.$modal.msgSuccess(chemForm.value.guid ? '修改成功' : '新增成功')
      chemFormOpen.value = false
      getList3()
    })
  })
}
function handleDelete3(row) {
  const guids = row ? [row.guid] : ids3.value
  if (!guids.length) { proxy.$modal.msgWarning('请先选择一条数据'); return }
  proxy.$modal.confirm('是否确认删除？').then(() => {
    delObjHach(guids.join(',')).then(() => { getList3(); proxy.$modal.msgSuccess('删除成功') })
  }).catch(() => {})
}
function handleChemRecord(row) {
  storageHachGuid.value = row.guid
  storageOpen.value = true
  loadStorList()
  loadDelList()
}
function loadStorList() {
  listBisHachStor({ hachGuid: storageHachGuid.value, ...storQuery }).then(res => { storList.value = res.rows || []; storTotal.value = res.total || 0 })
}
function loadDelList() {
  listBisHachDel({ hachGuid: storageHachGuid.value, ...delQuery }).then(res => { delList.value = res.rows || []; delTotal.value = res.total || 0 })
}
function handleStorAdd() {
  storForm.value = { storDate: '', storType: '', supplier: '', quantity: '', handle: '', acceptor: '', storLoc: '' }
  storFormOpen.value = true
}
function handleStorUpdate(row) {
  storForm.value = { ...row }
  storFormOpen.value = true
}
function handleStorDelete(row) {
  proxy.$modal.confirm('是否确认删除？').then(() => {
    delBisHachStor(row.guid).then(() => { loadStorList(); proxy.$modal.msgSuccess('删除成功') })
  })
}
function submitStorForm() {
  storFormRef.value.validate(valid => {
    if (!valid) return
    const api = storForm.value.guid ? updateBisHachStor : addBisHachStor
    api({ ...storForm.value, hachGuid: storageHachGuid.value }).then(() => {
      proxy.$modal.msgSuccess('保存成功')
      storFormOpen.value = false
      loadStorList()
    })
  })
}
function handleDelAdd() {
  delForm.value = { delTime: '', delType: '', quantity: '', handle: '', auditor: '' }
  delFormOpen.value = true
}
function handleDelUpdate(row) {
  delForm.value = { ...row }
  delFormOpen.value = true
}
function handleDelSingleDelete(row) {
  proxy.$modal.confirm('是否确认删除？').then(() => {
    delBisHachDel(row.guid).then(() => { loadDelList(); proxy.$modal.msgSuccess('删除成功') })
  })
}
function submitDelForm() {
  delFormRef.value.validate(valid => {
    if (!valid) return
    const api = delForm.value.guid ? updateBisHachDel : addBisHachDel
    api({ ...delForm.value, hachGuid: storageHachGuid.value }).then(() => {
      proxy.$modal.msgSuccess('保存成功')
      delFormOpen.value = false
      loadDelList()
    })
  })
}

function handleQuery() { queryParams.pageNum = 1; getList() }
function handleQuery2() { queryParams2.pageNum = 1; getList2() }
function handleQuery3() { queryParams3.pageNum = 1; getList3() }

function handleExport() { proxy.download("hazard/objhaz/export", { ...queryParams }, `工程危险源_${new Date().getTime()}.xlsx`) }
function handleExport2() { proxy.download("hazard/objhaz/export", { ...queryParams2 }, `标段危险源_${new Date().getTime()}.xlsx`) }
function handleExport3() { proxy.download("hazard/objhach/export", { ...queryParams3 }, `危险物品_${new Date().getTime()}.xlsx`) }

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

function handleAdd() {
  formTitle.value = '新增危险源'
  form.value = { hazName: '', engGuid: '', engName: '', tendName: '', orgGuid: userStore.orgGuid || '', hiddGrad: '1', riskGrad: '', type: '', supPers: '', offiTel: '', ifLiceNoti: '', proPart: '', note: '' }
  listAttEngBase({}).then(res => { engList.value = (res.rows || []).map(e => ({ value: e.guid, label: e.engName })) })
  formOpen.value = true
}
function handleUpdate(row) {
  const data = row || (ids.value.length ? list.value.find(d => d.guid === ids.value[0]) : null)
  if (!data) { proxy.$modal.msgWarning('请先选择一条数据'); return }
  formTitle.value = '修改危险源'
  form.value = { ...data, engGuid: data.engGuid || '', orgGuid: data.orgGuid || userStore.orgGuid || '' }
  listAttEngBase({}).then(res => { engList.value = (res.rows || []).map(e => ({ value: e.guid, label: e.engName })) })
  formOpen.value = true
}
function onEngChange(val) {
  form.value.engName = (engList.value.find(e => e.value === val) || {}).label || ''
}
function submitForm() {
  formRef.value.validate(valid => {
    if (!valid) return
    formLoading.value = true
    const api = form.value.guid ? updateObjHaz : addObjHaz
    api(form.value).then(() => {
      proxy.$modal.msgSuccess(form.value.guid ? '修改成功' : '新增成功')
      formOpen.value = false; formLoading.value = false
      if (activeTab.value === 'tend') getList2()
      else getList()
    }).catch(() => { formLoading.value = false })
  })
}
function handleRecord(row) {
  recHazGuid.value = row.guid
  recQuery.pageNum = 1
  recordOpen.value = true
  loadRecList()
}
function loadRecList() {
  listBisHazPatRec({ hazGuid: recHazGuid.value, pageNum: recQuery.pageNum, pageSize: recQuery.pageSize }).then(res => {
    recList.value = res.rows || []; recTotal.value = res.total || 0
  })
}
function handleRecSelectionChange(selection) { recIds.value = selection.map(item => item.guid); recSingle.value = selection.length !== 1; recMultiple.value = !selection.length }
function handleRecAdd() {
  recForm.value = { patTime: '', patPers: '', probFound: '', treaMeas: '' }
  recFormOpen.value = true
}
function handleRecUpdate(row) {
  recForm.value = { ...row }
  recFormOpen.value = true
}
function handleRecSingleDelete(row) {
  proxy.$modal.confirm('是否确认删除？').then(() => {
    delBisHazPatRec(row.guid).then(() => { loadRecList(); proxy.$modal.msgSuccess('删除成功') })
  })
}
function handleRecDelete() {
  if (!recIds.value.length) { proxy.$modal.msgWarning('请先选择数据'); return }
  proxy.$modal.confirm('是否确认删除？').then(() => {
    delBisHazPatRec(recIds.value.join(',')).then(() => { loadRecList(); proxy.$modal.msgSuccess('删除成功') })
  })
}
function submitRecForm() {
  recFormRef.value.validate(valid => {
    if (!valid) return
    const api = recForm.value.guid ? updateBisHazPatRec : addBisHazPatRec
    api({ ...recForm.value, hazGuid: recHazGuid.value }).then(() => {
      proxy.$modal.msgSuccess('保存成功')
      recFormOpen.value = false
      loadRecList()
    })
  })
}
function handleSubmit(row) {
  proxy.$confirm('确认将该危险源报送至水行政主管部门？', '提示', { type: 'warning' }).then(() => {
    row.hazStat = '1'
    proxy.$modal.msgSuccess('报送成功')
    if (activeTab.value === 'tend') getList2()
    else getList()
  }).catch(() => {})
}

function handleDelete(row) {
  const guids = row ? [row.guid] : ids.value
  if (!guids.length) { proxy.$modal.msgWarning('请先选择一条数据'); return }
  proxy.$modal.confirm('是否确认删除？').then(() => {
    delObjHaz(guids.join(',')).then(() => { getList(); proxy.$modal.msgSuccess('删除成功') })
  }).catch(() => {})
}

function handleUpdate2(row) {
  const data = row || (ids2.value.length ? list2.value.find(d => d.guid === ids2.value[0]) : null)
  if (!data) { proxy.$modal.msgWarning('请先选择一条数据'); return }
  formTitle.value = '修改危险源'
  form.value = { ...data, engGuid: data.engGuid || '', orgGuid: data.orgGuid || userStore.orgGuid || '' }
  listAttEngBase({}).then(res => { engList.value = (res.rows || []).map(e => ({ value: e.guid, label: e.engName })) })
  formOpen.value = true
}
function handleDelete2(row) {
  const guids = row ? [row.guid] : ids2.value
  if (!guids.length) { proxy.$modal.msgWarning('请先选择一条数据'); return }
  proxy.$modal.confirm('是否确认删除？').then(() => {
    delObjHaz(guids.join(',')).then(() => { getList2(); proxy.$modal.msgSuccess('删除成功') })
  }).catch(() => {})
}
function handleTabChange() {
  if (activeTab.value === 'eng') getList()
  else if (activeTab.value === 'tend') getList2()
  else getList3()
}

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