<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 label="日期类型" prop="dateType">
        <el-select v-model="queryParams.dateType" placeholder="请选择日期类型" clearable style="width: 150px">
          <el-option v-for="dict in eme_date_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="年份" prop="dateYear">
        <el-input v-model="queryParams.dateYear" 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:planeva:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['eme:planeva:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['eme:planeva:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['eme:planeva:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="eme-planeva-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="orgNum" v-if="columns.orgNum.visible" />
      <el-table-column label="综合预案" align="center" prop="zh" v-if="columns.zh.visible" />
      <el-table-column label="专项预案" align="center" prop="zx" v-if="columns.zx.visible" />
      <el-table-column label="现场处置方案" align="center" prop="xc" v-if="columns.xc.visible" />
      <el-table-column label="申报率" align="center" prop="sbl" v-if="columns.sbl.visible" />
      <el-table-column label="演练次数" align="center" prop="yl" v-if="columns.yl.visible" />
      <el-table-column label="预案分析" align="center" prop="planFx" v-if="columns.planFx.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:planeva: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:planeva: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="startDate">
              <el-date-picker v-model="form.startDate" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始日期" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="endDate">
              <el-date-picker v-model="form.endDate" 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="orgNum">
              <el-input v-model="form.orgNum" placeholder="请输入机构数量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="综合预案" prop="zh">
              <el-input v-model="form.zh" placeholder="请输入综合预案" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="专项预案" prop="zx">
              <el-input v-model="form.zx" placeholder="请输入专项预案" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="现场处置方案" prop="xc">
              <el-input v-model="form.xc" placeholder="请输入现场处置方案" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预案总数" prop="plan">
              <el-input v-model="form.plan" placeholder="请输入预案总数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="申报率" prop="sbl">
              <el-input v-model="form.sbl" placeholder="请输入申报率" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="演练次数" prop="yl">
              <el-input v-model="form.yl" placeholder="请输入演练次数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="演练率" prop="yll">
              <el-input v-model="form.yll" placeholder="请输入演练率" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="日期类型" prop="dateType">
              <el-select v-model="form.dateType" placeholder="请选择日期类型">
                <el-option v-for="dict in eme_date_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="dateYear">
              <el-input v-model="form.dateYear" placeholder="请输入年份" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="季度" prop="dateQuarter">
              <el-input v-model="form.dateQuarter" placeholder="请输入季度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="预案分析" prop="planFx">
              <el-input v-model="form.planFx" 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="BusiPlanEva">
import { listBusiPlanEva, getBusiPlanEva, delBusiPlanEva, addBusiPlanEva, updateBusiPlanEva } from "@/api/slaj/eme"
import { useTableHeight } from '@/hooks/useTableHeight'

const { tableHeight, containerRef } = useTableHeight()
const { proxy } = getCurrentInstance()
const { eme_date_type } = proxy.useDict("eme_date_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({
  orgGuid: { label: '所属单位', visible: true },
  orgNum: { label: '机构数量', visible: true },
  zh: { label: '综合预案', visible: true },
  zx: { label: '专项预案', visible: true },
  xc: { label: '现场处置方案', visible: true },
  sbl: { label: '申报率', visible: true },
  yl: { label: '演练次数', visible: true },
  planFx: { label: '预案分析', visible: true }
})

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

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

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

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

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

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

function reset() {
  form.value = {
    guid: undefined,
    orgGuid: undefined,
    startDate: undefined,
    endDate: undefined,
    orgNum: undefined,
    zh: undefined,
    zx: undefined,
    xc: undefined,
    plan: undefined,
    sbl: undefined,
    yl: undefined,
    yll: undefined,
    dateType: undefined,
    dateYear: undefined,
    dateQuarter: undefined,
    planFx: undefined
  }
  proxy.resetForm("formRef")
}

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