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

  /**
   * 页面的初始数据
   */
  data: {
    configInfo: {},
    reward_total: null,
    reward_amount_total: null,
    reward_type: "1",
    list: [],
    videoTime: 0,
    advertising_apply_id: null,

    ADradioItems:[
      {value: '1', name: '固定金额'},
      {value: '2', name: '随机金额', checked: 'true'}
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },
  getData() {
    let urlStr = app.getNetAddresss('plugin.ad-serving.frontend.advertising.index.getPutInAdvertisingData');
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            configInfo: res.data.config,
            list: res.data.list,
            ['configInfo.min_amount']: Number(res.data.config.min_amount) < 0.01 ? 0.01 : Number(res.data.config.min_amount)
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  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,
      reward_type: e.detail.value
    });
  },
  selectAdID(e){
    let detail = e.currentTarget.dataset;
    this.setData({
      advertising_apply_id: detail.id,
      videoTime: detail.time
    });
  },
  totalinp(e) {
    let val = e.detail.value;
    this.setData({
      reward_total : val
    });
  },
  rewardinp(e) {
    let val = e.detail.value;
    this.setData({
      reward_amount_total: val
    });
  },
  submitPut(){
    if (Number(this.data.reward_total) < 1) {
      wx.showToast({
        title: '请填写奖励人数',
        icon: 'none',
        duration: 1500
      });
      return;
    }
    if (Number(this.data.reward_amount_total) < Number(this.data.configInfo.min_amount)) {
      wx.showToast({
        title: `最低奖励金额${this.data.configInfo.min_amount}元`,
        icon: 'none',
        duration: 1500
      });
      return;
    }
    if (!this.data.advertising_apply_id) {
      wx.showToast({
        title: `请选择广告`,
        icon: 'none',
        duration: 1500
      });
      return;
    }
    if (Number(this.data.videoTime) > Number(this.data.configInfo.video_limit)) {
      wx.showToast({
        title: `视频时长最高为${this.data.configInfo.video_limit}秒`,
        icon: 'none',
        duration: 1500
      });
      return;
    }
    let json = {
      reward_total: this.data.reward_total,
      reward_amount_total: this.data.reward_amount_total,
      reward_type: this.data.reward_type,
      put_in_type: this.options.tag == "video" ? "2" : "1", //投放类型1-直播间，2-短视频
      put_in_id: this.options.id,
      advertising_apply_id: this.data.advertising_apply_id
    };

    let urlStr = app.getNetAddresss('plugin.ad-serving.frontend.advertising.index.putInAdvertisingAction');
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 1500
        });
        if (res.result == 1) {
          wx.navigateBack({
            delta: 1
          });
        } 
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
});