<template>
  <div class="app-container">
    <el-tabs v-model="activeTab">
      <!-- 巡视记录 -->
      <el-tab-pane label="巡视记录" name="patrol">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="巡视人员" prop="patPers">
            <el-input v-model="queryParams.patPers" 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:bishazpatrec:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['hazard:bishazpatrec:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['hazard:bishazpatrec:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['hazard:bishazpatrec:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="hazard-bishazpatrec-index"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="patRecList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="巡视时间" align="center" prop="patTime" v-if="columns.patTime.visible" width="180" />
          <el-table-column label="巡视人员" align="center" prop="patPers" v-if="columns.patPers.visible" />
          <el-table-column label="发现问题" align="center" prop="probFound" v-if="columns.probFound.visible" :show-overflow-tooltip="true" />
          <el-table-column label="处理措施" align="center" prop="treaMeas" v-if="columns.treaMeas.visible" :show-overflow-tooltip="true" />
          <el-table-column label="记录人员" align="center" prop="recPers" v-if="columns.recPers.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:bishazpatrec:edit']"></el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['hazard:bishazpatrec:remove']"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>

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

      <!-- 巡查任务 -->
      <el-tab-pane label="巡查任务" name="task">
        <el-form :model="taskQueryParams" ref="taskQueryRef" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="任务状态" prop="status">
            <el-select v-model="taskQueryParams.status" placeholder="请选择任务状态" clearable style="width: 240px">
              <el-option v-for="dict in inspect_status" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleTaskQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetTaskQuery">重置</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="handleTaskAdd" v-hasPermi="['hazard:tbusiinspecttask:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="taskSingle" @click="handleTaskUpdate" v-hasPermi="['hazard:tbusiinspecttask:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="taskMultiple" @click="handleTaskDelete" v-hasPermi="['hazard:tbusiinspecttask:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleTaskExport" v-hasPermi="['hazard:tbusiinspecttask:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getTaskList" :columns="taskColumns" storageKey="hazard-tbusiinspecttask-index"></right-toolbar>
        </el-row>

        <el-table v-loading="taskLoading" :data="taskList" @selection-change="handleTaskSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="开始时间" align="center" prop="startTime" v-if="taskColumns.startTime.visible" width="180" />
          <el-table-column label="周期数" align="center" prop="cycleNum" v-if="taskColumns.cycleNum.visible" />
          <el-table-column label="定量类型" align="center" prop="quantType" v-if="taskColumns.quantType.visible" />
          <el-table-column label="预警值" align="center" prop="early" v-if="taskColumns.early.visible" />
          <el-table-column label="阈值" align="center" prop="threshold" v-if="taskColumns.threshold.visible" />
          <el-table-column label="状态" align="center" prop="status" v-if="taskColumns.status.visible">
            <template #default="scope">
              <dict-tag :options="inspect_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" v-if="taskColumns.createTime.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="handleTaskUpdate(scope.row)" v-hasPermi="['hazard:tbusiinspecttask:edit']"></el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button link type="primary" icon="Delete" @click="handleTaskDelete(scope.row)" v-hasPermi="['hazard:tbusiinspecttask:remove']"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="taskTotal > 0" :total="taskTotal" v-model:page="taskQueryParams.pageNum" v-model:limit="taskQueryParams.pageSize" @pagination="getTaskList" />
      </el-tab-pane>

      <!-- 巡查记录 -->
      <el-tab-pane label="巡查记录" name="record">
        <el-form :model="recordQueryParams" ref="recordQueryRef" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="巡查人" prop="inspectPer">
            <el-input v-model="recordQueryParams.inspectPer" placeholder="请输入巡查人" clearable style="width: 240px" @keyup.enter="handleRecordQuery" />
          </el-form-item>
          <el-form-item label="巡查时间" style="width: 308px">
            <el-date-picker v-model="recordDateRange" 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="handleRecordQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetRecordQuery">重置</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="handleRecordAdd" v-hasPermi="['hazard:tbusiinspectrecord:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="recordSingle" @click="handleRecordUpdate" v-hasPermi="['hazard:tbusiinspectrecord:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="recordMultiple" @click="handleRecordDelete" v-hasPermi="['hazard:tbusiinspectrecord:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleRecordExport" v-hasPermi="['hazard:tbusiinspectrecord:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getRecordList" :columns="recordColumns" storageKey="hazard-tbusiinspectrecord-index"></right-toolbar>
        </el-row>

        <el-table v-loading="recordLoading" :data="recordList" @selection-change="handleRecordSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="巡查时间" align="center" prop="inspectTime" v-if="recordColumns.inspectTime.visible" width="180" />
          <el-table-column label="巡查人" align="center" prop="inspectPer" v-if="recordColumns.inspectPer.visible" />
          <el-table-column label="开始时间" align="center" prop="startTime" v-if="recordColumns.startTime.visible" width="180" />
          <el-table-column label="结束时间" align="center" prop="endTime" v-if="recordColumns.endTime.visible" width="180" />
          <el-table-column label="结果" align="center" prop="result" v-if="recordColumns.result.visible" :show-overflow-tooltip="true" />
          <el-table-column label="备注" align="center" prop="remark" v-if="recordColumns.remark.visible" :show-overflow-tooltip="true" />
          <el-table-column label="创建时间" align="center" prop="createTime" v-if="recordColumns.createTime.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="handleRecordUpdate(scope.row)" v-hasPermi="['hazard:tbusiinspectrecord:edit']"></el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button link type="primary" icon="Delete" @click="handleRecordDelete(scope.row)" v-hasPermi="['hazard:tbusiinspectrecord:remove']"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="recordTotal > 0" :total="recordTotal" v-model:page="recordQueryParams.pageNum" v-model:limit="recordQueryParams.pageSize" @pagination="getRecordList" />
      </el-tab-pane>
    </el-tabs>

    <!-- 巡视记录对话框 -->
    <el-dialog :title="patTitle" v-model="patOpen" width="800px" append-to-body>
      <el-form :model="patForm" :rules="patRules" ref="patRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="危险源" prop="hazGuid">
              <el-select v-model="patForm.hazGuid" placeholder="请选择危险源" filterable>
                <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="patTime">
              <el-date-picker v-model="patForm.patTime" type="datetime" placeholder="请选择巡视时间" value-format="YYYY-MM-DD HH:mm:ss" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="巡视人员" prop="patPers">
              <el-input v-model="patForm.patPers" placeholder="请输入巡视人员" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="发现问题" prop="probFound">
              <el-input v-model="patForm.probFound" type="textarea" placeholder="请输入发现问题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="处理措施" prop="treaMeas">
              <el-input v-model="patForm.treaMeas" 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="patForm.note" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitPatForm">确 定</el-button>
          <el-button @click="cancelPat">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 巡查任务对话框 -->
    <el-dialog :title="taskTitle" v-model="taskOpen" width="800px" append-to-body>
      <el-form :model="taskForm" :rules="taskRules" ref="taskFormRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker v-model="taskForm.startTime" type="datetime" placeholder="请选择开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="周期数" prop="cycleNum">
              <el-input-number v-model="taskForm.cycleNum" :min="1" placeholder="请输入周期数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="定量类型" prop="quantType">
              <el-input v-model="taskForm.quantType" placeholder="请输入定量类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务状态" prop="status">
              <el-select v-model="taskForm.status" placeholder="请选择任务状态">
                <el-option v-for="dict in inspect_status" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="预警值" prop="early">
              <el-input v-model="taskForm.early" placeholder="请输入预警值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="阈值" prop="threshold">
              <el-input v-model="taskForm.threshold" placeholder="请输入阈值" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitTaskForm">确 定</el-button>
          <el-button @click="taskOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 巡查记录对话框 -->
    <el-dialog :title="recordTitle" v-model="recordOpen" width="800px" append-to-body>
      <el-form :model="recordForm" :rules="recordRules" ref="recordFormRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="巡查时间" prop="inspectTime">
              <el-date-picker v-model="recordForm.inspectTime" type="datetime" placeholder="请选择巡查时间" value-format="YYYY-MM-DD HH:mm:ss" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="巡查人" prop="inspectPer">
              <el-input v-model="recordForm.inspectPer" placeholder="请输入巡查人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker v-model="recordForm.startTime" type="datetime" placeholder="请选择开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="endTime">
              <el-date-picker v-model="recordForm.endTime" type="datetime" placeholder="请选择结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="定量类型" prop="quantType">
              <el-input v-model="recordForm.quantType" placeholder="请输入定量类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警值" prop="early">
              <el-input v-model="recordForm.early" placeholder="请输入预警值" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="阈值" prop="threshold">
              <el-input v-model="recordForm.threshold" placeholder="请输入阈值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结果" prop="result">
              <el-input v-model="recordForm.result" placeholder="请输入结果" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="recordForm.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitRecordForm">确 定</el-button>
          <el-button @click="recordOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="HazardPatrol">
import { listBisHazPatRec, getBisHazPatRec, delBisHazPatRec, addBisHazPatRec, updateBisHazPatRec } from "@/api/slaj/hazard"
import { listTBusiInspectTask, getTBusiInspectTask, delTBusiInspectTask, addTBusiInspectTask, updateTBusiInspectTask } from "@/api/slaj/hazard"
import { listTBusiInspectRecord, getTBusiInspectRecord, delTBusiInspectRecord, addTBusiInspectRecord, updateTBusiInspectRecord } from "@/api/slaj/hazard"
import { listObjHaz } from "@/api/slaj/hazard"

const { proxy } = getCurrentInstance()
const { inspect_status } = proxy.useDict("inspect_status")

const activeTab = ref("patrol")
const hazOptions = ref([])

// ========== 巡视记录 ==========
const patRecList = ref([])
const patOpen = 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 patTitle = ref("")
const dateRange = ref([])

const columns = ref({
  patTime: { label: '巡视时间', visible: true },
  patPers: { label: '巡视人员', visible: true },
  probFound: { label: '发现问题', visible: true },
  treaMeas: { label: '处理措施', visible: true },
  recPers: { label: '记录人员', visible: true },
  collTime: { label: '采集时间', visible: true }
})

const data = reactive({
  patForm: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    patPers: undefined
  },
  patRules: {
    hazGuid: [{ required: true, message: "危险源不能为空", trigger: "change" }],
    patTime: [{ required: true, message: "巡视时间不能为空", trigger: "change" }],
    patPers: [{ required: true, message: "巡视人员不能为空", trigger: "blur" }]
  }
})

const { queryParams, patForm, patRules } = toRefs(data)

// ========== 巡查任务 ==========
const taskList = ref([])
const taskLoading = ref(true)
const taskIds = ref([])
const taskSingle = ref(true)
const taskMultiple = ref(true)
const taskTotal = ref(0)
const taskOpen = ref(false)
const taskTitle = ref("")
const taskForm = ref({})
const taskFormRef = ref(null)

const taskColumns = ref({
  startTime: { label: '开始时间', visible: true },
  cycleNum: { label: '周期数', visible: true },
  quantType: { label: '定量类型', visible: true },
  early: { label: '预警值', visible: true },
  threshold: { label: '阈值', visible: true },
  status: { label: '状态', visible: true },
  createTime: { label: '创建时间', visible: true }
})

const taskQueryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  status: undefined
})

const taskRules = {
  startTime: [{ required: true, message: "开始时间不能为空", trigger: "change" }],
  cycleNum: [{ required: true, message: "周期数不能为空", trigger: "blur" }]
}

// ========== 巡查记录 ==========
const recordList = ref([])
const recordLoading = ref(true)
const recordIds = ref([])
const recordSingle = ref(true)
const recordMultiple = ref(true)
const recordTotal = ref(0)
const recordDateRange = ref([])
const recordOpen = ref(false)
const recordTitle = ref("")
const recordForm = ref({})
const recordFormRef = ref(null)

const recordColumns = ref({
  inspectTime: { label: '巡查时间', visible: true },
  inspectPer: { label: '巡查人', visible: true },
  startTime: { label: '开始时间', visible: true },
  endTime: { label: '结束时间', visible: true },
  result: { label: '结果', visible: true },
  remark: { label: '备注', visible: true },
  createTime: { label: '创建时间', visible: true }
})

const recordQueryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  inspectPer: undefined
})

