const _Http = getApp().globalData.http;

Page({
    data: {
        content: {
            loading: false,
            sa_contractid: "",
            nocache: true,
            pageNumber: 1,
            pageSize: 20,
            pageTotal: 1,
            total: 0,
            where: {
                "condition": "",
            }
        },
        list: [],
    },
    getList(init = false) {
        const content = this.data.content;
        if (init) content.pageNumber = 1;
        if (content.pageNumber > content.pageTotal) return;
        _Http.basic({
            id: 20240923153304,
            content
        }).then(res => {
            console.log("通讯录列表", res)
            if (res.code != '1') return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            content.pageNumber = res.pageNumber + 1;
            content.pageTotal = res.pageTotal;
            content.total = res.total;
            this.setData({
                list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                content
            })
        })
    },
    onLoad(options) {
        if (options.id) this.setData({
            'content.sa_contractid': options.id
        });
        this.getList();
        getApp().globalData.Language.getLanguagePackage(this, '选择联系人');
    },
    onClick(e) {
        if (this.data.loading) return;
        const {
            item
        } = e.currentTarget.dataset,
            sa_contractid = this.data.content.sa_contractid;
        this.data.loading = true;
        _Http.basic({
            "id": "20240923153004",
            "content": {
                sa_contractid,
                "sys_phonebookid": item.sys_phonebookid
            }
        }).then(res => {
            console.log("添加合同联系人", res)
            this.data.loading = false;
            wx.showToast({
                title: getApp().globalData.Language.getMapText(res.code != '1' ? res.msg : '添加成功'),
                icon: "none",
                mask: res.code == '1'
            })
            if (res.code == '1') setTimeout(() => {
                wx.navigateBack()
                getCurrentPages().forEach(v => {
                    if (v.__route__ == 'packageA/contract/detail') {
                        let page = v.selectComponent("#Contacts");
                        page.getList(sa_contractid, 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();
    },
})