const _Http = getApp().globalData.http;
Page({
    data: {
        taskname: ""
    },
    onLoad(options) {
        let content = JSON.parse(options.content);
        console.log(content)
        _Http.basic({
            "id": 20221129152304,
            "content": {
                ...content,
                pageSize: 999
            }
        }).then(res => {
            console.log("获取评估项", res)
            content.actionnotes = '';
            let isUser = content.sa_project_partiesids.length ? true : false;
            if (content.sa_project_partiesids.length) {
                res.data.map(v => {
                    v.sa_projtaskmag_optionsid = v.option[0].sa_projtaskmag_optionsid || 0;
                    v.score = v.option[0].score
                })
            } else {
                res.data = [{
                    option: {
                        ...res.data
                    },
                    sa_projtaskmag_optionsid: res.data[0].sa_projtaskmag_optionsid || 0,
                    score: res.data[0].score || "",
                    sa_project_partiesid: 0,
                }]
            }
            this.setData({
                isUser,
                taskname: options.taskname,
                content,
                list: res.data
            })
            if (this.data.isUser) this.getTags();
        });
    },
    changeOption(e) {
        const {
            id,
            index,
            score
        } = e.currentTarget.dataset;
        this.setData({
            [`list[${index}].sa_projtaskmag_optionsid`]: id,
            [`list[${index}].score`]: score,
        });
    },
    /* 获取列表标签 */
    getTags() {
        let list = this.data.list,
            ownerids = list.map(v => v.sa_customersid);
        _Http.basic({
            "id": 20221018102001,
            "content": {
                nocache: true,
                "ownertable": "sa_customers",
                ownerids
            }
        }).then(res => {
            console.log("标签", res)
            for (let key in res.data) {
                let index = list.findIndex(v => v.sa_customersid == key);
                if (index != -1) list[index].tags = res.data[key]
            };
            console.log(list)
            this.setData({
                list
            })
        })
    },
    onInput(e) {
        this.setData({
            'content.actionnotes': e.detail.value
        })
    },
    submit() {

        const that = this;
        wx.showModal({
            title: '提示',
            content: '是否确认提交本次评估?',
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": "20221128145004",
                    "content": {
                        ...that.data.content,
                        option: that.data.list.map(v => {
                            return {
                                sa_project_partiesid: v.sa_project_partiesid,
                                sa_projtaskmag_optionsid: v.sa_projtaskmag_optionsid,
                                score: v.score
                            }
                        })
                    }
                }).then(res => {
                    console.log('提交评估', res)
                    wx.showToast({
                        title: res.msg == '成功' ? '提交成功' : res.msg,
                        icon: "none"
                    });
                    if (res.msg == '成功') setTimeout(() => {
                        let page = getCurrentPages().find(v => v.__route__ == 'packageA/project/detail');
                        if (page) page.selectComponent("#Task").getList(that.data.content.sa_projectid, true);
                        wx.navigateBack();
                    }, 300);
                })
            }
        })
    }
})