import axios from 'axios'
import Cookies from 'js-cookie'
import router from '@/router'
import qs from 'qs'
import { clearLoginInfo } from '@/utils'
import isPlainObject from 'lodash/isPlainObject'

const http = axios.create({
  baseURL: window.SITE_CONFIG['apiURL'],
  timeout: 1000 * 180,
  withCredentials: true
})
const baseURLL = 'http://192.168.10.157:8080/hmit-admin';
export function gettoken() {
  let token = localStorage.getItem("token");
  return token;
}
/**
 * 请求拦截
 */
http.interceptors.request.use(config => {
  config.headers['Accept-Language'] = Cookies.get('language') || 'zh-CN'
  config.headers['token'] = Cookies.get('token') || ''
  // 默认参数
  var defaults = {}
  // 防止缓存，GET请求默认带_t参数
  if (config.method === 'get') {
    config.params = {
      ...config.params,
      ...{ '_t': new Date().getTime() }
    }
  }
  if (isPlainObject(config.params)) {
    config.params = {
      ...defaults,
      ...config.params
    }
  }
  if (isPlainObject(config.data)) {
    config.data = {
      ...defaults,
      ...config.data
    }
    if (/^application\/x-www-form-urlencoded/.test(config.headers['content-type'])) {
      config.data = qs.stringify(config.data)
    }
  }
  return config
}, error => {
  return Promise.reject(error)
})

/**
 * 响应拦截
 */
http.interceptors.response.use(response => {
  if (response.data.code === 401 || response.data.code === 10001) {
    clearLoginInfo()
    router.replace({ name: 'login' })
    return Promise.reject(response.data.msg)
  }
  return response
}, error => {
  console.error(error)
  return Promise.reject(error)
})

export const getData = (url, data) => {
  let token = gettoken();
  return new Promise(function (resolve, reject) {
    axios
      .get(baseURLL + url, {
        params: data,
        headers: {
          "Content-Type": "application/json;charset=UTF-8",
          token: token,
        },
      })
      .then(
        (res) => {
          console.log("token", token);
          if (res.status === 200) {
            resolve(res);
          } else {
            reject(res);
          }
        },
        (err) => {
          reject(err);
        }
      );
  });
};

export const deleteData = (url, data) => {
  let token = gettoken();
  return new Promise(function (resolve, reject) {
    axios
      .delete(baseURLL + url, {
        params: data,
        headers: {
          "Content-Type": "application/json;charset=UTF-8",
          token: token,
        },
      })
      .then(
        (res) => {
          console.log("token", token);
          if (res.status === 200) {
            resolve(res);
          } else {
            reject(res);
          }
        },
        (err) => {
          reject(err);
        }
      );
  });
};
export const postData = (url, data) => {
  let token = gettoken();
  return new Promise(function (resolve, reject) {
    axios
      .post(baseURLL + url, data, {
        headers: {
          "Content-Type": "application/json;charset=UTF-8",
          token: token,
        },
      })
      .then(
        //zcps_token
        (res) => {
          console.log("token", token);
          if (res.status === 200) {
            resolve(res);
          } else {
            reject(res);
          }
        }
      );
  });
  console.log("aaaaaaaaa", token);
};

export const putData = (url, data) => {
  let token = gettoken();
  return new Promise(function (resolve, reject) {
    axios
      .post(baseURLL + url, data, {
        headers: {
          "Content-Type": "application/json;charset=UTF-8",
          token: token,
        },
      })
      .then(
        //zcps_token
        (res) => {
          console.log("token", token);
          if (res.status === 200) {
            resolve(res);
          } else {
            reject(res);
          }
        }
      );
  });
};
export const postDataFile = (url, data) => {
  return new Promise(function (resolve, reject) {
    axios
      .post(baseURLL + url, data, {

        headers: {
          "Content-Type": "multipart/form-data",
          Accept: "application/json",
        },
      })
      .then(
        //zcps_token
        (res) => {
          if (res.status === 200) {
            resolve(res);
          } else {
            reject(res);
          }
        }
      );
  });
};

export const postDataImage = (url, data, remark, flowchartid) => {
  return new Promise(function (resolve, reject) {
    axios
      .post(baseURLL + url, data, {
        params: { remark: remark, flowchartid: flowchartid },
        headers: {
          "Content-Type": "multipart/form-data",
          Accept: "application/json",
        },
      })
      .then(
        //zcps_token
        (res) => {
          if (res.status === 200) {
            resolve(res);
          } else {
            reject(res);
          }
        }
      );
  });
};
export const postFile = (url, data) => {
  /*  return new Promise(function (resolve,reject) {
    axios.post(`${baseUrl}${ url }?${{headers: { 'Content-Type': 'multipart/form-data' }}}${formatDataQuery(data)}`).then(
      (res)=>{
        if(res.status===200){
          resolve(res.data)
        }else{
          reject(res)
        }
      },
      (err)=>{
        console.log('err',err)
        reject(err)
      }
    )
  })*/
};
export const postDataWithQuery = (url, data) => {
  return new Promise(function (resolve, reject) {
    axios.post(`${baseURLL}${url}?${formatDataQuery(data)}`).then(
      (res) => {
        if (res.status === 200) {
          resolve(res.data);
        } else {
          reject(res);
        }
      },
      (err) => {
        console.log("err", err);
        reject(err);
      }
    );
  });
};

export default http
