<template>
  <div class="app-container">
    <el-tabs v-model="activeTab">
      <el-tab-pane label="补报登记" name="reg">
        <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="关联事故" prop="acciGuid">
            <el-select v-model="form.acciGuid" placeholder="请选择事故" filterable style="width: 100%">
              <el-option v-for="item in acciList" :key="item.guid" :label="item.acciName" :value="item.guid" />
            </el-select>
          </el-form-item>
          <el-form-item label="补报时间" prop="repTime">
            <el-date-picker v-model="form.repTime" type="datetime" placeholder="选择补报时间" value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
          </el-form-item>
          <el-form-item label="补报内容" prop="repCont">
            <el-input v-model="form.repCont" type="textarea" :rows="4" placeholder="请输入补报内容" />
          </el-form-item>
          <el-form-item label="附件说明" prop="fileDesc">
            <el-input v-model="form.fileDesc" placeholder="请输入附件说明" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">提交补报</el-button>
            <el-button @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>

      <el-tab-pane label="补报记录" name="rec">
        <el-form ref="queryRef" :model="queryParams" :inline="true" class="mb8">
          <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-table v-loading="loading" :data="repList" stripe>
          <el-table-column label="补报时间" align="center" prop="repTime" width="180" />
          <el-table-column label="关联事故" align="center" prop="acciName" :show-overflow-tooltip="true" />
          <el-table-column label="补报内容" align="center" prop="repCont" :show-overflow-tooltip="true" />
          <el-table-column label="记录人" align="center" prop="createBy" width="120" />
        </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-tabs>
  </div>
</template>

<script setup name="AcciFastRep">
import { getCurrentInstance, onMounted, reactive, ref, toRefs } from 'vue'
import { listObjAcci, listBisAcciExpRep, insertRepBisAcciExpRep } from "@/api/slaj/acci"

const { proxy } = getCurrentInstance()

const activeTab = ref('reg')
const loading = ref(false)
const total = ref(0)
const acciList = ref([])
const repList = ref([])

const data = reactive({
  form: {
    acciGuid: undefined,
    repTime: undefined,
    repCont: undefined,
    fileDesc: undefined
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  rules: {
    acciGuid: [{ required: true, message: "请选择关联事故", trigger: "change" }],
    repTime: [{ required: true, message: "请选择补报时间", trigger: "change" }],
    repCont: [{ required: true, message: "补报内容不能为空", trigger: "blur" }]
  }
})

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

/** 加载事故列表下拉 */
function loadAcciList() {
  listObjAcci({ pageNum: 1, pageSize: 999 }).then(res => {
    acciList.value = res.rows || []
  })
}

/** 查询补报记录 */
function getList() {
  loading.value = true
  listBisAcciExpRep(queryParams.value).then(res => {
    loading.value = false
    repList.value = res.rows || []
    total.value = res.total || 0
  }).catch(() => {
    loading.value = false
  })
}

function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 提交补报 */
function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (!valid) return
    insertRepBisAcciExpRep(form.value).then(() => {
      proxy.$modal.msgSuccess("补报成功")
      reset()
      activeTab.value = 'rec'
      getList()
    })
  })
}

function reset() {
  form.value = {
    acciGuid: undefined,
    repTime: undefined,
    repCont: undefined,
    fileDesc: undefined
  }
  proxy.resetForm("formRef")
}

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