<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="orgGuid">
        <el-input v-model="queryParams.orgGuid" placeholder="请输入所属单位" clearable style="width: 180px" @keyup.enter="handleQuery" />
      </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:plancount:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['eme:plancount:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['eme:plancount:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['eme:plancount:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="eme-plancount-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="orgGuid" v-if="columns.orgGuid.visible" :show-overflow-tooltip="true" />
      <el-table-column label="综合预案" align="center" prop="zhPlan" v-if="columns.zhPlan.visible" />
      <el-table-column label="专项预案" align="center" prop="zxPlan" v-if="columns.zxPlan.visible" />
      <el-table-column label="现场处置方案" align="center" prop="xcPlan" v-if="columns.xcPlan.visible" />
      <el-table-column label="机构数量" align="center" prop="orgNum" v-if="columns.orgNum.visible" />
      <el-table-column label="演练次数" align="center" prop="drillNum" v-if="columns.drillNum.visible" />
      <el-table-column label="预案演练比" align="center" prop="planDrillRa" v-if="columns.planDrillRa.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:plancount: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:plancount: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="800px" 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="orgGuid">
              <el-input v-model="form.orgGuid" placeholder="请输入所属单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="综合预案" prop="zhPlan">
              <el-input v-model="form.zhPlan" placeholder="请输入综合预案" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="专项预案" prop="zxPlan">
              <el-input v-model="form.zxPlan" placeholder="请输入专项预案" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="现场处置方案" prop="xcPlan">
              <el-input v-model="form.xcPlan" placeholder="请输入现场处置方案" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="机构数量" prop="orgNum">
              <el-input v-model="form.orgNum" placeholder="请输入机构数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应报预案机构" prop="reportPlanOrg">
              <el-input v-model="form.reportPlanOrg" placeholder="请输入应报预案机构" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="机构预案比" prop="orgPlanRa">
              <el-input v-model="form.orgPlanRa" placeholder="请输入机构预案比" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="演练次数" prop="drillNum">
              <el-input v-model="form.drillNum" placeholder="请输入演练次数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="预案演练比" prop="planDrillRa">
              <el-input v-model="form.planDrillRa" 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="BusiPlanCount">
import { listBusiPlanCount, getBusiPlanCount, delBusiPlanCount, addBusiPlanCount, updateBusiPlanCount } from "@/api/slaj/eme"
import { useTableHeight } from '@/hooks/useTableHeight'

const { tableHeight, containerRef } = useTableHeight()
const { proxy } = getCurrentInstance()

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({
  orgGuid: { label: '所属单位', visible: true },
  zhPlan: { label: '综合预案', visible: true },
  zxPlan: { label: '专项预案', visible: true },
  xcPlan: { label: '现场处置方案', visible: true },
  orgNum: { label: '机构数量', visible: true },
  drillNum: { label: '演练次数', visible: true },
  planDrillRa: { label: '预案演练比', visible: true }
})

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

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

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

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

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

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

function reset() {
  form.value = {
    guid: undefined,
    orgGuid: undefined,
    zhPlan: undefined,
    zxPlan: undefined,
    xcPlan: undefined,
    orgNum: undefined,
    reportPlanOrg: undefined,
    orgPlanRa: undefined,
    drillNum: undefined,
    planDrillRa: undefined
  }
  proxy.resetForm("formRef")
}

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