const _Http = getApp().globalData.http;

Page({
    data: {
        content: {
            loading: false,
            "ownertable": "sa_project",
            sa_projectid: "",
            nocache: true,
            pageNumber: 1,
            pageSize: 20,
            pageTotal: 1,
            total: 0,
            where: {
                "condition": "",
            }
        },
        list: [],
    },
    getList(init = false) {
        _Http.init(this.data.content, init).then(content => {
            _Http.basic({
                "id": 20240531152004,
                content
            }).then(res => {
                this.selectComponent('#ListBox').RefreshToComplete();
                console.log("通讯录列表", res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                });
                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    content: _Http.paging(content, res),
                })
            })
        })
    },
    onLoad(options) {
        if (options.sa_projectid) this.setData({
            'content.ownertable': "sa_project",
            'content.ownerid': options.sa_projectid,
            'content.sa_projectid': options.sa_projectid
        });
        this.getList();
        getApp().globalData.Language.getLanguagePackage(this, '选择联系人');
    },
    /* 默认添加 */
    created() {
        wx.redirectTo({
            url: '/packageA/project/modules/contacts/add/index?sa_projectid=' + this.data.content.sa_projectid,
        })
    },
    onClick(e) {
        if (this.data.loading) return;
        const {
            item
        } = e.currentTarget.dataset;
        delete(item.tag1)
        return wx.redirectTo({
            url: `/packageA/project/modules/contacts/add/index?sa_projectid=${this.data.content.sa_projectid}&data=${JSON.stringify(item)}&sys_phonebookid=${item.sys_phonebookid}`,
        })

        item.sa_project_contactsid = 0;
        item.sa_projectid = this.data.content.sa_projectid;
        item.ownertable = "sa_project";
        item.ownerid = item.sa_projectid;
        this.data.loading = true;
        _Http.basic({
            "id": "20221219135502",
            "content": item
        }).then(res => {
            console.log("添加项目联系人", res)
            this.data.loading = false;
            wx.showToast({
                title: res.code != '1' ? res.msg :  getApp().globalData.Language.getMapText('添加成功'),
                icon: "none",
                mask: true
            })
            if (res.code == '1') setTimeout(() => {
                wx.navigateBack()
                getCurrentPages().forEach(v => {
                    if (v.__route__ == 'packageA/project/detail') {
                        let page = v.selectComponent("#Contacts");
                        page.getList(item.sa_projectid, true)
                    }
                });
            }, 500)
        })
       
    },
    onSearch({
        detail
    }) {
        this.setData({
            'content.where.condition': detail ? detail : ""
        });
        this.getList(true);
    },
    onClear() {
        this.setData({
            'content.where.condition': ""
        });
        this.getList(true);
    },
    onReachBottom() {
        this.getList();
    },
})