// pages/noticeDetail/detail.js
const app = getApp();
let id = 0;
//创建播放管理器
let innerAudioContext = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false, // 显示modal弹窗
    single: false,
    reason: '',
    details: {},
    detail: [],
    taskContent: {
      title: '任务内容',
      showImg: '/images/icons/play.png',
      playImg: '/images/icons/play.png',
      stopPlayImg: '/images/icons/zanting.png',
      taskRadioType: 1,
      radioUrl: '',
      second: ''
    },
    star: 5,
    starMap: [
      '较差',
      '一般',
      '良好',
      '优秀',
      '完美'
    ],
    checked: '/images/icons/check_star.png',
    unchecked: '/images/icons/shoudaopingjia@2x.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    id = options.id;
    this.showData();
   // this.showTypeData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    app.ajaxReadyCallback = res => {
      var apiname = res.data.apiname;
      if (apiname == 'cancel') {
        wx.showToast({
          title: '取消成功!',
          icon: 'success',
          duration: 1500
        });
        that.setData({
          reason: ''
        });
        that.showData();
      } else if (apiname == 'comment') {
        wx.showToast({
          title: '感谢您的评价!',
          icon: 'success',
          duration: 1500
        });
        that.setData({
          complain: ''
        });
        that.showData();
      } else if (apiname == 'orderConveyType') {
        var arr = [];
        var detail = res.data.data;
        for (var i in detail) {
          var genre = {
            genre: i,
            detail: detail[i]
          }
          arr.push(genre);
        }
        that.setData({
          detail: arr,
        });
      } else {
        var detail = res.data.data;
        if (!detail.images) {
          detail.images = [];
        } else {
          detail.images = detail.images.split(',');
        }
        if (!detail.lastFinishImg) {
          detail.lastFinishImg = [];
        } else {
          detail.lastFinishImg = detail.lastFinishImg.split(',');
        }
        that.setData({
          details: detail,
        });
        wx.setNavigationBarTitle({
          title: '订单查看'
        })
      }
    }
  },

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

  showTypeData: function () {
    app.ajax({
      url: app.globalData.serverUrl + '/orderConveyType/list',
      type: 'POST',
      apiname: 'orderConveyType',
      data: {
        orderId: id
      }
    });
  },

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

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

  handleShowModel: function () {
    this.setData({ showModal: true })
  },

  //播放录音
  playRadio: function (event) {
    var taskContent = this.data.taskContent;
    var radioType = taskContent.taskRadioType;
    if (radioType == 1) {
      if (!this.data.details.voices) {
        wx.showToast({
          title: '无音频文件!',
          icon: 'none',
          duration: 1500
        })
        return false;
      }
      var radioUrl = this.data.details.voices.split(',');
      innerAudioContext = null;
      innerAudioContext = wx.createInnerAudioContext();
      innerAudioContext.autoplay = false;
      innerAudioContext.src = radioUrl[0];
      innerAudioContext.play();
      setTimeout(() => {
        innerAudioContext.duration;
      },300)
      innerAudioContext.onTimeUpdate(() => {
        var task = this.data.taskContent;
        task.second = innerAudioContext.duration.toFixed(1)+'"';
        this.setData({
          taskContent: task
        });
      })
      innerAudioContext.onPlay(() => {
        taskContent.taskRadioType = 2;
        taskContent.showImg = taskContent.stopPlayImg;
        this.setData({
          taskContent: taskContent
        })
      });
      innerAudioContext.onError((res) => {
        wx.showToast({
          title: '语音播放失败!',
          icon: 'none',
          duration: 500
        })
      });
      //监听音频自然播放至结束的事件
      innerAudioContext.onEnded((res) => {
        taskContent.taskRadioType = 1;
        taskContent.showImg = taskContent.playImg;
        this.setData({
          taskContent: taskContent
        })
      });
    } else if (radioType == 2) {
      innerAudioContext.stop();
      //监听音频停止事件
      innerAudioContext.onStop((res) => {
        taskContent.taskRadioType = 1;
        taskContent.showImg = taskContent.playImg;
        this.setData({
          taskContent: taskContent
        })
      });
    }
  },

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

  handleStarChoose(e) {
    let star = parseInt(e.target.dataset.star) || 0;
    this.setData({
      star: star,
    });
  },

  // 点击取消按钮的回调函数
  modalCancel() {
    this.setData({ showModal: false })
  },

  getValue(e) {
    this.setData({
      reason: e.detail.value
    })
  },

  // 取消订单
  handleCancelOrder: function () {
    if (this.data.reason == '') {
      wx.showToast({
        title: '取消原因不能为空!',
        icon: 'none',
        duration: 1500
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
    } else {
      app.ajax({
        url: app.globalData.serverUrl + 'orders/updateOrderModeByUser',
        type: 'POST',
        apiname: 'cancel',
        data: {
          orderId: this.data.details.id,
          reason: this.data.reason,
        }
      });
    }
  },

  // 评价
  handleComplainOrder: function (e) {
    if (e.detail.value.content.length == 0 || e.detail.value.content == '') {
      wx.showToast({
        title: '评价内容不能为空!',
        icon: 'none',
        duration: 1500
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
    } else {

      app.ajax({
        url: app.globalData.serverUrl + '/comment/add',
        type: 'POST',
        apiname: 'comment',
        data: {
            score: this.data.star,
            content: e.detail.value.content,
            type: this.data.details.workTypeMode,
            fromId: this.data.details.id
        }
      });
    }
  },
  handleTransportType(event) {
    var id = event.currentTarget.dataset.id;
    var type = event.currentTarget.dataset.type;
    var num = event.currentTarget.dataset.num;
    wx.navigateTo({
      url: '/pages/order/type/index?id=' + id + '&type=' + type + '&num=' + num
    })
  }
})
