import axios from 'axios'
import {
  MessageBox,
  Notification,
} from 'element-ui'

export const BASE_HOST = ""
export const BASE_URI = BASE_HOST + "";

const instance = axios.create({
  baseURL: BASE_URI,
  timeout: 5000,
  method: 'post',
});

export default class Request {
  static pget(url, data) {
    let token = window.sessionStorage.token
    let userId = window.sessionStorage.userId
    let headers = {
      userId,
      token,
      "terminal": "WEB"
    };
    if (url != `${BASE_URI}login` && token)
      headers.token = token;
    return new Promise((resolve, reject) => {
      axios.get(url, data, {
          headers,

          timeout: 50000,
        })
        .then(response => {
          const {
            data
          } = response;
          resolve(data)
        })
        .catch(error => {
          if (error.response) {
            // The request was made and the server responded with a status code
            // that falls out of the range of 2xx
            console.log(error.response.data);
            console.log(error.response.status);
            console.log(error.response.headers);
            resolve(error.response.status);
          } else if (error.request) {
            // The request was made but no response was received
            // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
            // http.ClientRequest in node.js
            console.log(error.request);
            resolve('error.request');
          } else {
            // Something happened in setting up the request that triggered an Error
            console.log('Error', error.message);
            resolve(error.message);
          }
          console.log(error.config);
        });

    })
  }
  static logout() {
    console.log('asdasdasdasdasd')
    window.location.href = "#/login";
  }
  // static _post(url, data, method = "post") {
  //   let token = window.sessionStorage.token
  //   let userId = window.sessionStorage.userId
  //   let headers = {
  //     userId,
  //     token,
  //     "terminal": "WEB"
  //   };
  //   if (url != `${BASE_URI}login` && token)
  //     headers.token = token;
  //   return new Promise((resolve, reject) => {
  //     axios.post(url, data, {
  //         headers,
  //         method,
  //         timeout: 50000,
  //       })
  //       .then(response => {
  //         const {
  //           data
  //         } = response;
  //         if (data && data.code != SUCCESS) {
  //           console.log("错误码：" + data.code + " 错误信息：" + data.message)
  //         }
  //         if (data && data.code == 'L0003') {
  //           window.sessionStorage.removeItem("token")
  //           window.sessionStorage.removeItem("userId")
  //           MessageBox.alert({
  //             title: '会话超时',
  //             message: '即将退出，请重新登录',
  //             showClose: false,
  //             callback: () => {
  //               window.location.href = "/?#/" + "login"
  //             }
  //           })
  //           return;
  //         }
  //         resolve(data)
  //       })
  //       .catch(error => {
  //         if (error.response) {
  //           // The request was made and the server responded with a status code
  //           // that falls out of the range of 2xx
  //           console.log(error.response.data);
  //           console.log(error.response.status);
  //           console.log(error.response.headers);
  //           resolve({
  //             retcod: '500',
  //             retmsg: error.response.status
  //           });
  //         } else if (error.request) {
  //           // The request was made but no response was received
  //           // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
  //           // http.ClientRequest in node.js
  //           console.log(error.request);
  //           resolve({
  //             retcod: '501'
  //           });
  //         } else {
  //           // Something happened in setting up the request that triggered an Error
  //           console.log('Error', error.message);
  //           resolve({
  //             retcod: '502',
  //             retmsg: error.message
  //           });
  //         }
  //         console.log(error.config);
  //       });
  //   });
  // }
  // static _get(url, data) {

  //   return this._post(url, data, "get");
  // }

  static _base(url, method, data, params) {

    let token = window.sessionStorage.token
    let userId = window.sessionStorage.userId
    
    let headers = {
      userId,
      token,
      "terminal": "WEB"
    };
    // 统一处理前端生成的交易流水号，加到 headers 里面
    if (params && params.serialNo) {
      headers['n-serial-no'] = params.serialNo
      delete params.serialNo
    }
    if (data && data.serialNo) {
      headers['n-serial-no'] = data.serialNo
      delete data.serialNo
    }
    
    if (url != `${BASE_URI}login` && token)
      headers.token = token;

    return new Promise((resolve, reject) => {
      axios({
          url: BASE_HOST + url,
          method: method,
          data,
          params,
          headers: headers,
          timeout: 50000,
        }).then(response => {
          const {
            data
          } = response;
          if (data && data.code != SUCCESS) {
            Notification({
              type: 'error',
              title: '提示',
              message: data.msg
            })
            console.log("错误码：" + data.code + " 错误信息：" + data.msg)
          }
          if (data && data.code == 'L0003') {
            window.sessionStorage.removeItem("token")
            window.sessionStorage.removeItem("userId")
            MessageBox.alert({
              title: '会话超时',
              message: '即将退出，请重新登录',
              showClose: false,
              callback: () => {
                window.location.href = "/?#/" + "login"
              }
            })
            return;
          }
          resolve(data)
        })
        .catch(error => {
          if (error.response) {
            // The request was made and the server responded with a status code
            // that falls out of the range of 2xx
            console.log(error.response.data);
            console.log(error.response.status);
            console.log(error.response.headers);
            resolve({
              retcod: '500',
              retmsg: error.response.status
            });
          } else if (error.request) {
            // The request was made but no response was received
            // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
            // http.ClientRequest in node.js
            console.log(error.request);
            resolve({
              retcod: '501'
            });
          } else {
            // Something happened in setting up the request that triggered an Error
            console.log('Error', error.message);
            resolve({
              retcod: '502',
              retmsg: error.message
            });
          }
          console.log(error.config);
        });
    });
  }
  static post(url, data = {}, params = {}) {
    return this._base(url, 'post', data, params, BASE_HOST)
  }

  static get(url, params) {
    return this._base(url, 'get', {}, params)
  }

  static delete(url, data = {}, params = {}) {
    return this._base(url, 'delete', data, params)
  }

  static put(url, data = {}, params = {}) {
    return this._base(url, 'put', data, params)
  }
}