import Vuex from 'vuex';
import Vue from 'vue';
import * as types from './types';

Vue.use(Vuex);
export default new Vuex.Store({
  state: {
    userInfo: {}, // 用户信息
    token: null,
    title: '',
    show: false,
    bgHeight: window.screen.availHeight - 440 - 24 + 'px',
    baseInfo: {}, // 基础信息
    frontInfo: {}, // 前端配置
    backInfo: {}, // 后端配置
    resultInfo: {}, // 返回结果配置
    tempBackData: [] // 后端判断禁用数据
  },
  mutations: {
    [types.LOGIN]: (state, data) => {
      sessionStorage.token = data;
      state.token = data;
    },
    [types.LOGOUT]: state => {
      sessionStorage.removeItem('token');
      state.token = null;
    },
    [types.TITLE]: (state, data) => {
      state.title = data;
    },
    [types.SHOW]: (state, data) => {
      state.show = data;
    },
    saveUser: (state, data) => {
      localStorage.removeItem('userInfo');
      localStorage.setItem('userInfo', JSON.stringify(data));
      state.userInfo = JSON.parse(localStorage.getItem('userInfo'));
    },
    clearUser: state => {
      state.userInfo = {};
      if (localStorage.getItem('userInfo')) {
        localStorage.removeItem('userInfo');
      }
    },
    saveBase: (state, data) => {
      state.baseInfo = data;
    },
    saveFront: (state, data) => {
      state.frontInfo = data;
    },
    saveBack: (state, data) => {
      state.backInfo = data;
    },
    saveResult: (state, data) => {
      state.resultInfo = data;
    },
    saveTempBack: (state, data) => {
      state.tempBackData = data;
    },
    clearTempBack: state => {
      state.tempBackData = [];
    },
    clearData: state => {
      state.baseInfo = {};
      state.frontInfo = {};
      state.backInfo = {};
      state.resultInfo = {};
    }
  },
  actions: {
    saveUserInfo(context, data) {
      context.commit('saveUser', data);
    },
    clearUserInfo(context, data) {
      context.commit('clearUser', data);
    },
    saveBaseInfo(context, data) {
      context.commit('saveBase', data);
    },
    saveFrontInfo(context, data) {
      context.commit('saveFront', data);
    },
    saveBackInfo(context, data) {
      context.commit('saveBack', data);
    },
    saveResultInfo(context, data) {
      context.commit('saveResult', data);
    },
    saveTempBackData(context, data) {
      context.commit('saveTempBack', data);
    },
    clearTempBackData(context, data) {
      context.commit('clearTempBack');
    },
    clearApiData(context, data) {
      context.commit('clearData');
    }
  }
});
