import { ElLoading, ElMessage } from 'element-plus'
import axios from 'axios'
import { useAuthStore } from '@/store';//导入用户仓库  
const env = import.meta.env

import { useRouter,useRoute } from 'vue-router';


console.debug('env',env,env.VITE_BASE_API)

const instance = axios.create({
  baseURL: env.VITE_BASE_API,
  timeout: 30000,
  showLoading: false,
  showError: true,
  token: true,//控制是否需要携带请求头令牌
  returnData: true,//返回
});

let loading = null;

// 添加请求拦截器
instance.interceptors.request.use(function (config) {
  //如果需要携带请求头令牌,则从用户仓库中获取token设置请求头x-token中
  if (config.token) {
    let authStore = useAuthStore();
    if (authStore.isLogin) {
      config.headers["Authorization"] = "Bearer "+authStore.token;
    }
  }
  if (config.showLoading) {
    loading = ElLoading.service({
      fullscreen: true,
      lock: true,
      text: 'Loading',
      background: 'rgba(0, 0, 0, 0.1)',
    });
  }

  // 在发送请求之前做些什么
  return config;
}, function (error) {
  if (loading != null) {
    loading.close();
    loading = null;
  }
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  if (loading != null) {
    loading.close();
    loading = null;
  }

  // console.debug(response);
  //对错误信息进行全局提示
  if (response.config.showError) {
    if ("code" in response.data) {
      // if (!response.data.ok) {
      //   ElMessage.error(response.data.message);
      // }
      if (response.data.code != 200) {
        ElMessage.error(response.data.msg || response.data.message);
        if(response.data.code == 401) {
          let authStore = AuthStore();
          authStore.logout();
          useRouter().push({path:'/login', query: { to: encodeURIComponent(useRoute().fullPath) }});
        }
      }
    }
  }
  // console.debug(response)
  if (response.config.returnData) {
    return response.data;
  } else {
    return response;
  }
}, function (error) {
  if (loading != null) {
    loading.close();
    loading = null;
  }
  // 对响应错误做点什么
  ElMessage.error("响应错误:" + error.message);
  return Promise.reject(error);
});

/**
* 格式化参数成URLSearchParams
* @param {String|Array|Object|HTMLFormElement|FormData|URLSearchParams} param
* @return {URLSearchParams}
*/
instance.params = function (param) {
  if (param == null || param == "") {
    return new URLSearchParams();
  }
  if (param.constructor == Array) {
    let param1 = new URLSearchParams();
    for (let obj of param) {
      param1.append(obj.name, obj.value);
    }
    param = param1;
  } else if (param.constructor == Object) {
    let param1 = new URLSearchParams();
    for (let name in param) {
      param1.append(name, param[name]);
    }
    param = param1;
  } else {
    if (param.constructor == HTMLFormElement) {
      param = new FormData(param);
    }
    if (param.constructor == FormData || param.constructor == String) {
      param = new URLSearchParams(param);
    }
  }

  return param;
}

instance.formData = function (param) {
  if (param == null || param == "") {
    return new FormData();
  }
  if (param.constructor == Array) {
    let param1 = new FormData();
    for (let obj of param) {
      param1.append(obj.name, obj.value);
    }
    return param1;
  }
  if (param.constructor == Object) {
    let param1 = new FormData();
    for (let name in param) {
      param1.append(name, param[name]);
    }
    return param1;
  }

  if (param.constructor == HTMLFormElement) {
    return new FormData(param);
  }
  return new FormData();
}


export const request = instance;
export const params = instance.params;
export const formData = instance.formData;