<template>
  <div ref="containerRef" class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="预案ID" prop="basicId">
        <el-input v-model="queryParams.basicId" placeholder="请输入预案ID" clearable style="width: 180px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="演练名称" prop="drillName">
        <el-input v-model="queryParams.drillName" placeholder="请输入演练名称" clearable style="width: 180px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="演练类型" prop="drillType">
        <el-select v-model="queryParams.drillType" placeholder="请选择演练类型" clearable style="width: 150px">
          <el-option v-for="dict in eme_drill_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </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="['eme:drillrecord:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['eme:drillrecord:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['eme:drillrecord:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['eme:drillrecord:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="eme-drillrecord-index"></right-toolbar>
    </el-row>

    <div class="table-wrapper">
    <el-table :height="tableHeight" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="演练名称" align="center" prop="drillName" v-if="columns.drillName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="演练类型" align="center" prop="drillType" v-if="columns.drillType.visible">
        <template #default="scope">
          <dict-tag :options="eme_drill_type" :value="scope.row.drillType" />
        </template>
      </el-table-column>
      <el-table-column label="演练时间" align="center" prop="drillTime" v-if="columns.drillTime.visible" width="180" />
      <el-table-column label="参演人数" align="center" prop="partakeNum" v-if="columns.partakeNum.visible" />
      <el-table-column label="参演单位" align="center" prop="partakeOrg" v-if="columns.partakeOrg.visible" :show-overflow-tooltip="true" />
      <el-table-column label="记录状态" align="center" prop="corStatus" v-if="columns.corStatus.visible" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['eme:drillrecord: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="['eme:drillrecord: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="750px" append-to-body>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="预案ID" prop="basicId">
              <el-input v-model="form.basicId" placeholder="请输入预案ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="演练类型" prop="drillType">
              <el-select v-model="form.drillType" placeholder="请选择演练类型">
                <el-option v-for="dict in eme_drill_type" :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="drillTime">
              <el-date-picker v-model="form.drillTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择演练时间" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参演人数" prop="partakeNum">
              <el-input v-model="form.partakeNum" placeholder="请输入参演人数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="参演单位" prop="partakeOrg">
              <el-input v-model="form.partakeOrg" placeholder="请输入参演单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="演练名称" prop="drillName">
              <el-input v-model="form.drillName" placeholder="请输入演练名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="预案演练ID" prop="pdrGuid">
              <el-input v-model="form.pdrGuid" placeholder="请输入预案演练ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="演练记录" prop="rdb">
              <el-input v-model="form.rdb" placeholder="请输入演练记录" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="记录状态" prop="corStatus">
              <el-input v-model="form.corStatus" 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="BusiDrillRecord">
import { listBusiDrillRecord, getBusiDrillRecord, delBusiDrillRecord, addBusiDrillRecord, updateBusiDrillRecord } from "@/api/slaj/eme"
import { useTableHeight } from '@/hooks/useTableHeight'

const { tableHeight, containerRef } = useTableHeight()
const { proxy } = getCurrentInstance()
const { eme_drill_type } = proxy.useDict("eme_drill_type")

const dataList = 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({
  drillName: { label: '演练名称', visible: true },
  drillType: { label: '演练类型', visible: true },
  drillTime: { label: '演练时间', visible: true },
  partakeNum: { label: '参演人数', visible: true },
  partakeOrg: { label: '参演单位', visible: true },
  corStatus: { label: '记录状态', visible: true }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    basicId: undefined,
    drillName: undefined,
    drillType: undefined
  },
  rules: {}
})

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

function getList() {
  loading.value = true
  listBusiDrillRecord(queryParams.value).then(res => {
    loading.value = false
    dataList.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) {
  const guid = row.guid || ids.value
  reset()
  getBusiDrillRecord(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/eme/drillrecord/detail', query: { guid: guid } })
}

function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      if (form.value.guid != undefined) {
        updateBusiDrillRecord(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addBusiDrillRecord(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

function handleDelete(row) {
  const guids = row.guid || ids.value
  proxy.$modal.confirm('是否确认删除该演练实施记录？').then(function () {
    return delBusiDrillRecord(guids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

function cancel() {
  open.value = false
  reset()
}

function handleExport() {
  proxy.download("eme/drillrecord/export", { ...queryParams.value }, `drillrecord_${new Date().getTime()}.xlsx`)
}

function reset() {
  form.value = {
    guid: undefined,
    basicId: undefined,
    drillType: undefined,
    drillTime: undefined,
    partakeNum: undefined,
    partakeOrg: undefined,
    drillName: undefined,
    pdrGuid: undefined,
    rdb: undefined,
    corStatus: undefined
  }
  proxy.resetForm("formRef")
}

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