const _Http = getApp().globalData.http;

Page({
    data: {
        content: {
            nocache: true,
            sa_projectid: 1,
            pageSize: 20,
            pageNumber: 1,
            pageTotal: 1,
            "where": {
                "condition": ""
            }
        },
        list: [],
        result: []
    },
    onLoad(options) {
        this.setData({
            "content.sa_projectid": options.sa_projectid
        });
        this.getList();
    },
    /* 提交 */
    submit() {
        let that = this;
        wx.showModal({
            title: '提示',
            content: '是否确认添加联系人',
            complete: ({
                confirm
            }) => {
                if (confirm) {
                    _Http.basic({
                        "id": 20221111131104,
                        "content": {
                            "sa_project_contactsid": 0,
                            "contactsids": that.data.result,
                            "sa_projectid": that.data.content.sa_projectid,
                            "remarks": ""
                        }
                    }).then(res => {
                        console.log("项目添加联系人", res)
                        if (res.msg != '成功') return wx.showToast({
                            title: res.data,
                            icon: "none"
                        });
                        wx.showToast({
                            title: '添加成功',
                            icon: "none"
                        });
                        setTimeout(() => {
                            wx.navigateBack();
                            let pages = getCurrentPages();
                            pages[pages.length - 2].partialRenewal(true);
                        }, 300)
                    })
                }
            }
        })

    },
    /* 开始搜索 */
    onSearch({
        detail
    }) {
        if (this.data.content.where.condition == detail) return;
        this.setData({
            'content.where.condition': detail
        });
        this.getList(true)
    },
    onClear() {
        this.setData({
            'content.where.condition': ""
        });
        this.getList(true)
    },
    /* 选中 */
    onChange(e) {
        const contactsid = e.currentTarget.dataset.item.contactsid + "";
        if (!contactsid) return;
        let result = this.data.result;
        if (result.some(v => v == contactsid)) {
            result = result.filter(v => v != contactsid)
        } else {
            result.push(contactsid)
        }
        this.setData({
            result
        })
    },
    //获取列表
    getList(init = false) {
        let content = this.data.content;
        if (init) content.pageNumber = 1;
        if (content.pageNumber > content.pageTotal) return;
        _Http.basic({
            "id": 20221111130704,
            content
        }).then(res => {
            console.log("可添加联系人", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            this.setData({
                list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                "content.pageNumber": res.pageNumber + 1,
                "content.pageTotal": res.pageTotal,
            })

        })
    },
    onReachBottom() {
        this.getList();
    }
})