const _Http = getApp().globalData.http;
Component({
    options: {
        addGlobalClass: true
    },
    properties: {
        sa_projectid: {
            type: String
        },
        sa_projstagetempid: {
            type: String
        },
        changeCallBack: {
            type: Function
        },
        disabled: {
            type: Boolean
        }
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
        }
    },
    data: {
        active: null, //现在阶段
        content: {
            total: null
        }
    },
    methods: {
        getList(change = false) {
            _Http.basic({
                "id": 20221024102402,
                "content": {
                    "nacache": true,
                    "sa_projectid": this.data.sa_projectid,
                    "sa_projstagetempid": this.data.sa_projstagetempid
                }
            }).then(res => {
                console.log("工作阶段查询", res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                });
                const active = res.data.findIndex(v => v.active == 1);
                this.setData({
                    list: res.data,
                    active,
                    "content.total": res.total
                });
                this.viewData(change);
            })
        },
        /* 切换阶段 */
        changeViewItem(e) {
            if (!this.data.disabled) return;
            const {
                index
            } = e.currentTarget.dataset;
            let {
                stagename,
                sa_projectid,
                sa_projstagetempid,
                sa_project_stageid
            } = this.data.list[index],
                that = this;

            if (index == this.data.active) return getApp().globalData.Language.showToast([{
                v: '当前已在',
                t: 1,
                r: " “"
            }, {
                v: stagename,
                r: "” "
            }, {
                v: '阶段',
                t: 1
            }])

            wx.showModal({
                title: getApp().globalData.Language.getMapText('提示'),
                content: getApp().globalData.Language.joint([{
                    v: '是否进入',
                    t: 1,
                    r: " “"
                }, {
                    t: 1,
                    v: stagename,
                    r: "” "
                }, {
                    v: '阶段',
                    t: 1,
                    r: "?"
                }]),
                cancelText: getApp().globalData.Language.getMapText('取消'),
                confirmText: getApp().globalData.Language.getMapText('确定'),
                complete: ({
                    confirm
                }) => {
                    if (confirm) _Http.basic({
                        "id": 20221024160102,
                        "content": {
                            sa_projectid,
                            sa_projstagetempid,
                            sa_project_stageid
                        }
                    }).then(res => {
                        wx.showToast({
                            title: res.code == '1' ? getApp().globalData.Language.joint([{
                                v: '已进入',
                                t: 1,
                                r: " “"
                            }, {
                                v: stagename,
                                r: "” "
                            }, {
                                v: '阶段',
                                t: 1,
                            }]) : res.data,
                            icon: "none"
                        });
                        if (res.code == '1') {
                            that.setData({
                                active: index
                            })
                            that.viewData(true);
                        }
                    })
                }
            })
        },
        viewData(change = false) {
            this.triggerEvent("changeCallBack", {
                list: this.data.list[this.data.active],
                change
            })
        },
    }
})