const _Http = getApp().globalData.http;

Page({
    data: {
        year: new Date().getFullYear(),
        content: {
            nocache: true,
            "year": new Date().getFullYear(),
            "pageNumber": 1,
            "pageTotal": 1,
            "pageSize": 20,
            "where": {
                "condition": ""
            },
        },
        projectList: [],
        YL: []
    },
    onLoad(options) {
        this.setData({
            year: options.year,
            'content.year': options.year,
            'content.hrid': wx.getStorageSync('userMsg').hrid,
            yearArr: options.yearArr.split(","),
            pickerIndex: options.pickerIndex,
            YL: JSON.parse(options.PYL)
        })
        this.getList();
        this.getProjectList();
        getApp().globalData.Language.getLanguagePackage(this, '项目目标');
    },
    /* 添加项目 */
    getResult({
        detail
    }) {
        console.log(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 => {
            console.log("年度项目目标", res)
            if (res.code != '1') 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.code != '1') 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: getApp().globalData.Language.getMapText('提示'),
            content: getApp().globalData.Language.getMapText('是否确认删除') + item.projectname,
            cancelText: getApp().globalData.Language.getMapText('取消'),
            confirmText: getApp().globalData.Language.getMapText('确定'),
            success({
                confirm
            }) {
                if (confirm) {
                    _Http.basic({
                        "id": 20220905165302,
                        "content": {
                            "sa_salestargetid": [
                                item.sa_salestargetid
                            ]
                        },
                    }).then(res => {
                        if (res.code != '1') return wx.showToast({
                            title: res.data,
                            icon: "none"
                        })
                        that.setData({
                            list: that.data.list.filter(v => v.sa_salestargetid != item.sa_salestargetid),
                            total: that.data.total - 1
                        })
                        wx.showToast({
                            title: getApp().globalData.Language.getMapText('删除成功'),
                            icon: "none"
                        })
                    })
                }
            }
        })
    },
    /* 开启添加项目 */
    addProject() {
        this.selectComponent("#Plist").setData({
            show: true
        })
    },
    onReachBottom() {
        this.getList();
    }
})