const recordRules = {
  inspectTime: [{ required: true, message: "巡查时间不能为空", trigger: "change" }],
  inspectPer: [{ required: true, message: "巡查人不能为空", trigger: "blur" }]
}

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

// ========== 巡视记录方法 ==========
function getList() {
  loading.value = true
  listBisHazPatRec(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false
    patRecList.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() {
  resetPatForm()
  patOpen.value = true
  patTitle.value = "新增巡视记录"
}

function handleUpdate(row) {
  resetPatForm()
  const guid = row.guid || ids.value
  getBisHazPatRec(guid).then(response => {
    patForm.value = response.data
    patOpen.value = true
    patTitle.value = "修改巡视记录"
  })
}

function submitPatForm() {
  proxy.$refs["patRef"].validate(valid => {
    if (valid) {
      if (patForm.value.guid != undefined) {
        updateBisHazPatRec(patForm.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          patOpen.value = false
          getList()
        })
      } else {
        addBisHazPatRec(patForm.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          patOpen.value = false
          getList()
        })
      }
    }
  })
}

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

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

function resetPatForm() {
  patForm.value = {
    guid: undefined,
    hazGuid: undefined,
    patTime: undefined,
    patPers: undefined,
    probFound: undefined,
    treaMeas: undefined,
    note: undefined,
    collTime: undefined,
    updTime: undefined,
    recPers: undefined,
    toDate: undefined
  }
  proxy.resetForm("patRef")
}

