
$.fn.dataStatistics = function(options){
		options = $.extend({
	        max  : 500,       //初始数值
	        min  : 0, //最大数字
	        time : 60000,  //时长
	        len:6, //数字是几位数
			add:false
	    },options || {});

	    var ths = this;//解决this指向问题

	    //初始化---------------------------------------start

  		var el = ths.find('.set_last');
			var html = '<div class="digit">' +
				'  <div class="digit_top">' +
				'    <span class="digit_wrap"></span>' +
				'  </div>' +
				'  <div class="shadow_top"></div>' +
				'  <div class="digit_bottom">' +
				'    <span class="digit_wrap"></span>' +
				'  </div>' +
				'  <div class="shadow_bottom"></div>' +
				'</div>'




  		//初始化值
	var nowNums=zfill(options.max, options.len).toString().split("");
  		//补0
  		function zfill(num, size) {
			    var s = "000" + num;
			    return s.substr(s.length-size);
			}

			 ths.find('.digit_set').each(function() {
          for(i=0; i<=9; i++) {
            $(this).append(html);
            currentDigit = $(this).find('.digit')[i];
            $(currentDigit).find('.digit_wrap').append(9-i);
          }
        });

      //初始化数值填入
      $.each(nowNums, function(index,val) {
      	 var set =ths.find('.digit_set').eq(index);
      	 var i =parseInt(val)
      	 set.find('.digit').eq(9-i).addClass('active');
      	 set.find('.digit').eq(9-i+1).addClass('previous');
      });

      //初始化---------------------------------------end

      //执行
	var difference = 0; //全局的执行次数
	var nownumber = options.min; //全局的现在数字
	var Activityendtime = ''; //活动结束时间
			function run(diff){
				if(diff){
					difference = diff
				}else {
					 difference =options.max-options.min;//要执行动画的次数

				}


				//后一位数
	  		function increase() {
	  			//执行次数为0时,停止执行
	  			if (difference<1) {
					clearInterval(timer1);
					if(nownumber == 0){ //优惠券没有的时候处理
						clearInterval(timer2); //清楚定时器
						if( (new Date().getTime()) > new Date('2019-10-25T15:04:00').getTime()){

							layer.msg('活动结束')
						}else {
							layer.msg('优惠券被抢光了！')

						}

							$('.box_content_t').hide()
					}
					return false;
					console.info('结束')

				}
	  			difference--;

	        //翻页动画
	        var current = el.find('.active'),
	            previous = el.find('.previous');
	        previous.removeClass('previous');
	        current.removeClass('active').addClass('previous');

	        if (current.next().length == 0) {
	          el.find('.digit:first-child').addClass('active');
	          var prev = el.prev();
	          prevNumber(prev);
	        } else {
	          current.next().addClass('active');
	        }
	      }

	  		var timer1 = setInterval(increase,1000);

			}
  		//当数字翻到9的时候，前一位数执行一次动画
  		function prevNumber(ths){
  			var current = ths.find('.active'),
            previous = ths.find('.previous');
        previous.removeClass('previous');
        current.removeClass('active').addClass('previous');

        if (current.next().length == 0) {
          ths.find('.digit:first-child').addClass('active');
          var prev = ths.prev();
          if (prev.length>0) {
          	prevNumber(prev);
          }
        } else {
          current.next().addClass('active');
        }
  		}
  		run();

		var timer2 = setInterval(()=>{
			$.ajax({
				type: "POST",
				url: `${getApiUrl().urlJava}api/appActivity/getUserPurchaseTime`,
				contentType: "application/json",
				data: getJavaA({}, token),
				async: false,
				success: function (res) {
					if (res.resultCode === 1) {
						if(nownumber > 0){
							let num = nownumber -  (res.data.couponCount  - res.data.couponReceiveCount) ; // 算出动画执行的次数
							nownumber = res.data.couponCount  - res.data.couponReceiveCount; // 算出现在的数字

							if(num>0){
								run(num)
							}
							if(nownumber == 0 ){
								Activityendtime = res.data.expirationDate;
							}
							if(res.data.count== 0){
								$(".received_img").attr("src","");
								$('.img_btn p').html('9.9元抢购')

							}else {
								$(".received_img").attr("src","../images/received@3x.png");
								$('.img_btn p').html('去使用')

							}
						}


					}
				},
				error: function (res) {

				}
			});
		},(difference+5)*1000) //5s请求一次接口

	$('.img_btn').click(function () {
		if($('.img_btn p').css('color')=='rgb(224, 50, 52)'){  //判断按钮是否可以点击
			if( $('.img_btn p').text() == '9.9元抢购' ){  //判断是否9.9元抢购 点击可以抢购
				$.ajax({
					type: "POST",
					url: `${getApiUrl().urlJava}api/appActivity/rushPurchaseByUid`,
					contentType: "application/json",
					data: getJavaA({}, token),
					async: false,
					success: function (res) {
						if (res.resultCode === 1) {
							layer.msg(res.message)
							$('.img_btn p').html('去使用')
							$(".received_img").attr("src","../images/received@3x.png");

							var  timer3 = setInterval(()=>{ //1,需要timer2的定时器走完了 才走timer3的定时器

								nownumber = nownumber -1;
								run(1)
								clearInterval(timer3) //2,执行动画之后清除timer3
							},difference*1000)
						}else {
							layer.msg(res.message)
							window.location.reload()
						}
					},
					error: function (res) {
						layer.msg(res.message)
					}
				});
			}else {
				layer.msg('去使用优惠券')


			}
		}else {

			layer.msg('活动还未开始')
		}

	})
	};



