<!DOCTYPE html>
<html lang="en">
<head>
	<!-- header -->
		<?php $this->load->view('admin/common_header'); ?>
	<!-- /header -->
	
	<!-- Bootsrap form validation JS files -->
	<script type="text/javascript" src="<?php echo base_url();?>assets/js/formValidation.js"></script>
	<script type="text/javascript" src="<?php echo base_url();?>assets/js/bootstrap.js"></script>

	<script type="text/javascript" src="<?php echo base_url();?>assets/js/plugins/forms/styling/uniform.min.js"></script>
	<script type="text/javascript" src="<?php echo base_url();?>assets/js/plugins/notifications/pnotify.min.js"></script>
	<script type="text/javascript" src="<?php echo base_url();?>assets/js/plugins/forms/selects/bootstrap_multiselect.js"></script>
	<!-- Theme JS files -->
	<script type="text/javascript" src="<?php echo base_url();?>assets/js/core/app.js"></script>
	<!-- /theme JS files -->

	<!-- Theme JS files -->
	<script type="text/javascript" src="<?php echo base_url();?>assets/js/core/libraries/jquery_ui/interactions.min.js"></script>
	<script type="text/javascript" src="<?php echo base_url();?>assets/js/plugins/forms/selects/select2.min.js"></script>

	
	<!-- /theme JS files -->
	<!-- Theme JS files -->
	<!-- /theme JS files -->

    <!-- notify JS files -->
    <script type="text/javascript" src="<?php echo base_url();?>assets/js/plugins/notifications/jgrowl.min.js"></script>

	<!-- /select box JS files -->
	
	<style>
	    .has-error span.select2-container{
	    	border:1px solid #d84315;
	    }
	    .has-success span.select2-container{
	    	border:1px solid #43a047;
	    }


	</style>
</head>

