const _Http = getApp().globalData.http;
Page({
    data: {
        ownertable: null,
        ownerid: null,
        copyTeams: [], //用来本地搜索
        keyword: "", //搜索关键字
        activeNames: [0],
        userid: null
    },
    onLoad(options) {
        if (options.item) {
            this.setData({
                ...JSON.parse(options.item),
                userid: wx.getStorageSync('userMsg').userid
            });
            this.getList()
        };
    },
    onInput(e) {
        let {
            value
        } = e.detail;
        let index = this.data.teams.findIndex(v => v.ismyteam == 1);
        let copyTeams = JSON.parse(JSON.stringify(this.data.copyTeams[0]));
        this.setData({
            keyword: value,
            [`teams[${index}].team`]: value ? copyTeams.filter(v => v.name.includes(value) || v.position.includes(value)) : copyTeams
        });
    },
    cancelTheSearch() {
        let index = this.data.teams.findIndex(v => v.ismyteam == 1);
        let copyTeams = JSON.parse(JSON.stringify(this.data.copyTeams[0]));
        this.setData({
            keyword: "",
            [`teams[${index}].team`]: copyTeams
        });
    },
    onChange(event) {
        this.setData({
            activeNames: event.detail,
        });
    },
    toAdd() {
        wx.navigateTo({
            url: `/pages/group/select?data=${
              JSON.stringify({
                ownertable:this.data.ownertable,
                ownerid:this.data.ownerid
              })
          }`,
        })
    },
    /* 处理添加 */
    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 => {
                        if (v.getGroup) v.getGroup();
                    })
                    setTimeout(wx.navigateBack, 300);
                })
            }
        })
    },
    //获取列表
    getList() {
        _Http.basic({
            "id": 20220930103501,
            "content": {
                "nocache": true,
                "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();
    },
    onUnload() {
        let page = getCurrentPages()[getCurrentPages().length-2];
        page.getGroup && page.getGroup();
    }
})