<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Live</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
<link href="https://fonts.googleapis.com/css?family=Muli" rel="stylesheet">
</head>
<body style="font-family: 'Muli', sans-serif;">
<div class="jumbotron"><h2 class="text-center">Live Auction</h2></div>
<div class="container">
    <div class="row">
    <div class="col-md-12"><a href="<?php echo base_url();?>admin/reset_bids" class="btn btn-xs btn-warning pull-right">Reset Auction</a>
<table class="table table-condensed table-striped table-bordered">
<thead><tr><th>S.No.</th><th>Image</th><th>Product</th><th>Max Bids</th><th>Min Bids</th><th>Live Auctions</th><th>Timer</th></tr></thead><tbody>
<?php $i=1;foreach($auctions as $live){
    $dd= date("Y-m-d H:i:s",$live->start_time);
    $timeFirst  = strtotime(date("Y-m-d H:i:s"));
    $timeSecond = strtotime($dd);
    $differenceInSeconds = $timeSecond - $timeFirst;?>
    <input type="hidden" name="prebid" id="prebid<?php echo $live->apid;?>" value="0">
    <tr id="trr<?php echo $i;?>"><td><?php echo $i;?></td>
	<td><img width="50px" src="<?php echo base_url();?>assets/images/products/<?php echo $live->img;?>" class="img img-responsive"></td>
    <td><h4><?php echo $live->title;?></h4><label class="label label-default">AUC<?php echo $live->apid;?></label></td><td>
 <h5>Max Auction Price : <?php echo $live->max_price;?><br/>
 (<b class="text-danger">MaxBidCoins - <?php echo ceil($live->max_price/$this->session->userdata("bidrate"));?></b>)<br/>
 (<b class="text-success">MaxBids - <?php echo ceil(ceil($live->max_price/$this->session->userdata("bidrate"))/$live->req_credit);?></b>)</h5></td>
 <td><h5>Min Auction Price : <?php echo $live->min_price;?><br/>
(<b class="text-danger">MinBidCoins - <?php echo ceil($live->min_price/$this->session->userdata("bidrate"));?></b>)<br/>
(<b class="text-success">MinBids - <?php echo ceil(ceil($live->min_price/$this->session->userdata("bidrate"))/$live->req_credit);?></b>)<br/></h5></td>
 <td>
Total Bids - <b id="prebidd<?php echo $live->apid;?>"><?php echo $this->db->get_where("auctions",array("auc_dayid"=>$live->apid))->num_rows();?></b><br/>
Actual Bids - <b id="actual<?php echo $live->apid;?>"><?php echo $this->db->get_where("auctions",array("auc_dayid"=>$live->apid,"bid_type"=>""))->num_rows();?></b><br/>
Virtual Bids -<b id="virtual<?php echo $live->apid;?>"><?php echo $this->db->get_where("auctions",array("auc_dayid"=>$live->apid,"bid_type"=>"robot"))->num_rows();?></b></td>
<td><h3 id="timer<?php echo $live->apid;?>" title="<?php echo $live->apid.'-'.$live->pid.'-'.$live->duration;?>" class="timer_running text-warning" secc="<?php echo rand(5,10);?>"><?php if($differenceInSeconds>0){echo $differenceInSeconds;}else{echo $live->duration;}?></h3></td></tr>
<?php $i++;}?></tbody></table>
<button class="btn btn-danger" type="button" id="refreshbtn">Load Auctions</button>
        </div>
		</div>
	</div>
