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

Component({
  relations: {
    './custom-ul': {
      type: 'parent', // 关联的目标节点应为父节点
      linked: function(target) {
        // 每次被插入到custom-ul时执行,target是custom-ul节点实例对象,触发在attached生命周期之后
      },
      linkChanged: function(target) {
        // 每次被移动后执行,target是custom-ul节点实例对象,触发在moved生命周期之后
      },
      unlinked: function(target) {
        // 每次被移除时执行,target是custom-ul节点实例对象,触发在detached生命周期之后
      }
    }
  },
  /**
   * 组件的属性列表
   */
  properties: {
    discussData: {
      type: Array,
      value: []
    },
    uid: {
      type: String,
      value: ''
    }
  },

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

  /**
   * 组件的方法列表
   */
  methods: {
    onTap (e) {
      let flage = e.target.dataset.flag;
      if(flage){
        this.util(flage, '0rpx');
        this.setData({'flag': false})
      } else {
        this.util(flage, '800rpx');
        this.setData({'flag': true})
      }
    },
    /* 创建动画并执行 */
    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;
      let header = {
        uid: 'e7e0d43a-36b1-4e71-a3a3-61469c90d0a2'
      }
      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 = {
        "userId": header.uid,
        "postsId": postsId,
        "content": text
      };
      APIClient.getDiscussSchedule(header, 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
          });
        };
      });
      this.setData({
        text: ''
      })
    }
  },
  ready () {
    this.setData({
      discussDatas: this.properties.discussData
    })
  }
})