function cancelPat() {
  patOpen.value = false
  resetPatForm()
}

// ========== 巡查任务方法 ==========
function getTaskList() {
  taskLoading.value = true
  listTBusiInspectTask(taskQueryParams).then(res => {
    taskLoading.value = false
    taskList.value = res.rows
    taskTotal.value = res.total
  })
}

function handleTaskQuery() {
  taskQueryParams.pageNum = 1
  getTaskList()
}

function resetTaskQuery() {
  proxy.resetForm("taskQueryRef")
  handleTaskQuery()
}

function handleTaskSelectionChange(selection) {
  taskIds.value = selection.map(item => item.guid)
  taskSingle.value = selection.length != 1
  taskMultiple.value = !selection.length
}

function handleTaskAdd() {
  taskForm.value = {
    guid: undefined,
    hazIdenGuid: undefined,
    startTime: undefined,
    cycleNum: undefined,
    quantType: undefined,
    early: undefined,
    threshold: undefined,
    orgGuid: undefined,
    status: undefined
  }
  taskTitle.value = "新增巡查任务"
  taskOpen.value = true
}

function handleTaskUpdate(row) {
  const guid = row.guid || taskIds.value
  getTBusiInspectTask(guid).then(response => {
    taskForm.value = response.data
    taskTitle.value = "修改巡查任务"
    taskOpen.value = true
  })
}

