const _Http = getApp().globalData.http;

Page({
    data: {
        result: [],
        content: {
            "nocache": true,
            "pageNumber": 1,
            "pageSize": 20,
            "total": null,
            "where": {
                "condition": "",
                "type": 11,
                "sa_projectid": null
            }
        },
        radio: false,
    },
    onLoad(options) {
        this.setData({
            "content.where.sa_projectid": options.sa_projectid ? options.sa_projectid : null,
            radio: options.radio ? true : false
        })
        this.getList();
        getApp().globalData.Language.getLanguagePackage(this, '关联客户');
    },
    tabChange(e) {
        this.data.content.where.type = e.detail.name == '客户' ? 11 : 12;
        this.getList(true)
    },
    submit() {
        let pages = getCurrentPages();
        let list = this.data.result.map(v => this.data.list.find(value => value.sys_enterpriseid == v));
        try {
            setTimeout(() => {
                pages[pages.length - 2].getTags();
            }, 300)
            pages[pages.length - 2].selectComponent("#Treaty").submit({
                "sa_projectid": this.data.content.where.sa_projectid,
                "sys_enterpriseids": this.data.result,
                "remarks": ""
            }, list)
        } catch (e) {
            pages[pages.length - 2].submit({
                "sa_projectid": this.data.content.where.sa_projectid,
                "sys_enterpriseids": this.data.result,
                "remarks": ""
            }, list);
        }
    },
    /* 获取列表 */
    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.code != '1') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            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)
            })
            this.getTags();
        })
    },
    /* 选中 */
    changeResult(e) {
        const {
            id
        } = e.currentTarget.dataset;
        let result = this.data.result;
        if (this.data.radio) {
            result = [id]
        } else {
            if (result.some(v => v == id)) {
                result = result.filter(v => v != id);
            } else {
                result.push(id)
            };
        }
        this.setData({
            result
        })
    },
    /* 开始搜索 */
    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() {
        let list = this.data.list,
            ownerids = list.map(v => v.sa_customersid);
        _Http.basic({
            "id": 20221018102001,
            "content": {
                "ownertable": "sa_customers",
                ownerids
            }
        }).then(res => {
            console.log("标签", res)
            if (res.code != '1') return;
            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
            })
        })
    }
})