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

  /**
   * 页面的初始数据
   */
  data: {
    type:"",
    id:0,

    rewardShow:false,

    details:{},

    //关注奖励
    attentionRewaedData:{},
    attentionRewaedShow:false,

    commentsShow:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.type){
      this.setData({type:options.type});
    }
    if(options.id){
      this.data.id = options.id;
    }
    if(options.mid){
      app._setMid(options.mid);
      this.sendForwardReward();
    }
    this.getDetails();
  },

  setCommentShow(){
    this.setData({commentsShow:!this.data.commentsShow});
  },

  setRewardShow(){
    this.setData({
      rewardShow:!this.data.rewardShow
    });
  },
  setAttentionRewaedShow(){
    this.setData({
      attentionRewaedShow:!this.data.attentionRewaedShow
    });
  },
  handAttentionRewaed(evt){
    this.setData({
      attentionRewaedData:evt.detail,
      attentionRewaedShow:true
    });
  },
  handCommentSuccess(evt){
    console.log(evt.detail.type);
    let type = evt.detail.type;
    let comment_count = this.data.details.comment_count;
    if(type == "add"){
      comment_count++;
    }else if(type=="reduce"){
      comment_count--;
    }
    this.setData({
      'details.comment_count':comment_count
    });
  },
  //关注接口
  sendAttention() {
    let details = this.data.details;
    let is_attention = details.is_attention;
    let urlStr="";
    let json={};
    if(is_attention){
      urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.media-advertisingt.cancel-attention");
      json.advertisingt_member = details.member_id;
    }else {
      urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.media-advertisingt.attention");
      json.advertisingt_id =  details.id;
    }

    app._getNetWork({
      url: urlStr,
      data:json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);

        let changeData={};
        changeData['details.is_attention'] = is_attention ? false : true;
        if(res.data.is_reward==1){
          changeData['attentionRewaedData'] = res.data;
          changeData['attentionRewaedShow'] = true;
        }

        this.setData(changeData);
      }
    });
  },

  handFavorites(evt){
    let favorites_count = evt.detail.favorites_count;
    let is_favorites = evt.detail.is_favorites;
    let changeData={};
    changeData['details.is_favorites'] = is_favorites ? false : true;
    changeData['details.favorites_count'] = favorites_count;
    this.setData(changeData);
  },
  handLike(evt){
    let like_count = evt.detail.like_count;
    let is_like = evt.detail.is_like;
    let changeData={};
    changeData['details.is_like'] = is_like ? false : true;
    changeData['details.like_count'] = like_count;
    this.setData(changeData);
  },

  

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

  },

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

  },

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

  },

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

  },

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

  },

  //转发分享进来的奖励
  sendForwardReward(){
    let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.media-advertisingt.forward-reward");
    app._getNetWork({
      url: urlStr,
      data: {
        advertisingt_id: this.data.id
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
      }
    });
  },

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (evt) {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    let details = this.data.details;
    let path = '/packageI/newMedia/newMediaAdDetails/newMediaAdDetails?id='+details.id+"&mid="+mid;
    let title = details.title;
    console.log(path);
    return {
      path,title
    };
  },


  getDetails(){
    let id = this.data.id;
    let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.media-advertisingt.advertisingt-info");
    app._getNetWork({
      url: urlStr,
      data: {
        advertisingt_id:id
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        let details = res.data;
        let typeArr = ["","news","article","audio"];
        let type = typeArr[details.genre];
        let changeData={details,type};
        if(details.behavioral && details.behavioral.is_reward && details.behavioral.is_reward==1){
          changeData['attentionRewaedData'] = details.behavioral;
          changeData['attentionRewaedShow'] = true;
        }
        this.setData(changeData);
        
      }
    });
  }




});