function submitTaskForm() {
  taskFormRef.value.validate(valid => {
    if (valid) {
      if (taskForm.value.guid != undefined) {
        updateTBusiInspectTask(taskForm.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          taskOpen.value = false
          getTaskList()
        })
      } else {
        addTBusiInspectTask(taskForm.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          taskOpen.value = false
          getTaskList()
        })
      }
    }
  })
}

function handleTaskDelete(row) {
  const guids = row.guid || taskIds.value
  proxy.$modal.confirm('是否确认删除巡查任务编号为"' + guids + '"的数据项？').then(function () {
    return delTBusiInspectTask(guids)
  }).then(() => {
    getTaskList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

function handleTaskExport() {
  proxy.download("hazard/tbusiinspecttask/export", {
    ...taskQueryParams
  }, `tBusiInspectTask_${new Date().getTime()}.xlsx`)
}

// ========== 巡查记录方法 ==========
function getRecordList() {
  recordLoading.value = true
  listTBusiInspectRecord(proxy.addDateRange(recordQueryParams, recordDateRange.value)).then(res => {
    recordLoading.value = false
    recordList.value = res.rows
    recordTotal.value = res.total
  })
}

function handleRecordQuery() {
  recordQueryParams.pageNum = 1
  getRecordList()
}

function resetRecordQuery() {
  recordDateRange.value = []
  proxy.resetForm("recordQueryRef")
  handleRecordQuery()
}

function handleRecordSelectionChange(selection) {
  recordIds.value = selection.map(item => item.guid)
  recordSingle.value = selection.length != 1
  recordMultiple.value = !selection.length
}

function handleRecordAdd() {
  recordForm.value = {
    guid: undefined,
    hazIdenGuid: undefined,
    taskGuid: undefined,
    startTime: undefined,
    endTime: undefined,
    quantType: undefined,
    early: undefined,
    threshold: undefined,
    result: undefined,
    remark: undefined,
    inspectPerGuid: undefined,
    inspectPer: undefined,
    inspectTime: undefined,
    orgGuid: undefined
  }
  recordTitle.value = "新增巡查记录"
  recordOpen.value = true
}

function handleRecordUpdate(row) {
  const guid = row.guid || recordIds.value
  getTBusiInspectRecord(guid).then(response => {
    recordForm.value = response.data
    recordTitle.value = "修改巡查记录"
    recordOpen.value = true
  })
}

function submitRecordForm() {
  recordFormRef.value.validate(valid => {
    if (valid) {
      if (recordForm.value.guid != undefined) {
        updateTBusiInspectRecord(recordForm.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          recordOpen.value = false
          getRecordList()
        })
      } else {
        addTBusiInspectRecord(recordForm.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          recordOpen.value = false
          getRecordList()
        })
      }
    }
  })
}

function handleRecordDelete(row) {
  const guids = row.guid || recordIds.value
  proxy.$modal.confirm('是否确认删除巡查记录编号为"' + guids + '"的数据项？').then(function () {
    return delTBusiInspectRecord(guids)
  }).then(() => {
    getRecordList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

function handleRecordExport() {
  proxy.download("hazard/tbusiinspectrecord/export", {
    ...recordQueryParams
  }, `tBusiInspectRecord_${new Date().getTime()}.xlsx`)
}

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