<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="培训名称" prop="trainName">
        <el-input v-model="queryParams.trainName" placeholder="请输入培训名称" clearable style="width: 180px" @keyup.enter="handleQuery" />
      </el-form-item>
      <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="trainType">
        <el-select v-model="queryParams.trainType" placeholder="请选择培训类型" clearable style="width: 150px">
          <el-option v-for="dict in eme_train_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:trainrecord:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['eme:trainrecord:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['eme:trainrecord:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['eme:trainrecord:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="eme-trainrecord-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="trainName" v-if="columns.trainName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="培训类型" align="center" prop="trainType" v-if="columns.trainType.visible">
        <template #default="scope">
          <dict-tag :options="eme_train_type" :value="scope.row.trainType" />
        </template>
      </el-table-column>
      <el-table-column label="培训时间" align="center" prop="trainTime" v-if="columns.trainTime.visible" width="180" />
      <el-table-column label="参训人数" align="center" prop="partNum" v-if="columns.partNum.visible" />
      <el-table-column label="培训地点" align="center" prop="trainAddr" v-if="columns.trainAddr.visible" :show-overflow-tooltip="true" />
      <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:trainrecord: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:trainrecord: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="700px" 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="trainType">
              <el-select v-model="form.trainType" placeholder="请选择培训类型">
                <el-option v-for="dict in eme_train_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="trainTime">
              <el-date-picker v-model="form.trainTime" 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="partNum">
              <el-input v-model="form.partNum" placeholder="请输入参训人数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="培训名称" prop="trainName">
              <el-input v-model="form.trainName" placeholder="请输入培训名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="培训地点" prop="trainAddr">
              <el-input v-model="form.trainAddr" placeholder="请输入培训地点" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="培训内容" prop="trainCont">
              <el-input v-model="form.trainCont" 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="BusiTrainRecord">
import { listBusiTrainRecord, getBusiTrainRecord, delBusiTrainRecord, addBusiTrainRecord, updateBusiTrainRecord } from "@/api/slaj/eme"
import { useTableHeight } from '@/hooks/useTableHeight'

const { tableHeight, containerRef } = useTableHeight()
const { proxy } = getCurrentInstance()
const { eme_train_type } = proxy.useDict("eme_train_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({
  trainName: { label: '培训名称', visible: true },
  trainType: { label: '培训类型', visible: true },
  trainTime: { label: '培训时间', visible: true },
  partNum: { label: '参训人数', visible: true },
  trainAddr: { label: '培训地点', visible: true }
})

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

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

function getList() {
  loading.value = true
  listBusiTrainRecord(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()
  getBusiTrainRecord(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/trainrecord/detail', query: { guid: guid } })
}

function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      if (form.value.guid != undefined) {
        updateBusiTrainRecord(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addBusiTrainRecord(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 delBusiTrainRecord(guids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

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

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

function reset() {
  form.value = {
    guid: undefined,
    basicId: undefined,
    trainType: undefined,
    trainTime: undefined,
    partNum: undefined,
    trainCont: undefined,
    trainName: undefined,
    trainAddr: undefined
  }
  proxy.resetForm("formRef")
}

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