const _Http = getApp().globalData.http;
Page({
    data: {
        tabs: [{
            title: "我收到的",
            id: "Receive"
        }, {
            title: "我发出的",
            id: "SendOut"
        }],
        active: "Receive",
        filterShow: false,
        filtratelist: [{
            label: "模板类型",
            index: null,
            showName: "reportname", //显示字段
            valueKey: "sys_workreportmodelid", //返回Key
            selectKey: "sys_workreportmodelid", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: []
        }, {
            label: "阅读状态",
            index: null,
            showName: "name", //显示字段
            valueKey: "readstatus", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: [{
                name: "全部",
                value: ""
            }, {
                name: "已读",
                value: "1"
            }, {
                name: "未读",
                value: "0"
            }]
        }, {
            label: "时间筛选",
            index: null,
            showName: "name", //显示字段
            valueKey: "date", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: [{
                name: "今日",
                value: "today"
            }, {
                name: "最近三天",
                value: "lastthreedays"
            }, {
                name: "最近一周",
                value: "lastWeek"
            }]
        }],
        isread: {
            label: "阅读状态",
            index: null,
            showName: "name", //显示字段
            valueKey: "readstatus", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: [{
                name: "全部",
                value: ""
            }, {
                name: "已读",
                value: "1"
            }, {
                name: "未读",
                value: "0"
            }]
        },
        where: {
            condition: "",
        }
    },
    onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '工作汇报')
        this.getList();
    },
    onChange(e) {
        let list = this.selectComponent("#Yl_Filtrate1").data.list;
        if (e.detail.name == 'SendOut') {
            this.data.isread = list[1];
            this.setData({
                filtratelist: list.filter(v => v.label != '阅读状态')
            })
        } else {
            list.splice(1, 0, this.data.isread)
            this.setData({
                filtratelist: list
            })
        }
        this.setData({
            active: e.detail.name
        });
        this.getList();
    },
    getList(init = false) {
        if (init.detail != undefined) init = init.detail;
        this.selectComponent("#" + this.data.active).getList(init);
        this.getModel();
    },
    updateList() {
        let Receive = this.selectComponent("#Receive");
        if (Receive) {
            let content = JSON.parse(JSON.stringify(Receive.data.content))
            content.pageSize = (content.pageNumber - 1) * content.pageSize;
            content.pageNumber = 1;
            _Http.basic({
                "id": '20230524103102',
                content
            }).then(res => {
                console.log("Receive更新列表", res)
                if (res.code != '0') Receive.setData({
                    list: res.data
                })
            })
        }

        let SendOut = this.selectComponent("#SendOut");
        if (SendOut) {
            let Scontent = JSON.parse(JSON.stringify(SendOut.data.content))
            Scontent.pageSize = (Scontent.pageNumber - 1) * Scontent.pageSize;
            Scontent.pageNumber = 1;
            _Http.basic({
                "id": "20230524102802",
                content: Scontent
            }).then(res => {
                console.log("SendOut更新列表", res)
                if (res.code != '0') SendOut.setData({
                    list: res.data
                })
            })
        }
    },
    /* 获取模版列表 */
    getModel() {
        _Http.basic({
            "id": "20230524091902",
            "content": {
                "hrid": wx.getStorageSync('userMsg').hrid
            }
        }).then(res => {
            console.log("可创建模版列表", res)
            if (res.code != '1') return wx.showToast({
                title: res.msg,
                icon: 'none',
                mask: true
            })
            this.setData({
                modelList: res.data,
                "filtratelist[0].list": res.data
            })
        })
    },
    /* 新建汇报 */
    newData(e, i = 0) {
        wx.navigateTo({
            url: '/packageA/report/insert?model=' + JSON.stringify(this.data.modelList[i]),
        })
    },
    /* Picker选择器选择新建模版 */
    onPickerSelected(e) {
        this.newData("", e.detail.value)
    },
    /* 搜索 */
    onSearch({
        detail
    }) {
        if (this.data.where.condition == detail) return;
        this.data.where.condition = detail;
        this.selectComponent("#" + this.data.active).getList(true);
    },
    /* 筛选 */
    handleFilter({
        detail
    }) {
        detail.condition = this.data.where.condition;
        detail.today = 0
        detail.lastthreedays = 0
        detail.lastWeek = 0
        if (detail.date) detail[detail.date] = 1;
        detail.begindate = detail.startdate || ''
        this.data.where = detail;

        this.selectComponent("#" + this.data.active).getList(true);
    },
    onClick() {
        this.setData({
            filterShow: true
        })
    }
})