export default {

    data(){
        return {
            couponModal:false,
            couponData:undefined,
            couponReceiveIds:{}
        }
    },

    methods:{
        cancelCoupon(){
            if (this.couponModal){
                this.couponModal = false;
            }
        },
        openCoupon(){
            if (this.couponLoading || this.loadingStatus) return;
            if (!this.couponModal){

                if (this.couponData === undefined || this.couponDataTokenStatus===false && this.isLogin) {
                    this.couponDataTokenStatus = this.isLogin;
                    this.$request({
                        url:'product/getCouponList',
                        cache:{
                            type:1
                        },
                        loading:'加载中',
                        failMessage:true,
                        token:true,
                        next:({status})=> this.couponLoading = status
                    }).then((data)=>{

                        this.couponData = data.data || [];
                        this.couponModal = true;
                    })
                } else {
                    this.couponModal = true;
                }
            }
        },
        receiveCoupon({item},index){
            if (this.receiveStatus) return ;
            return this.routerLogin(()=>{

                this.$request({
                    url:'product/receiveCoupon',
                    data:{
                        coupon_id: item.id
                    },
                    token:true,
                    message:true,
                    failMessage:true,
                    loading:'领取中',
                    next:({status})=> this.receiveStatus = status
                }).then((data)=>{
                    if (data.isSuccess) {
                        this.$set(this.couponData[index],'is_get',1);
                    }
                });

            },()=>{
                this.couponModal = false;
            });
        }
    }

}