import axios from "axios";

//store/modules/user.js

// 声明变量
const state = {
  collapse: false,
  activeIndex: 1,
  username: "",
  password: "",
  api: "/carstop/carbook",
  newsNum: "",
};

// 修改变量（state不能直接赋值修改，只能通过mutations）
const mutations = {
  menuClose: (state, value) => {
    state.collapse = value;
  },
  indexUp: (state, value) => {
    state.activeIndex = value;
  },
  userName: (state, value) => {
    state.username = value;
  },
  passWord: (state, value) => {
    state.password = value;
  },
  newsUp: (state, value) => {
    state.newsNum = value;
  },
};
// mutations的值由actions传入(异步)
const actions = {
  addAsync: ({ commit }) => {
    setTimeout(() => {
      commit("add");
    }, 1000);
  },
  newsAsync: async ({ commit, state }) => {
    let data = new FormData();
    data.set("state", 0);
    data.set("page", 1);
    data.set("rows", 100); //前面的key记得对应！
    let res = await axios({
      method: "post",
      url: state.api + "/carBook/userqueryMes.action",
      headers: {
        token: sessionStorage.getItem("token"),
      },
      data: data,
    });
    commit("newsUp", res.data.total);
    sessionStorage.setItem("newsNum", res.data.total);
    // console.log("vuex", res.data.total);
  },
};

export default {
  state,
  mutations,
  actions,
};
