<template>
  <div class="p-2">
    <Pageheader title="公告管理"></Pageheader>
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="公告标题" prop="noticeTitle">
              <el-input v-model="queryParams.noticeTitle" placeholder="请输入公告标题" clearable @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-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['notice:notice:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['notice:notice:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['notice:notice:remove']"
              >删除</el-button
            >
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="noticeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="公告标题" align="center" prop="noticeTitle" />
        <el-table-column label="作者" align="center" prop="author" />
        <el-table-column label="紧急程度" align="center" prop="urgencyLevel">
          <template #default="scope">
            <dict-tag :options="com_noticepc_level" :value="scope.row.urgencyLevel"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="发布状态" align="center" prop="publishStatus">
          <template #default="scope">
            <dict-tag :options="com_publish_status" :value="scope.row.publishStatus"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="已读" align="center" prop="readNum" />
        <el-table-column label="未读" align="center" prop="unReadNum" />
        <el-table-column label="发布人" align="center" prop="publishBy" />
        <el-table-column label="发布日期" align="center" prop="publishTime" width="100">
          <template #default="scope">
            <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="220" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['notice:notice:edit']">修改</el-button>
            <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['notice:notice: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-card>
  </div>
</template>

<script setup name="Notice" lang="ts">
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from '@/api/system/NoticePc/index';
import { NoticeVO, NoticeQuery, NoticeForm } from '@/api/system/NoticePc/type';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { com_noticepc_level } = toRefs<any>(proxy?.useDict('com_noticepc_level'));
const { com_publish_status } = toRefs<any>(proxy?.useDict('com_publish_status'));

const noticeList = ref<NoticeVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const noticeFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: NoticeForm = {
  noticeId: undefined,
  noticeTitle: undefined,
  noticeContent: undefined,
  author: undefined,
  urgencyLevel: undefined,
  publishStatus: undefined,
  publishBy: undefined,
  publishTime: undefined
};
const data = reactive<PageData<NoticeForm, NoticeQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    noticeTitle: undefined,
    noticeContent: undefined,
    author: undefined,
    urgencyLevel: undefined,
    publishStatus: undefined,
    publishBy: undefined,
    publishTime: undefined,
    params: {}
  },
  rules: {
    noticeId: [{ required: true, message: '公告ID不能为空', trigger: 'blur' }],
    noticeTitle: [{ required: true, message: '公告标题不能为空', trigger: 'blur' }],
    urgencyLevel: [{ required: true, message: '紧急程度不能为空', trigger: 'blur' }],
    publishStatus: [{ required: true, message: '发布状态不能为空', trigger: 'change' }]
  }
});

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

/** 查询公告信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listNotice(queryParams.value);
  noticeList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: NoticeVO[]) => {
  ids.value = selection.map((item) => item.noticeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};
const router = useRouter();
/** 新增按钮操作 */
const handleAdd = () => {
  router.push({
    path: '/repair/addNoticePc'
  });
};

/** 修改按钮操作 */
const handleUpdate = async (row?: NoticeVO) => {
  router.push({
    path: '/repair/addNoticePc',
    query: {
      id: row.noticeId
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: NoticeVO) => {
  const _noticeIds = row?.noticeId || ids.value;
  await proxy?.$modal.confirm('该信息删除后无法恢复，确定要删除吗？').finally(() => (loading.value = false));
  await delNotice(_noticeIds);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

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