const _Http = getApp().globalData.http;

Page({
    data: {
        year: new Date().getFullYear(),
        content: {
            nocache: true,
            "year": new Date().getFullYear(),
            "hrid": wx.getStorageSync('userMsg').hrid,
            "pageNumber": 1,
            "pageTotal": 1,
            "pageSize": 20,
            "where": {
                "condition": ""
            },
        },
        projectList: []
    },
    onLoad(options) {
        this.setData({
            year: options.year,
            'content.year': options.year,
            yearArr: options.yearArr.split(","),
            pickerIndex: options.pickerIndex
        })
        this.getList();
        this.getProjectList();
    },
    /* 添加项目 */
    getResult({
        detail
    }) {
        let project = [];
        detail.forEach(v => {
            let data = this.data.projectList.find(value => value.sa_projectid == v);
            project.push({
                "target_l": data.signamount_due || "",
                "target_h": data.signamount_due || "",
                "month": data.signdate_due.split("-")[1] || "",
                ...data
            })
        });
        wx.navigateTo({
            url: './addProject?data=' + JSON.stringify(project) + '&year=' + this.data.year,
        })
    },
    /* 选择年份 
    bindDateChange({
        detail
    }) {
        if (this.data.year == detail.value) return;
        this.setData({
            "year": detail.value,
            "content.year": detail.value
        });
        this.getList(true);
    },*/
    /* 选择年份 */
    bindDateChange({
        detail
    }) {
        let index = detail.value;
        if (this.data.pickerIndex == detail.value) return;
        this.setData({
            "year": this.data.yearArr[detail.value],
            "content.year": this.data.yearArr[detail.value],
            pickerIndex: index
        });
        this.getList(true);
    },
    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": 20220906104002,
            "content": this.data.content
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            this.setData({
                list: res.data,
                'content.pageNumber': res.pageNumber + 1,
                'content.pageTotal': res.pageTotal,
                total: res.total
            })
        })
    },
    getProjectList() {
        _Http.basic({
            "id": 20220905151902,
            "content": {
                nocache: true,
                pageSize: 99999,
                "where": {
                    "condition": ""
                }
            },
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            this.setData({
                projectList: res.data
            })
        })
    },
    /* 删除项目 */
    deleteItem(e) {
        const {
            item
        } = e.currentTarget.dataset,
            that = this;
        wx.showModal({
            title: '提示',
            content: `是否确认删除${item.projectname}`,
            success({
                confirm
            }) {
                if (confirm) {
                    _Http.basic({
                        "id": 20220905165302,
                        "content": {
                            "sa_salestargetid": [
                                item.sa_salestargetid
                            ]
                        },
                    }).then(res => {
                        if (res.msg != '成功') return wx.showToast({
                            title: res.data,
                            icon: "none"
                        })
                        that.setData({
                            list: that.data.list.filter(v => v.sa_salestargetid != item.sa_salestargetid)
                        })
                        wx.showToast({
                            title: '删除成功',
                            icon: "none"
                        })
                    })
                }
            }
        })
    },
    /* 开启添加项目 */
    addProject() {
        this.selectComponent("#Plist").setData({
            show: true
        })
    },
    onReachBottom() {
        this.getList();
    },
    onShareAppMessage() {}
})