<?php
session_start();
include "connect.php";
$currentpage=10;
$activepage="generate_pin_a";

if(!isset($_SESSION['admin_login'])){ header("location:index.php");}

	
if(isset($_POST['generate_coupon']) && $_POST['generate_coupon']=='User')
	{
		 $plan_id = $_POST['id'];    
		$no_of_pin = $_POST['no_of_pin'];    
		$user_id = $_POST['user_id'];    
		    
		for($i = 0 ; $i < $no_of_pin; $i++){
			$pin_num = randomPassword();
		$checkLogin=mysqli_query($conn,"INSERT INTO `pin_table`(`id`, `pin_num`, `plan_id`, `user_id`)
		VALUES (NULL,'$pin_num','$plan_id','$user_id')");
		}
		if($checkLogin){
					
				echo "<script> window.location = 'generate_pin_a.php?success'; </script>";			
		}else{
			 header("location:generate_pin_a.php?err");			 
		}
	}
$success='<div class="alert alert-success alert-dismissible" role="alert" style="margin-top: -18px; padding: 10px;"><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true" style="margin-left:-50px; color:white;">&times;</span><span class="sr-only">Close</span></button><strong>Success!</strong> Insert is successfull.</div>';
$err='<div class="alert alert-danger alert-dismissible" role="alert" style="margin-top: -18px; padding: 10px;"><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true" style="margin-left:-50px; color:white;">&times;</span><span class="sr-only">Close</span></button><strong>Wrong!</strong> Invalid credentials.</div>';

	function randomPassword() {
    $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 8; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>:: SCSCROWD  ::</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<?php include "common/CSS_link.php";?>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="js/jquery-ui.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
    $('#MyTable').DataTable( {
        initComplete: function () {
            this.api().columns().every( function () {
                var column = this;
                var select = $('<select><option value=""></option></select>')
                    .appendTo( $(column.footer()).empty() )
                    .on( 'change', function () {
                        var val = $.fn.dataTable.util.escapeRegex(
                            $(this).val()
                        );
                //to select and search from grid
                        column
                            .search( val ? '^'+val+'$' : '', true, false )
                            .draw();
                    } );
 
                column.data().unique().sort().each( function ( d, j ) {
                    select.append( '<option value="'+d+'">'+d+'</option>' )
                } );
            } );
        }
    } );
} );
</script>
<script>
  var dateToday = new Date(); 
	$( function() {	
    $( ".datepicker" ).datepicker({
    dateFormat: 'yy-mm-dd'   
	});
  } ); 
 
    
function isNumber(evt) {
	var iKeyCode = (evt.which) ? evt.which : evt.keyCode
	if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57)){
		return false;
	}
	return true;
}
	$(document).ready(function (e){
		$("#editUser").on('submit',(function(e){
			e.preventDefault();
			var url = $(this).attr('action');			
			console.log(url);			
				$.ajax({
					url: url,
					type: "POST",
					data:  new FormData(this),
					contentType: false,
					cache: false,
					processData:false,
						success: function(data){										
						$('#ajax_edit').html(data);	
						 setTimeout('window.location.href="generate_coupon.php"',2000); 
						},
					error: function(){} 	        
				});
			}));
		});

$(function() {

	$(".name").focus();
	var clients = [
	<?php						
		$q_pps=mysqli_query($conn,"SELECT * FROM user_tbl");
		while($r_pps=mysqli_fetch_array($q_pps)){
	?>
	  {        
        label: "<?php echo $r_pps['user_name'];?>",        
        icon: "<?php echo $r_pps['user_id'];?>"
      },
	<?php	}	?>
	 ];
    $( ".name" ).autocomplete({
      minLength: 0,
      source: clients,
      focus: function( event, ui ) {
        $( ".name" ).val( ui.item.label );
		return false;
      },
      select: function( event, ui ) {			
        $( ".name" ).val( ui.item.label );        
        return false;
      }
    })
    .autocomplete( "instance" )._renderItem = function( ul, item ) {		
	 return $( "<li>" )
       .append( "<a>" + item.label + "<br><span style='color:#808080; font-size:10px;'>" + item.icon + "</span></a>" )
	    //.append( "<a>" + item.label + "</a>" )
        .appendTo( ul );
		};
  });

