import Vue from 'vue';
import Vuex from 'vuex';
import _ from 'lodash';
Vue.use(Vuex);
const api = {
  mechanism: `/api/live/v0/cysci/mechanism`,
};
const state = () => ({});
const mutations = {};

const actions = {
  async query({ commit }, { skip = 0, limit, ...info } = {}) {
    const res = await this.$axios.$get(`${api.mechanism}`, {
      skip,
      limit,
      ...info,
    });
    return res;
  },
  async create({ commit }, payload) {
    const res = await this.$axios.$post(`${api.mechanism}`, payload);
    return res;
  },
  async fetch({ commit }, payload) {
    const res = await this.$axios.$get(`${api.mechanism}/${payload}`);
    return res;
  },
  async update({ commit }, { id, ...data }) {
    const res = await this.$axios.$post(`${api.mechanism}/update/${id}`, data);
    return res;
  },

  async delete({ commit }, payload) {
    const res = await this.$axios.$delete(`${api.mechanism}/${payload}`);
    return res;
  },
  async password({ commit }, { id, passwd }) {
    const res = await this.$axios.$post(`${api.mechanism}/password/${id}`, { passwd });
    return res;
  },
  async login({ commit }, payload) {
    const res = await this.$axios.$post(`${api.mechanism}/login`, payload);
    const jwt = require('jsonwebtoken');
    if (res.errcode === 0) {
      localStorage.setItem('token', res.data);
      localStorage.setItem('type', 'ZJJG');
      const user = jwt.decode(res.data);
      commit('setUser', user, { root: true });
    }
    return res;
  },
};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
