const _Http = getApp().globalData.http;

Page({
    data: {
        content: {
            nocache: true,
            pageNumber: 1,
            pageSize: 20,
            pageTotal: 1,
            total: 0,
            where: {
                "condition": "",
            }
        },
        list: [],
        sa_customersid: 0,
        loading: false
    },
    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.sys_enterpriseid) this.setData({
            'content.ownertable': 'sys_enterprise',
            'content.ownerid': options.sys_enterpriseid,
            enterprisename: options.enterprisename,
            sa_customersid: options.sa_customersid
        });
        this.getList();
        getApp().globalData.Language.getLanguagePackage(this, '选择联系人');
    },
    /* 默认添加 */
    created() {
        wx.redirectTo({
            url: '/packageA/setclient/modules/contacts/add/index?sys_enterpriseid=' + this.data.content.ownerid + "&enterprisename=" + this.data.enterprisename + "&sa_customersid=" + this.data.sa_customersid,
        })
    },
    onClick(e) {
        if (this.data.loading) return;
        const {
            item
        } = e.currentTarget.dataset;
        delete(item.tag1)
        item.sys_enterpriseid = this.data.content.ownerid;
        this.data.loading = true;
        _Http.basic({
            "id": "20240531151704",
            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/setclient/modules/contacts/detail/index' || v.__route__ == 'packageA/contacts/detail') {
                        v.getDetail();
                    } else if (v.selectComponent("#Contacts")) {
                        let page = v.selectComponent("#Contacts");
                        page.getList(page.data.content.sys_enterpriseid, true)
                    } else if (v.__route__ == 'packageA/project/modules/contacts/search/index') {
                        let index = v.data.list.findIndex(value => value.sys_phonebookid == res.data.sys_phonebookid);
                        if (index != -1) v.setData({
                            [`list[${index}]`]: res.data
                        })
                    }
                });
            }, 500)

        });

        return;
        wx.redirectTo({
            url: `/packageA/setclient/modules/contacts/add/index?sys_enterpriseid=${this.data.content.ownerid}&enterprisename=${this.data.enterprisename}&sa_customersid=${this.data.sa_customersid}&data=${JSON.stringify(item)}`,
        })
    },
    onSearch({
        detail
    }) {
        this.setData({
            'content.where.condition': detail ? detail : ""
        });
        this.getList(true);
    },
    onClear() {
        this.setData({
            'content.where.condition': ""
        });
        this.getList(true);
    },
    onReachBottom() {
        this.getList();
    },
})