import {
    getVipInfo,
} from '~/api/user'
import {
    formatDate
} from '~/utils/util'
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        show: false,
        type: '',
        vipTime: ''
    },
    methods: {
        open() {
            console.log(this.getTabBar());
            if (typeof this.getTabBar === 'function' && this.getTabBar()) {
                this.getTabBar().setData({
                    mask: true
                })
            }
            this.getVipInfo()
            this.setData({
                show: true
            })
        },
        closeModal() {
            this.setData({
                show: false
            })
            if (typeof this.getTabBar === 'function' && this.getTabBar()) {
                this.getTabBar().setData({
                    mask: false
                })
            }
        },
        async getVipInfo() {
            let vipTime = await getVipInfo()
            this.setData({
                type: vipTime == '1' ? 'svip' : 'vip',
                vipTime
            })
        },
        creatShare() {
            let type = this.data.type
            let context = wx.createSelectorQuery();
            context
                .select('#vip')
                .fields({
                    node: true,
                    size: true
                }).exec((res) => {
                    const canvas = res[0].node;
                    const ctx = canvas.getContext('2d');
                    const dpr = wx.getSystemInfoSync().pixelRatio;
                    canvas.width = res[0].width * dpr;
                    canvas.height = res[0].height * dpr;
                    ctx.scale(dpr, dpr);
                    ctx.font = '18px PingFang';
                    let pic = canvas.createImage();
                    pic.src = type == 'svip' ? 'http://reader-wx.ai160.com/images/reader/v3/learn/vip1.png' : 'http://reader-wx.ai160.com/images/reader/v3/learn/vip2.png'
                    pic.onload = () => {
                        ctx.drawImage(pic, 0, 0, 375, 201);
                        if (type == 'svip') {
                            ctx.fillStyle = "#D7E6FF";
                            ctx.fillText('终身使用', 16, 184)
                        } else {
                            ctx.fillStyle = "#FFE6D2";
                            ctx.fillText('有效期至:' + formatDate(this.data.vipTime, 5), 16, 184)
                        }
                        setTimeout(() => {
                            wx.canvasToTempFilePath({
                                canvas: canvas,
                                success(res) {
                                    wx.saveImageToPhotosAlbum({
                                        filePath: res.tempFilePath,
                                        success(res) {
                                            wx.showToast({
                                                title: '保存成功!',
                                                icon: "none",
                                                duration: 3000
                                            })
                                        }
                                    })
                                },
                                fail(res) {
                                    console.log('fail', res);
                                }
                            }, this)
                        }, 500)
                    }
                })
        },
    }
})