const getHeight = require("../../utils/getRheRemainingHeight"),
    _Http = getApp().globalData.http;
Page({
    data: {
        content: {
            "nochace": true,
            "isAll": false,
            "pageNumber": 1,
            "pageSize": 20,
            "where": {
                "condition": "",
                "status": "待跟进"
            }
        },
        list: [],
        sort: null,
        tagColor: '#FA8C16',
        sheetTitle: '',
        scrollHeight: 0, //滚动区域高度
        initInsert: true, //用于初始化新建组件
        isReverse: false,
    },
    onLoad(options) {
        let auth = [],
            apps = options.auth ? JSON.parse(options.auth) : getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ['营销工具'], ['销售线索'])[0].apps;
        apps[0].meta.auth.forEach(v => auth.push(v.optionname))
        this.setData({
            auth
        })
        this.getList()
    },
    /* 打开弹窗 */
    openSheet(e) {
        this.setData({
            sheetTitle: "添加线索"
        })
    },
    /* 结束新建 */
    endInsert() {
        this.setData({
            initInsert: false,
            sheetTitle: ""
        })
        setTimeout(() => {
            this.setData({
                initInsert: true
            })
        }, 10);
        this.getList(true);
    },
    /* 查询列表 */
    getList(init = false) {
        if (init.detail != undefined) init = init.detail;
        if (init) this.setData({
            ['content.pageNumber']: 1
        })
        if (this.data.content.pageNumber > this.data.content.pageTotal) return;
        let content = this.data.content;
        if (this.data.sort != null) content.sort = this.data.sort;
        _Http.basic({
            "classname": "saletool.orderclue.web.orderclue",
            "method": "selectList",
            content
        }).then(res => {
            console.log("列表", res)
            this.selectComponent('#ListBox').RefreshToComplete();
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            this.setData({
                list: (res.pageNumber == 1) ? res.data : this.data.list.concat(res.data),
                ['content.pageNumber']: res.pageNumber + 1,
                ['content.pageTotal']: res.pageTotal,
                sort: res.sort
            })
        })
    },
    /* 切换tab分类 */
    tabsChange({
        detail
    }) {
        let tagColor = '#BBBBBB';
        switch (detail.title) {
            case '待跟进':
                tagColor = '#FA8C16'
                break;
            case '跟进中':
                tagColor = '#52C41A'
                break;
            case '已成交':
                tagColor = '#ffffff'
                break;
            case '丢单':
                tagColor = '#FF3B30'
                break;
        }
        this.setData({
            'content.where.status': detail.title,
            tagColor,
            list: []
        })
        this.getList(true);
    },
    /* 搜索 */
    inputSharch({
        detail
    }) {
        this.setData({
            'content.where.condition': detail
        });
        this.getList(true);
    },
    /* 选择排序方式 */
    selectSort() {
        if (this.data.sort[0].sorted == 1) {
            this.setData({
                'sort[0].sorted': 0,
                'sort[1].sorted': 1,
            })
        } else {
            this.setData({
                'sort[0].sorted': 1,
                'sort[1].sorted': 0,
            })
        };
        this.setData({
            isReverse: !this.data.isReverse
        })
        this.getList(true)
    },
    onReady() {
        getHeight.getHeight('.tabs', this).then(res => {
            this.setData({
                scrollHeight: res
            })
        })
    },
    onShareAppMessage(res) { }
})