const _Http = getApp().globalData.http;

Page({
    options: {
        addGlobalClass: true
    },
    data: {
        sheetShow: false,
        sheetTitle: "",
        showWhere: "所有日期",
        timeRangeShow: false,
        sat_orderclueid: 0,
        detailsData: null,
        followList: null,
        isdesc: 1,
        whereType: {
            value: "start",
            list: {
                "start": "",
                "end": ""
            }
        },
        where: {
            "start": "",
            "end": ""
        },
        teamList: [],
        isEdit: false, //退出时是否重新获取列表
        actions: [{
            name: "跟进"
        }, {
            name: "成交"
        }, {
            name: "无效"
        }]
    },
    handleSelect({
        detail
    }) {
        const that = this;
        if (detail.name == '跟进') {
            this.selectComponent("#follow").updateTime();
            this.setData({
                sheetTitle: '跟进计划'
            })
        } else {
            wx.showModal({
                title: "提示",
                content: `是否设置该线索为"${detail.name}"状态，确定后无法撤销！`,
                success({
                    confirm
                }) {
                    if (!confirm) return;
                    _Http.basic({
                        "classname": "saletool.orderclue.web.orderclue",
                        "method": "addFollowUpLog",
                        "content": {
                            sat_orderclueid: that.data.detailsData.sat_orderclueid,
                            "content": "",
                            "followupmode": "",
                            "logtype": detail.name,
                            "competitor": ""
                        },
                    }).then(res => {
                        if (res.msg != '成功') return wx.showToast({
                            title: res.msg,
                            icon: "none"
                        });
                        that.selectDetail();
                        that.getFollowList();
                    });
                    const pages = getCurrentPages();
                    pages[pages.length - 2].getList();
                    setTimeout(() => {
                        wx.showToast({
                            title: '操作成功',
                            icon: "none"
                        })
                    }, 100)
                }
            })
        }
        this.selectCancel();
    },
    selectCancel() {
        this.setData({
            sheetShow: false
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let auth = [];
        options.auth ? auth = options.auth.split(",") : getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ['营销工具'], ['销售线索'])[0].apps[0].meta.auth.forEach(v => auth.push(v.optionname));
        this.setData({
            sat_orderclueid: options.id,
            tagColor: options.color,
            auth
        })
        this.selectDetail();
        this.getFollowList();
        this.query_teamList();
    },
    /* 团队成员 */
    query_teamList(i = 0) {
        if (i == 5) return;
        _Http.basic({
            "classname": "sale.team.team",
            "method": "query_teamList",
            "content": {
                "pageNumber": 1,
                "pageSize": 99999,
                "where": {
                    "condition": ""
                }
            }
        }).then(res => {
            if (res.msg != '成功') return this.selectDetail(i + 1);
            this.setData({
                teamList: res.data
            })
        })
    },
    /* 查询详情 */
    selectDetail(i = 0) {
        if (i == 5) return wx.showToast({
            title: '详情信息查询失败!',
            icon: "none"
        })
        _Http.basic({
            "classname": "saletool.orderclue.web.orderclue",
            "method": "selectDetail",
            "content": {
                "sat_orderclueid": this.data.sat_orderclueid
            }
        }).then(res => {
            if (res.msg != '成功') return this.selectDetail(i + 1);
            let data = res.data;
            for (let i in data) {
                if (data[i] === '') data[i] = '-';
            }
            this.setData({
                detailsData: data
            })
        })
    },
    /* 跟进列表 */
    getFollowList(i = 0) {
        if (i == 5) return wx.showToast({
            title: '跟进记录查询失败!',
            icon: "none"
        })
        _Http.basic({
            "classname": "saletool.orderclue.web.orderclue",
            "method": "getFollowList",
            "content": {
                "sat_orderclueid": this.data.sat_orderclueid,
                "isdesc": this.data.isdesc,
                where: this.data.where
            }
        }).then(res => {
            console.log("跟进", res)
            if (res.msg != '成功') return this.getFollowList(i + 1);
            this.setData({
                followList: res.data
            })
        })
    },
    /* 打开弹出 */
    openPoput(e) {
        const {
            name
        } = e.currentTarget.dataset;
        if (name == '跟进计划') {
            this.setData({
                sheetShow: true
            })
        } else {
            if (name == '线索编辑') this.selectComponent("#edit").initData()
            this.setData({
                sheetTitle: name
            })
        }
    },
    /* 结束编辑 */
    endEdit() {
        this.setData({
            sheetTitle: "",
            isEdit: true
        })
        this.selectDetail();
    },
    /* 结束跟进 */
    endFollow() {
        this.endEdit();
        this.getFollowList();
    },
    /* 修改排序方式 */
    changeIsdesc() {
        this.setData({
            isdesc: this.data.isdesc == 1 ? 0 : 1
        });
        this.getFollowList();
    },
    /* 打开选择时间 */
    openSelectDate() {
        this.setData({
            timeRangeShow: true
        })
    },
    /* 查看所有日期 */
    toSeeAll() {
        this.setData({
            showWhere: "所有日期"
        })
        this.timeRangeClose();
        this.getFollowList();
    },
    /* 关闭时间范围选择 */
    timeRangeClose() {
        this.setData({
            timeRangeShow: false,
            "where": {
                "start": "",
                "end": ""
            }
        })
    },
    /* 选择日期类型 */
    selectDateType(e) {
        const {
            name
        } = e.target.dataset;
        this.setData({
            "whereType.value": name
        })
    },
    /* 得到选择时间 */
    getDate({
        detail
    }) {
        let obj = this.data.whereType;
        obj.list[obj.value] = detail;
        this.setData({
            whereType: obj
        })
    },
    /* 确定时间范围 */
    determineScope() {
        const {
            list
        } = this.data.whereType;
        this.setData({
            "where": list,
            showWhere: list.start + '~' + list.end,
            timeRangeShow: false
        });
        this.getFollowList();
    },
    callOut(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phonenumber
        })
    },
    onUnload() {
        if (this.data.isEdit) {
            let pages = getCurrentPages();
            if (pages[pages.length - 2].route == 'pages/threadedTree/index') pages[pages.length - 2].getList(true);
        }
    },
    onShareAppMessage(res) {}
})