const _Http = getApp().globalData.http;
Page({
    data: {
        ownertable: null,
        ownerid: null,
        active: 0,
        copyTeams: [], //用来本地搜索
        keyword: "", //搜索关键字
    },
    handelSubmit(userids) {
        const that = this;
        wx.showModal({
            title: '提示',
            content: '是否确认添加成员',
            success: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": 20220930103601,
                    "content": {
                        "ownertable": that.data.ownertable,
                        "ownerid": that.data.ownerid,
                        userids
                    }
                }).then(res => {
                    console.log("添加团队成员", res)
                    if (res.msg != '成功') return wx.showToast({
                        title: res.data,
                        icon: "none"
                    });
                    that.getList();
                    wx.showToast({
                        title: '添加成功',
                        icon: "none"
                    })
                    getCurrentPages().forEach(v => {
                        switch (v.__route__) {
                            case "packageA/setclient/detail":
                                v.getGroup();
                                break;
                        }
                    })
                    setTimeout(wx.navigateBack, 300);
                })
            }
        })
    },
    onLoad(options) {
        if (options.item) {
            let item = JSON.parse(options.item);
            this.setData({
                ...item
            });
            this.getList()
        }
    },
    onInput({
        detail
    }) {
        let list = this.data.copyTeams[this.data.active];
        if (detail.value) list = list.filter(v => v.position.includes(detail.value) || v.name.includes(detail.value));
        this.setData({
            [`teams[${this.data.active}].team`]: list
        })
    },
    toAdd() {
        wx.navigateTo({
            url: `/packageA/group/select?data=${
              JSON.stringify({
                ownertable:this.data.ownertable,
                ownerid:this.data.ownerid
              })
          }`,
        })
    },
    /* 取消搜索 */
    cancelTheSearch() {
        this.setData({
            keyword: "",
            [`teams[${this.data.active}].team`]: this.data.copyTeams[this.data.active]
        })
    },
    /* 切换tabs */
    onChange({
        detail
    }) {
        this.setData({
            active: detail.index
        })
    },
    //获取列表
    getList() {
        _Http.basic({
            "id": 20220930103501,
            "content": {
                "ownertable": this.data.ownertable,
                "ownerid": this.data.ownerid
            }
        }).then(res => {
            console.log("团队成员列表", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            this.setData({
                teams: res.data,
                copyTeams: res.data.map(v => v.team)
            })
        })
    },
    onReachBottom() {
        this.getList();
    }
})