const {
    getCurrentPage
} = require("../../miniprogram_npm/@vant/weapp/common/utils");

const _Http = getApp().globalData.http;
Page({
    data: {
        list: [],
        params: {
            classname: "webmanage.saletool.orderclue.publicclue.PublicClue",
            "method": "getMatchAgentList",
            "content": {
                "nocache": true,
                "pageNumber": 1,
                "pageSize": 20,
                "sat_orderclueid": '',
                "where": {
                    "condition": ""
                }
            },
        },
        detail: {}
    },
    onLoad(options) {
        let data = JSON.parse(options.data)
        this.setData({
            detail: data,
            'params.content.sat_orderclueid': data.sat_orderclueid
        })
        this.getList();
    },
    /* 开始搜索 */
    onSearch({
        detail
    }) {
        console.log(detail);
        if (this.data.params.content.where.condition == detail) return;
        this.setData({
            'params.content.where.condition': detail
        });
        this.getList(true)
    },
    onClear() {
        this.setData({
            'params.content.where.condition': ""
        });
        this.getList(true)
    },
    /* 选中 */
    onChange(e) {
        const {
            item
        } = e.currentTarget.dataset;
        wx.showModal({
            title: '提示',
            content: `是否将线索分配给"${item.name}"?`,
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    classname: "webmanage.saletool.orderclue.publicclue.PublicClue",
                    "method": this.data.detail.cluetype == '经销商' ? 'matchAgent' : 'matchAgent_Sales',
                    "content": {
                        matchlist: [{
                            sat_orderclueid: this.data.detail.sat_orderclueid,
                            [this.data.detail.cluetype == '经销商' ? 'sa_agentsid' : 'hrid']: item.hrid
                        }]
                    }
                }).then(res => {
                    console.log("分配线索", res)
                    wx.showToast({
                        title: res.msg == '成功' ? '分配成功' : res.msg,
                        icon: "none",
                        mask: true
                    });
                    if (res.msg != '成功') return;
                    setTimeout(() => {
                        getCurrentPages().forEach(v => {
                            if (v.__route__ == "packageA/publicClue/index") {
                                v.getList(true)
                            } else if (v.__route__ == "packageA/publicClue/detail") {
                                wx.navigateBack()
                                getCurrentPages().forEach(v => {
                                    if (v.__route__ == "packageA/publicClue/index") {
                                        v.getList(true)
                                    }
                                })
                            }
                        })
                        wx.navigateBack()
                    }, 500)
                })
            }
        })
    },
    //获取列表
    getList(init = false) {
        let params = this.data.params;
        console.log(this.data.detail);
        if (init) params.content.pageNumber = 1;
        if (params.content.pageNumber > params.content.pageTotal) return;
        if (this.data.detail.cluetype == '业务员') {
            params.method = 'getMatchSalesList'
        } else {
            params.method = 'getMatchAgentList'
        }
        _Http.basic(params).then(res => {
            console.log("联系人", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            res.data = res.data.filter(v => v.userid != wx.getStorageSync('userMsg').userid)
            this.setData({
                list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                "params.content.pageNumber": res.pageNumber + 1,
                "params.content.pageTotal": res.pageTotal,
            })
        })
    },
    onReachBottom() {
        this.getList();
    }
})