<?php
session_start();
include 'session_check.php';
include 'include/constant.php';
include 'include/functions.php';
include 'get_user_info.php';

$_SESSION['affiliate_security'] = md5(substr(str_shuffle(str_repeat("0123456789", 15)), 0, 10));

?>
	<head>
    <meta charset="UTF-8">
    <title>AdminLTE 2 | Dashboard</title>
    <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
    <!-- Bootstrap 3.3.2 -->
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />    
    <!-- FontAwesome 4.3.0 -->
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <!-- Ionicons 2.0.0 -->
    <link href="https://code.ionicframework.com/ionicons/2.0.0/css/ionicons.min.css" rel="stylesheet" type="text/css" />    
    <!-- Theme style -->
    <link href="dist/css/AdminLTE.min.css" rel="stylesheet" type="text/css" />
    <!-- AdminLTE Skins. Choose a skin from the css/skins 
         folder instead of downloading all of them to reduce the load. -->
    <link href="dist/css/skins/_all-skins.min.css" rel="stylesheet" type="text/css" />
    <!-- iCheck -->
    <link href="plugins/iCheck/flat/blue.css" rel="stylesheet" type="text/css" />
    <!-- Morris chart -->
    <link href="plugins/morris/morris.css" rel="stylesheet" type="text/css" />
    <!-- jvectormap -->
    <link href="plugins/jvectormap/jquery-jvectormap-1.2.2.css" rel="stylesheet" type="text/css" />
    <!-- Date Picker -->
    <link href="plugins/datepicker/datepicker3.css" rel="stylesheet" type="text/css" />
    <!-- Daterange picker -->
    <link href="plugins/daterangepicker/daterangepicker-bs3.css" rel="stylesheet" type="text/css" />
    <!-- bootstrap wysihtml5 - text editor -->
    <link href="plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css" rel="stylesheet" type="text/css" />
	
	<!-----		sweet alert css and js link	---->
	<link rel="stylesheet" href="dist/sweetalert.css">
	<script src="dist/sweetalert.min.js"> </script>

	<link rel="stylesheet" type="text/css" href="custom/custom.css">
	
	<!---- Ck editor-->
	 <script src="ckeditor/ckeditor.js"></script>
	  <!----    data table css file   ---->
	  <link href="DataTables/datatables.min.css" rel="stylesheet" type="text/css">
  </head>
