const _Http = getApp().globalData.http,
    MFT = require("../../utils/matchingFeilType"),
    checkFile = require("../../utils/checkFile");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        sat_sharematerialid: null,
        detailsData: {},
        dataType: 0,
        addNewCount: 0,
        popupShow: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        this.setData({
            dataType: options.type,
            sat_sharematerialid: options.id
        })
        let isToken = wx.getStorageSync('userMsg').token; //判断是否为无状态进入
        const params = {
            "classname": "saletool.sharematerial.sharematerial",
            "method": isToken ? "selectDetail" : "selectDetailNoToken",
            "content": {
                "sat_sharematerialid": options.id
            }
        };
        const res = isToken ? await _Http.basic(params) : await _Http.base(params);
        if (res.msg != '成功') res = await _Http.base(params);
        if (res.msg == '成功') {
            if (res.data.attinfos.length) res.data.attinfos = MFT.fileList(res.data.attinfos);
            this.setData({
                detailsData: res.data
            });
            //分享进入
            if ([1007, 1008, 1155].includes(wx.getLaunchOptionsSync().scene)) this.setData({
                "shareData.userid": options.userid,
                "shareData.systemclient": "wechatsaletool",
                "shareData.sat_sharematerialid": options.id,
                popupShow: true
            })
        } else {
            wx.showToast({
                title: res.msg,
                icon: "none"
            })
        }
    },
    /* 获取手机号 */
    getphonenumber({
        detail
    }) {
        this.setData({
            "shareData.code_phone": detail.code ? detail.code : '',
            addNewCount: this.data.addNewCount + 1
        })
        this.handleAddNew();
    },
    /* 新增拉新用户 */
    async addNew(gain = true) {
        let name = '微信用户',
            channel = null,
            that = this;
        if (gain) name = await new Promise((res, rej) => {
            wx.getUserProfile({
                desc: '用于完善用户资料',
                success: ({
                    userInfo
                }) => {
                    res(userInfo.nickName)
                },
                fail: () => {
                    res('微信用户')
                }
            })
        })
        switch (wx.getLaunchOptionsSync().scene) {
            case 1007:
                channel = '单人会话分享'
                break;
            case 1008:
                channel = '群聊会话分享'
                break;
            case 1155:
                channel = '朋友圈分享'
                break;
            default:
                channel = '其他方式'
                break;
        };
        wx.login({
            success(res) {
                that.setData({
                    "shareData.fname": name,
                    "shareData.channel": channel,
                    "shareData.code_login": res.code,
                    addNewCount: that.data.addNewCount + 1
                })
                that.handleAddNew();
            }
        });
    },
    handleAddNew() {
        if (this.data.addNewCount != 2) return;
        _Http.base({
            "classname": "saletool.sharematerial.sharematerial",
            "method": "addNew",
            "content": this.data.shareData
        }, false)
        this.setData({
            popupShow: false
        })
    },
    openFile(e) {
        checkFile.checkFile(e.currentTarget.dataset.item);
    },
    updateShareLog(id) {
        _Http.basic({
            "classname": "saletool.sharematerial.sharematerial",
            "method": "updateShareLog",
            "content": {
                "sat_sharematerialid": id,
                "channel": "wechat"
            }
        })
        this.setData({
            'detailsData.sharecount': this.data.detailsData.sharecount + 1
        })
    },
    onShareAppMessage() {
        const promise = new Promise(resolve => {
            let data = this.data.detailsData;
            this.updateShareLog(data.sat_sharematerialid);
            resolve({
                title: data.title,
                path: `/pages/promotional/details?id=${data.sat_sharematerialid}&type=${data.type}&userid=${wx.getStorageSync('userMsg').userid}`,
                imageUrl: data.cover
            })
        })
        return {
            promise
        }
    },
    onShareTimeline: function () {
        let data = this.data.detailsData;
        return {
            title: data.title,
            path: `/pages/promotional/details?id=${data.sat_sharematerialid}&type=${data.type}&userid=${wx.getStorageSync('userMsg').userid}`,
            imageUrl: data.cover
        }
    }
})