// compontents/discuss/discuss.js
const APIClient = require('../../utils/APIClient.js');
const login = require('../../utils/loginSchedule.js');

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    discussData: {
      type: Array,
      value: []
    },
    uid: {
      type: String,
      value: ''
    },
    type: {
      type: String,
      value: ''
    },
    postsId: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    flag: false,
    text: '',
    discussDatas: [],
    animationData: {},
    messageLength: '',
    downUp: '展开'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onTap (e) {
      let flage = e.target.dataset.flag;
      if(flage){
        this.util(flage, '0rpx');
        this.setData({
          'flag': false,
          'downUp': '展开'
        })
      } else {
        this.util(flage, '800rpx');
        this.setData({
          'flag': true,
          'downUp': '收起'
        })
      }
    },
    /* 创建动画并执行 */
    util (flag, height) {
      // 创建动画实例   
      var animation = wx.createAnimation({  
        duration: 200,  //动画时长  
        timingFunction: "linear", //线性  
        delay: 0  //0则不延迟  
      });
      
      this.animation = animation;

      animation.height(height).step();

      this.setData({  
        animationData: animation.export()  
      })  
    },
    /* 获取输入内容 */
    bindKeyInput (e) {
      this.setData({
        text: e.detail.value
      })
    },
    /* 发送评论 */
    sendText () {
      //console.log(this.data.text.trim())
      let text = this.data.text.trim();
      let postsId = this.properties.postsId;
      let _this = this;
      if(text == ''){
        wx.showModal({
          title: '提示',
          content: '请输入评论内容',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        return false;
      }
      this.data.text = "";
      let data = {
        "postsId": postsId,
        "content": text
      };
      login.getOpenidSessionKey(function(res) {
        //console.log(res.data.data.uid);
        APIClient.getDiscussSchedule({
          uid: res.data.data.uid
        }, data).success(function (res) {
          console.log(res.data)
          if(res.data.success) {
            _this.properties.discussData.push(res.data.data);
            _this.setData({
              discussDatas: _this.properties.discussData,
              messageLength: _this.properties.discussData.length
            });
          };
        });        
      }, function() {
        wx.showModal({
          title: '提示',
          content: '需要获取您的公开信息(昵称、头像等),请从小程序列表删除小学王者班后再次扫码进入,允许授权后可正常使用',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      });
      this.setData({
        text: ''
      })
    }
  },
  ready () {
    this.setData({
      discussDatas: this.properties.discussData
    })
  }
})