<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="事故名称" prop="acciName">
        <el-input v-model="queryParams.acciName" 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="['acci:bisacciinve:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['acci:bisacciinve:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['acci:bisacciinve:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['acci:bisacciinve:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="acci-bisacciinve-index"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="事故名称" align="center" prop="acciName" v-if="columns.acciName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="调查人员" align="center" prop="invePers" v-if="columns.invePers.visible" :show-overflow-tooltip="true" />
      <el-table-column label="调查时间" align="center" prop="inveTime" v-if="columns.inveTime.visible" width="180" />
      <el-table-column label="调查结果" align="center" prop="inveResult" v-if="columns.inveResult.visible" :show-overflow-tooltip="true" />
      <el-table-column label="事故原因" align="center" prop="acciCause" v-if="columns.acciCause.visible" :show-overflow-tooltip="true" />
      <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="['acci:bisacciinve: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="['acci:bisacciinve: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="inveRef" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="事故关联" prop="acciGuid">
              <el-select v-model="form.acciGuid" placeholder="请选择事故" filterable remote :remote-method="remoteAcciList" :loading="acciLoading" style="width: 100%">
                <el-option v-for="item in acciOptions" :key="item.guid" :label="item.acciName" :value="item.guid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="调查人员" prop="invePers">
              <el-input v-model="form.invePers" placeholder="请输入调查人员" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调查时间" prop="inveTime">
              <el-date-picker v-model="form.inveTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择调查时间" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="调查内容" prop="inveCont">
              <el-input v-model="form.inveCont" type="textarea" :rows="3" 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" :rows="3" placeholder="请输入事故原因" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="处理结果" prop="handleResult">
              <el-select v-model="form.handleResult" placeholder="请选择处理结果" style="width: 100%">
                <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-col :span="12">
            <el-form-item label="调查结果" prop="inveResult">
              <el-input v-model="form.inveResult" type="textarea" :rows="1" 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="700px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="事故名称" :span="2">{{ detailData.acciName }}</el-descriptions-item>
        <el-descriptions-item label="调查人员">{{ detailData.invePers }}</el-descriptions-item>
        <el-descriptions-item label="调查时间">{{ detailData.inveTime }}</el-descriptions-item>
        <el-descriptions-item label="调查内容" :span="2">{{ detailData.inveCont }}</el-descriptions-item>
        <el-descriptions-item label="事故原因" :span="2">{{ detailData.acciCause }}</el-descriptions-item>
        <el-descriptions-item label="调查结果" :span="2">{{ detailData.inveResult }}</el-descriptions-item>
        <el-descriptions-item label="处理结果">
          <dict-tag :options="sys_yes_no" :value="detailData.handleResult" />
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup name="BisAcciInve">
import { listBisAcciInve, getBisAcciInve, delBisAcciInve, addBisAcciInve, updateBisAcciInve, listObjAcci } from "@/api/slaj/acci"

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

const inveList = 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 acciOptions = ref([])
const acciLoading = ref(false)

// 详情
const detailOpen = ref(false)
const detailData = ref({})

const columns = ref({
  acciName: { label: '事故名称', visible: true },
  invePers: { label: '调查人员', visible: true },
  inveTime: { label: '调查时间', visible: true },
  inveResult: { label: '调查结果', visible: true },
  acciCause: { label: '事故原因', visible: true }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    acciName: undefined,
    beginInveTime: undefined,
    endInveTime: undefined
  },
  rules: {
    acciGuid: [{ required: true, message: "请选择事故", trigger: "change" }],
    invePers: [{ required: true, message: "调查人员不能为空", trigger: "blur" }],
    inveTime: [{ required: true, message: "调查时间不能为空", trigger: "change" }],
    acciCause: [{ required: true, message: "事故原因不能为空", trigger: "blur" }]
  }
})

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

/** 查询调查记录列表 */
function getList() {
  loading.value = true
  listBisAcciInve(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false
    inveList.value = res.rows
    total.value = res.total
  })
}

/** 远程搜索事故列表 */
function remoteAcciList(query) {
  if (query) {
    acciLoading.value = true
    listObjAcci({ acciName: query, pageNum: 1, pageSize: 20 }).then(res => {
      acciLoading.value = false
      acciOptions.value = res.rows
    }).catch(() => {
      acciLoading.value = false
    })
  }
}

/** 搜索按钮操作 */
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()
  loadAcciList()
  open.value = true
  title.value = "添加调查记录"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const guid = row.guid || ids.value
  loadAcciList()
  getBisAcciInve(guid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改调查记录"
  })
}

/** 详情按钮操作 */
function handleDetail(row) {
  const guid = row.guid || ids.value
  getBisAcciInve(guid).then(response => {
    detailData.value = response.data
    detailOpen.value = true
  })
}

/** 加载事故列表 */
function loadAcciList() {
  acciLoading.value = true
  listObjAcci({ pageNum: 1, pageSize: 50 }).then(res => {
    acciLoading.value = false
    acciOptions.value = res.rows
  }).catch(() => {
    acciLoading.value = false
  })
}

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

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

/** 取消按钮 */
function cancel() {
  open.value = false
  proxy.resetForm("inveRef")
}

/** 重置表单 */
function reset() {
  form.value = {
    guid: undefined,
    acciGuid: undefined,
    invePers: undefined,
    inveTime: undefined,
    inveCont: undefined,
    acciCause: undefined,
    handleResult: undefined,
    inveResult: undefined
  }
  proxy.resetForm("inveRef")
}

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