const _Http = getApp().globalData.http;
Page({
    data: {
        item: null,
        radio: true,
        content: {
            "nocache": true,
            "pageNumber": 1,
            "pageTotal": 1,
            "pageSize": 20,
            "where": {
                "condition": ""
            }
        }
    },
    onLoad(options) {
        this.data.content.sa_projectid = getCurrentPages().find(v => v.__route__ == 'packageA/project/detail').data.sa_projectid
        /* 从表单组件进入进入 */
        this.getList()
    },
    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": "20221027143702",
            content
        }).then(res => {
            console.log("选择客户列表", res)
            this.selectComponent('#ListBox').RefreshToComplete();
            if (res.msg != '成功') 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();
        })
    },
    /* 获取列表标签 */
    getTags() {
        let list = this.data.list,
            ownerids = list.map(v => v.sys_enterpriseid)
        _Http.basic({
            "id": 20221018102001,
            "content": {
                "ownertable": "sa_customers",
                ownerids
            }
        }).then(res => {
            for (let key in res.data) {
                let index = list.findIndex(v => v.sys_enterpriseid == key);
                list[index].tags = res.data[key]
            };
            this.setData({
                list
            })
        })
    },
    /* 选中 */
    changeResult(e) {
        let {
            id
        } = e.currentTarget.dataset, result = this.data.result;
        if (this.data.radio) {
            result = [id];
        } else {
            result.some(v => v == id) ? result = result.filter(v => v != id) : result.push(id)
        }
        this.setData({
            result
        });
        this.submit();
    },
    /* 提交 */
    submit() {
        let result = this.data.result,
            obj = this.data.radio ? {
                id: result,
                item: this.data.list.find(value => value.sys_enterpriseid == result),
                value: [this.data.list.find(value => value.sys_enterpriseid == result).enterprisename, result]
            } : {
                result,
                list: result.map(v => this.data.list.find(value => value.sys_enterpriseid == v)),
                value: [result.map(v => {
                    let data = this.data.list.find(value => value.sys_enterpriseid == v);
                    return data ? data.enterprisename : ""
                }), result]
            }
        getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
    },
    /* 开始搜索 */
    startSearch({
        detail
    }) {
        let condition = this.data.content ? this.data.content.where.condition : this.data.content.where.condition;
        if (detail == condition) return;
        this.setData({
            'content.where.condition': detail
        });
        this.getList(true);
    },
    /* 取消搜索 */
    onClear() {
        this.setData({
            'content.where.condition': ""
        });
        this.getList(true);
    },
    onReady() {
        this.selectComponent("#ListBox").setHeight(".total", this);
    }
})