var app = getApp();
var that;
var animation;
Page({
  data: {
    users: [],
    item: {
      remark: '',
      employee_id: []
    },
    defaultVal: '',
    popupCx: ''
  },
  onLoad() {
    that = this;
    dd.setNavigationBar({ title: "发放奖票" });
    this.setData({ isAdministrator: app.globalData.isAdministrator })
  },
  //备注输入
  bindTextAreaBlur: function (e) {
    this.setData({
      [`item.remark`]: e.detail.value,
    });
  },
  formSubmit() {
    if (that.data.users.length == 0) {
      app.globalData.showToast('请选择奖扣对象');
      return;
    }
    if (!that.data.item.remark) {
      app.globalData.showToast('请输入奖票原因');
      return;
    }
    var items=[];
    var obj;
    that.data.users.forEach(item=>{
        obj={
            employee_id:item.id,
            count:1,
            remark:this.data.item.remark,
        }
        items.push(obj);
    })
    this.setData({ disabled: true });
    app.$post('api/integral/ticket/create', {items:JSON.stringify(items)}).then((res) => {
      app.globalData.showToast("奖票成功")
      setTimeout(() => {
        dd.navigateBack({ delta: 1 })
      }, 1000);
    }).finally(err => {
      this.setData({ disabled: false });
    })
  },
  //选择人
  onSelectUser(item) {
    //空数据
    var arr = [];
    if (item.length > 0) {
      item.forEach(user => {
        arr.push(user.id);
      })
      that.setData({
        users: item.length > 0 ? item : [],
        [`item.employee_id`]: arr
      })
    }
  },
  // 选择积分事件
  onSelectEnevt(item, key) {
    that.setData({
      [`item.remark`]:item.remark,
      [`item.id`]:item.id,
      defaultVal: key
    })
  },
  //选择弹窗
  openSelect(e) {
    var num = e.target.dataset.num;
    animation.translateY(0).step();
    that.setData({
      isShowType: num,
      popupCx: 'container__mask',
      animationInfo: animation.export()
    });
  },
  onShow() {
    animation = dd.createAnimation({
      duration: 200,
      timeFunction: "linear",
    });
  },
  //关闭规则
  onClose() {
    animation.translateY(1200).step();
    that.setData({
      popupCx: '',
      isShowType: '', 
      animationInfo: animation.export()
    });
  },
});
