const _Http = getApp().globalData.http,
    MFT = require("../../utils/matchingFeilType"),
    deleteMark = require("../../utils/deleteMark");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        tabsTitle: "详情",
        rate: {
            nubmer: 5,
            text: "非常满意"
        },
        videoList: [],
        loading: false,
        isEvaluate: false
    },
    onLoad(options) {
        let auth = wx.getStorageSync('auth').wnotice.optionnames,
            dataAuth = '';
        if (auth.some(v => v == '团队内部数据分析') && auth.some(v => v == '经销商数据分析')) {
            dataAuth = 'all';
        } else {
            dataAuth = auth.some(v => v == '团队内部数据分析') ? '内部' : '经销商'
        };
        this.setData({
            viewData: auth.some(v => v == '团队内部数据分析') || auth.some(v => v == '经销商数据分析'),
            dataAuth,
            auth,
            sat_noticeid: options.id
        });
        this.queryNoticeMain(); //获取详情
    },
    /* 通告详情 */
    queryNoticeMain() {
        _Http.basic({
            "classname": "saletool.notice.notice",
            "method": "queryNoticeMain",
            "content": {
                "sat_noticeid": this.data.sat_noticeid
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            if (this.data.viewData) this.queryReadRecord(); //查询数据
            let list = MFT.fileList(res.data.attinfos.filter(v => v.usetype != 'cover')), //过滤封面文件
                videoList = [], //视频附件列表
                attinfos = []; //其他附件
            list.forEach(v => v.fileType == 'video' ? videoList.push(v) : attinfos.push(v));
            res.data.attinfos = attinfos;
            this.setData({
                detailsData: res.data,
                videoList
            });
            /* getCurrentPages().forEach(v => {
                if (v.__route__ == 'pages/tabbar/home/index') v.queryNoticeList()
            }) */
        });
    },
    /* 查询数据 */
    queryReadRecord() {
        _Http.basic({
            "classname": "saletool.notice.notice",
            "method": "queryReadRecord",
            "content": {
                "sat_noticeid": this.data.sat_noticeid
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            let isEvaluate = false;
            if (res.data[0].score != 0) {
                this.rateChange({
                    detail: res.data[0].score
                })
                isEvaluate = true;
            }
            res.data[0].leavemessage = res.data[0].leavemessage ? res.data[0].leavemessage : "";
            this.setData({
                evaluate: res.data[0],
                isEvaluate
            })
        });
    },
    /* 评分 */
    rateChange({
        detail
    }) {
        let rate = {
            nubmer: detail,
            text: ""
        };
        switch (detail) {
            case 1:
                rate.text = '很不满意'
                break;
            case 2:
                rate.text = '不满意'
                break;
            case 3:
                rate.text = '一般'
                break;
            case 4:
                rate.text = '满意'
                break;
            case 5:
                rate.text = '非常满意'
                break;
        }
        this.setData({
            rate
        })
    },
    /* 文本框输入 */
    textInput({
        detail
    }) {
        this.setData({
            "evaluate.leavemessage": deleteMark.queryStr(detail.value)
        })
    },
    /* 提交建议 */
    submit() {
        if (this.data.isEvaluate) return wx.showToast({
            title: '您已提交过建议',
            icon: "none"
        })
        const evaluate = this.data.evaluate,
            that = this;
        if (evaluate.leavemessage.length > 0) {
            that.updateReadRecord()
        } else {
            wx.showModal({
                title: "提示",
                content: "通告评分（建议与反馈）没有完成，是否确认提交，提交后无法修改",
                success: res => {
                    if (res.confirm) that.updateReadRecord()
                }
            })
        }
    },
    updateReadRecord() {
        this.setData({
            loading: true
        })
        _Http.basic({
            "classname": "saletool.notice.notice",
            "method": "updateReadRecord",
            "content": {
                "sat_noticeid": this.data.detailsData.sat_noticeid,
                "score": this.data.rate.nubmer,
                "leavemessage": this.data.evaluate.leavemessage
            }
        }).then(res => {
            this.setData({
                loading: false
            });
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            wx.showToast({
                title: '提交成功!',
            });
            this.setData({
                isEvaluate: true
            })
        })
    },
    /* tabs切换 */
    tabChange(e) {
        this.setData({
            tabsTitle: e.detail.title
        })
    },
    /* 下载附件 */
    downLoadRecord() {
        _Http.basic({
            "classname": "saletool.notice.notice",
            "method": "updateDownloadRecord",
            "content": {
                "sat_noticeid": this.data.detailsData.sat_noticeid
            }
        })
    },
    onShareAppMessage(res) {}
})