<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="危化品名称" prop="hachName">
        <el-input v-model="queryParams.hachName" placeholder="请输入危化品名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="化学品名称" prop="chemName">
        <el-input v-model="queryParams.chemName" 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="['hazard:objhach:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['hazard:objhach:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['hazard:objhach:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['hazard:objhach:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="hazard-objhach-index"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hachList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="危化品名称" align="center" prop="hachName" v-if="columns.hachName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="化学品名称" align="center" prop="chemName" v-if="columns.chemName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="存放地点" align="center" prop="locName" v-if="columns.locName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="剧毒品数量" align="center" prop="toxicSubsNum" v-if="columns.toxicSubsNum.visible" />
      <el-table-column label="危险品数量" align="center" prop="dangNum" v-if="columns.dangNum.visible" />
      <el-table-column label="现有储量" align="center" prop="currRese" v-if="columns.currRese.visible" />
      <el-table-column label="存储包装" align="center" prop="storPack" v-if="columns.storPack.visible" />
      <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:objhach: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:objhach: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-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form :model="form" :rules="rules" ref="hachRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="危化品名称" prop="hachName">
              <el-input v-model="form.hachName" placeholder="请输入危化品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="化学品名称" prop="chemName">
              <el-input v-model="form.chemName" placeholder="请输入化学品名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="存放地点" prop="locName">
              <el-input v-model="form.locName" placeholder="请输入存放地点" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="存储包装" prop="storPack">
              <el-input v-model="form.storPack" placeholder="请输入存储包装" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="剧毒品数量" prop="toxicSubsNum">
              <el-input v-model="form.toxicSubsNum" placeholder="请输入剧毒品数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="危险品数量" prop="dangNum">
              <el-input v-model="form.dangNum" placeholder="请输入危险品数量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="现有储量" prop="currRese">
              <el-input v-model="form.currRese" placeholder="请输入现有储量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="超量处理方式" prop="ovduProcMode">
              <el-input v-model="form.ovduProcMode" placeholder="请输入超量处理方式" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="note">
              <el-input v-model="form.note" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ObjHach">
import { listObjHach, getObjHach, delObjHach, addObjHach, updateObjHach } from "@/api/slaj/hazard"

const { proxy } = getCurrentInstance()

const hachList = 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 columns = ref({
  hachName: { label: '危化品名称', visible: true },
  chemName: { label: '化学品名称', visible: true },
  locName: { label: '存放地点', visible: true },
  toxicSubsNum: { label: '剧毒品数量', visible: true },
  dangNum: { label: '危险品数量', visible: true },
  currRese: { label: '现有储量', visible: true },
  storPack: { label: '存储包装', visible: true },
  recPers: { label: '记录人员', visible: true },
  collTime: { label: '采集时间', visible: true }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    hachName: undefined,
    chemName: undefined
  },
  rules: {
    hachName: [{ required: true, message: "危化品名称不能为空", trigger: "blur" }]
  }
})

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

/** 查询危化品列表 */
function getList() {
  loading.value = true
  listObjHach(queryParams.value).then(res => {
    loading.value = false
    hachList.value = res.rows
    total.value = res.total
  })
}

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

/** 重置按钮操作 */
function resetQuery() {
  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
  getObjHach(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/hazard/chemDetail', query: { guid: guid } })
}

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

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

/** 重置表单 */
function reset() {
  form.value = {
    guid: undefined,
    hachName: undefined,
    orgGuid: undefined,
    chemName: undefined,
    locName: undefined,
    toxicSubsNum: undefined,
    dangNum: undefined,
    currRese: undefined,
    storPack: undefined,
    ovduProcMode: undefined,
    note: undefined,
    collTime: undefined,
    updTime: undefined,
    recPers: undefined,
    toDate: undefined
  }
  proxy.resetForm("hachRef")
}

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