import { create } from 'zustand';
import { query } from '@/modules';
import { message } from '@/modules/message';
type ContainerStore = {
  showEdit: boolean;
  setShowEdit: (showEdit: boolean) => void;
  showPublish: boolean;
  setShowPublish: (showPublish: boolean) => void;
  formData: any;
  setFormData: (formData: any) => void;
  loading: boolean;
  setLoading: (loading: boolean) => void;
  list: any[];
  getList: () => Promise<void>;
  updateData: (data: any, opts?: { closePublish?: boolean; closeEdit?: boolean; refresh?: boolean; showTips?: boolean }) => Promise<any>;
  deleteData: (id: string) => Promise<void>;
  openDrawEdit: boolean;
  setOpenDrawEdit: (openDrawEdit: boolean) => void;
  getOne: (id: string) => Promise<void>;
  data: any;
  setData: (data: any) => void;
};
export const useContainerStore = create<ContainerStore>((set, get) => {
  return {
    showEdit: false,
    setShowEdit: (showEdit) => set({ showEdit }),
    showPublish: false,
    setShowPublish: (showPublish) => set({ showPublish }),
    formData: {},
    setFormData: (formData) => set({ formData }),
    loading: false,
    setLoading: (loading) => set({ loading }),
    list: [],
    getList: async () => {
      set({ loading: true });
      const res = await query.post({
        path: 'container',
        key: 'list',
      });
      set({ loading: false });
      if (res.code === 200) {
        set({ list: res.data });
      } else {
        message.error(res.message || 'Request failed');
      }
    },
    updateData: async (data, opts) => {
      const { getList } = get();
      const closePublish = opts?.closePublish ?? true;
      const closeEdit = opts?.closeEdit ?? true;
      const refresh = opts?.refresh ?? true;
      const res = await query.post({
        path: 'container',
        key: 'update',
        data,
      });

      if (res.code === 200) {
        if (opts?.showTips ?? true) {
          message.success('Success', {
            position: 'top-right',
          });
        }
        set({ formData: res.data });
        if (refresh) {
          getList();
        }
        if (closePublish) {
          set({ showPublish: false });
        }
        if (closeEdit) {
          set({ showEdit: false });
        }
      } else {
        message.error(res.message || 'Request failed');
      }
      return res;
    },
    deleteData: async (id) => {
      const { getList } = get();
      const res = await query.post({
        path: 'container',
        key: 'delete',
        id,
      });
      if (res.code === 200) {
        getList();
        message.success('Success');
      } else {
        message.error(res.message || 'Request failed');
      }
    },
    openDrawEdit: false,
    setOpenDrawEdit: (openDrawEdit) => set({ openDrawEdit }),
    getOne: async (id) => {
      set({ loading: true, data: {} });
      const res = await query.post({
        path: 'container',
        key: 'get',
        id,
      });
      set({ loading: false });
      if (res.code === 200) {
        set({ data: res.data });
      } else {
        message.error(res.message || 'Request failed');
      }
    },
    data: {},
    setData: (data) => set({ data }),
  };
});