<body class="navbar-top-md-md">
	<!-- <div class="pre_loader"></div> -->
	<!-- Fixed navbars wrapper -->
	<div class="navbar-fixed-top">

		<!-- Main navbar -->
		<?php $this->load->view('admin/mainnav'); ?>
		<!-- /main navbar -->


		<!-- Second navbar -->
		<?php $this->load->view('admin/secondnav'); ?>
		<!-- /second navbar -->

	</div>
	<!-- /fixed navbars wrapper -->


	<!-- Page header -->
	<?php $this->load->view('admin/'.$module_name.'_header'); ?>
	<!-- /page header -->



	<!-- Page container -->
	<div class="page-container">

		<!-- Page content -->
		<div class="page-content">

			<!-- Main content -->
			<div class="content-wrapper">

				<!-- Horizontal form -->
				<div class="panel panel-flat">
					<div class="panel-heading">
						<h5 class="panel-title"><? echo $this->lang->line('fund_transfer_title'); ?></h5>
						<div class="heading-elements">
							<ul class="icons-list">
		                		<li><a data-action="collapse"></a></li>
		                		<li><a data-action="close"></a></li>
		                	</ul>
	                	</div>
                	</div>
                	<?php $currency_info = defaultcurrency();?>
					<div class="panel-body">
						<form class="form-horizontal fundtransfer" action="<?php echo $action;?>" method="post" enctype="multipart/form-data">

							<?php 
								if(validation_errors()) { 
									$form_err_data = str_replace('<p>', '', validation_errors());
									$form_err_data = str_replace('</p>', '', $form_err_data);
									$form_err_data = str_replace('.', '', $form_err_data);
							?>
									
									<script type="text/javascript">
									
										(function( $ ) {
											$.jGrowl.defaults.closer = false;
									        var error_data = '<?php echo $this->lang->line("fund_transfer_amount_bet"); ?>'
											$.jGrowl(error_data, {
									            header: '<?php echo $this->lang->line("error");?>',
									            theme: 'bg-danger'
									        });
										})(jQuery);

									</script>
							<?php
								}
							?>

							<div class="form-group  <?php if(form_error('select_payer')) echo "has-error";?>">
								<label class="control-label col-lg-3 text-semibold"><?php echo $this->lang->line('fund_transfer_sender'); ?> <span class="text-danger">*</span></label>
								<div class="col-lg-4">
									<select name="select_payer" id="select_payer_id" class="select-search form-control">
										<option value=""><?php echo $this->lang->line('none_selected'); ?></option>
					               		<?php foreach ($uusers_list as $users) { ?>
										<option value="<?php echo $users->uusersid; ?>"><?php echo ucfirst($users->username); ?></option>
										<?php } ?>	
									</select>
									<?php echo form_error('select_payer', '<div class="form-control-feedback"><i class="icon-cancel-circle2"></i></div><span class="help-block">', '</span>'); ?>
								</div>
							</div>

							<div class="form-group  <?php if(form_error('select_receiver')) echo "has-error";?>">
								<label class="control-label col-lg-3 text-semibold"><?php echo $this->lang->line('fund_transfer_receiver'); ?> <span class="text-danger">*</span></label>
								<div class="col-lg-4">
									<select name="select_receiver"  class="select-search form-control">
										<option value=""><?php echo $this->lang->line('none_selected'); ?></option>
					               		<?php foreach ($uusers_list as $receiver) { ?>
										<option value="<?php echo $receiver->uusersid; ?>"><?php echo ucfirst($receiver->username); ?></option>
										<?php } ?>
									</select>
									<?php echo form_error('select_receiver', '<div class="form-control-feedback"><i class="icon-cancel-circle2"></i></div><span class="help-block">', '</span>'); ?>
								</div>
							</div>

							<!-- <div class="form-group  <?php if(form_error('payment')) echo "has-error";?>">
								<label class="control-label col-lg-3 text-semibold"><?php echo $this->lang->line('fund_transfer_payment'); ?> <span class="text-danger">*</span></label>
								<div class="col-lg-4">
									
										<select name="payment"  id="payment_check" class="select-search form-control">
											<option value=""><?php echo $this->lang->line('none_selected'); ?></option>
					               <?php foreach ($payment_list as $list) { ?>
											<option value="<?php echo $list->payment_id; ?>"><?php echo ucfirst($list->payment_name); ?></option>
											<?php } ?>
										</select>
									
									<?php echo form_error('payment', '<div class="form-control-feedback"><i class="icon-cancel-circle2"></i></div><span class="help-block">', '</span>'); ?>
								</div>
							</div> -->
                           
                            <div class="form-group">
								<label class="control-label col-lg-3 text-semibold"><?php echo $this->lang->line('fund_transfer_balance'); ?></label>
								<div class="col-lg-4">
									<label style="margin-top: 5px;border:none;" ><span id="balanceVal"><?php echo number_format('0',$currency_info->deimal_point, '.', ''); ?></span>&nbsp<span id="deduct"><?php echo strtoupper($currency_info->currency_unit);?></span></label>
									<div id="ip-exist" style="display:none;color:#d84315;" class="error_msg"><?php echo $this->lang->line('fund_balance'); ?></div>
								</div>
							</div>

							<div class="form-group">
								<label class="control-label col-lg-3 text-semibold"><?php echo $this->lang->line('fund_transfer_amount'); ?> <span class="text-danger">*</span></label>
								<div class="col-lg-4">
									<div class="input-group">
									    <span class="input-group-addon" style="font-size: 18px;"><strong><?php echo $currency_info->currency_symbol;?></strong></span>										
											<input type="text" name="amount" id="transfer_amount" class="form-control" placeholder="<?php echo $this->lang->line('plac_transfer_amount'); ?>" value="<?php echo set_value('amount'); ?>">
					                    <span class="input-group-addon"><?php echo strtoupper($currency_info->currency_unit);?></span>
									</div>
									<div id="blan" style="display:none;color:#d84315;" class="error_msg1"><?php echo $this->lang->line('fund_balance'); ?></div>
								</div>
							</div>

							<div class="form-group">
								<label class="control-label col-lg-3 text-semibold"><?php echo $this->lang->line('fund_transfer_fee'); ?></label>
								<div class="col-lg-4">
									<label style="margin-top: 5px;" ><strong id="trans_fee"><?php echo $this->data['transfer_fee'] ?>%</strong> will be deducted</label>
								    <input type="hidden" id="transfee" name="transfee" value="">
								</div>
							</div>

							<div class="text-right col-lg-7">
	                            <button id="btn" type="submit" class="btn btn-primary"><?php echo $this->lang->line('bonus_button'); ?> <i class="icon-arrow-right14 position-right"></i></button>
							</div>
						</form>
					</div>
				</div>
				<!-- /horizotal form -->

			</div>
			<!-- /main content -->

		</div>
		<!-- /page content -->
		

		<!-- Footer -->
		<div class="footer text-muted">
			<?php echo site_footer();?>
		</div>
		<!-- /footer -->

	</div>
	<!-- /page container -->

	<script type="text/javascript">
	jQuery.noConflict();
	(function( $ ) {
	$(function() {

		$(document).ready(function() {

			$('.select-search').select2();


			$('#transfer_amount').change(function(){

                $.ajax({
                    type: 'post',
                    url : '<?php echo base_url();?>admin/finance/transationfee',
                    data : {'amount':$('#transfer_amount').val()},
                    success : function(msg)
                    {
                        if(msg) {
                            $('#transfee').html(msg);
                        }
                    }
                });

            })

            $('#select_payer_id').change(function(){

      			// var id = $('#payment_check').val();
      			var user_id = $('#select_payer_id').val();
      			var amount = $('#balanceVal').html();

      			if($('#transfer_amount').val()) {
                    $('.fundtransfer').formValidation('revalidateField', 'amount');
                }

      			// if($('#transfer_amount').val() > amount){
      			// 	console.log('invalid');
      			// 	$('#transfer_amount').trigger('change');
      			// } else {
      			// 	console.log('valid');
      			// 	$('#transfer_amount').trigger('change');
      			// }

      			if(user_id) {
		        	$.ajax({
		                type: 'post',
		                url : '<?php echo base_url();?>admin/finance/checkbalance',
		                data : {'sender_id':user_id},
		                success : function(msg)
		                {
		                    if(msg) {
		                    	$('#balanceVal').html(msg);
	                        
	                        	if(msg=='<?php echo number_format('0',$currency_info->deimal_point, '.', ''); ?>' || msg < '0') {
		                    
			                    	$('#ip-exist').css('display','block');
			                    	$('#balanceVal').css('color','#d84315');
			                    	$('#deduct').css('color','#d84315');
			                    	$('.error_msg').parent().parent().addClass('has-error');
			                    	$('#btn').addClass('disabled');

		                    	} else {

			                    	$('#ip-exist').css('display','none');
			                    	$('#balanceVal').css('color','#333333');
			                    	$('#deduct').css('color','#333333');
			                    	$('.error_msg').parent().parent().removeClass('has-error');
			                    }
		                    }
		                }
		            });
				}
      
             });

			$('#payment_check').change(function(){

      			var id = $('#payment_check').val();
      			var user_id = $('#select_payer_id').val();

      			if($('#transfer_amount').val()) {
                    $('.fundtransfer').formValidation('revalidateField', 'amount');
                }

      			if(user_id) {
		        	$.ajax({
		                type: 'post',
		                url : '<?php echo base_url();?>admin/finance/checkbalance',
		                data : {'id':id,'sender_id':user_id},
		                success : function(msg)
		                {
		                    if(msg) {
		                    	$('#balanceVal').html(msg);
	                        
	                        	if(msg=='<?php echo number_format('0',$currency_info->deimal_point, '.', ''); ?>' || msg < '0') {
		                    
			                    	$('#ip-exist').css('display','block');
			                    	$('#balanceVal').css('color','#d84315');
			                    	$('#deduct').css('color','#d84315');
			                    	$('.error_msg').parent().parent().addClass('has-error');
			                    	$('#btn').addClass('disabled');

		                    	} else {

			                    	$('#ip-exist').css('display','none');
			                    	$('#balanceVal').css('color','#333333');
			                    	$('#deduct').css('color','#333333');
			                    	$('.error_msg').parent().parent().removeClass('has-error');
			                    }
		                    }
		                }
		            });
				}
      
             });

			// $('#transfer_amount').change(function(){

   //    			var id = $('#transfer_amount').val();
   //    			var balance_val = $('#balanceVal').html();
   //    			var user_id = $('#select_payer_id').val();
   //    			if(user_id) {
	  //     			if(id!=<?php echo number_format('0'); ?>){
			// 	        $.ajax({
			// 	            type: 'post',
			// 	            url : '<?php echo base_url();?>admin/finance/checkfee/',
			// 	            // data : {'id':id,'user':user_id},
			// 	            data : {'id':id,'user':user_id,'balance':balance_val},
			// 	            success : function(msg)
			// 	            {
			//                     if(msg) {
			//                     	if(msg=='Insufficient') {

			//                             $('#blan').css('display','block');
			//                             $('.error_msg1').parent().parent().addClass('has-error');
			//                             $('#btn').addClass('disabled');

			//                     	} else {
			// 		                    $('#trans_fee').html('<?php echo $currency_info->currency_symbol;?>'+msg);
			// 		                    $('#transfee').val(msg);
			// 		                    $('#blan').css('display','none');
			//                         }
			//                     }
			// 	            }
			// 	        });

		 //         	} else{

		 //         		$('#blan').css('display','block');
	  //               	$('.error_msg1').parent().parent().addClass('has-error');
	  //               	$('#btn').addClass('disabled');
		 //         	}
		 //        }
      
   //          });

			$.jGrowl.defaults.closer = false;

			<?php if($this->session->flashdata('error_message')) { ?>
				$.jGrowl("<?php echo $this->session->flashdata('error_message');?>", {
		            header: '<?php echo $this->lang->line("error");?>',
		            theme: 'bg-danger'
		        });
            <?php } ?>
                    
            <?php if($this->session->flashdata('success_message')) { ?>
                $.jGrowl("<?php echo $this->session->flashdata('success_message');?>", {
		            header: '<?php echo $this->lang->line("success");?>',
		            theme: 'bg-success'
		        });
            <?php } ?>

		    $('.fundtransfer').formValidation({
		    	framework: 'bootstrap',
		    	excluded: [':disabled'],
		        icon: {
		           
		            validating: 'glyphicon glyphicon-refresh'
		        },
		        fields: {
		            select_payer: {
		                message: '<?php echo $this->lang->line("fund_transfer_sender_valid");?>',
		                validators: {
		                    notEmpty: {
		                        message: '<?php echo $this->lang->line("fund_transfer_sender_required");?>'
		                    }
		                }
		            },
		            select_receiver: {
		                message: '<?php echo $this->lang->line("fund_transfer_receiver_valid");?>',
		                validators: {
		                    notEmpty: {
		                        message: '<?php echo $this->lang->line("fund_transfer_receiver_required");?>'
		                    }
		                }
		            },
		            // payment: {
		            //     message: '<?php echo $this->lang->line("fund_transfer_payment_valid");?>',
		            //     validators: {
		            //         notEmpty: {
		            //             message: '<?php echo $this->lang->line("fund_transfer_payment_required");?>'
		            //         }
		            //     }
		            // },
		            amount: {
		                message: '<?php echo $this->lang->line("fund_transfer_amount_valid");?>',
		                validators: {
		                    notEmpty: {
		                        message: '<?php echo $this->lang->line("fund_transfer_amount_required");?>'
		                    },
		                    regexp: {
		                        regexp: /^[0-9.]+$/,
		                        message: '<?php echo $this->lang->line("fund_transfer_amount_invalid"); ?>'
		                    },
		                    remote: {
		                    	url: '<?php echo base_url();?>admin/finance/checkfee',
                                // url: function(validator, $field, value) {
                                    
                                //     return '<?php echo base_url();?>admin/finance/checkfee/'+$('#payment_check').val();
                                // },
                                type: 'post',
				                data: {          
						            payid: function() {  
						                return $( "#payment_check" ).val();
						            },
						            sender_id: function() {  
						                return $( "#select_payer_id" ).val();
						            }
						        },
                                delay: 1000
                            }
		                    
		                }
		            }
		            
		        }
		    })

		});
	 
		$('#icon-bck').on('click',function() {
			window.history.back();
		});

	});
	})(jQuery);
	</script>

</body>
</html>
