<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="planName">
        <el-input v-model="queryParams.planName" placeholder="请输入预案名称" clearable style="width: 180px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="预案类型" prop="planType">
        <el-select v-model="queryParams.planType" placeholder="请选择预案类型" clearable style="width: 150px">
          <el-option v-for="dict in eme_plan_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="预案状态" prop="planStatus">
        <el-select v-model="queryParams.planStatus" placeholder="请选择预案状态" clearable style="width: 150px">
          <el-option v-for="dict in eme_plan_status" :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:conplanbasic:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['eme:conplanbasic:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['eme:conplanbasic:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['eme:conplanbasic:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="eme-conplanbasic-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="planName" v-if="columns.planName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="预案类型" align="center" prop="planType" v-if="columns.planType.visible">
        <template #default="scope">
          <dict-tag :options="eme_plan_type" :value="scope.row.planType" />
        </template>
      </el-table-column>
      <el-table-column label="预案状态" align="center" prop="planStatus" v-if="columns.planStatus.visible">
        <template #default="scope">
          <dict-tag :options="eme_plan_status" :value="scope.row.planStatus" />
        </template>
      </el-table-column>
      <el-table-column label="录入时间" align="center" prop="entryTime" v-if="columns.entryTime.visible" width="180" />
      <el-table-column label="修订信息" align="center" prop="revisionInfo" v-if="columns.revisionInfo.visible" :show-overflow-tooltip="true" />
      <el-table-column label="上级预案" align="center" prop="parentName" v-if="columns.parentName.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:conplanbasic: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:conplanbasic: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="预案名称" prop="planName">
              <el-input v-model="form.planName" placeholder="请输入预案名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属单位" prop="orgGuid">
              <el-input v-model="form.orgGuid" placeholder="请输入所属单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="录入时间" prop="entryTime">
              <el-date-picker v-model="form.entryTime" 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="evaCycle">
              <el-input v-model="form.evaCycle" placeholder="请输入评估周期" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否修订" prop="revised">
              <el-input v-model="form.revised" placeholder="请输入是否修订" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预案类型" prop="planType">
              <el-select v-model="form.planType" placeholder="请选择预案类型">
                <el-option v-for="dict in eme_plan_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="planStatus">
              <el-select v-model="form.planStatus" placeholder="请选择预案状态">
                <el-option v-for="dict in eme_plan_status" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上级预案ID" prop="parentId">
              <el-input v-model="form.parentId" placeholder="请输入上级预案ID" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="修订信息" prop="revisionInfo">
              <el-input v-model="form.revisionInfo" type="textarea" placeholder="请输入修订信息" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="上级预案" prop="parentName">
              <el-input v-model="form.parentName" placeholder="请输入上级预案" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="修订时间" prop="reviseTime">
              <el-date-picker v-model="form.reviseTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择修订时间" style="width: 100%" />
            </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="BusiConplanBasic">
import { listBusiConplanBasic, getBusiConplanBasic, delBusiConplanBasic, addBusiConplanBasic, updateBusiConplanBasic } from "@/api/slaj/eme"
import { useTableHeight } from '@/hooks/useTableHeight'

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

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({
  planName: { label: '预案名称', visible: true },
  planType: { label: '预案类型', visible: true },
  planStatus: { label: '预案状态', visible: true },
  entryTime: { label: '录入时间', visible: true },
  revisionInfo: { label: '修订信息', visible: true },
  parentName: { label: '上级预案', visible: true }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    planName: undefined,
    planType: undefined,
    planStatus: undefined
  },
  rules: {
    planName: [{ required: true, message: "预案名称不能为空", trigger: "blur" }]
  }
})

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

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

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

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

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

function reset() {
  form.value = {
    guid: undefined,
    planName: undefined,
    orgGuid: undefined,
    entryTime: undefined,
    evaCycle: undefined,
    revised: undefined,
    planType: undefined,
    planStatus: undefined,
    parentId: undefined,
    revisionInfo: undefined,
    parentName: undefined,
    reviseTime: undefined
  }
  proxy.resetForm("formRef")
}

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