import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';
import _ from 'lodash';
Vue.use(Vuex);
const api = {
  list: `/api/cms/column/list`,
  fetch: id => `/api/cms/column/fetch/${id}`,
};
export const state = () => ({});
export const mutations = {};
export const actions = {
  //请求所有的栏目
  async getAllColumn({ commit, dispatch }, payload) {
    let toGet = () => {
      let res = [];
      for (const id of payload) {
        res.push(dispatch('getColumn', { type: 'list', data: { parent_id: id } }));
      }
      return res;
    };
    let result = await axios.all(toGet());
    //降维
    let arr = _.flattenDeep(_.flattenDeep(result).map(item => item.data));
    return arr;
  },
  async getColumn({ commit }, { type, data }) {
    let result;
    if (type === `list`) {
      result = await this.$axios.$get(api.list, { ...data, is_use: '0' });
    } else {
      let { id } = data;
      result = await this.$axios.$get(api.fetch(id));
    }

    return result;
  },
};
