<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="reviType">
        <el-select v-model="queryParams.reviType" placeholder="请选择评审类型" clearable style="width: 150px">
          <el-option v-for="dict in eme_revi_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="评审结果" prop="reviResu">
        <el-select v-model="queryParams.reviResu" placeholder="请选择评审结果" clearable style="width: 150px">
          <el-option v-for="dict in eme_revi_resu" :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:reviewinfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['eme:reviewinfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['eme:reviewinfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['eme:reviewinfo:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="eme-reviewinfo-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="预案ID" align="center" prop="basicId" v-if="columns.basicId.visible" :show-overflow-tooltip="true" />
      <el-table-column label="评审时间" align="center" prop="reviTime" v-if="columns.reviTime.visible" width="180" />
      <el-table-column label="评审机构" align="center" prop="reviOrg" v-if="columns.reviOrg.visible" :show-overflow-tooltip="true" />
      <el-table-column label="评审人" align="center" prop="reviPers" v-if="columns.reviPers.visible" />
      <el-table-column label="评审意见" align="center" prop="reviOpin" v-if="columns.reviOpin.visible" :show-overflow-tooltip="true" />
      <el-table-column label="评审结果" align="center" prop="reviResu" v-if="columns.reviResu.visible">
        <template #default="scope">
          <dict-tag :options="eme_revi_resu" :value="scope.row.reviResu" />
        </template>
      </el-table-column>
      <el-table-column label="评审类型" align="center" prop="reviType" v-if="columns.reviType.visible">
        <template #default="scope">
          <dict-tag :options="eme_revi_type" :value="scope.row.reviType" />
        </template>
      </el-table-column>
      <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:reviewinfo: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:reviewinfo: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="reviTime">
              <el-date-picker v-model="form.reviTime" 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="reviOrg">
              <el-input v-model="form.reviOrg" placeholder="请输入评审机构" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评审人" prop="reviPers">
              <el-input v-model="form.reviPers" placeholder="请输入评审人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="评审意见" prop="reviOpin">
              <el-input v-model="form.reviOpin" type="textarea" placeholder="请输入评审意见" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="评审结果" prop="reviResu">
              <el-select v-model="form.reviResu" placeholder="请选择评审结果">
                <el-option v-for="dict in eme_revi_resu" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评审类型" prop="reviType">
              <el-select v-model="form.reviType" placeholder="请选择评审类型">
                <el-option v-for="dict in eme_revi_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </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="BusiReviewInfo">
import { listBusiReviewInfo, getBusiReviewInfo, delBusiReviewInfo, addBusiReviewInfo, updateBusiReviewInfo } from "@/api/slaj/eme"
import { useTableHeight } from '@/hooks/useTableHeight'

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

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({
  basicId: { label: '预案ID', visible: true },
  reviTime: { label: '评审时间', visible: true },
  reviOrg: { label: '评审机构', visible: true },
  reviPers: { label: '评审人', visible: true },
  reviOpin: { label: '评审意见', visible: true },
  reviResu: { label: '评审结果', visible: true },
  reviType: { label: '评审类型', visible: true }
})

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

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

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

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

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

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

function reset() {
  form.value = {
    guid: undefined,
    basicId: undefined,
    reviTime: undefined,
    reviOrg: undefined,
    reviPers: undefined,
    reviOpin: undefined,
    reviResu: undefined,
    reviType: undefined
  }
  proxy.resetForm("formRef")
}

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