// packageI/newMedia/newMediaPublish/newMediaPublish.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:"news",
    typeArr:["","news","article","audio","video","advideo"],
    failPopup:false,

    title:"",   //标题
    faceImg:"",   //封面
    textarea:"",  //文本

    stream_univalent:"",
    duration:"",  //投放时长
    link:"",    //广告链接

    setting:{}, //设置数据，包括视频时长的数据

    newsImageList:[], //图文图片列表
    videoUrl:"",
    audioUrl:"",

    errorData:{},
    sendBtnFlag:false,  //防止多次点击
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.type){
      this.setData({type:options.type});
    }
    
  },

  setFailPopup(){
    this.setData({
      failPopup:!this.data.failPopup
    });
  },

  //封面地址回调
  updateFaceImage(evt){ 
    this.data.faceImg = evt.detail[0] || "";
  },
  //图文列表地址回调
  updateNewImageList(evt){
    this.data.newsImageList = evt.detail;
  },
  //视频上传成功回调
  successUpVideo(evt){
    let res = evt.detail.res;
    if(res.result!=1) return app.tips(res.msg);
    this.setData({
      videoUrl:res.data.img_url
    });
  },
  //音频上传完成回调
  successUpAudio(evt){
    let res = evt.detail.res;
    if(res.result!=1) return app.tips(res.msg);
    //this.data.audioUrl = 
    this.setData({
      audioUrl:res.data.img_url
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getSetting();
  },

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

  },

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

  },

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

  },

  send(){
    if(this.data.sendBtnFlag==true) return;
    let {typeArr,type,faceImg,title,textarea,newsImageList,audioUrl,videoUrl,stream_univalent,duration,link,setting} = this.data;
    let json={advertising_type:1,genre:typeArr.indexOf(type)};
    if(!faceImg || faceImg=='') return app.tips("请上传封面");
    if(!title || title=='') return app.tips("请填写标题");
    json.title = title;
    json.cover_picture = faceImg;

    //图文或文章需要验证  正文
    if(type=="news" || type=="article"){  
      if(!textarea || textarea=='') return app.tips("请填写正文");
      json.content = textarea;
    }

    //图文需要验证图片列表
    if(type=="news"){
      if(!newsImageList || newsImageList.length==0) return app.tips("请上传图片");
      json.images = newsImageList;
    }

    //音频发布需要上传音频
    if(type=="audio"){
      if(!audioUrl || audioUrl=='') return app.tips("请上传音频");
      json.resources_url=audioUrl;
    }

    //视频跟广告需要上传视频
    if(type=="video" || type=="advideo"){
      if(!videoUrl || videoUrl=='') return app.tips("请上传视频");
      json.resources_url=videoUrl;
    }

    //广告需要验证投放时长跟链接
    if(type=="advideo"){
      if(!stream_univalent || stream_univalent=="") return app.tips("请填写流量值单价");
      if(stream_univalent<setting.min_stream_hour) return app.tips("流量值单价不能小于最低值");
      if(!duration || duration=='') return app.tips("请填写投放时长");
      //if(!link || link=='') return app.tips("请填写链接");
      json.advertising_type=2;
      json.stream_univalent = stream_univalent;
      json.duration =duration;
      json.link = link;
    }
    
    this.data.sendBtnFlag = true;
    let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.media-advertisingt.release-advertising");
    app._postNetWork({
      url: urlStr,
      data:json,
      success: (resdata) => {
       
        var res = resdata.data;
        if (res.result != 1){
          this.data.sendBtnFlag = false;
          if(res.data.code=="2"){
            this.setData({
              failPopup:true,
              errorData:res.data
            });
          }else {
            app.tips(res.msg);
          }
          
          return;
        }
        let pages = getCurrentPages();
        let page = pages[pages.length-2];
        page.getListData();
        setTimeout(()=>{
          wx.navigateBack();
        },500);
        
      },
      fail(){
        this.data.sendBtnFlag = false;
      }
    });

  },

  getSetting(){
    let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.media-advertisingt.get-set");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.setData({
          setting:res.data
        });

      }
    });
  },

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

  },

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

  },

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

  }
});