$(function() {

	$("#name123").focus();
	var clients = [
	<?php						
		$q_pps123=mysqli_query($conn,"SELECT * FROM company_tbl GROUP BY company_id,company_name ASC LIMIT 10");
		while($r_pps123=mysqli_fetch_array($q_pps123)){
	?>
	  {        
        label: "<?php echo $r_pps123['company_id'];?>",
		icon: "<?php echo $r_pps123['company_name'];?>",
      },
	<?php	}	?>
	<?php						
		$q_pps=mysqli_query($conn,"SELECT * FROM user_tbl GROUP BY user_id,user_name ASC LIMIT 10");
		while($r_pps=mysqli_fetch_array($q_pps)){
	?>
	  {               
        label: "<?php echo $r_pps['user_id'];?>",
		icon: "<?php echo $r_pps['user_name'];?>",
      },
	<?php	}	?>
	 ];
    $( "#name123" ).autocomplete({
      minLength: 0,
      source: clients,
      focus: function( event, ui ) {
        $( "#name123" ).val( ui.item.label );
		return false;
      },
      select: function( event, ui ) {			
        $( "#name123" ).val( ui.item.label );        
        return false;
      }
    })
    .autocomplete( "instance" )._renderItem = function( ul, item ) {		
	 return $( "<li>" )
       .append( "<a>" + item.label + "<br><span style='color:#808080; font-size:10px;'>" + item.icon + "</span></a>" )
	   // .append( "<a>" + item.icon + "</a>" )
        .appendTo( ul );
		};
  });  
</script>
<style>
[class*=" pe-7s-"], [class^=pe-7s-] {
    display: inline-block;
    font-family: Pe-icon-7-stroke;
    speak: none;
    font-style: normal;
    font-weight: 400;
    font-variant: normal;
    text-transform: none;
    line-height: 1;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}
* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
user agent stylesheet
i, cite, em, var, address, dfn {
    font-style: italic;
}
.content-header .header-icon {
    font-size: 55px;
    color: #37a000;
    width: 68px;
    float: left;
    margin-top: -4px;
    line-height: 0;
}
.link {padding: 10px 15px;background: transparent;border:#bccfd8 1px solid;border-left:0px;cursor:pointer;color:#607d8b}
.disabled {cursor:not-allowed;color: #bccfd8;}
.current {background: #bccfd8;}
.first{border-left:#bccfd8 1px solid;}
.question {font-weight:bold;}
.answer{padding-top: 10px;}
#pagination{margin-top: 20px;padding-top: 30px;border-top: #F0F0F0 1px solid;}
.dot {padding: 10px 15px;background: transparent;border-right: #bccfd8 1px solid;}
#overlay {background-color: rgba(0, 0, 0, 0.6);z-index: 999;position: absolute;left: 0;top: 0;width: 100%;height: 100%;display: none;}
#overlay div {position:absolute;left:50%;top:50%;margin-top:-32px;margin-left:-32px;}
.page-content {padding: 20px;margin: 0 auto;}
.pagination-setting {padding:10px; margin:5px 0px 10px;border:#bccfd8  1px solid;color:#607d8b;}
</style>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
  <header class="main-header">
    <?php include "common/header.php";?>
  </header> 
  <aside class="main-sidebar">    
    <section class="sidebar">
      <?php include "common/LEFT_sidebar.php";?>
  </aside>  
  <div class="content-wrapper">    
    <section class="content-header">
	<div class="header-icon">
	 <i class="fa fa-file-text-o" style="color:#37a000; font-size:50px;"></i>
	    </div>
      <h1 style="fonyt-family:'Alegreya Sans',sans-serif; margin: 0; font-size: 24px">Generate $100 Pin <br><span style="font-size: 13px; display: inline-block; padding-left: 4px; font-weight: 600; color: #7a7a7a;">Generate $100 Pin </span></h1>
      <ol class="breadcrumb">
        <li><a href="home.php"><i class="fa fa-dashboard"></i>Home</a></li>        
        <li class="active" style="color:#37a000; font-weight:bold;">Generate $100 Pin </li>
      </ol>
    </section>    
    <section class="invoice">      
      <div class="row">
	  <div id='msg'><?php if(isset($_GET['success'])){ echo $success; echo "<script> setTimeout(\"window.location = 'generate_pin_a.php'\",2000); </script>"; } ?><?php if(isset($_GET['err'])){ echo $err; echo "<script> setTimeout(\"window.location = 'generate_pin_a.php'\",2000); </script>"; } ?></div>                        
        <!--  <div class="col-xs-12">
          
			<div style="clear:both;">
				
				<div style="float:left;"><input type="text" class="form-control name" id='name' placeholder="Name"  name="name"> </div>
				<div style="float:left;">&nbsp;<button class="btn btn-sm btn-primary" type="submit" id='item-search' style="margin-top:2px;"><i class="fa fa-search"></i></button></div>
				
				<div style="float:left; margin-left:10px;"><input type="text" class="form-control" id='name123' placeholder="Name"  name="name"> </div>
				<div style="float:left;">&nbsp;<button class="btn btn-sm btn-success" type="submit" id='item-search123' style="margin-top:2px;"><i class="fa fa-search"></i></button></div>
			</div>
			
            <div class="form-inline" role="form">				
			<div style="display: inline-block" class="pull-right">
			<div class="form-group">
				<label>From</label>
				<input type="text" class="form-control datepicker" id='from' placeholder="Start date"  name="from">
			</div>
			<div class="form-group">
				<label>To</label>
				<input type="text" class="form-control datepicker" id='to' placeholder="End date"  name="to">                         
				<button class="btn btn-sm btn-primary" style="" type="submit" id='date-search'>Search</button>
			</div> 
			</div>                       
            </div>   
        </div>   -->     
      </div>     
      <div class="row">
        <div class="col-xs-12 table-responsive">
		<div id="result" style="margin:15px 0;"></div>
		<a db_id ='<?php echo $id;?>' role="button" onclick="get_userData('2');" href="#modal-container-666931" role="button" data-toggle="modal" class="" title="Pin"><button class="btn btn-sm btn-success" type="submit" id='item-search123' style="margin-top:2px;">Generate $100 Pin</button></a>
         <br> <br> 
		 <table id="MyTable" class="table" border="1" style="text-align:center; width:100%;" >
            
            <thead style="font-family:sans; background:#008080; color:#fff;">
			  <td>SL</td>
			  <td>Pin</td>
            </thead> 
<tbody>
									<?php $co1 = 1; 
									 $sno = 1; 
									
									//$flag = 1; ?>
            <?php 
				
					 $q_sql_agent = "SELECT * FROM `pin_table`  WHERE plan_id = '2' AND status = 0 ORDER BY `pin_table`.`id` DESC";
					 $q_query_agent = mysqli_query($conn,$q_sql_agent);
				while($row_agent = mysqli_fetch_array($q_query_agent)){
				?>
				
				  <tr>			       
			  <td><?php echo $sno;?></td>		
			   <td><?php echo $row_agent['pin_num'];?></td>
            </tr>
			
			
				<?php
				$sno = $sno + 1;
				 // number
				   } // end while 
			?>
           
                                 </tbody>			
           </table>	
						
        </div>       
      </div>        
       
      
	  <div class="row no-print">
	   <div class="box box-primary" style="box-shadow:none; border:none;">
            <div class="box-header ">
              <h3 class="box-title"></h3>
            </div>   
            <div class="box-body" style="height:550px;">             
            </div>     
          </div>
	  </div>
	   <div class="modal fade" id="modal-container-666931" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header" style="background:#605ca8; color:#FFF;">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="color:#FFF;">×</button>
				<h4 class="modal-title" id="myModalLabel">Generate $100 Pin <span style="font-weight:bold;"></span></h4>
			</div>
			
			<div class="modal-body"> 
				<div id='ajax_edit'></div>                        
				<form class="form-horizontal" role="form" id="editUser" action="" method="POST" enctype="multipart/form-data" >
									<input id="generate_coupon" name="generate_coupon" type="hidden" value="User" />
									<input id="id" name="id" type="hidden" />
				<div class="form-group">					
					<div class="col-sm-12">
						<label class="control-label">Choose Member ID <span style="color:red;">*</span></label> <br>
						<select name="user_id" id="user_id" class="form-control select2 select2-search__field"  style="width:100%;">
							<option value="" >Select Member ID</option>
							<?php
								$q_agent=mysqli_query($conn,"SELECT * FROM user_tbl ORDER BY user_tbl.id DESC");
								while($r_agent=mysqli_fetch_array($q_agent)){
							?>
							<option value="<?php echo $r_agent['id'];?>"><?php echo $r_agent['user_unique_id'];?></option>
								<?php } ?>
						</select>
					</div>					
				</div><div class="form-group">					
					<div class="col-sm-12">
						<label class="control-label">Number Of Pin <span style="color:red;">*</span></label>						
						<input type="text" class="form-control" name="no_of_pin" id="no_of_pin" placeholder="Number" autocomplete="off" required>
					</div>					
				</div>
				  <div class="form-group">					
					<div class="col-sm-6">
					<div class=" col-sm-12">
					<label class="control-label">&nbsp;&nbsp;&nbsp;	</label>
					<label class="control-label">&nbsp;&nbsp;&nbsp;	</label> </div>
					  <button type="submit" class="btn btn-success" name="submit" id="new_submit">Generate</button>
				 
					</div>				
				</div>                       
			</form>
			</div>
			</div>                    
		</div>
	</div>       
    
    </section>
    <div class="clearfix"></div>
  </div>
  <footer class="main-footer">
    <?php include "common/footer.php";?>
  </footer>
</div>
<script>
  $.widget.bridge('uibutton', $.ui.button);
</script>
<script src="assets/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="assets/bower_components/raphael/raphael.min.js"></script>
<script src="assets/bower_components/morris.js/morris.min.js"></script>
<script src="assets/bower_components/jquery-sparkline/dist/jquery.sparkline.min.js"></script>
<script src="assets/plugins/jvectormap/jquery-jvectormap-1.2.2.min.js"></script>
<script src="assets/plugins/jvectormap/jquery-jvectormap-world-mill-en.js"></script>
<script src="assets/bower_components/jquery-knob/dist/jquery.knob.min.js"></script>
<script src="assets/bower_components/moment/min/moment.min.js"></script>

<script src="assets/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js"></script>
<script src="assets/bower_components/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<script src="assets/bower_components/fastclick/lib/fastclick.js"></script>
<script src="assets/dist/js/adminlte.min.js"></script>
<script src="assets/dist/js/pages/dashboard.js"></script>
<script src="assets/dist/js/demo.js"></script>
</body>
</html>
<script src="assets/bower_components/select2/dist/js/select2.full.min.js"></script>
<script>
$('.select2').select2({  
  sorter: function(results) {
    var query = $('.select2-search__field').val().toLowerCase();
    return results.sort(function(a, b) {
      return a.text.toLowerCase().indexOf(query) -
        b.text.toLowerCase().indexOf(query);
    });
  }
});
</script>
<script>
function getresult(e){		
		$.ajax({
		url: "ajax/user_view.php",
		type: "GET",
		data:  {page:e},
		
		success: function(data){
			var res=data.split("$",2);
			$("#response-table").html(res[0]);
			$("#pagi").html(res[1]);			
			},
			error: function() 
			{} 	        
		});
	}
function get_userData(id){
	document.getElementById('id').value=id;
}

 $("#item-search").on('click', function (e){
    e.preventDefault();
    var item = $("#name").val();	
    console.log(item);	
        $.ajax({
            type:'POST',
            url:'ajax/user_view.php',
            data:'q='+item
        }).done(function (data){
            $("#response-table").html(data);			
        });
    
});


$("#item-search123").on('click', function (e){
    e.preventDefault();
    var item = $("#name123").val();		
    console.log(item);	
        $.ajax({
            type:'POST',
            url:'ajax/user_view.php',
            data:'q123='+item
        }).done(function (data){
            var res=data.split("$",2);
		$("#response-table").html(res[0]);
		$("#pagi").html(res[1]);			
        });
    
});


$("#date-search").on('click', function (e){	
    e.preventDefault();
    var from = $("#from").val();	
    var to = $("#to").val();
	var e = $("#page_row").val();
	if($("#from").val() !='' && $("#to").val()!=''){	
        $.ajax({
            type:'POST',
            url:'ajax/user_view.php',
            data:'from='+from+'&to='+to+'&page='+e
        }).done(function (data){
          var res=data.split("$",2);
		$("#response-table").html(res[0]);
		$("#pagi").html(res[1]);			
        });
    }else{
            alert("Choose start date & end date.");
        }
});
	
 
</script>