const _Http = getApp().globalData.http,
    getHeight = require("../../utils/getRheRemainingHeight");
Page({
    data: {
        navList: [{
            label: "我负责的",
            icon: "icon-webxialaxuanxiangjiantou",
            color: "",
            width: "",
            id: "1"
        }, {
            label: "创建时间",
            icon: "icon-quxiao",
            color: "",
            width: "",
            id: "3"
        }, {
            label: "筛选",
            icon: "icon-daoruxialajiantou",
            color: "",
            width: "",
            id: "2"
        }],
        content: {
            "type": 1,
            "pageNumber": 1,
            "pageSize": 2,
            "isExport": 0,
            "where": {
                "condition": "",
                "status": "", //状态
                "startdate": "",
                "enddate": ""
            },
            "sort": [{
                sortname: "默认",
                sorted: 1,
                sortid: 0,
                reversed: 0
            }]
        },
        list: [],
        classShow: false, //type类型
        classActions: [{
                name: '我负责的',
                index: 1
            },
            {
                name: '我参与的',
                index: 2
            },
            {
                name: "我下属负责的",
                index: 3
            }, {
                name: "我下属参与的",
                index: 4
            }
        ],
        filter: {
            show: false,
            status: ['潜在', '合作中', '已终止'],
            statusActive: "",
            startdate: "",
            enddate: ""
        }
    },
    /* 去添加 */
    toAdd() {
        wx.navigateTo({url: './addAndEditor'})
    },
    onLoad(options) {
        this.getList()
    },
    /* 筛选状态选择 */
    selectStatus(e) {
        const {
            item
        } = e.currentTarget.dataset;
        this.setData({
            "filter.statusActive": this.data.filter.statusActive == item ? "" : item
        })
    },
    /* 筛选日期范围 */
    changeDate(e) {
        const name = e.currentTarget.dataset.name,
            value = e.detail.value;
        this.setData({
            [`filter.${name}`]: value
        })
    },
    /* 处理筛选 */
    handleFilter({
        detail
    }) {
        const data = this.data.filter;
        switch (detail) {
            case 'confirm':
                this.setData({
                    'content.where.status': data.statusActive,
                    'content.where.startdate': data.startdate,
                    'content.where.enddate': data.enddate,
                    'filter.show': false
                });
                this.getList(true);
                break;
            case 'reset':
                this.setData({
                    'filter.statusActive': "",
                    'filter.startdate': "",
                    'filter.enddate': "",
                });
                break;
            case 'close':
                this.setData({
                    'filter.show': false
                });
                break;
        }
    },
    getList(init = false) {
        //init 用于初始化分页
        if (init.detail != undefined) init = init.detail;
        if (init) this.setData({
            ['content.pageNumber']: 1
        })
        if (this.data.content.pageNumber > this.data.content.pageTotal) return;
        _Http.basic({
            "id": 20221012164402,
            content: this.data.content
        }).then(res => {
            console.log(res)
            this.selectComponent('#ListBox').RefreshToComplete();
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            this.getTags(res.data.map(v => v.sys_enterpriseid));
            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)
            })
        })
    },
    /* 获取列表标签 */
    getTags(ownerids = []) {
        _Http.basic({
            "id": 20221018102001,
            "content": {
                "ownertable": "sa_customers",
                ownerids
            }
        }).then(res => {
            for (let key in res.data) {
                let index = this.data.list.findIndex(v => v.sys_enterpriseid == key);
                this.data.list[index].tags = res.data[key]
            };
            this.setData({
                list: this.data.list
            })
        })

    },
    /* 顶部条件导航回调 */
    navClick({
        detail
    }) {
        switch (detail.id) {
            case '1':
                this.setData({
                    classShow: true
                })
                break;
            case '2':
                this.setData({
                    'filter.show': true
                })
                break;
            case '3':
                this.setData({
                    'content.sort[0].reversed': this.data.content.sort[0].reversed == 0 ? 1 : 0
                });
                this.getList(true)
                break;
            default:

                break;
        }
    },
    navSearch() {
        console.log('搜索')
    },
    classClose() {
        this.setData({
            classShow: false
        })
    },
    classSelect({
        detail
    }) {
        if (this.data.content.type == detail.index) return this.classClose();
        this.setData({
            "content.type": detail.index,
            'navList[0].label': detail.name
        })
        this.classClose();
        this.getList(true)
    },
    onReady() {
        getHeight.getHeight('.total', this).then(res => this.setData({
            listHeight: res
        }));
    },
    onShareAppMessage() {}
})