<?php
include 'include/header.php';
include 'include/left-nav.php';
?>
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
 <section class="content-header">
          <h1>
            Affiliate Manage
            
          </h1>
          <ol class="breadcrumb">
            <li><a href="dashboard.php"><i class="fa fa-dashboard"></i> Home</a></li>
            <li class="active"> Affiliate Manage</li>
          </ol>
        </section>

        <!-- Main content -->
        <section class="content">
		
		<?php
		 if(isset($got_unique_id) && strtoupper($got_unique_id) != ADMIN_ID)
		 {
			?>
				<h4 class="blink_me"> <a target="_blank" href="https://sellercentral.amazon.in/ap/register?showRememberMe=true&openid.return_to=https%3A%2F%2Fsellercentral.amazon.in%2Fsw%2Fin%2FINSSR%2Fstep%2FSignUp%3Fpassthrough%252Faccount%3Dsoa%26passthrough%252FsuperSource%3DOAR%26ref_%3Das_in_soa_nav%26passthrough%252FmarketplaceID%3DA21TJRUUN4KGV%26passthrough%252Fld%3Dinrgooginkenshoo_502X208344_p_c_424947039510_asret__SOANav_benefits%26productTier%3DSILVER%26productType%3DSellOnAmazon%26marketplaceId%3DA21TJRUUN4KGV%26redirectAP%3D1&prevRID=95R49KQEAEAYK4GS8X21&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=amzn_sw_signup_in&openid.mode=checkid_setup&prepopulatedLoginId=&marketPlaceId=A21TJRUUN4KGV&failedSignInCount=0&pageId=amzn_sw_signup_in&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0"> Click here to go Amazon seller account </a> </h4>
			<?php				
		 }
		?>
		<div class="btn-group affiliate-button-group">
			<?php
			  if(isset($got_unique_id) && strtoupper($got_unique_id) != ADMIN_ID)
			  {
				  echo ' <button type="button" class="btn btn-primary affiliate-button affiliate-add-btn" data-val="1">Add Affiliate</button>';
			  }
			?>
		  <button type="button" class="btn btn-primary affiliate-button affiliate-history-btn active" data-val='2'>Affiliate Hisory</button>
		  <button type="button" class="btn btn-primary affiliate-button  affiliate-income-history-btn" data-val='3'>Affiliate Income History</button>
		</div>
		
		<?php
		  if(isset($got_unique_id) && strtoupper($got_unique_id) != ADMIN_ID)
		  {
		?>
		<div class="row d-none" id="affiliate_add">
			<form class="affiliate-form" method="POST">
			<input type="hidden" name="affiliate_security" value="<?php echo isset($_SESSION['affiliate_security']) ? $_SESSION['affiliate_security'] : ''?>">
			<div class="well col-sm-6 col-sm-offset-3" style="margin-top: 20px">
				<div class="row">
					<div class="col-sm-8 col-sm-offset-2 text-center">
						<h3> Add your affiliate  </h3>
					</div>
				</div>
				 <br>
				<div class="row">
					<div class="col-sm-8 col-sm-offset-2">
						<input type="text" class="form-control" name="product_name" placeholder="Enter your affiliate product name" maxlength="40" required>
					</div>
				</div>
				<br>
				<div class="row">
					<div class="col-sm-8 col-sm-offset-2">
						<input type="text" class="form-control" name="product_order_id" placeholder="Enter affiliate product order id" maxlength="40" required>
					</div>
				</div>
				
				<br>
				 <div class="row">
					<div class="col-sm-8 col-sm-offset-2">
						<textarea class="form-control" id="remarks" name="remarks" placeholder="Enter the remark" maxlength="60" rows="4"></textarea>
					</div>
				</div>
				<br>
				 <div class="row">
					<div class="col-sm-8 col-sm-offset-2">
						<input type="submit" class="form-control" name="affiliate_submit" value="Submit">
					</div>
				</div>
			</div>
		   </form>
		</div>
		<?php 
		}
		?>
		<div class="row" id="affiliate_history">
            <div class="col-sm-12">
              <div class="box">
                <div class="box-header">
				  <div class="well">
				     <div class="row" style="max-width: 500px">
					   <div class="col-md-6">
						 Filter by status
					   </div>
					   <div class="col-md-6">
						   <select class="form-control filter_status" name="filter_status">
							 <option value=""> Select any one </option>
							 <option value="1"> Accepted & Paid </option>
							 <option value="2"> Pending </option>
							  <option value="3"> Declined </option>
						   </select>
					   </div>
					  </div>
				   </div>
				   <?php
				     if(isset($got_unique_id) && strtoupper($got_unique_id) == ADMIN_ID)
					 {
			        ?>
				   <div class="row">
				      <div class="col-md-12">
					     <button class="btn btn-danger decline-affiliate-request"> Decline affiliate request </button>
					  </div>
				   </div>
				   <?php
					 }
				   ?>
                </div><!-- /.box-header -->
                <div class="box-body table-responsive">
				
				    <table id="product_affiliate_history" class="table table-striped table-bordered nowrap" style="width:100%">
						<!----  code will append here  ---->	
					</table>
					 </div><!-- /.box-body -->
             
			  </div><!-- /.box -->

           
            </div><!-- /.col -->
          </div><!-- /.row -->
		
		
          <div class="row d-none" id="affiliate_income">
            <div class="col-sm-12">
              <div class="box">
                <div class="box-header">
                </div><!-- /.box-header -->
                <div class="box-body table-responsive">
				    <table id="affiliate_income_details" class="table table-striped table-bordered nowrap" style="width:100%">
						<!----  code will append here  ---->	
					</table>
					 </div><!-- /.box-body -->
             
			  </div><!-- /.box -->
            </div><!-- /.col -->
          </div><!-- /.row -->
        </section><!-- /.content -->
      </div><!-- /.content-wrapper -->
	
<!-------  Declined affiliate modal  ---------->

<div class="container">
  <div class="modal fade" id="declined-affiliate-modal" role="dialog">
    <div class="modal-dialog modal-md" style="margin-top: 10%">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title text-center">Affiliate Decline</h4>
        </div>
		<form class="affiliate-decline-form">
			<input type="hidden" name="affiliate_id" value="">
			<div class="modal-body">
			  <h4 class="text-left"> Affiliate Name - <span class="affiliate-name"></span> </h4>
			  <h4 class="text-left"> Affiliate Order Number - <span class="affiliate-order-no"></span> </h4>
			  <div class="row">
				  <div class="col-sm-3">
					 <h4> Decline Reason </h4>
				  </div>
				  <div class="col-sm-9">
					<textarea rows="4" class="decline_msg" name="decline_msg" placeholder="Please elaborate decline reason" style="width: 100%; padding: 5px 6px"></textarea>
				  </div>
			  </div>
			  <br>
			  <div class="text-right">
				  <button type="button" class="btn btn-primary affiliate-decline-submit"> Submit </button>
				  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
			  </div>
			</div>
		</form>
      </div>
    </div>
  </div>
