import Vue from 'vue';
import { IGetUserFoldersPayload } from './types';
const state = {
  sysTemplateList: [] = [],
  userFolderList: [] = [],
  isFinished: false,
  loading: true,
  page: 0,
};
const getters = {

};
const mutations = {
  updateSysTemplateList(state: any, payload: any) {
    state.sysTemplateList = payload || [];
  },
  updateUserFolders( state: any, payload: any) {
    state.userFolderList = payload || [];
  },
  setIsFinished(state: any, payload: any) {
    state.isFinished = payload;
  },
  setPage(state: any, payload: number) {
    state.page = payload;
  },
  save(state: any, payload: any) {
    state = {
      ...state,
      ...payload,
    };
  },
  clearData(state: any) {
    state.userFolderList = [];
    state.isFinished = false;
    state.page = 0;
  },
};
const actions = {
  async getSysTemplateList({ commit }: any) {
    if (state.sysTemplateList.length <= 0) {
      const { results } = await Vue.prototype.$api.template.systemList();
      commit('updateSysTemplateList', results || []);
    }
  },
  // 如果给函数传payload，并且page有值
  async getUserFolders({ commit, state }: any, payload: IGetUserFoldersPayload = {}) {
    // tslint:disable-next-line:variable-name
    const page_size = 20;
    const { results, total } = await Vue.prototype.$api.template.folderList({
      template_type: 1,
      page: payload.page !== undefined ? payload.page : 1,
      page_size,
    });
    const ids = (results || []).map((folder: any) => folder.id);
    try {
      const res = await Vue.prototype.$api.template.list({ id: ids });
      const list = (payload.page === undefined || payload.page === 1)
      ? (res.results || [] ) : state.userFolderList.concat(res.results || []) ;
      commit('updateUserFolders', list);
      const isFinished = total < page_size;
      commit('setIsFinished', isFinished);
    } catch (err) {
      commit('updateUserFolders', []);
      // 是否完成所有内容的加载
      commit('setIsFinished', true);
    }
  },
  async getUserFoldersFixed({ commit, state }: any, payload: IGetUserFoldersPayload = {}) {
    // tslint:disable-next-line:variable-name
    commit('save', {
      loading: true,
    });
    try {
      const page_size = 20;
      const { results, total } = await Vue.prototype.$api.template.folderList({
        template_type: 1,
        page: payload.page !== undefined ? payload.page : 1,
        page_size,
      });
      const list = (payload.page === undefined || payload.page === 1)
      ? (results || [] ) : state.userFolderList.concat(results || []) ;
      commit('updateUserFolders', list);
      const isFinished = total < page_size;
      commit('setIsFinished', isFinished);
      commit('save', {
        loading: false,
      });
    } catch (err) {
      commit('updateUserFolders', []);
      // 是否完成所有内容的加载
      commit('setIsFinished', true);
      commit('save', {
        loading: false,
      });
    }
  },
};
const namespaced: boolean = true;
export const template = {
  namespaced,
  state,
  getters,
  mutations,
  actions,
};
