//index.js
//获取应用实例
const app = getApp()
var id = 0;
var playimg = '/images/icons/play.png';
var pauseimg = '/images/icons/zanting.png';

let innerAudioContext = null;

Page({
  data: {
    taskContent: {
      title: '投诉内容',
      img: playimg,
      second: ''
    },
    image: {
      title: '图片'
    },
    video: {
      title: '视频'
    },
    summary: {
      title: '总结'
    },
    info: {},
    flag: 0
  },
  onLoad: function (res) {
    id = res.id;
    this.showData();
  },
  onShow: function () {
    var that = this;
    app.ajaxReadyCallback = res => {
      var imgs = [];
      if (res.data.data.images){
        imgs = res.data.data.images.split(',');
      }
      var info = {
        content: res.data.data.content,
        imgs: imgs,
        videos: res.data.data.videos,
        voices: res.data.data.voices,
        note:res.data.data.note,
        summary:res.data.data.summary,
        orderId:res.data.data.orderId,
      }
      that.setData({
        info: info
      });
      
    }
  },

  showData: function () {
    app.ajax({
      url: app.globalData.serverUrl + '/complain/detail',
      type: 'POST',
      data: {
        id: id,
      }
    });
  },

  playaudio: function(e){
    innerAudioContext = null;
    innerAudioContext = wx.createInnerAudioContext();
    innerAudioContext.autoplay = false;
    var audiosrc = e.currentTarget.dataset.audio;
    var flag = e.currentTarget.dataset.flag;
    var that = this;
    if(!audiosrc){
      wx.showToast({
        title: '未上传音频',
        icon: 'none',
        duration: 1500
      });
      return false;
    }
    if(flag == 0){ //播放
      innerAudioContext.autoplay = false;
      innerAudioContext.src = that.data.info.voices;
      innerAudioContext.play();
    }else{ //停止
      innerAudioContext.stop();
    }
    setTimeout(() => {
      innerAudioContext.duration;
    },300)
    innerAudioContext.onTimeUpdate(() => {
      var task = that.data.taskContent;
      task.second = innerAudioContext.duration.toFixed(1)+'"';
      that.setData({
        taskContent: task
      });
    })
    innerAudioContext.onPlay(() => {
      var task = that.data.taskContent;
      task.img = pauseimg;
      that.setData({
        flag: 1,
        taskContent: task
      })
    })
    innerAudioContext.onStop(() => {
      var task = that.data.taskContent;
      task.img = playimg;
      that.setData({
        flag: 0,
        taskContent: task
      })
    })
    innerAudioContext.onEnded(() => {
      var task = that.data.taskContent;
      task.img = playimg;
      that.setData({
        flag: 0,
        taskContent: task
      })
    })
    innerAudioContext.onError((res) => {
      wx.showToast({
        title: '音频播放失败',
        icon: 'none',
        duration: 1500
      });
      return false;
    })
  },

  //图片预览
  previewImage: function (e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.info.imgs, // 需要预览的图片http链接列表  
      success: function (e) {
        console.log(e);
      }
    })
  },

  videotap: function(e){
    var video = e.target.dataset.video;
    app.gotopage('/pages/video/index', { video: video });
  },

  orderDetail: function(e){ //跳转到详情
    wx.navigateTo({
      url: '/pages/order/detail/index?id=' + this.data.info.orderId
    })
  },
  
})
