import { create } from 'zustand';
import { query } from '@/modules';
import { message } from 'antd';

type EditStore = {
  loading: boolean;
  setLoading: (loading: boolean) => void;
  formData: any;
  setFormData: (formData: any) => void;
  showEditModal: boolean;
  setShowEditModal: (showEditModal: boolean) => void;
  list: any[];
  getList: () => Promise<void>;
  updateData: (data: any) => Promise<void>;
  deleteData: (id: string) => Promise<void>;
  showPublishModal: boolean;
  setShowPublishModal: (showPublishModal: boolean) => void;
  publishData: (data: any) => Promise<void>;
  downloadData: (data: any) => Promise<void>;
};
export const useEditStore = create<EditStore>((set, get) => {
  return {
    loading: false,
    setLoading: (loading) => set({ loading }),
    formData: {},
    setFormData: (formData) => set({ formData }),
    showEditModal: false,
    setShowEditModal: (showEditModal) => set({ showEditModal }),
    list: [],
    getList: async () => {
      set({ loading: true });

      const res = await query.post({ path: 'page', key: 'list' });
      set({ loading: false });
      if (res.code === 200) {
        set({ list: res.data });
      } else {
        message.error(res.message || 'Request failed');
      }
    },
    updateData: async (data) => {
      const { getList } = get();
      const res = await query.post({
        path: 'page',
        key: 'update',
        data,
      });
      if (res.code === 200) {
        message.success('Success');
        set({ showEditModal: false, formData: [] });
        getList();
      } else {
        message.error(res.message || 'Request failed');
      }
    },
    publishData: async (data) => {
      const { getList, list } = get();
      const res = await query.post({
        path: 'page',
        key: 'publish',
        data,
      });
      if (res.code === 200) {
        message.success('Success');
        set({ formData: res.data });
        const newList = list.map((item) => {
          if (item.id === data.id) {
            return res.data;
          }
          return item;
        });
        set({ list: newList });
      } else {
        message.error(res.message || 'Request failed');
      }
    },
    downloadData: async (id) => {
      const res = await query.post({
        path: 'page',
        key: 'download',
        id: id,
      });
      if (res.code === 200) {
        message.success('Success');
        const url = new URL('resources/' + res.data, window.location.origin);
        window.open(url.href, '_blank');
      } else {
        message.error(res.message || 'Request failed');
      }
    },
    deleteData: async (id) => {
      const { getList } = get();
      const res = await query.post({
        path: 'page',
        key: 'delete',
        id,
      });
      if (res.code === 200) {
        message.success('Success');
        getList();
      } else {
        message.error(res.message || 'Request failed');
      }
    },
    showPublishModal: false,
    setShowPublishModal: (showPublishModal) => set({ showPublishModal }),
  };
});
