const _Http = getApp().globalData.http,
    getHeight = require("../../../../../utils/getRheRemainingHeight");

Page({
    data: {
        result: [],
        content: {
            "nocache": true,
            "pageNumber": 1,
            "pageSize": 20,
            "total": null,
            "where": {
                "condition": "",
                "type": 5,
                "sa_projectid": null
            }
        }
    },
    onLoad(options) {
        if (options.sa_projectid) this.setData({
            "content.where.sa_projectid": options.sa_projectid
        })
        this.getList();
    },
    submit() {
        _Http.basic({
            "id": 20221111102902,
            "content": {
                "sa_projectid": this.data.content.where.sa_projectid,
                "sys_enterpriseids": this.data.result,
                "remarks": ""
            }
        }).then(res => {
            console.log("批量添加客户", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            wx.showToast({
                title: '添加成功',
                icon: "none"
            })
            setTimeout(() => {
                getCurrentPages().forEach(v => {
                    if (v.selectComponent('#Treaty')) {
                        console.log(v.selectComponent('#Treaty'))
                        v.selectComponent('#Treaty').getList(this.data.content.where.sa_projectid, true);
                    }
                });
                wx.navigateBack();
            }, 300)
        })
    },
    /* 选中 */
    changeResult(e) {
        const {
            id
        } = e.currentTarget.dataset;
        let result = this.data.result;
        if (result.some(v => v == id)) {
            result = result.filter(v => v != id);
        } else {
            result.push(id)
        };
        this.setData({
            result
        })
    },
    /* 获取列表 */
    getList(init = false) {
        //init 用于初始化分页
        if (init.detail != undefined) init = init.detail;
        let content = this.data.content;
        if (init) content.pageNumber = 1;
        if (content.pageNumber > content.pageTotal) return;
        _Http.basic({
            "id": 20220920083901,
            content
        }).then(res => {
            console.log("关联客户列表", res)
            this.selectComponent('#ListBox').RefreshToComplete();
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            this.getTags(res.data.map(v => v.sa_customersid));
            this.setData({
                'content.pageNumber': res.pageNumber + 1,
                'content.pageTotal': res.pageTotal,
                'content.total': res.total,
                list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data)
            })
        })
    },
    /* 开始搜索 */
    startSearch({
        detail
    }) {
        if (detail == this.data.content.where.condition) return;
        this.setData({
            'content.where.condition': detail
        });
        this.getList(true);
    },
    /* 取消搜索 */
    onClear() {
        this.setData({
            'content.where.condition': ""
        });
        this.getList(true);
    },
    /* 获取标签 */
    getTags(ownerids = []) {
        _Http.basic({
            "id": 20221018102001,
            "content": {
                "ownertable": "sa_customers",
                ownerids
            }
        }).then(res => {
            console.log("标签", res)
            if (res.msg != '成功') return;
            let list = this.data.list;
            for (let key in res.data) {
                let index = list.findIndex(v => v.sa_customersid == key);
                if (index != -1) list[index].tags = res.data[key]
            };
            this.setData({
                list
            })
        })
    },
    onReady() {
        getHeight.getHeight('.search', this).then(res => this.setData({
            listHeight: res
        }));
    },
})