<script>
	var socket=io.connect('http://localhost:5000');
	$(document).ready(function(){
		setInterval(function(){ $("#refreshbtn").trigger("click"); }, 7200000);
		$("#refreshbtn").click(function(){
			$.ajax({
					 type: "POST",
					 url: '<?php echo base_url();?>admin/loadmore_auctions',
					 data: {},
					 success: function(res){if(res!=""){
						 $("tr:last").after(res);}
						 }
					 });
		});
		setInterval(function(){
			$(".timer_running").each(function() {
				var trr=$(this).closest("tr").attr("id");
				//alert(trr);
				var t=parseInt($(this).text());
				var apid=$(this).attr("title").split("-");
				var change_sec=$(this).attr("secc");
				var id=$(this).attr("id");
				var prebid=parseInt($("#prebid"+apid[0]).val());
			//	console.log(prebid);
				if(t>0){t--;
				if(t==change_sec){
					//$("h3#"+apid[0]).text(apid[3]); 
					$.ajax({
					 type: "POST",
					 url: '<?php echo base_url();?>admin/robot_bid',
					 data: {apid:apid[0],pid:apid[1]},
					 success: function(res){//alert(res);
						if(res!=0){
							var r=res.split("=");
							$("#prebid"+apid[0]).val(prebid+1);
							$("#prebidd"+apid[0]).text(prebid+1);
							$("#"+id).attr("secc",r[6]);
							var bidval=prebid+1;
							$("#timer"+apid[0]).text(apid[2]);
							//$("#prebid<?php //echo $myad->apid;?>").text(prebid+1);
							socket.emit("front_bid_update",{user:r[4],secs:apid[2],divid:apid[0],bidval:bidval,imgsrc:r[5]});
						}}
					 });
				}
			    if(t==15){
							 $.ajax({
							 type: "POST",
							 url: '<?php echo base_url();?>admin/autobid',
							 data: {apid:apid[0],pid:apid[1]},
							 success: function(res){
								 //console.log(res);
								 if(res!=0){
									var r=res.split("=");
									$("#prebid"+apid[0]).val(prebid+1);
									$("#prebidd"+apid[0]).text(prebid+1);
									var bidval=prebid+1;
									$("#timer"+apid[0]).text(apid[2]);
									socket.emit("front_bid_update",{user:r[0],secs:apid[2],divid:apid[0],bidval:bidval,imgsrc:r[1]});
								}
							 	}
							 });
						 }
				$(this).text(t);
				}if(t==1){
					     //if($(this).text()!="Next Auction..."){
						 $.ajax({
						 type: "POST",
						 url: '<?php echo base_url();?>admin/close_auction',
						 data: {apid:apid[0]},
						 success: function(res){//$("#timer"+apid[0]).text("Next Auction...");
						// console.log(res);
							// setTimeout(function(){ 
									if(res!=0){
									 var rr=res.split("=");
									 var ext=parseInt(apid[2])+30;
									 $("#timer"+apid[0]).text(ext);
									 $("#prebid"+apid[0]).val(0);
									 $("#prebidd"+apid[0]).text('0');
									 $("#prebid"+apid[0]).attr("id","prebid"+rr[0]);
									 $("#prebidd"+apid[0]).attr("id","prebidd"+rr[0]);
									 $("#timer"+apid[0]).attr("title",rr[0]+'-'+apid[1]+'-'+apid[2]);
									 $("#timer"+apid[0]).attr("id","timer"+rr[0]);
									 socket.emit("new_auction_load",{oldaid:apid[0],newaid:rr[0],sec:ext,start:rr[1]});
									}else{
									$("#"+trr).remove();	
									}
									// socket.emit("front_bid_update",{user:r[4],secs:apid[2],divid:apid[0],bidval:bidval,imgsrc:r[5]});
								// },5000);
							 }
						 });//}
					//$(this).text(apid[2]);
				//$(this).text("Auction Closed");
				//$(this).removeClass("timer_running");		
				}
				socket.emit("time_clock",{current:t,apid:apid[0]});
				});
				//var time = new Date();
//				var pageload=time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds();
//				//console.log(pageload);
//				if(pageload=="0:00:10"){
//					window.location.href = "<?php ///echo base_url();?>admin/live"; 
//				}
		 }, 1000);
		
		socket.on("front_bid_update",function(data){
				$("#prebid"+data.divid).val(data.bidval);
				$("#prebidd"+data.divid).text(data.bidval);	
				$("#timer"+data.divid).text(data.secs);
				//$(".auc_price"+data.divid).text(data.bidval);
		});
	});
	//socket.on("front_bid_update",function(data){
//		$("#user"+data.divid).text(data.user);	
//		$("#"+data.divid).text(data.secs);
//	});
</script>
<script type="text/javascript" src=""></script>
</body>
</html>