import Vue from "vue";
import Vuex from "vuex";
import { getElevatorList } from "../api/elevator";
import { silenceLogin } from "../api/login";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    app_name: "住宅小区电梯管理平台",

    isShowMiddleMenu: true,
    leftName: "智慧电梯云平台",
    elevatorList: [],
    showSearchArea: true,
    areaCode:''
  },
  mutations: {
    CHANGE_APP_NAME(state, value) {
      state.app_name = value;
    },
    CHANGE_LEFT_NAME(state, value) {
      state.leftName = value;
    },
    CHANGE_MIDDLE_MENU(state, value) {
      state.isShowMiddleMenu = value;
    },
    RESET_APP_NAME(state) {
      state.app_name = "江夏住宅小区电梯管理平台";
    },
    SET_ELEVATOR_LIST(state, value) {
      state.elevatorList = value;
    },
    CHANGE_SEARCH(state, value) {
      state.showSearchArea = value;
    },
    CHANGE_AREA_CODE(state, value){
      state.areaCode = value
    }

  },
  actions: {
    async GET_ELEVATOR_LIST({ commit }, params) {
      try {
        const res = await getElevatorList({
          ...params,
        });
        console.log(res, "elvatorlist");
        await commit("SET_ELEVATOR_LIST", res.rows);
      } catch (error) {
        console.error(error);
      }
    },
    async SILENCE_LOGIN({ commit }, params) {
      const token = localStorage.getItem("token");
      if (token) return;

      const userInfo = await silenceLogin({
        clientId: "e5cd7e4891bf95d1d19206ce24a7b32e",
        grantType: "password",
        password: "admin123",
        tenantId: "000000",
        username: "admin",
      });
      if (userInfo.data.access_token) {
        localStorage.setItem("token", userInfo.data.access_token);
        location.reload()
      }
    },
  },
  modules: {},
});
