const _Http = getApp().globalData.http;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        myProject: false,
        list: [],
        total: null,
        projectList: null,
        sa_salesforecastmodelid: null,
        "content": {
            "nocache": true,
            "sa_salesforecastbillid": null,
            "pageNumber": 1,
            "pageSize": 20,
            "pageTotal": 1,
            "where": {
                "condition": ""
            }
        },
    },
    onLoad(options) {
        this.setData({
            'content.sa_salesforecastbillid': options.id,
            'sa_salesforecastmodelid': options.mId
        })
        this.getList();
        if (options.my) this.setData({
            myProject: true
        })

    },
    /* 添加项目 */
    addProjece() {
        _Http.basic({
            "id": 20220906154803,
            "version": 1,
            "content": {
                "nocache": true,
                pageSize: 9999,
                "sa_salesforecastbillid": this.data.content.sa_salesforecastbillid,
                "where": {
                    "condition": ""
                }
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            this.setData({
                projectList: res.data
            });
            this.selectComponent("#Plist").setData({
                show: 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;
        return _Http.basic({
            "id": 20220916115203,
            "version": 1,
            content: this.data.content
        }).then(res => {
            console.log("列表", res)
            this.selectComponent('#ListBox').RefreshToComplete();
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            const list = res.data.filter(v => v.sa_projectid != 0);
            this.setData({
                'content.pageNumber': res.pageNumber + 1,
                'content.pageTotal': res.pageTotal,
                list: res.pageNumber == 1 ? list : this.data.list.concat(list),
                total: res.total - 1
            })
        })
    },
    /* 得到结果创建项目 */
    getResult({
        detail
    }) {
        _Http.basic({
            "id": 20220916134103,
            "version": 1,
            "content": {
                "sa_salesforecastmodelid": this.data.sa_salesforecastmodelid,
                "sa_salesforecastbillid": this.data.content.sa_salesforecastbillid,
                "sa_projectids": detail
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            this.getList(true)
        })
    },
    /* 开始搜索 */
    startSearch({
        detail
    }) {
        this.setData({
            'content.where.condition': detail.trim()
        });
        this.getList(true);
    },
    cancelSearch() {
        this.setData({
            'content.where.condition': ""
        });
        this.getList(true);
    },
    onReady() {
        //滚动区域高度
        this.selectComponent("#ListBox").setHeight("#Search", this);
    },
    onClose(event) {
        const {
            instance
        } = event.detail;
        instance.close();
    },
    /* 删除提报 */
    deleteProject(e) {
        const {
            item
        } = e.currentTarget.dataset, that = this;
        wx.showModal({
            title: "提示",
            content: `是否确认删除${item.projectname}`,
            success({
                confirm
            }) {
                if (confirm) _Http.basic({
                    "id": 20220908134803,
                    "version": 1,
                    "content": {
                        "sa_salesforecastbillid": item.sa_salesforecastbillid
                    }
                }).then(res => {
                    console.log(res)
                    if (res.msg != '成功') return wx.showToast({
                        title: res.data,
                        icon: "none"
                    })
                    let arr = that.data.list.filter(v => v.sa_salesforecastbillid != item.sa_salesforecastbillid);
                    that.setData({
                        list: arr
                    })
                    wx.showToast({
                        title: '删除成功',
                        icon: 'none'
                    })
                })
            }
        })
    },
    toReportForms(e) {
        const {
            item
        } = e.currentTarget.dataset;
        let isEdit = this.data.myProject;
        item.sa_salesforecastmodelid = this.data.sa_salesforecastmodelid;
        wx.navigateTo({
            url: `./reportForms?item=${JSON.stringify(item)}&isEdit=${isEdit}`,
        })
    },
    onShareAppMessage() {}
})