const _Http = getApp().globalData.http;
Component({
  properties: {
    add: {
      type: Boolean
    }, //是否允许新增
    ownertable: {
      type: String
    },
    ownerid: {
      type: String
    }
  },
  data: {
    list: [],
    ismyteam: 0,
    editable: 0
  },
  options: {
    addGlobalClass: true
  },
  lifetimes: {
    attached: function () {
      getApp().globalData.Language.getLanguagePackage(this)
    }
  },
  methods: {
    getList() {
      return new Promise((resolve, reject) => {
        _Http.basic({
          "id": 20220930103501,
          "content": {
            ownertable: this.data.ownertable,
            ownerid: this.data.ownerid
          }
        }).then(res => {
          console.log("团队列表", res)
          if (res.code != '1') {
            resolve([])
            wx.showToast({
              title: res.data,
              icon: "none"
            });
            return
          }
          if (res.data.length == 0) return resolve([]);
          let list = res.data.map(v => v.teamleader.concat(v.team));
          const newArr = function (arr) {
            return arr.reduce((pre, cur) => pre.concat(Array.isArray(cur) ? newArr(cur) : cur), [])
          }
          list = newArr(list).slice(0, 5);
          resolve(list)
          console.log("是否在数据团队中", res.data[0].ismyteam == 1)
          let editable = 0;
          try {
            editable = res.data.find(v => v.ismyteam).team.find(v => v.userid == wx.getStorageSync('userMsg').userid).editable || 0;
            console.log("editable", editable)
          } catch (error) {
            try {
              editable = res.data.find(v => v.ismyteam).teamleader.find(v => v.userid == wx.getStorageSync('userMsg').userid).editable || 0;
              console.log("teamleadereditable", editable)
            } catch (error) {
              editable = 0;
            }
          }
          this.setData({
            ismyteam: res.data[0].ismyteam,
            editable,
            list
          })
        });
      })
    },
    addUser() {
      if (this.data.add) {
        wx.navigateTo({
          url: '/pages/group/index?item=' + JSON.stringify({
            ownertable: this.data.ownertable,
            ownerid: this.data.ownerid
          })
        })
      } else {
        getApp().globalData.Language.showToast('当前状态不可查询')
      }
    },
    query() {
      return this.data.list
    }
  }
})