<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </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="['portal:djxx:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['portal:djxx:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['portal:djxx:remove']">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="djxxList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="编号" align="center" prop="id" width="80" />
      <el-table-column label="标题" align="center" prop="title" min-width="200" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="state" width="100" />
      <el-table-column label="发布人" align="center" prop="pubUser" width="120" />
      <el-table-column label="发布时间" align="center" prop="pubTm" width="160">
        <template #default="scope">
          <span>{{ parseTime(scope.row.pubTm) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleDetail(scope.row)">查看</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['portal:djxx:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['portal:djxx:remove']">删除</el-button>
        </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 ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" maxlength="200" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="状态" prop="state">
              <el-select v-model="form.state" placeholder="请选择状态" clearable style="width: 100%">
                <el-option label="草稿" value="草稿" />
                <el-option label="已发布" value="已发布" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发布人" prop="pubUser">
              <el-input v-model="form.pubUser" placeholder="请输入发布人" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发布时间" prop="pubTm">
              <el-date-picker v-model="form.pubTm" type="datetime" placeholder="选择发布时间" value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="内容" prop="content">
              <el-input v-model="form.content" type="textarea" :rows="8" placeholder="请输入内容" maxlength="5000" />
            </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="700px" append-to-body>
      <div v-if="detailData">
        <h2 style="text-align:center;margin-bottom:12px">{{ detailData.title }}</h2>
        <div style="text-align:center;color:#999;font-size:13px;margin-bottom:16px">
          <span>发布人：{{ detailData.pubUser }}</span>
          <span style="margin-left:16px">发布时间：{{ parseTime(detailData.pubTm) }}</span>
          <span style="margin-left:16px">状态：{{ detailData.state }}</span>
        </div>
        <div style="border-top:1px solid #ebeef5;padding-top:16px;white-space:pre-wrap;line-height:1.8">{{ detailData.content }}</div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detailOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Djxx">
import { listDjxx, getDjxx, addDjxx, updateDjxx, delDjxx } from "@/api/portal/djxx"

const { proxy } = getCurrentInstance()

const djxxList = ref([])
const open = ref(false)
const detailOpen = ref(false)
const detailData = ref(null)
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 data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: undefined
  },
  rules: {
    title: [{ required: true, message: "标题不能为空", trigger: "blur" }]
  }
})

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

function getList() {
  loading.value = true
  listDjxx(queryParams.value).then(res => {
    loading.value = false
    djxxList.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.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

function handleDetail(row) {
  getDjxx(row.id).then(res => {
    detailData.value = res.data
    detailOpen.value = true
  })
}

function handleAdd() {
  reset()
  open.value = true
  title.value = "新增党建信息"
}

function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getDjxx(id).then(res => {
    form.value = res.data
    open.value = true
    title.value = "修改党建信息"
  })
}

function handleDelete(row) {
  const idsToDel = row.id || ids.value
  proxy.$modal.confirm('是否确认删除编号为"' + idsToDel + '"的数据项？').then(function() {
    return delDjxx(idsToDel)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

function reset() {
  form.value = {
    id: undefined,
    title: undefined,
    state: "草稿",
    pubTm: undefined,
    pubUser: undefined,
    pubUserId: undefined,
    content: undefined
  }
  proxy.resetForm("formRef")
}

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

function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updateDjxx(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addDjxx(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

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