const _Http = getApp().globalData.http;
let ownertable = '',
    ownerid = '',
    id = "1";

Page({
    data: {
        isleader: false,
        manage: "管理",
        result: []
    },
    onLoad(options) {
        ownertable = options.ownertable;
        ownerid = options.ownerid;
        if (options.ownertable == "sa_workorder") id = "3";
        this.getList();
    },
    getList() {
        _Http.basic({
            "id": 2022093010350 + id,
            content: {
                ownertable,
                ownerid,
            }
        }).then(res => {
            console.log(`数据团队${ownertable+ownerid}`, res)
            if (res.msg != '成功') {
                wx.showToast({
                    title: res.msg,
                    icon: "none",
                    mask: true
                });
                return setTimeout(wx.navigateBack, 1500);
            } else {
                this.setData({
                    list: res.data[0].team,
                    isleader: res.data[0].teamleader[0].userid == wx.getStorageSync('userMsg').userid
                })
            }
        })
    },
    /* 添加成员 */
    insert() {
        let params = id == 3 ? {
            "id": "20230213143003",
            "version": 1,
            "content": {
                nocache: true,
                "where": {
                    "condition": "",
                    "isleader": 0
                }
            }
        } : {
            "id": 20221018122201,
            "content": {
                "ownertable": ownertable,
                "ownerid": ownerid,
                nocache: true,
                "where": {
                    "condition": "",
                    "withoutselect": 1
                }
            }
        };
        wx.navigateTo({
            url: `/select/contacts/index?params=${JSON.stringify(params)}`,
        })
        getApp().globalData.handleSelect = this.insertUser.bind(this);
    },
    insertUser({
        value
    }) {
        let that = this;
        wx.showModal({
            title: '提示',
            content: `是否确认添加${value[0]+value[0].length}位成员`,
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "accesstoken": "7eb155cb0a9ed5895056afc151af7d7f",
                    "id": 20220930103601,
                    "content": {
                        ownertable,
                        ownerid,
                        "userids": value[1],
                        "justuserids": 1
                    }
                }).then(res => {
                    console.log("添加成员", res)
                    wx.showToast({
                        title: res.msg == '成功' ? '添加成功' : res.msg,
                        icon: "none",
                        mask: true
                    });
                    if (res.msg == '成功') {
                        that.getList()
                        setTimeout(wx.navigateBack, 1000)
                    }
                })
            }
        })
    },
    /* 设置负责人 */
    setLeader(e) {
        const {
            item
        } = e.currentTarget.dataset,
            that = this;
        wx.showModal({
            title: '提示',
            content: `是否确认将负责人转让到“${item.name}”`,
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": 2022093010370 + id,
                    "content": {
                        ownertable,
                        ownerid,
                        "userid": item.userid
                    }
                }).then(res => {
                    wx.showToast({
                        title: res.msg == '成功' ? '转让成功' : res.msg,
                        icon: "none",
                        mask: true
                    });
                    if (res.msg == '成功') that.getList()
                })
            }
        })
    },
    /* 开启设置 */
    setTeam() {
        this.setData({
            manage: this.data.manage == '管理' ? '取消' : "管理"
        })
    },
    /* 选中成员 */
    toggle(event) {
        const {
            name
        } = event.currentTarget.dataset;
        let result = this.data.result;
        result.some(v => v == name) ? result = result.filter(v => v != name) : result.push(name);
        this.setData({
            result
        })
    },
    /* 移除成员 */
    onRemove() {
        let result = this.data.result,
            that = this;
        wx.showModal({
            title: '提示',
            content: `是否确认移除${result.length}位成员`,
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": 2022093010380 + id,
                    "content": {
                        ownertable,
                        ownerid,
                        "userids": result
                    }
                }).then(res => {
                    wx.showToast({
                        title: res.msg == '成功' ? '移除成功' : res.msg,
                        icon: "none",
                        mask: true
                    });
                    if (res.msg == '成功') {
                        that.setData({
                            result: []
                        });
                        that.getList()
                    }
                })
            }
        })
    }
})