const app = getApp()
function is_loading() {
  wx.showLoading({
    title: '加载中',
  })
}

function over_loading() {
  wx.hideLoading()
}

function tips_fail() {
  wx.showToast({
    title: '网络错误',
    icon: 'none',
    duration: 2000,
  })
}
function postDataByName(data, success_fn) {
  wx.setStorageSync('isLoading', 'true')
  wx.request({
    url: app.globalData.g_url + 'authdata/PostDataByName',
    data: data,
    method: 'POST',
    header: {
      'Content-Type': 'json',
      token: app.globalData.g_token,
    },
    success: function (res) {
      //console.log("res",res)
      over_loading()
      wx.setStorageSync('isLoading', 'false')
      if (res.data.code == 200) {
        success_fn(res.data)
      }
    },
    fail: function (error) {
      console.log(error)
      tips_fail()
      over_loading()
      wx.setStorageSync('isLoading', 'false')
    },
  })
}

function execDataByConfig(data, success_fn) {
  wx.setStorageSync('isLoading', 'true')
  is_loading()
  wx.request({
    url: app.globalData.g_url + 'authdata/ExecDataByConfig',
    data: data,
    method: 'POST',
    header: {
      'Content-Type': 'json',
      token: app.globalData.g_token,
    },
    success: function (res) {
      //console.log("res",res)
      if (res.data.code == 200) {
        success_fn(res.data)
      }
      over_loading()
      wx.setStorageSync('isLoading', 'false')
    },
    fail: function (error) {
      console.log(error)
      tips_fail()
      over_loading()
      wx.setStorageSync('isLoading', 'false')
    },
  })
}

function getDataByName(data, success_fn) {
  wx.setStorageSync('isLoading', 'true')
  is_loading()
  wx.request({
    url: app.globalData.g_url + 'authdata/GetDataByName',
    data: data,
    method: 'POST',
    header: {
      'Content-Type': 'json',
      token: app.globalData.g_token,
    },
    success: function (res) {
      //console.log("res",res)
      if (res.data.code == 200) {
        success_fn(res.data)
      }
      over_loading()
      wx.setStorageSync('isLoading', 'false')
    },
    fail: function (error) {
      console.log(error)
      tips_fail()
      over_loading()
      wx.setStorageSync('isLoading', 'false')
    },
  })
}

function getDataByNames(data, success_fn) {
  wx.setStorageSync('isLoading', 'true')
  is_loading()
  wx.request({
    url: app.globalData.g_url + 'authdata/GetDataByNames',
    data: data,
    method: 'POST',
    header: {
      'Content-Type': 'json',
      token: app.globalData.g_token,
    },
    success: function (res) {
      //console.log("res",res)
      if (res.data.code == 200) {
        success_fn(res.data)
      }
      over_loading()
      wx.setStorageSync('isLoading', 'false')
    },
    fail: function (error) {
      console.log(error)
      tips_fail()
      over_loading()
      wx.setStorageSync('isLoading', 'false')
    },
  })
}

function ajax(url, data, method, success_fn, error_fn) {
  wx.setStorageSync('isLoading', 'true')
  is_loading()
  wx.request({
    url: url,
    data: data,
    method: method,
    header: {
      'Content-Type': 'json',
    },
    success: function (res) {
      //console.log("res",res)

      if (res.data.code == 200) {
        success_fn(res.data)
      } else {
        if (error_fn) {
          error_fn(res)
        }
      }

      over_loading()
      wx.setStorageSync('isLoading', 'false')
    },
    fail: function (error) {
      console.log(error)
      tips_fail()
      over_loading()
      wx.setStorageSync('isLoading', 'false')
    },
  })
}

// function ajax(url, data, method, success_fn, error_fn) {
//   is_loading()
//   wx.request({
//     url: url,
//     data: data,
//     method: method,
//     header: {
//       "Content-Type": "json"
//     },
//     success: function (res) {

//       if (res.data.code == 1 || res.data.code == 0) {
//         success_fn(res)
//         over_loading()
//       } else {
//         if (error_fn) {
//           error_fn(res)
//         }
//         over_loading()
//       }
//     },
//     fail: function (error) {
//       console.log(error)
//       over_loading()
//       tips_fail()
//     }
//   })
// }

function getYesterday() {
  var day1 = new Date()
  day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000)
  var s1 = day1.getFullYear() + '-' + (day1.getMonth() + 1) + '-' + day1.getDate()
  return s1
}

function getRealToday() {
  var day = new Date()
  day.setTime(day.getTime())
  var s =
    day.getFullYear() +
    '-' +
    (day.getMonth() + 1).toString().padStart(2, '0') +
    '-' +
    day.getDate().toString().padStart(2, '0')

  return s
}

function getReal7daysToday() {
  var day11 = new Date()
  day11.setTime(day11.getTime() - 24 * 6 * 60 * 60 * 1000)
  var s11 = day11.getFullYear() + '-' + (day11.getMonth() + 1) + '-' + day11.getDate()
  return s11
}

function getToday() {
  var day2 = new Date()
  day2.setTime(day2.getTime())
  var s2 =
    day2.getFullYear() +
    '-' +
    String(day2.getMonth() + 1).padStart(2, '0') +
    '-' +
    String(day2.getDate() + 1).padStart(2, '0')
  return s2
}

function get7daysAgo() {
  var day4 = new Date()
  day4.setTime(day4.getTime() - 24 * 6 * 60 * 60 * 1000)
  var s4 =
    day4.getFullYear() +
    '-' +
    String(day4.getMonth() + 1).padStart(2, '0') +
    '-' +
    String(day4.getDate()).padStart(2, '0')
  return s4
}

function get15daysAgo() {
  var day5 = new Date()
  day5.setTime(day5.getTime() - 24 * 15 * 60 * 60 * 1000)
  var s5 = day5.getFullYear() + '-' + (day5.getMonth() + 1) + '-' + day5.getDate()
  return s5
}

function get91daysAgo() {
  var day6 = new Date()
  day6.setTime(day6.getTime() - 24 * 90 * 60 * 60 * 1000)
  var s6 = day6.getFullYear() + '-' + (day6.getMonth() + 1) + '-' + day6.getDate()
  return s6
}

function getTomorrow() {
  var day3 = new Date()
  day3.setTime(day3.getTime() + 24 * 60 * 60 * 1000)
  var s3 = day3.getFullYear() + '-' + (day3.getMonth() + 1) + '-' + day3.getDate()
  return s3
}

module.exports = {
  ajax: ajax,
  getDataByName: getDataByName,
  postDataByName: postDataByName,
  execDataByConfig: execDataByConfig,
  getYesterday: getYesterday,
  getToday: getToday,
  getRealToday: getRealToday,
  getReal7daysToday: getReal7daysToday,
  get7daysAgo: get7daysAgo,
  get91daysAgo: get91daysAgo,
  get15daysAgo: get15daysAgo,
  getTomorrow: getTomorrow,
  getDataByNames: getDataByNames,
}
