const _Http = getApp().globalData.http;
Component({
    properties: {
        sa_projectid: String,
        sa_projstagetempid: String
    },
    data: {
        content: {
            "nacache": true,
            "total": null,
        },
        active: null, //现在阶段
        viewIndex: 0, //查看阶段
        workList: [], //工作
    },
    methods: {
        getList() {
            _Http.basic({
                "id": 20221024102402,
                "content": {
                    ...this.data.content,
                    "sa_projectid": this.data.sa_projectid,
                    "sa_projstagetempid": this.data.sa_projstagetempid
                }
            }).then(res => {
                console.log("项目阶段", res)
                if (res.msg != '成功') return wx.showToast({
                    title: res.data,
                    icon: "none"
                });
                const list = res.data.map(v => {
                        v.finish = v.work.filter(e => e.finished == 1).length;

                        v.progress = v.finish == 0 ? 0 : v.finish / v.work.length * 100;
                        return v
                    }),
                    active = res.data.findIndex(v => v.active == 1);
                this.setData({
                    list,
                    active,
                    viewIndex: active,
                    "content.total": res.total
                });
                this.progress();
            })
        },
        progress() {
            let that = this;
            this.createSelectorQuery().selectAll(".parallel").boundingClientRect(function (rect) {
                rect.forEach((v, i) => {
                    that.data.list[i].width = i != rect.length - 1 ? (v.width / 2) + (rect[i + 1].width / 2) : 0;
                })
                that.setData({
                    list: that.data.list
                })
            }).exec();
        },
        /* 切换查看 */
        changeViewItem(e) {
            const {
                index
            } = e.currentTarget.dataset;
            this.setData({
                viewIndex: index
            });
        },
        /* 设置开始阶段 */
        setActive() {
            let {
                stagename,
                sa_projectid,
                sa_projstagetempid,
                sa_project_stageid
            } = this.data.list[this.data.viewIndex],
                that = this;
            wx.showModal({
                title: '提示',
                content: `是否确定进入${stagename} 阶段`,
                complete: ({
                    confirm
                }) => {
                    if (confirm) _Http.basic({
                        "id": 20221024160102,
                        "content": {
                            sa_projectid,
                            sa_projstagetempid,
                            sa_project_stageid
                        }
                    }).then(res => {
                        if (res.msg == '成功') that.getList();
                        wx.showToast({
                            title: res.msg == '成功' ? `已成功进入${stagename} 阶段` : res.data,
                            icon: "none"
                        });
                    })
                }
            })
            console.log(sa_projectid,
                sa_projstagetempid,
                sa_project_stageid)
        },
    }
})