<template>
  <div ref="containerRef" class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="隐患名称" prop="hiddName">
        <el-input v-model="queryParams.hiddName" placeholder="请输入隐患名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="会商主题" prop="consTheme">
        <el-input v-model="queryParams.consTheme" placeholder="请输入会商主题" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="会商时间" style="width: 308px">
        <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </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="['hidd:objhidd:add']">新增会商</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['hidd:objhidd:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['hidd:objhidd:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['hidd:objhidd:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="hidd-consult-index"></right-toolbar>
    </el-row>

    <div class="table-wrapper">
    <el-table :height="tableHeight" v-loading="loading" :data="consultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="隐患名称" align="center" prop="hiddName" v-if="columns.hiddName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="会商主题" align="center" prop="consTheme" v-if="columns.consTheme.visible" :show-overflow-tooltip="true" />
      <el-table-column label="会商时间" align="center" prop="consTime" v-if="columns.consTime.visible" width="180" />
      <el-table-column label="会商地点" align="center" prop="consPlace" v-if="columns.consPlace.visible" :show-overflow-tooltip="true" />
      <el-table-column label="主持人" align="center" prop="consHost" v-if="columns.consHost.visible" />
      <el-table-column label="会商结论" align="center" prop="consResult" v-if="columns.consResult.visible" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['hidd:objhidd: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="['hidd:objhidd: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="consultRef" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="隐患名称" prop="hiddGuid">
              <el-input v-model="form.hiddName" placeholder="请选择隐患" disabled />
              <el-input v-model="form.hiddGuid" v-show="false" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="会商主题" prop="consTheme">
              <el-input v-model="form.consTheme" placeholder="请输入会商主题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="会商时间" prop="consTime">
              <el-date-picker v-model="form.consTime" type="datetime" placeholder="请选择会商时间" value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会商地点" prop="consPlace">
              <el-input v-model="form.consPlace" placeholder="请输入会商地点" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="主持人" prop="consHost">
              <el-input v-model="form.consHost" placeholder="请输入主持人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参会人员" prop="consMembers">
              <el-input v-model="form.consMembers" placeholder="请输入参会人员" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="会商内容" prop="consContent">
              <el-input v-model="form.consContent" type="textarea" :rows="4" placeholder="请输入会商内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="会商结论" prop="consResult">
              <el-input v-model="form.consResult" type="textarea" :rows="3" 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>

    <!-- 会商详情对话框 -->
    <el-dialog title="会商详情" v-model="detailOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="隐患名称" :span="2">{{ detailData.hiddName }}</el-descriptions-item>
        <el-descriptions-item label="会商主题" :span="2">{{ detailData.consTheme }}</el-descriptions-item>
        <el-descriptions-item label="会商时间">{{ detailData.consTime }}</el-descriptions-item>
        <el-descriptions-item label="会商地点">{{ detailData.consPlace }}</el-descriptions-item>
        <el-descriptions-item label="主持人">{{ detailData.consHost }}</el-descriptions-item>
        <el-descriptions-item label="参会人员">{{ detailData.consMembers }}</el-descriptions-item>
        <el-descriptions-item label="会商内容" :span="2">{{ detailData.consContent }}</el-descriptions-item>
        <el-descriptions-item label="会商结论" :span="2">{{ detailData.consResult }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup name="HiddConsult">
import { listObjCons, getObjCons, delObjCons, addObjCons, updateObjCons } from "@/api/slaj/hidd"
import { useTableHeight } from '@/hooks/useTableHeight'

const { tableHeight, containerRef } = useTableHeight()

const { proxy } = getCurrentInstance()

const consultList = 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 dateRange = ref([])
const detailOpen = ref(false)
const detailData = ref({})

const columns = ref({
  hiddName: { label: '隐患名称', visible: true },
  consTheme: { label: '会商主题', visible: true },
  consTime: { label: '会商时间', visible: true },
  consPlace: { label: '会商地点', visible: true },
  consHost: { label: '主持人', visible: true },
  consResult: { label: '会商结论', visible: true }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    hiddName: undefined,
    consTheme: undefined
  },
  rules: {
    hiddGuid: [{ required: true, message: "隐患不能为空", trigger: "change" }],
    consTheme: [{ required: true, message: "会商主题不能为空", trigger: "blur" }],
    consTime: [{ required: true, message: "会商时间不能为空", trigger: "change" }]
  }
})

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

/** 查询会商列表 */
function getList() {
  loading.value = true
  listObjCons(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false
    consultList.value = res.rows
    total.value = res.total
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  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) {
  reset()
  const guid = row.guid || ids.value
  getObjCons(guid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改会商"
  })
}

/** 详情按钮操作 */
function handleDetail(row) {
  detailData.value = row
  detailOpen.value = true
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["consultRef"].validate(valid => {
    if (valid) {
      if (form.value.guid != undefined) {
        updateObjCons(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addObjCons(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const guids = row.guid || ids.value
  proxy.$modal.confirm('是否确认删除会商编号为"' + guids + '"的数据项？').then(function () {
    return delObjCons(guids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("hidd/cons/export", {
    ...queryParams.value
  }, `hiddConsult_${new Date().getTime()}.xlsx`)
}

/** 重置表单 */
function reset() {
  form.value = {
    guid: undefined,
    hiddGuid: undefined,
    hiddName: undefined,
    consTheme: undefined,
    consTime: undefined,
    consPlace: undefined,
    consHost: undefined,
    consMembers: undefined,
    consContent: undefined,
    consResult: undefined
  }
  proxy.resetForm("consultRef")
}

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