var env = 'development';
const md5 = require('./md5.js');
var wiki = require('./wiki.js');
var location = require('./location.js');
const baseUrl = "https://edu.ndjsxh.cn:8443/api"
// const baseUrl = "http://localhost:8888"

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const filterTime = ( str) => {
  if(!str) return "---";
  return str.substr(0,19).replace("T", " ");
}

const curDate = () =>{
  var nowDate = new Date();
  var year = nowDate.getFullYear();
  var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1)  : nowDate.getMonth() + 1;
  var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
  return year + "-" + month + "-" + day;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 显示繁忙提示
var showBusy = text => wx.showToast({
    title: text,
    icon: 'loading',
    duration: 10000
})

// 显示成功提示
var showSuccess = text => wx.showToast({
    title: text,
    icon: 'success'
})

var showMsg = text => wx.showToast({
  title: text,
  image: '/img/img_close.png',
  duration: 1000,
  mask: true
})

var showToast = (errCode, okText, text) => {
  if( errCode ==200 ){
    showSuccess( okText );
  }else{
    showMsg( text )
  }
}

// 显示失败提示
var showModel = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: JSON.stringify(content),
        showCancel: false
    })
}

// 请求
var http = (method, data, fun) => {
    let header = { 'content-type': 'application/json'}
    let authDB = wx.getStorageSync("@yunyuanqu");
    let user_id = authDB&&authDB.ID||0;
    let token=authDB&&authDB.token||''
    console.log( "authDB", authDB)
    if( user_id && token ){
      header['x-token'] = token
      header['x-user-id']= user_id
    }
    let body = JSON.stringify(data);
    wx.request({
    method: "POST",
    url: `${baseUrl}${method}`,
    data: body,
    header:header,
    success: function(res) {
      if( res.statusCode === 200){
        let { code, data, msg} = res.data
        if( code == 0 ){
          fun&&fun(code, data)
        }else{
          showModel("请求异常", msg)
          fun&&fun(code, msg)
        }  
      }else{
        fun&&fun(404, "请求异常")
      }
    },
    fail: function(res) {
      showModel("请求异常", "网络异常")
      fun&&fun(404, "请求异常")
    }
  })
}
var uploadFile = (tempFilePaths, fun) =>{
    let authDB = wx.getStorageSync("@yunyuanqu");
    let user_id = authDB&&authDB.ID||0;
    let token=authDB&&authDB.token||''
    let header={}
    if( user_id && token ){
      header['x-token'] = token
      header['x-user-id']= user_id
    }
    wx.uploadFile({
      url: `${baseUrl}/wx/upload`,
      filePath: tempFilePaths,
      name: "file",
      header:header,
      success: function (res) {
        if( res.statusCode === 200){
          let { code, data, msg} = JSON.parse(res.data)
          console.log( code, data, msg )
          if( code == 0 ){
            fun&&fun(code, data)
          }else{
            fun&&fun(code, msg)
          }  
        }else{
          fun&&fun(404, "请求异常")
        }
      },
      fail: function (res) {
        fun&&fun(404, "" );
      }
    })
}

module.exports = { 
  formatTime, 
  curDate,
  showBusy, 
  showSuccess, 
  showMsg, 
  showModel, 
  filterTime,
  http, 
  uploadFile, 
  showToast, 
  wiki,
  baseUrl,
  location
}
