<template>
  <div class="p-2">
    <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="title">
              <el-input v-model="queryParams.title" placeholder="请输入公告标题" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="操作人员" prop="createByName">
              <el-input v-model="queryParams.createByName" placeholder="请输入操作人员" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="公告状态" clearable>
                <el-option v-for="dict in sys_notice_status" :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-card>
      </div>
    </transition>

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

      <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="false" label="序号" align="center" prop="notificationId" width="100" />
        <el-table-column label="公告标题" align="center" prop="title" :show-overflow-tooltip="true" />
        <el-table-column label="通知渠道" align="center" prop="channels" width="120">
          <template #default="scope">
            <div class="channel-tags">
              <el-tag
                v-for="channel in getChannelList(scope.row.channels)"
                :key="channel"
                size="small"
                type="info"
                style="margin: 1px"
              >
                {{ getChannelName(channel) }}
              </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" width="100">
          <template #default="scope">
            <dict-tag :options="sys_notice_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="有效期" align="center" prop="validDays" width="80">
          <template #default="scope">
            <span>{{ scope.row.validDays || '-' }}天</span>
          </template>
        </el-table-column>
        <el-table-column label="接收人数" align="center" width="100">
          <template #default="scope">
            <span class="stat-value">{{ scope.row.totalUsers || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建者" align="center" prop="createByName" width="100" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ proxy.parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['system:notification:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['system:notification:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>

    <!-- 引入公告表单组件 -->
    <notice-form v-model:visible="formVisible" :edit-type="editType" :row-data="currentRow" @save="handleSave"></notice-form>
  </div>
</template>

<script setup name="Notice" lang="ts">
import { delNotice, listNotice } from '@/api/system/notice';
import { NoticeQuery, NoticeVO } from '@/api/system/notice/types';
import NoticeForm from './noticeForm.vue';

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

// 表单控制状态
const formVisible = ref(false);
const editType = ref('');
const currentRow = ref<NoticeVO | null>(null);

const noticeList = ref<NoticeVO[]>([]);
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 queryParams = reactive<NoticeQuery>({
  pageNum: 1,
  pageSize: 10,
  title: '',                        // 改为title
  createByName: '',
  status: ''
});

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

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

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

/** 多选框选中数据 */
const handleSelectionChange = (selection: NoticeVO[]) => {
  ids.value = selection.map((item) => item.notificationId);     // 改为notificationId
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  currentRow.value = null;
  editType.value = 'add';
  formVisible.value = true;
};

/** 修改按钮操作 */
const handleUpdate = (row?: NoticeVO) => {
  if (row) {
    currentRow.value = row;
  } else {
    const selectedNotice = noticeList.value.find((item) => item.notificationId === ids.value[0]);    // 改为notificationId
    currentRow.value = selectedNotice || null;
  }
  editType.value = 'edit';
  formVisible.value = true;
};

/** 保存成功后的处理 */
const handleSave = () => {
  getList();
};

/** 删除按钮操作 */
const handleDelete = async (row?: NoticeVO) => {
  const noticeIds = row?.notificationId || ids.value;          // 改为notificationId
  await proxy?.$modal.confirm('是否确认删除该数据项');
  await delNotice(noticeIds);
  await getList();
  proxy?.$modal.msgSuccess('删除成功');
};

/** 获取通知渠道列表 */
const getChannelList = (channels: string) => {
  if (!channels) return [];
  try {
    return JSON.parse(channels);
  } catch {
    return [];
  }
};

/** 获取通知渠道名称 */
const getChannelName = (channel: string) => {
  const channelMap: Record<string, string> = {
    system: '系统',
    sms: '短信',
    email: '邮件'
  };
  return channelMap[channel] || channel;
};

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

<style scoped>
.channel-tags {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 2px;
}

.stat-value {
  font-weight: 500;
  color: #409eff;
}
</style>
