const baseUrl = "http://192.168.3.2:8000/"
// const baseUrl = "https://edu.ndjsxh.cn:8443/"
const ossUrl = "https://sm-sign.oss-cn-shanghai.aliyuncs.com/cert/"
const md5 = require('./md5.js');

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 formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


const formatSeconds = theTime => {
  let theTime1 = 0
  let theTime2 = 0
  if (theTime > 60) {
    theTime1 = parseInt(theTime / 60)
    theTime = parseInt(theTime % 60)
    if (theTime1 > 60) {
      theTime2 = parseInt(theTime1 / 60)
      theTime1 = parseInt(theTime1 % 60)
    }
  }
  let result = '' + parseInt(theTime) + '秒'
  if (theTime1 > 0) {
    result = '' + parseInt(theTime1) + '分' + result
  }
  if (theTime2 > 0) {
    result = '' + parseInt(theTime2) + '小时' + result
  }
  return result
}
const getMultResult = select=>{
  let prevList = ["", "A","B","C", "D", "E","F"]
  let slist = ''+select
  let res = ""
  for( let i in slist){
    res+=prevList[slist[i]]
    console.log(slist[i],  prevList[slist[i]] )
  }
  return res
}

var uploadFile = (tempFilePaths, fun) =>{
  let header={}
  let murl = `${baseUrl}upload`
  let version= "1.0"
  let mtime = parseInt(Date.now() / 1000);
  // 未登入
  let signstr = "hall_" + version +  mtime;
  let sign = md5.md5(signstr).toLowerCase()
  wx.uploadFile({
    url: `${murl}?t=${mtime}&v=1&s=${sign}&u=0`,
    filePath: tempFilePaths,
    name: "avatar",
    header:header,
    success: function (res) {
      if( res.statusCode === 200){
        let { code, data, msg} = JSON.parse(res.data)
        if( code == 200 ){
          fun&&fun(code, data)
        }else{
          fun&&fun(code, msg)
        }  
      }else{
        fun&&fun(404, "请求异常")
      }
    },
    fail: function (res) {
      fun&&fun(404, "" );
    }
  })
}

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

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

var showMsg = text => wx.showToast({
title: text,
image: '/assets/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 downloadFile = ( imgSrc, cb )=>{
  var save = wx.getFileSystemManager();
  var number = Date.now()
  save.writeFile({
    filePath: wx.env.USER_DATA_PATH + '/pic_' + number + '.png',
    data: imgSrc,
    encoding: 'base64',
    success: res => {
      wx.saveImageToPhotosAlbum({
        filePath: wx.env.USER_DATA_PATH + '/pic_' + number + '.png',
        success: function (res) {
          wx.showToast({
            title: '保存成功',
          })
          cb &&cb( true );
        },
        fail: function (err) {
          cb &&cb( false );
        }
      })
    }, fail: err => {
      cb &&cb( false );
    }
  })
}
   

module.exports = {
  baseUrl,
  ossUrl,
  downloadFile,
  formatSeconds: formatSeconds,
  formatTime: formatTime,
  getMultResult,
  uploadFile,
  showModel,
  showSuccess,
  showToast,
  showMsg,
  showBusy
}