// packageH/Advertising/advertisingRelease/advertisingRelease.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: "",
    video_cover: [],
    bottom_banner: [],
    bottomADradio: "0",
    bottom_title: "",
    bottom_remark: "",
    configInfo: {
      video_limit: 0
    },
    videoUrl: "", //https://test-1251768088.cos.ap-guangzhou.myqcloud.com/image/4fd4d0d48d0b1bed50215abf52537292.mp4
    videoPath:'',//本地路径
    videoModel: [],
    video_time: 0, //视频时长

    ADradioItems:[
      {value: '1', name: '开'},
      {value: '0', name: '关', checked: 'true'}
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getPublishAdvertisingData();
  },
  getPublishAdvertisingData(){
    let urlStr = app.getNetAddresss('plugin.ad-serving.frontend.advertising.index.getPublishAdvertisingData');
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            configInfo: res.data
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  onReadVideo() {
    let that = this;
    //iOS中选择上传视频不能超过60秒
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: 'back',
      success(res) {
        that.getDuration(res.duration, res.tempFilePath);
        that.setData({
          videoPath: res.tempFilePath
        });
      }
    });
  },
  getDuration(durationData, videoData) {
    let duration = durationData;
    //获取视频或者音频时长
    this.data.video_time = duration;
    if (this.data.video_time < 1) {
      this.setData({
        videoPath: null
      });
      wx.showToast({
        title: '视频时长太短了！',
        icon: 'none',
        duration: 1500
      });
    } else if (this.data.configInfo.video_limit && this.data.video_time > Number(this.data.configInfo.video_limit) + 1) {
      this.setData({
        videoPath: null
      });
      wx.showToast({
        title: `视频时长不能超过${this.data.limit}秒`,
        icon: 'none',
        duration: 1500
      });
    } else {
      this.uploadAudioM(videoData);
    }
  },
  uploadAudioM(realVideo) {
    console.log("realVideo",realVideo);
    let that = this;
    //视频上传
    wx.showLoading({
      title: '视频上传中...',
    });
    let wx_token = wx.getStorageSync('wx_token');
    let session = wx.getStorageSync('session');
    let urlStr = app.getNetAddresss("plugin.ad-serving.frontend.advertising.upload.uploadAudio");
    urlStr += '&3rd_session=' + session;
    wx.uploadFile({
      url: urlStr, //仅为示例，非真实的接口地址
      filePath: realVideo,
      name: 'file',
      formData: {},
      header:{
        "Content-Type": "application/json",
        Accept: "application/json",
        Cookie: "PHPSESSID=" + wx_token,
      },
      success(res) {
        var resData = JSON.parse(res.data);
        if (resData.result == 1) {
          wx.hideLoading();
          that.setData({
            videoUrl: resData.data.audio_url
          });
        } else {
          wx.hideLoading();
          wx.showToast({
            title: resData.msg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    });
  },
  sumbitAD(){
    if (app._isTextEmpty(this.data.title)) {
      wx.showToast({
        title: '请填写广告标题',
        icon: 'none',
      });
      return;
    }
    if (app._isTextEmpty(this.data.video_cover)) {
      wx.showToast({
        title: '请上传视频封面',
        icon: 'none',
      });
      return;
    }
    if (app._isTextEmpty(this.data.videoUrl)) {
      wx.showToast({
        title: '请上传视频',
        icon: 'none',
      });
      return;
    }
    let json = {
      title: this.data.title,
      video_cover: this.data.video_cover[0],
      video_link: this.data.videoUrl,
      bottom_status: this.data.bottomADradio,
      bottom_banner: this.data.bottom_banner[0],
      bottom_title: this.data.bottom_title,
      bottom_remark: this.data.bottom_remark,
      bottom_link: this.data.bottom_link,
      video_time: this.data.video_time
    };

    let urlStr = "";
    urlStr = app.getNetAddresss("plugin.ad-serving.frontend.advertising.index.publishAdvertisingAction");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: json,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500
          });
          wx.navigateBack({
            delta: 1
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {},
    });
  },
  titleinp(e){
    this.setData({
      title: e.detail.value
    });
  },
  updateVideoCover(e){
    this.setData({
      video_cover: e.detail
    });
  },
  onChangeADradio(e){
    const items = this.data.ADradioItems;
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value === e.detail.value;
    }

    this.setData({
      ADradioItems: items,
      bottomADradio: e.detail.value
    });
  },
  updatebottomBanner(e){
    this.setData({
      bottom_banner: e.detail
    });
  },
  bottomtitleinp(e){
    this.setData({
      bottom_title: e.detail.value
    });
  },
  bottomremarkinp(e){
    this.setData({
      bottom_remark: e.detail.value
    });
  },
  bottomLinkinp(e){
    console.log(e);
    this.setData({
      bottom_link: e.detail.value
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
});