<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="hazGrade">
        <el-select v-model="queryParams.hazGrade" 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="创建时间" 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:tbusihaziden:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['hazard:tbusihaziden:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['hazard:tbusihaziden:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['hazard:tbusihaziden:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="hazard-tbusihaziden-index"></right-toolbar>
    </el-row>

    <div class="table-wrapper">
    <el-table :height="tableHeight" v-loading="loading" :data="idenList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="危险源等级" align="center" prop="hazGrade" v-if="columns.hazGrade.visible">
        <template #default="scope">
          <dict-tag :options="haz_grad" :value="scope.row.hazGrade" />
        </template>
      </el-table-column>
      <el-table-column label="危险源描述" align="center" prop="hazRemark" v-if="columns.hazRemark.visible" :show-overflow-tooltip="true" />
      <el-table-column label="组织管控责任人" align="center" prop="orgPer" v-if="columns.orgPer.visible" />
      <el-table-column label="监督责任人" align="center" prop="supePer" v-if="columns.supePer.visible" />
      <el-table-column label="管控责任人" align="center" prop="controlPer" v-if="columns.controlPer.visible" />
      <el-table-column label="是否导则内" align="center" prop="guideIn" v-if="columns.guideIn.visible">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.guideIn" />
        </template>
      </el-table-column>
      <el-table-column label="事故原因" align="center" prop="acciCause" v-if="columns.acciCause.visible" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns.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="handleUpdate(scope.row)" v-hasPermi="['hazard:tbusihaziden: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="Delete" @click="handleDelete(scope.row)" v-hasPermi="['hazard:tbusihaziden: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="idenRef" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="危险源" prop="hazGuid">
              <el-select v-model="form.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="hazGrade">
              <el-select v-model="form.hazGrade" 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="orgPer">
              <el-input v-model="form.orgPer" placeholder="请输入组织管控责任人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="责任人电话" prop="orgPhone">
              <el-input v-model="form.orgPhone" placeholder="请输入责任人电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="监督责任人" prop="supePer">
              <el-input v-model="form.supePer" placeholder="请输入监督责任人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="监督电话" prop="supePhone">
              <el-input v-model="form.supePhone" placeholder="请输入监督电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="管控责任人" prop="controlPer">
              <el-input v-model="form.controlPer" placeholder="请输入管控责任人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="管控电话" prop="controlPhone">
              <el-input v-model="form.controlPhone" placeholder="请输入管控电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否导则内" prop="guideIn">
              <el-select v-model="form.guideIn" 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-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="危险源描述" prop="hazRemark">
              <el-input v-model="form.hazRemark" type="textarea" 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-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="detailOpen" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="危险源等级">{{ detailRow.hazGrade }}</el-descriptions-item>
        <el-descriptions-item label="是否导则内">{{ detailRow.guideIn }}</el-descriptions-item>
        <el-descriptions-item label="组织管控责任人">{{ detailRow.orgPer }}</el-descriptions-item>
        <el-descriptions-item label="监督责任人">{{ detailRow.supePer }}</el-descriptions-item>
        <el-descriptions-item label="管控责任人">{{ detailRow.controlPer }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detailRow.createTime }}</el-descriptions-item>
        <el-descriptions-item label="危险源描述" :span="2">{{ detailRow.hazRemark }}</el-descriptions-item>
        <el-descriptions-item label="事故原因" :span="2">{{ detailRow.acciCause }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button @click="detailOpen = false">关 闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TBusiHazIden">
import { listTBusiHazIden, getTBusiHazIden, delTBusiHazIden, addTBusiHazIden, updateTBusiHazIden } from "@/api/slaj/hazard"
import { useTableHeight } from '@/hooks/useTableHeight'
import { listObjHaz } from "@/api/slaj/hazard"

const { tableHeight, containerRef } = useTableHeight()

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

const idenList = ref([])
const hazOptions = 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 detailOpen = ref(false)
const detailRow = ref({})

const columns = ref({
  hazGrade: { label: '危险源等级', visible: true },
  hazRemark: { label: '危险源描述', visible: true },
  orgPer: { label: '组织管控责任人', visible: true },
  supePer: { label: '监督责任人', visible: true },
  controlPer: { label: '管控责任人', visible: true },
  guideIn: { label: '是否导则内', visible: true },
  acciCause: { label: '事故原因', visible: true },
  createTime: { label: '创建时间', visible: true }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    hazGrade: undefined
  },
  rules: {
    hazGuid: [{ required: true, message: "危险源不能为空", trigger: "change" }],
    hazGrade: [{ required: true, message: "危险源等级不能为空", trigger: "change" }]
  }
})

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

/** 查询辨识列表 */
function getList() {
  loading.value = true
  listTBusiHazIden(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false
    idenList.value = res.rows
    total.value = res.total
  })
}

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

/** 搜索按钮操作 */
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
  getTBusiHazIden(guid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改辨识"
  })
}

/** 详情按钮操作 */
function handleDetail(row) {
  detailRow.value = row
  detailOpen.value = true
}

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

/** 导出按钮操作 */
function handleExport() {
  proxy.download("hazard/tbusihaziden/export", {
    ...queryParams.value
  }, `tBusiHazIden_${new Date().getTime()}.xlsx`)
}

/** 重置表单 */
function reset() {
  form.value = {
    guid: undefined,
    hazGuid: undefined,
    hazGrade: undefined,
    hazRemark: undefined,
    orgPer: undefined,
    orgPhone: undefined,
    supePer: undefined,
    supePhone: undefined,
    controlPer: undefined,
    controlPhone: undefined,
    guideIn: undefined,
    createPer: undefined,
    createTime: undefined,
    updatePer: undefined,
    updateTime: undefined,
    acciCause: undefined
  }
  proxy.resetForm("idenRef")
}

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