import dayjs from "dayjs";
import { message } from "@/utils/message";
import { addDialog } from "@/components/ReDialog";
import {
  getAnnouncementList,
  getAnnouncementById,
  createAnnouncement,
  updateAnnouncement,
  deleteAnnouncement,
  publishAnnouncement,
  offlineAnnouncement,
  setAnnouncementTop
} from "@/api/announcement";
import type { PaginationProps } from "@pureadmin/table";
import { deviceDetection } from "@pureadmin/utils";
import { onMounted, reactive, ref, toRaw } from "vue";
import {
  ElForm,
  ElInput,
  ElFormItem,
  ElSelect,
  ElOption,
  ElRadioGroup,
  ElRadioButton
} from "element-plus";
import type { SearchFormProps, AnnouncementItemProps } from "./types";

export function useAnnouncement() {
  const form = reactive<SearchFormProps>({
    title: "",
    type: undefined,
    status: undefined
  });
  const loading = ref(true);
  const dataList = ref([]);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });

  const typeMap = {
    1: "系统公告",
    2: "活动公告",
    3: "维护公告",
    4: "其他"
  };

  const statusMap = {
    0: { text: "草稿", type: "info" },
    1: { text: "已发布", type: "success" },
    2: { text: "已下线", type: "warning" }
  };

  const columns: TableColumnList = [
    {
      label: "公告ID",
      prop: "id",
      width: 80
    },
    {
      label: "标题",
      prop: "title",
      minWidth: 200,
      showOverflowTooltip: true
    },
    {
      label: "类型",
      prop: "type",
      minWidth: 100,
      formatter: ({ type }) => typeMap[type] || "未知"
    },
    {
      label: "置顶",
      prop: "isTop",
      minWidth: 80,
      cellRenderer: ({ row }) => (
        <el-tag type={row.isTop === 1 ? "danger" : "info"}>
          {row.isTop === 1 ? "置顶" : "否"}
        </el-tag>
      )
    },
    {
      label: "阅读量",
      prop: "readCount",
      minWidth: 90
    },
    {
      label: "状态",
      prop: "status",
      minWidth: 100,
      cellRenderer: ({ row }) => {
        const item = statusMap[row.status] || { text: "未知", type: "info" };
        return <el-tag type={item.type}>{item.text}</el-tag>;
      }
    },
    {
      label: "发布时间",
      prop: "publishTime",
      minWidth: 160,
      formatter: ({ publishTime }) =>
        publishTime ? dayjs(publishTime).format("YYYY-MM-DD HH:mm:ss") : "-"
    },
    {
      label: "操作",
      fixed: "right",
      width: 340,
      slot: "operation"
    }
  ];

  async function onSearch(resetPage = false) {
    loading.value = true;
    if (resetPage) pagination.currentPage = 1;
    try {
      const searchParams: any = {
        page: pagination.currentPage,
        pageSize: pagination.pageSize
      };
      
      // 只添加非空的搜索条件
      if (form.title) searchParams.title = form.title;
      if (form.type !== undefined) searchParams.type = form.type;
      if (form.status !== undefined) searchParams.status = form.status;
      
      const { data } = await getAnnouncementList(searchParams);
      if (data) {
        dataList.value = data.list || [];
        pagination.total = Number(data.total) || 0;
      }
    } finally {
      loading.value = false;
    }
  }

  function resetForm(formEl) {
    if (!formEl) return;
    formEl.resetFields();
    pagination.currentPage = 1;
    onSearch();
  }

  async function handleDelete(row) {
    const { code, message: msg } = await deleteAnnouncement(row.id);
    if (code === 200) {
      message("删除成功", { type: "success" });
      onSearch();
    } else {
      message(msg || "删除失败", { type: "error" });
    }
  }

  async function handlePublish(row) {
    const { code, message: msg } = await publishAnnouncement(row.id);
    if (code === 200) {
      message("发布成功", { type: "success" });
      onSearch();
    } else {
      message(msg || "发布失败", { type: "error" });
    }
  }

  async function handleOffline(row) {
    const { code, message: msg } = await offlineAnnouncement(row.id);
    if (code === 200) {
      message("下线成功", { type: "success" });
      onSearch();
    } else {
      message(msg || "下线失败", { type: "error" });
    }
  }

  async function handleSetTop(row) {
    const newIsTop = row.isTop === 1 ? 0 : 1;
    const { code, message: msg } = await setAnnouncementTop(row.id, newIsTop);
    if (code === 200) {
      message(newIsTop === 1 ? "置顶成功" : "取消置顶成功", { type: "success" });
      onSearch();
    } else {
      message(msg || "操作失败", { type: "error" });
    }
  }

  function handleSizeChange(val: number) {
    pagination.pageSize = val;
    pagination.currentPage = 1;
    onSearch();
  }

  function handleCurrentChange(val: number) {
    pagination.currentPage = val;
    onSearch();
  }

  function openDialog(title = "新增", row?: AnnouncementItemProps) {
    const formInline = reactive({
      id: row?.id,
      title: row?.title ?? "",
      content: row?.content ?? "",
      type: row?.type ?? 1,
      coverImage: row?.coverImage ?? "",
      status: row?.status ?? 0
    });

    const ruleFormRef = ref();

    addDialog({
      title: `${title}公告`,
      props: {
        formInline,
        ruleFormRef
      },
      width: "46%",
      draggable: true,
      fullscreen: deviceDetection(),
      contentRenderer: () => (
        <ElForm
          ref={(el: any) => { ruleFormRef.value = el; }}
          model={formInline}
          labelWidth="100px"
        >
          <ElFormItem
            label="公告标题"
            prop="title"
            rules={[{ required: true, message: "请输入公告标题", trigger: "blur" }]}
          >
            <ElInput v-model={formInline.title} placeholder="请输入公告标题" clearable />
          </ElFormItem>
          <ElFormItem
            label="公告类型"
            prop="type"
            rules={[{ required: true, message: "请选择公告类型", trigger: "change" }]}
          >
            <ElSelect v-model={formInline.type} placeholder="请选择类型" class="w-full">
              <ElOption label="系统公告" value={1} />
              <ElOption label="活动公告" value={2} />
              <ElOption label="维护公告" value={3} />
              <ElOption label="其他" value={4} />
            </ElSelect>
          </ElFormItem>
          <ElFormItem
            label="公告内容"
            prop="content"
            rules={[{ required: true, message: "请输入公告内容", trigger: "blur" }]}
          >
            <ElInput
              v-model={formInline.content}
              type="textarea"
              placeholder="请输入公告内容"
              rows={6}
            />
          </ElFormItem>
          <ElFormItem label="封面图片" prop="coverImage">
            <ElInput v-model={formInline.coverImage} placeholder="请输入封面图片URL" clearable />
          </ElFormItem>
          <ElFormItem label="状态" prop="status">
            <ElRadioGroup v-model={formInline.status}>
              <ElRadioButton label={0}>草稿</ElRadioButton>
              <ElRadioButton label={1}>立即发布</ElRadioButton>
            </ElRadioGroup>
          </ElFormItem>
        </ElForm>
      ),
      beforeSure: async (done, { options }) => {
        if (!ruleFormRef.value) {
          message("表单未初始化，请关闭弹窗重试", { type: "error" });
          return;
        }

        try {
          const valid = await ruleFormRef.value.validate().catch(() => false);
          if (!valid) {
            message("请完善必填信息", { type: "warning" });
            return;
          }

          const formData = toRaw(formInline);
          if (formData.id) {
            const { code, message: msg } = await updateAnnouncement(formData.id, formData);
            if (code === 200) {
              message(`修改公告 ${formData.title} 成功`, { type: "success" });
              done();
              onSearch();
            } else {
              message(msg || "修改失败", { type: "error" });
            }
          } else {
            const { code, message: msg } = await createAnnouncement(formData);
            if (code === 200) {
              message(`新增公告 ${formData.title} 成功`, { type: "success" });
              done();
              onSearch();
            } else {
              message(msg || "新增失败", { type: "error" });
            }
          }
        } catch (error) {
          message("操作失败", { type: "error" });
        }
      }
    });
  }

  onMounted(() => {
    onSearch();
  });

  return {
    form,
    loading,
    columns,
    dataList,
    pagination,
    typeMap,
    statusMap,
    onSearch,
    resetForm,
    openDialog,
    handleDelete,
    handlePublish,
    handleOffline,
    handleSetTop,
    handleSizeChange,
    handleCurrentChange
  };
}
