const _Http = getApp().globalData.http;
Component({
    options: {
        addGlobalClass: true,
    },
    data: {
        yearArr: [],
        pickerIndex: 0,
        domainList: null, //领域列表
        createYear: null,
        "content": {
            nocache: true,
            "sys_enterpriseid": 1, //企业id
            "sa_contractid": 1,
            total: null,
            "where": {
                year: "",
                tradefield: ""
            }
        },
        domainList: ['全部'],
        domainIndex: 0,
    },
    methods: {
        /* 获取产品列表 */
        getList(id, init = false) {
            let sys_enterpriseid = getCurrentPages()[getCurrentPages().length - 1].data.detail.sys_enterpriseid;
            _Http.basic({
                "id": 20230103102302,
                "content": {
                    sys_enterpriseid,
                    pageSize: 9999
                }
            }).then(res => {
                console.log("获取开启年度", res)
                this.setData({
                    yearArr: res.data.length == 0 ? [new Date().getFullYear()] : res.data.map(v => v.year),
                    pickerIndex: res.data.length == 0 ? 0 : res.data.length - 1,
                    "content.sys_enterpriseid": sys_enterpriseid,
                    "content.sa_contractid": sys_enterpriseid,
                    "content.where.year": res.data.length == 0 ? new Date().getFullYear() : res.data[res.data.length - 1].year,
                    "content.total": res.total
                });

                this.getList1(init)
            });

            /* 获取领域 */
            _Http.basic({
                "id": 20221223141802,
                "content": {
                    "pageNumber": 1,
                    "pageSize": 9999,
                    "where": {
                        "condition": ""
                    }
                }
            }, false).then(res => {
                console.log("获取领域", res)
                let domainList = []
                if (res.msg == '成功') domainList = res.data.map(v => {
                    return v.tradefield
                });
                domainList.unshift("全部")
                this.setData({
                    domainList
                });
            })
        },
        getList1(init = false, isEdit = false) {
            let content = this.data.content;
            if (init) content.pageNumber = 1
            _Http.basic({
                "id": "20221209150102",
                content
            }).then(res => {
                console.log("业绩目标", res)
                if (res.msg != '成功') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                this.setData({
                    list: res.data,
                    "content.total": res.total,
                })
                if (isEdit) this.editObjective();
            })
        },
        bindDateChange(e) {
            this.setData({
                "content.where.year": this.data.yearArr[e.detail.value]
            });
            this.getList1(true)
        },
        domainChange(e) {
            let tradefield = this.data.domainList[e.detail.value];
            this.setData({
                "content.where.tradefield": tradefield == '全部' ? "" : tradefield,
                domainIndex: e.detail.value
            });
            this.getList1(true)
        },
        /* 编辑年度 */
        editObjective() {
            if (this.data.list.length == 0) return wx.showToast({
                title: '当前年度还未开启',
                icon: "none"
            });
            wx.navigateTo({
                url: '/packageA/contract/modules/objective/add?params=' + JSON.stringify(this.data.list[0]),
                fail(err) {
                    console.log(err)
                }
            })
        },
        submit(e) {
            let that = this;
            wx.showModal({
                title: '提示',
                content: `是否确认保存${e.year}年度目标`,
                complete: (res) => {
                    if (res.confirm) _Http.basic({
                        id: "20221229111502",
                        content: {
                            ...e,
                            sa_contractid: that.data.content.sa_contractid
                        }
                    }).then(res => {
                        console.log('保存年度目标', res)
                        wx.showToast({
                            title: res.msg == '成功' ? '保存成功' : res.msg,
                            icon: "none"
                        })
                        if (res.msg == '成功') {
                            that.setData({
                                ['list[0]']: e
                            })
                            setTimeout(wx.navigateBack, 500)
                        }
                    })
                }
            })
        },
    }
})