</div>	
	
		 <!-- jQuery 2.1.3 -->
    <script src="plugins/jQuery/jQuery-2.1.3.min.js"></script>
    <!------    custom js    -------->
    <!--<script src="js/custom.js"></script>-->
    <!-- jQuery UI 1.11.2 -->
    <script src="https://code.jquery.com/ui/1.11.2/jquery-ui.min.js" type="text/javascript"></script>
    <?php
    if($status=='Deactive')
    {
        include 'modal_show.php';
    }
    ?>
    <!-- Resolve conflict in jQuery UI tooltip with Bootstrap tooltip -->
    <script>
      $.widget.bridge('uibutton', $.ui.button);
    </script>
    <!-- Bootstrap 3.3.2 JS -->
    <script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script>    
    <!-- Morris.js charts -->
   <!-- <script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>-->
    <!---<script src="plugins/morris/morris.min.js" type="text/javascript"></script>-->
    <!-- Sparkline -->
    <script src="plugins/sparkline/jquery.sparkline.min.js" type="text/javascript"></script>
    <!-- jvectormap -->
    <script src="plugins/jvectormap/jquery-jvectormap-1.2.2.min.js" type="text/javascript"></script>
    <script src="plugins/jvectormap/jquery-jvectormap-world-mill-en.js" type="text/javascript"></script>
    <!-- jQuery Knob Chart -->
    <script src="plugins/knob/jquery.knob.js" type="text/javascript"></script>
    <!-- daterangepicker -->
    <script src="plugins/daterangepicker/daterangepicker.js" type="text/javascript"></script>
    <!-- datepicker -->
    <script src="plugins/datepicker/bootstrap-datepicker.js" type="text/javascript"></script>
    <!-- Bootstrap WYSIHTML5 -->
    <script src="plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js" type="text/javascript"></script>
    <!-- iCheck -->
    <script src="plugins/iCheck/icheck.min.js" type="text/javascript"></script>
    <!-- Slimscroll -->
    <script src="plugins/slimScroll/jquery.slimscroll.min.js" type="text/javascript"></script>
    <!-- FastClick -->
    <script src='plugins/fastclick/fastclick.min.js'></script>
    <!-- AdminLTE App -->
    <script src="dist/js/app.min.js" type="text/javascript"></script>
    <!--  Data table js link   --->
    <script src="DataTables/datatables.min.js"></script>
    <script>
	   
	  </script>
    
    <!-- AdminLTE dashboard demo (This is only for demo purposes) -->
   <!-- <script src="dist/js/pages/dashboard.js" type="text/javascript"></script>-->
	 <script>
	  $(document).ready(function(){
		  
		  (function blink() {
			  $('.blink_me').fadeOut(400).fadeIn(400, blink);
			})();


		  affiiate_history_load();
		  //  filter by status
		  $('.filter_status').change(function(){
			  affiiate_history_load();
		  });
		  
		  $('.filter-history-status').change(function(){
			  affiiate_income_history_load();
		  });
		  
		  
		  //  Affiliate button manage */
		  $('.affiliate-button-group').on('click','.affiliate-button', function(){
			  var Obj = $(this);
			   $('.affiliate-button-group').find('.affiliate-button').removeClass('active');
			   Obj.addClass('active');
			   if(Obj.attr('data-val') ==1)
			   {
				   $('#affiliate_income, #affiliate_history, #affiliate_add').removeClass('d-none');
				   $('#affiliate_income, #affiliate_history').addClass('d-none');
				   $('.affiliate-form')[0].reset();
			   }
			   else if(Obj.attr('data-val') ==2)
			   {
				    affiiate_history_load();
				     $('#affiliate_income, #affiliate_history, #affiliate_add').removeClass('d-none');
				   $('#affiliate_income, #affiliate_add').addClass('d-none');
			   }
			   else
			   {
				    affiiate_income_history_load();
				   $('#affiliate_income, #affiliate_history, #affiliate_add').removeClass('d-none');
				   $('#affiliate_history, #affiliate_add').addClass('d-none');
			   }
			   
		  });
		  
		  /* Affiliate submit by user */
		  $('input[name=affiliate_submit]').click(function(e){
			  e.preventDefault();
			  var Obj = $(this);
			  var formdata = $('.affiliate-form').serialize();
			  var resource = 'user_affiliate_submit';
			  
			  if($('input[name=product_nae]').val() =='')
			  {
				  alert("Please enter affiliate product name");
			  }
			  else if($('input[name=product_order_id]').val() =='')
			  {
				  alert("Please enter affiliate product order id");
			  }
			  else
			  {
				$.ajax({
				  type:"POST",
				  data:formdata+"&resource="+resource,
				  url:"ajax_call_manage.php",
				  beforeSend:function(){
					  Obj.prop('disabled', true);
					  Obj.html('Submitting <i class="fa fa-spinner fa-spin"></i>');
				  },
				  success:function(response){
					  Obj.prop('disabled', false);
					  Obj.html('Submit');
					  if(response.trim() !='' && response.trim() ==1)
					  {
						  alert("Affiliate added successfully");
						  $('.affiliate-history-btn').click();
					  }
					  else
					  {
						  alert("Bad request");
					  }
				  }
				});
			  }
		  });
		  
		  /* decline affiliate request */
		  $('.decline-affiliate-request').click(function(){
			  if($('.affiliate-checkbox:checked').length == 0)
			  {
				  alert("Please select any pending affiliate");
				  return false;
			  }
			  else if($('.affiliate-checkbox:checkbox:checked').length > 1)
			  {
				   alert("You can decline one affiliate at a time");
				  return false;
			  }
			  else
			  {
				  $('.affiliate-checkbox:checkbox:checked').each(function(){
					  $('input[name=affiliate_id]').val($(this).val());
					  $('.affiliate-name').text($(this).closest('tr').find("td:eq(2)").text());
					   $('.affiliate-order-no').text($(this).closest('tr').find("td:eq(3)").text());
				  });
				  $('#declined-affiliate-modal').modal('show');
			  }
		  });
		  
		  /* affiliate decline submit */
		  $('.affiliate-decline-submit').click(function(){
			  
			  var Obj = $(this);
			  var affiliate_id = $('input[name=affiliate_id]').val();
			  var declined_msg = $('.decline_msg').val();
			  if(typeof(declined_msg) == 'undefined' || declined_msg =='')
			  {
				  alert("Please enter decline reason");
				  return false;
			  }
			  else
			  {
				  if(typeof(affiliate_id) !='undefined' && affiliate_id !='')
				  {
					var resource = 'admin_decline_affiliate';
					$.ajax({
					  type:"POST",
					  data:{resource:resource, affiliate_id:affiliate_id, declined_msg:declined_msg},
					  url:"ajax_call_manage.php",
					  beforeSend:function(){
						  Obj.prop('disabled', true);
						  Obj.html('Submittig <i class="fa fa-spinner fa-spin"></i>');
					  },
					  success:function(response){
						  Obj.prop('disabled', false);
						  Obj.html('Submit');
						  if(response.trim() !='' && response.trim() ==1)
						  {
							  $('.affiliate-decline-form')[0].reset();
							   $('.filter_status').val(3);
							  affiiate_history_load();
							  alert("Affiliate decline successfully");
							  setTimeout(function(){
								  $('#declined-affiliate-modal').modal('hide');
							  },1000);
						  }
					  }
					});
				  }
			  }
		  });
		  
	  });
	  
	  function affiiate_history_load(){
		  
		  var status = $('.filter_status').children("option:selected").val();
		 // console.log('st- '+status);
		  var resource = 'affiliate_hostory';
		  $.ajax({
			  type:"POST",
			  data:{resource:resource, status:status},
			  url:"ajax_call_manage.php",
			  success:function(response){
				  if(response.trim() !='')
				  {
					  $('#product_affiliate_history').html(response.trim());
				  }
				  setTimeout(function(){
					  $('#product_affiliate_history').DataTable({ 
                      "destroy": true, //use for reinitialize datatable
                   });
				  }, 1000);
			  }
		  });
	  }
	  
	  function affiiate_income_history_load(){
		  
		  var status = $('.filter-history-status').children("option:selected").val();
		 // console.log('st- '+status);
		  var resource = 'affiliate_income_hostory';
		  $.ajax({
			  type:"POST",
			  data:{resource:resource, status:status},
			  url:"ajax_call_manage.php",
			  success:function(response){
				  if(response.trim() !='')
				  {
					  $('#affiliate_income_details').html(response.trim());
				  }
				  setTimeout(function(){
					  $('#affiliate_income_details').DataTable({ 
                      "destroy": true, //use for reinitialize datatable
                   });
				  }, 1000);
				  
			  }
		  });
	  }
	 </script>
	  
<?php
include 'include/footer.php';
?>