<template>
  <div 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="dateRange">
        <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:rectimpl:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['hidd:rectimpl:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['hidd:rectimpl:remove']">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="hidd-rectimpl-index"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="implList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="实施时间" align="center" prop="collTime" v-if="columns.collTime.visible" width="180" />
      <el-table-column label="实施内容" align="center" prop="corrMeas" v-if="columns.corrMeas.visible" :show-overflow-tooltip="true" />
      <el-table-column label="记录人" align="center" prop="recPers" v-if="columns.recPers.visible" />
      <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" 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:rectimpl:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['hidd:rectimpl:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <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="implRef" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="隐患关联" prop="hiddGuid">
              <el-select v-model="form.hiddGuid" placeholder="请选择隐患" filterable clearable style="width: 100%">
                <el-option v-for="item in hiddList" :key="item.guid" :label="item.hiddName" :value="item.guid" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="实施时间" prop="collTime">
              <el-date-picker v-model="form.collTime" 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="recPers">
              <el-input v-model="form.recPers" placeholder="请输入记录人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="整改措施" prop="corrMeas">
              <el-input v-model="form.corrMeas" type="textarea" placeholder="请输入整改措施" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="note">
              <el-input v-model="form.note" 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="BisHiddRectImpl">
import { listBisHiddRectImpl, getBisHiddRectImpl, delBisHiddRectImpl, addBisHiddRectImpl, updateBisHiddRectImpl } from "@/api/slaj/hidd"
import { listObjHidd } from "@/api/slaj/hidd"

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

const implList = ref([])
const hiddList = 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 columns = ref({
  collTime: { label: '实施时间', visible: true },
  corrMeas: { label: '整改措施', visible: true },
  recPers: { label: '记录人', visible: true },
  hiddStat: { label: '隐患状态', visible: true }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    hiddName: undefined,
    beginTime: undefined,
    endTime: undefined
  },
  rules: {
    hiddGuid: [{ required: true, message: "隐患关联不能为空", trigger: "change" }],
    collTime: [{ required: true, message: "实施时间不能为空", trigger: "blur" }],
    corrMeas: [{ required: true, message: "整改措施不能为空", trigger: "blur" }],
    recPers: [{ required: true, message: "记录人不能为空", trigger: "blur" }]
  }
})

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

/** 查询综治实施列表 */
function getList() {
  loading.value = true
  listBisHiddRectImpl(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false
    implList.value = res.rows
    total.value = res.total
  })
}

/** 查询隐患列表 */
function getHiddList() {
  listObjHidd({ pageNum: 1, pageSize: 1000 }).then(res => {
    hiddList.value = res.rows
  })
}

/** 搜索按钮操作 */
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
  getBisHiddRectImpl(guid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改综治实施"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["implRef"].validate(valid => {
    if (valid) {
      if (form.value.guid != undefined) {
        updateBisHiddRectImpl(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addBisHiddRectImpl(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 delBisHiddRectImpl(guids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

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

/** 重置表单 */
function reset() {
  form.value = {
    guid: undefined,
    hiddGuid: undefined,
    collTime: undefined,
    corrMeas: undefined,
    recPers: undefined,
    note: undefined,
    hiddStat: undefined
  }
  proxy.resetForm("implRef")
}

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