// component/uploadVideo/uploadVideo.js
var app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const envInfo = require('../../config/index').envInfo
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    video:{ // 显示视频
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    pics:[],
    // 视频
    video: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //点击上传图片
    uploadVideo(src){
      console.log(envInfo)
      var url = envInfo.ctx + '/merchant/upload/file'
      var that = this
      console.log(url)
      wx.uploadFile({
        url: url,//服务器接口
        // method: 'POST',//这句话好像可以不用
        filePath: src,
        header: {
          'content-type': 'multipart/form-data',
          'buyer-token': wx.getStorageSync('token')
        },
        name: 'files',//服务器定义的Key值
        success: function (res) {
          if (res.statusCode == 200){
            var imgdata = JSON.parse(res.data);
            var img = imgdata.data[0];
            console.log(img)
            wxService.nextTick(() => {
              that.triggerEvent('_uploadVideo', img)
            })
          }
        },
        fail: function () {
        }
      })
    },

    // 刪除視頻
    deleteImage() {
      this.setData({
        video: ''
      })
      wxService.nextTick(() => {
        this.triggerEvent('_deleteVideo', true)
      })
    },

    //选取图片
    chooseVideo(){
      let that = this;
      let pics = this.data.pics;
      wx.chooseVideo({
        sourceType: ['album','camera'],
        maxDuration: 60,
        camera: 'back',
        success(res) {
          console.log(res.tempFilePath)
          const video = res.tempFilePath;
          that.setData({
            video: video
          })
          that.uploadVideo(that.data.video)
        }
      })
    }
  }
})
