
var app = getApp()
var that;
var animation;
Page({
  data: {
    selectUser: [],
    title: '',
    id: '',
    isCompile: true,
  },
  onLoad(e) {
    that = this;
    if (e.id) {
      this.getData(e.id);
      this.setData({ isbj: e.id, });
    }
    dd.setNavigationBar({ title: "新建分组" });
  },
  onShow() {
    animation = dd.createAnimation({
      duration: 200,
      timeFunction: "linear",
    });
  },
  setTitle(e) {
    this.setData({
      title: e.detail.value,
    });
  },
  //确定人员选择
  onSelectUser(e) {
    this.setData({
      selectUser: e
    })
  },
  //选择人员
  openAdd() {
    animation.translateY(0).step();
    that.setData({
      animationInfo: animation.export()
    });
  },
  //关闭选择人员
  onClose() {
    animation.translateY(750).step();
    that.setData({
      animationInfo: animation.export()
    });
  },
  //编辑
  compile() {
    if (!this.data.title) {
      app.globalData.showToast("请输入分组名称");
      return;
    }
    var arr = [];
    this.data.selectUser.forEach(element => {
      arr.push(element.id);
    });
    if(arr.length==0){
      app.globalData.showToast("至少选择一名员工参与");
      return
    }
    var data = {
      group_id:that.data.isbj,
      group_name: this.data.title,
      employees: arr.join(',')
    }
    app.$post("api/integral/statistics/groups", data).then((res) => {
      app.globalData.showToast("保存成功");
      setTimeout(() => {
        dd.navigateBack({
          delta: 1
        })
      }, 1000);
    })
  },
  //保存
  save() {
    if (!this.data.title) {
      app.globalData.showToast("请输入分组名称");
      return;
    }
    var arr = [];
    this.data.selectUser.forEach(element => {
      arr.push(element.id);
    });
    var data = {
      group_name: this.data.title,
      employees: arr.join(',')
    }
    app.$post("api/integral/statistics/groups/create", data).then((res) => {
      app.globalData.showToast("保存成功");
      setTimeout(() => {
        dd.navigateBack({
          delta: 1
        })
      }, 1000);
    })
  },
  //删除
  delete() {
    app.$post("api/integral/statistics/groups/drop", { group_id: that.data.isbj }).then((res) => {
      app.globalData.showToast("已删除");
      setTimeout(() => {
        dd.navigateBack({
          delta: 1
        })
      }, 1000);
    })
  },
  getData(id) {
    app.$get("api/integral/statistics/groups/info", { group_id: id }).then((res) => {
      this.setData({
        selectUser: res.data.data.employees,
        title: res.data.data.name,
        id: res.data.data.id,
        isCompile: true
      })
    })
  }
});
