// import $ from 'jquery';
import axios from 'axios';

const ModulerUser = {
    state: {
        username:'',
        access: "",
        refresh: "",
        is_login: false,
        path:[],
        filecontent:'',
    },
    getters: {
    },
    mutations: {
        updateUser(state,user){
          state.username = user.username,
          state.access = user.access,
          state.refresh = user.refresh,
          state.is_login = user.is_login;
        },
        logout(state){
          state.username = '',
          state.access =  "",
          state.refresh = "",
          state.is_login = false,
          state.path = []
        },
        forwardPath(state,item){
          state.path.push(item);
        },
        backPath(state){
          state.path.pop();
        },
        updateFileContent(state,content){
          state.filecontent = content;
        },
    },
    actions: {
        login(context,data){
            axios({
              url:'http://1.94.171.222:8000/token/',
              method:'POST',
              data:{
                username:data.username,
                password:data.password,
              },
            })
            .then(resp => {
                const {access,refresh} = resp.data;
                axios({
                    url:'http://1.94.171.222:8000/getinfo/',
                    method:'GET',
                    headers:{
                        'Authorization': "Bearer " + access,
                    },
                })
                .then(resp => {
                      context.commit('updateUser',{
                        username:resp.data.username,
                        access:access,
                        refresh:refresh,
                        is_login: true,
                      });
                      context.commit('forwardPath',resp.data.username + '/'
                      );
                      data.success();
                    }
                );
              })
              .catch(() => {
                  data.error();
              }
              );
          }
    },
    modules: {
    }
}

export default ModulerUser;