// components/diy-coupon/coupon.js
var Nova = getApp().Nova;
const Parse = getApp().Parse;
const company = getApp().globalData.company;
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        options: null

    },

    /**
     * 组件的初始数据
     */
    data: {

    },

    ready: function () { // 在组件布局完成后执行，确保options参数中有data信息
        this.loadData();
        this.getUser()
    },

    /**
     * 组件的方法列表
     */
    methods: {
        async loadData() {
            await Nova.checkComponentsDataProperties(this);
            console.log(this.data.options.data)
            let list = await Nova.getBlockData(this.data.options.data)

            let {
                options
            } = this.data
            console.log('优惠券组', options);
            console.log(list)
            let {
                column,
                data,
                style,
            } = options
            let {
                className,
                filter
            } = data
            this.setData({
                column,
                list,
                data,
                style,
                className,
                filter
            })
        },
        async getUser() {
            let _user = new Parse.Query("_User");
            console.log(_user);
            // let user = await _user.get(Parse.User.current().id);
            let user = await _user.get('YcIJyTHmY0');
            console.log(user);
            this.setData({
                user: user.toJSON(),
            });
        },

        async coupon(e) {
            this.receive(e);
        },

        async receive(e) {
            let coupon = e.currentTarget.dataset.coupon;
            console.log(coupon);
            // let uid = Parse.User.current().id
            let uid = this.data.user.objectId
            if (uid) {
                let couponLog = new Parse.Query('CouponLog')
                couponLog.equalTo('company', company)
                couponLog.equalTo('user', uid)
                couponLog.equalTo('coupon', coupon)
                let haslog = await couponLog.first()
                console.log(haslog)
                if (haslog && haslog.id) {
                    wx.showToast({
                        title: '该优惠券已领取',
                        icon: 'none',
                        duration: 1500,
                    });
                    return
                } else {
                    let CouponLog = Parse.Object.extend("CouponLog");
                    let log = new CouponLog();
                    log.set('used', false);
                    log.set('company', {
                        __type: "Pointer",
                        className: "Company",
                        objectId: company
                    });
                    log.set('user', {
                        __type: "Pointer",
                        className: "_User",
                        objectId: uid
                    });
                    log.set('coupon', {
                        __type: "Pointer",
                        className: "Coupon",
                        objectId: coupon
                    });
                    if (this.data.list.goods) {
                        log.set('goods', {
                            __type: "Pointer",
                            className: "ShopGoods",
                            objectId: this.data.list.goods.objectId
                        });
                    }
                    let loged = await log.save();
                    console.log(log, "111")
                    if (loged && loged.id) {
                        wx.showToast({
                            title: '领取成功',
                            icon: 'success',
                            duration: 1500,
                        });
                    }
                }
            }
        },
    }
})