<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="上报期间" prop="repPeri">
        <el-input v-model="queryParams.repPeri" placeholder="请输入上报期间" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="上报状态" prop="repStat">
        <el-select v-model="queryParams.repStat" placeholder="请选择上报状态" clearable style="width: 240px">
          <el-option v-for="dict in rep_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="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['hidd:monrepperi:add']">快速新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['hidd:monrepperi:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['hidd:monrepperi:remove']">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="hidd-fast-index"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="monRepPeriList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="上报单位" align="center" prop="repOrgGuid" v-if="columns.repOrgGuid.visible" :show-overflow-tooltip="true" />
      <el-table-column label="上报期间" align="center" prop="repPeri" v-if="columns.repPeri.visible" />
      <el-table-column label="上报状态" align="center" prop="repStat" v-if="columns.repStat.visible">
        <template #default="scope">
          <dict-tag :options="rep_stat" :value="scope.row.repStat" />
        </template>
      </el-table-column>
      <el-table-column label="上报时间" align="center" prop="repTime" v-if="columns.repTime.visible" width="180" />
      <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:monrepperi: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:monrepperi: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="800px" append-to-body>
      <el-form :model="form" :rules="rules" ref="monRepPeriRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="上报期间" prop="repPeri">
              <el-input v-model="form.repPeri" placeholder="请输入上报期间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上报状态" prop="repStat">
              <el-select v-model="form.repStat" placeholder="请选择上报状态">
                <el-option v-for="dict in rep_stat" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">快速录入隐患</el-divider>

        <el-table :data="quickHiddList" border>
          <el-table-column label="隐患名称" align="center">
            <template #default="scope">
              <el-input v-model="scope.row.hiddName" placeholder="请输入隐患名称" />
            </template>
          </el-table-column>
          <el-table-column label="隐患等级" align="center" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.hiddGrad" placeholder="请选择">
                <el-option v-for="dict in hidd_grad" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="隐患类别" align="center" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.hiddClas" placeholder="请选择">
                <el-option v-for="dict in hidd_clas" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="涉及部位" align="center">
            <template #default="scope">
              <el-input v-model="scope.row.proPart" placeholder="请输入涉及部位" />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="80">
            <template #default="scope">
              <el-tooltip content="删除" placement="top">
                <el-button link type="primary" icon="Delete" @click="removeQuickHidd(scope.$index)"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" plain icon="Plus" @click="addQuickHidd" class="mt10">添加隐患</el-button>
      </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="HiddFast">
import { listBisOrgMonRepPeri, getBisOrgMonRepPeri, delBisOrgMonRepPeri, addBisOrgMonRepPeri, updateBisOrgMonRepPeri } from "@/api/slaj/hidd"
import { addObjHidd } from "@/api/slaj/hidd"

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

const monRepPeriList = 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 quickHiddList = ref([])

const columns = ref({
  repOrgGuid: { label: '上报单位', visible: true },
  repPeri: { label: '上报期间', visible: true },
  repStat: { label: '上报状态', visible: true },
  repTime: { label: '上报时间', visible: true }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    repPeri: undefined,
    repStat: undefined
  },
  rules: {
    repPeri: [{ required: true, message: "上报期间不能为空", trigger: "blur" }]
  }
})

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

/** 查询月报期间列表 */
function getList() {
  loading.value = true
  listBisOrgMonRepPeri(queryParams.value).then(res => {
    loading.value = false
    monRepPeriList.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()
  quickHiddList.value = [
    { hiddName: '', hiddGrad: '', hiddClas: '', proPart: '' }
  ]
  open.value = true
  title.value = "快速新增月报"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const guid = row.guid || ids.value
  getBisOrgMonRepPeri(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/hidd/monthDetail', query: { guid: guid } })
}

/** 添加隐患行 */
function addQuickHidd() {
  quickHiddList.value.push({ hiddName: '', hiddGrad: '', hiddClas: '', proPart: '' })
}

/** 删除隐患行 */
function removeQuickHidd(index) {
  quickHiddList.value.splice(index, 1)
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["monRepPeriRef"].validate(valid => {
    if (valid) {
      // 先保存月报
      addBisOrgMonRepPeri(form.value).then(response => {
        const monthGuid = response.data
        // 保存快速录入的隐患
        const promises = quickHiddList.value.map(hidd => {
          if (hidd.hiddName) {
            const hiddData = {
              ...hidd,
              repGuid: monthGuid,
              hiddStat: '0'
            }
            return addObjHidd(hiddData)
          }
          return Promise.resolve()
        })
        Promise.all(promises).then(() => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      })
    }
  })
}

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

/** 重置表单 */
function reset() {
  form.value = {
    guid: undefined,
    repOrgGuid: undefined,
    repPeri: undefined,
    repStat: undefined,
    repTime: undefined,
    returnDesc: undefined
  }
  proxy.resetForm("monRepPeriRef")
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

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

<style scoped>
.mt10 {
  margin-top: 10px;
}
</style>
