<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="hiddGrad">
        <el-select v-model="queryParams.hiddGrad" placeholder="请选择隐患等级" clearable style="width: 240px">
          <el-option v-for="dict in hidd_grad" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="隐患状态" prop="hiddStat">
        <el-select v-model="queryParams.hiddStat" placeholder="请选择隐患状态" clearable style="width: 240px">
          <el-option v-for="dict in hidd_stat" :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="danger" plain icon="CircleCheck" :disabled="multiple" @click="handleBatchCancel" v-hasPermi="['hidd:objhidd:edit']">批量销号</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="hidd-cancel-index"></right-toolbar>
    </el-row>

    <div class="table-wrapper">
    <el-table :height="tableHeight" v-loading="loading" :data="objHiddList" @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="hiddGrad" v-if="columns.hiddGrad.visible">
        <template #default="scope">
          <dict-tag :options="hidd_grad" :value="scope.row.hiddGrad" />
        </template>
      </el-table-column>
      <el-table-column label="隐患类别" align="center" prop="hiddClas" v-if="columns.hiddClas.visible">
        <template #default="scope">
          <dict-tag :options="hidd_clas" :value="scope.row.hiddClas" />
        </template>
      </el-table-column>
      <el-table-column label="隐患状态" align="center" prop="hiddStat" v-if="columns.hiddStat.visible">
        <template #default="scope">
          <dict-tag :options="hidd_stat" :value="scope.row.hiddStat" />
        </template>
      </el-table-column>
      <el-table-column label="涉及部位" align="center" prop="proPart" v-if="columns.proPart.visible" :show-overflow-tooltip="true" />
      <el-table-column label="采集时间" align="center" prop="collTime" v-if="columns.collTime.visible" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <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="CircleCheck" @click="handleCancel(scope.row)" v-hasPermi="['hidd:objhidd:edit']"></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="隐患销号" v-model="cancelOpen" width="600px" append-to-body>
      <el-form :model="cancelForm" :rules="cancelRules" ref="cancelRef" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="隐患名称">
              <el-input v-model="cancelForm.hiddName" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="销号原因" prop="cancelReason">
              <el-input v-model="cancelForm.cancelReason" type="textarea" placeholder="请输入销号原因" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitCancel">确 定</el-button>
          <el-button @click="cancelOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 批量销号对话框 -->
    <el-dialog title="批量销号" v-model="batchCancelOpen" width="600px" append-to-body>
      <el-form :model="batchCancelForm" :rules="batchCancelRules" ref="batchCancelRef" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="销号数量">
              <el-tag type="danger">{{ batchCancelForm.count }} 条隐患</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="销号原因" prop="cancelReason">
              <el-input v-model="batchCancelForm.cancelReason" type="textarea" placeholder="请输入销号原因" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitBatchCancel">确 定</el-button>
          <el-button @click="batchCancelOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="HiddCancel">
import { listObjHidd, getObjHidd, updateObjHidd } from "@/api/slaj/hidd"
import { useTableHeight } from '@/hooks/useTableHeight'

const { tableHeight, containerRef } = useTableHeight()

const { proxy } = getCurrentInstance()
const { hidd_grad, hidd_stat, hidd_clas } = proxy.useDict("hidd_grad", "hidd_stat", "hidd_clas")

const objHiddList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)

const columns = ref({
  hiddName: { label: '隐患名称', visible: true },
  hiddGrad: { label: '隐患等级', visible: true },
  hiddClas: { label: '隐患类别', visible: true },
  hiddStat: { label: '隐患状态', visible: true },
  proPart: { label: '涉及部位', visible: true },
  collTime: { label: '采集时间', visible: true }
})

// 单个销号对话框
const cancelOpen = ref(false)
const cancelForm = ref({})
const cancelRules = {
  cancelReason: [{ required: true, message: "销号原因不能为空", trigger: "blur" }]
}

// 批量销号对话框
const batchCancelOpen = ref(false)
const batchCancelForm = ref({})
const batchCancelRules = {
  cancelReason: [{ required: true, message: "销号原因不能为空", trigger: "blur" }]
}

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    hiddName: undefined,
    hiddGrad: undefined,
    hiddStat: undefined
  }
})

const { queryParams } = toRefs(data)

/** 查询隐患列表 */
function getList() {
  loading.value = true
  listObjHidd(queryParams.value).then(res => {
    loading.value = false
    objHiddList.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 handleDetail(row) {
  const guid = row.guid
  proxy.$router.push({ path: '/slaj/hidd/detail', query: { guid: guid } })
}

/** 单个销号按钮 */
function handleCancel(row) {
  cancelForm.value = {
    guid: row.guid,
    hiddName: row.hiddName,
    cancelReason: undefined
  }
  cancelOpen.value = true
}

/** 提交单个销号 */
function submitCancel() {
  proxy.$refs["cancelRef"].validate(valid => {
    if (valid) {
      const data = {
        guid: cancelForm.value.guid,
        hiddStat: '2',
        note: cancelForm.value.cancelReason
      }
      updateObjHidd(data).then(response => {
        proxy.$modal.msgSuccess("销号成功")
        cancelOpen.value = false
        getList()
      })
    }
  })
}

/** 批量销号按钮 */
function handleBatchCancel() {
  batchCancelForm.value = {
    guids: ids.value,
    count: ids.value.length,
    cancelReason: undefined
  }
  batchCancelOpen.value = true
}

/** 提交批量销号 */
function submitBatchCancel() {
  proxy.$refs["batchCancelRef"].validate(valid => {
    if (valid) {
      const reason = batchCancelForm.value.cancelReason
      const promises = batchCancelForm.value.guids.map(guid => {
        const data = {
          guid: guid,
          hiddStat: '2',
          note: reason
        }
        return updateObjHidd(data)
      })
      Promise.all(promises).then(() => {
        proxy.$modal.msgSuccess("批量销号成功")
        batchCancelOpen.value = false
        getList()
      })
    }
  })
}

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