const _Http = getApp().globalData.http;

Page({
    data: {
        year: null,
        arr: [],
        disabled: true,
        loading: false,
        range: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    },
    onLoad(options) {
        this.setData({
            project: JSON.parse(options.data),
            year: options.year
        })
    },
    submit() {
        if (this.data.disabled || this.data.loading) return;
        this.setData({
            loading: true
        })
        _Http.basic({
            "id": 20220905154102,
            "content": {
                "year": this.data.year,
                "hrid": wx.getStorageSync('userMsg').hrid,
                "project": this.data.project
            },
        }).then(res => {
            this.setData({
                loading: false
            });
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            this.setData({
                disabled: true
            })
            let pages = getCurrentPages();
            pages[pages.length - 2].getList(true);
            setTimeout(() => {
                wx.navigateBack({
                    delta: 0,
                })
            }, 300)
            wx.showToast({
                title: '添加成功',
                icon: "none"
            });
        })
    },
    inputChange(e) {
        const {
            index,
            name
        } = e.target.dataset;
        this.setData({
            [`project[${index}].${name}`]: e.detail.value
        });
        this.isDisabled(index);
    },
    bindDateChange(e) {
        const {
            index
        } = e.target.dataset;
        this.setData({
            [`project[${index}].month`]: e.detail.value - 0 + 1
        });
        this.isDisabled(index);
    },
    isDisabled(i) {
        let data = this.data.project[i],
            arr = this.data.arr;
        arr[i] = data.target_h != '' && data.target_l != '' && data.month != '';
        this.setData({
            arr,
            disabled: !arr.every(v => v)
        })
    },
    onShareAppMessage() {}
})