// 모아컴즈 : 20240506 : 쿠폰 다운받기
var McEventCouponDownload = function(obj_name) { this.id = $(obj_name); };
McEventCouponDownload.prototype = {
	init: function (argv) {
		var _this = this;

		// argv를 merge 함
		_this = argv_merge(_this, argv);

		_this.set_event();
	},
	set_event : function() {
		var _this = this;

		// $('.btn-coupon-download').unbind('click').on('click', function() {
		// 	var couponSeq = $(this).data('coupon-seq');
		// 	var type = $(this).data('type');
		// 	if(couponSeq) {
		// 		_this.proc(couponSeq, type);
		// 	}
		// });

		$('.btn-coupon-download[data-disable-check="y"]').each(function() {
			var couponSeq = $(this).data('coupon-seq');

	        $.ajax({
	            type: 'post',
	            url: '/promotion/promotion_ps',
	            data: {"mode" : "apply", "event_name": "eventCouponDownloadStatus", "coupon_seq" : couponSeq},
	            dataType: 'json',
	            success: function(res) {
					console.log(res);
					
	                if(res.result == 'failure') {
						console.log(res.msg);
	                }
					else if(res.result == 'success') {
						if(res.code == 'expire_today') {
							$('.btn-coupon-download[data-disable-check="y"][data-coupon-seq="'+res.coupon_seq+'"]').hide();
							$('.btn-coupon-download-expire-today[data-coupon-seq="'+res.coupon_seq+'"]').show();
						}
						else if(res.code == 'expire_event') {
							$('.btn-coupon-download[data-disable-check="y"][data-coupon-seq="'+res.coupon_seq+'"]').hide();
							$('.btn-coupon-download-expire-today[data-coupon-seq="'+res.coupon_seq+'"]').show();
						}
	                }
	            },
	            error : function(res) {
	            }
	        });
		});
	},
	loading : false,
	proc : function(couponSeq, type) {
		var _this = this;

		if(_this.loading) return;
		_this.loading = true;

        $.ajax({
            type: 'post',
            url: '/promotion/promotion_ps',
            data: {"mode" : "apply", "event_name": "eventCouponDownload", "coupon_seq" : couponSeq, "type" : type},
            dataType: 'json',
            success: function(res) {
                _this.loading = false;

                if(res.result == 'failure') {
                    alert(res.msg);
                    if(res.reason == 'session_not_found') {
						var url = encodeURIComponent(location.href.replace(location.protocol + '//' + location.host, ' '));
						location.href = '/member/login?return_url=' + url;
                    }
                    return false;
                }
                else if(res.result == 'success') {
					alert('쿠폰이 발급되었습니다. [마이페이지 > 쿠폰함]에서 쿠폰을 확인해주세요.');
                }

				return false;
            },
            error : function(res) {
                _this.loading = false;
                console.log(res.responseText);
            }
        });
	}
}

// 모아컴즈 : 20240506 : 쿠폰 다운받기
var mcEventCouponDownload = null;
$(function() {
    $('#lotteryScratch1').html('');
    mcEventCouponDownload = new McEventCouponDownload();
    mcEventCouponDownload.init({});
});

// 모아컴즈 : 20240509 : 쿠폰 다운받기 클릭
function mc_coupon_download(couponSeq, type) {
	mcEventCouponDownload.proc(couponSeq, type);
}