const api = require('../../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mask: false,
    animationStatus: false,
    recodeText:'长按录音',
    setting:true
  },
  // 打开遮罩层
  mask: function () {
    let that = this;
    that.setData({
      mask: true
    })
  },
  // 关闭遮罩层
  clear: function () {
    let that = this;
    that.setData({
      mask: false
    })
  },
  recodeEnd: function (e) {
    let that = this
    
    that.setData({
      animationStatus: false
    })
    // that.data.recorderManager = wx.getRecorderManager();
    that.data.recorderManager.stop();
    console.log(e)
    wx.showLoading({
      title: '正在搜索',
    })
    that.data.recorderManager.onStop((res) => {
      this.tempFilePath = res.tempFilePath;
      console.log(res.tempFilePath)
      that.data.bdaudio = res.tempFilePath
      wx.uploadFile({
        url: api.UploadAudio,
        filePath: res.tempFilePath,
        header: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8', },
        name: 'voiceFile',
        success(res){
          wx.hideLoading()
          res.data = JSON.parse(res.data)
          if (res.data.errno==-1){
            wx.showToast({
              title: '没有数据',
              icon:'none'
            })
          }else{
            that.setData({
              data: res.data.data,
              mask: false
            })
          }
        }
      })
      const {
        tempFilePath
      } = res
    })
  }, 
  inText:function(e){
    let that=this
    if(e.detail.value==''){
      that.setData({
        data: '',
        search:wx.getStorageSync('search')
      })
    }
  },
  // 清除历史
  clearData:function(){
    wx.setStorageSync('search',[])
    wx.showToast({
      title: '清除成功',
      icon:'none'
    })
    this.setData({
      search:''
    })
  },
  //表单搜索
  blur:function(e){
    let that=this
    if (String(e.detail.value).trim()==''){
      that.setData({
        data:''
      })
      return false;
    }
    wx.showLoading({
      title: '正在搜索',
      mask: true
    })
    if(e.currentTarget.dataset.keys){
      e.detail.value = e.currentTarget.dataset.keys
    }
    wx.request({
      url: api.UploadAudio,
      data: { serviceName: String(e.detail.value).trim()},
      success(res){
        wx.hideLoading()
        var search=[]
        if (!wx.getStorageSync('search')){
          wx.setStorageSync('search', [])
        }
        search = wx.getStorageSync('search')
        search.push(String(e.detail.value).trim())
        var arr = new Set(search)
        wx.setStorageSync('search', [...arr])
        that.setData({
          data:res.data.data
        })
      }
    })
  },
  //跳转
  nav:function(e){
    wx.navigateTo({
      url: '/pages/handle/serverDetails/serverDetails?unid='+e.currentTarget.dataset.unid+'&search=1',
    })
  },
  recodeClick: function () {
    let that=this
    this.setData({
      animationStatus: true,
      recodeText:'录音中'
    })
    if (that.data.recorderManager){
      that.data.recorderManager.stop()
    }
    that.data.recorderManager = wx.getRecorderManager();
    const options = {
      duration: 10000, //指定录音的时长，单位 ms
      sampleRate: 16000, //采样率
      numberOfChannels: 1, //录音通道数
      encodeBitRate: 96000, //编码码率
      format: 'mp3', //音频格式，有效值 aac/mp3
      frameSize: 50, //指定帧大小，单位 KB
    }
    //开始录音
    that.data.recorderManager.start(options);
    that.data.recorderManager.onStart(() => {
      console.log('recorder start')
    });
    //错误回调
    that.data.recorderManager.onError((res) => {
      console.log(res);
    })

  },
  openSetting:function(e){
    let that = this
    if(wx.getStorageSync('settings')){
      wx.openSetting({
        success(res) {
          console.log(res.authSetting)
          if (res.authSetting['scope.record']) {
            wx.setStorageSync('setting', false)
            that.setData({
              setting: false
            })
          }
        }
      })
    }else{
      wx.authorize({
        scope: 'scope.record',
        success(res) {
          that.setData({
            setting: false
          })
        }, fail(res) {
          wx.showToast({
            title: '拒绝录音授权，可能会影响功能使用',
            icon:'none'
          })
          wx.setStorageSync('setting', true)
          wx.setStorageSync('settings', true)
          that.setData({
            setting: true
          })
          return false;
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSetting({
      success:res=>{
        if(!res.authSetting['scope.record']){
          wx.setStorageSync('settings', '')
        }
      }
    })
    this.setData({
      search: wx.getStorageSync('search')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that=this
    wx.request({
      url: api.HotService,
      success(res){
        that.setData({
          hotService:res.data.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})