const _Http = getApp().globalData.http;
Component({
    options: {
        addGlobalClass: true
    },
    data: {
        content: {
            nocache: true,
            type: 0,
            dataid: 0,
            pageNumber: 1,
            pageSize: 20,
            dataType: "10",
            dateType: '月',
            where: {
                dateType: '月',
                begindate: "",
                enddate: ""
            }
        },
        showFiltrete: false,
        filtratelist: [{
            label: "时间筛选",
            index: 2,
            default: 2,
            showName: "name", //显示字段
            valueKey: "dateType", //返回Key
            value: "value", //选中值
            selectKey: "value",
            interrupt: true,
            relevance: "dateRange",
            list: [{
                name: "上月",
                value: "上月"
            }, {
                name: "本日",
                value: "日"
            }, {
                name: "本周",
                value: "周"
            }, {
                name: "本月",
                value: "月",
            }, {
                name: "本年",
                value: "年"
            }]
        }]
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
            this.selectComponent("#Yl_Filtrate1").getMonthRange()
        }
    },
    methods: {
        handleFilter({
            detail
        }) {
            if (detail.name == "reset") {
                this.selectComponent("#Yl_Filtrate1").getMonthRange()
                this.setData({
                    'filtratelist[0].index': 2
                })
            }
            this.setData({
                "content.dateType": detail.dateType,
                "content.where.dateType": detail.dateType,
                "content.where.begindate": detail.startdate,
                "content.where.enddate": detail.enddate,
            });
            this.getList(true);
        },
        interrupt({
            detail
        }) {
            if (detail.data.label == "时间筛选") {
                switch (detail.item.name) {
                    case '上月':
                        this.selectComponent("#Yl_Filtrate1").getLastMonthRange()
                        break;
                    case '本日':
                        this.selectComponent("#Yl_Filtrate1").queryDays(1)
                        break;
                    case '本周':
                        this.selectComponent("#Yl_Filtrate1").getWeekRange()
                        break;
                    case '本月':
                        this.selectComponent("#Yl_Filtrate1").getMonthRange()
                        break;
                    case '本年':
                        this.selectComponent("#Yl_Filtrate1").getYearRange()
                        break;
                }
            }
        },
        init(type, dataid) {
            if (this.data.content.dataid != dataid) {
                this.setData({
                    'content.dataid': dataid,
                    'content.type': type,
                    list: []
                });
                this.getList(true);
            }
        },
        getList(init = false) {
            if (init.detail != undefined) init = init.detail;
            let content = JSON.parse(JSON.stringify(this.data.content));
            if (init) {
                content.pageNumber = 1;
                content.pageTotal = 1;
            }
            if (content.pageNumber > content.pageTotal) return;
            content.sort = [{
                sortid: 208,
                sorted: 1,
                reversed: 0
            }]
            _Http.basic({
                "id": 20230617143104,
                content
            }).then(res => {
                console.log(content.dataType + "项目", res)
                if (this.data.content.dataType != content.dataType) return;
                this.selectComponent("#ListBox").setHeight(".head", this);
                this.selectComponent('#ListBox').RefreshToComplete();
                if (res.code != '1') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                });
                content.pageNumber = res.pageNumber + 1;
                content.pageTotal = res.pageTotal;
                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    content
                })
            })
        },
        changeDataType(e) {
            const id = e.target.id;
            if (!id || this.data.content.dataType == id) return;
            this.setData({
                "content.dataType": e.target.id,
                list: []
            });
            this.getList(true)
        },
        openFiltrete() {
            this.setData({
                showFiltrete: true
            })
        },
        callphonenumber(e) {
            const {
                phonenumber
            } = e.currentTarget.dataset;
            if (!phonenumber) return;
            wx.makePhoneCall({
                phoneNumber: phonenumber
            })
        }
    }
})