<?php
ob_start();
error_reporting(0);
include("connect.php");
session_start(); 
$ver = $_SESSION["admin_id"];
$dis_id = $_SESSION["manager_id"];
$user_type = $_SESSION["user_type"];
$user_session = $_SESSION["user_session"];
if(!isset($_SESSION["admin_id"])){
   header('location:http://www.btcover.appexperts.net');
}
date_default_timezone_set('Asia/Calcutta');
	$cdate  = date("Y-m-d");
	$q=mysqli_query($con,"select * from tbl_manager where manager_id='$dis_id'");
$res=mysqli_fetch_assoc($q);
$imei_no=$res['imei_no'];
?>
<!DOCTYPE html>
<html dir="ltr" lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- Tell the browser to be responsive to screen width -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- Favicon icon -->
    <link rel="icon" type="image/png" sizes="16x16" href="assets/images/favicon.png">
    <title>BTCOVER : Admin Dashboard</title>
    <!-- Custom CSS -->
    <link rel="stylesheet" type="text/css" href="assets/extra-libs/multicheck/multicheck.css">
    <link href="assets/libs/datatables.net-bs4/css/dataTables.bootstrap4.css" rel="stylesheet">
    <link href="dist/css/style.min.css" rel="stylesheet">
	<script>
		function setagentid(transaction_id)
		{
			
			var transaction_id1=document.getElementById('transaction_id1').value;
		
			var policy_no1=document.getElementById('policy_no1').value;
			
			var agent_id1=document.getElementById('agent_id1').value;
			var unique_id1=document.getElementById('unique_id1').value;
			var premium_amt1=document.getElementById('premium_amt1').value;
			document.getElementById('transaction_id').value=transaction_id1;
			document.getElementById('policy_no').value=policy_no1;
			document.getElementById('agent_id').value=agent_id1;
			document.getElementById('unique_id').value=unique_id1;
			document.getElementById('pre_amt').value=premium_amt1;
		
		}
		</script>
		<script>
		function setagentid3(transaction_id)
		{
			
			var transaction_id1=document.getElementById('transaction_id1').value;
		
			var policy_no1=document.getElementById('policy_no1').value;
			
			var agent_id1=document.getElementById('agent_id1').value;
			var unique_id1=document.getElementById('unique_id1').value;
			var premium_amt1=document.getElementById('premium_amt1').value;
			document.getElementById('transaction_id3').value=transaction_id1;
			document.getElementById('policy_no3').value=policy_no1;
			document.getElementById('agent_id3').value=agent_id1;
			document.getElementById('unique_id3').value=unique_id1;
			document.getElementById('pre_amt3').value=premium_amt1;
		
		}
		</script>
		<script>
		function setagentid1(str)
		{
			
			var pre_amt=document.getElementById('pre_amt').value;
			var pre_amt1=parseInt(pre_amt);
			var str1=parseInt(str);
		
			
			if(pre_amt1>=str1)
			{
				var received_amt1=parseFloat(pre_amt)-parseFloat(str);
			document.getElementById('difference_amt').value=received_amt1;
				
			
			}
			else
			{
			alert('received amount shold be less than premium amount');
			//document.getElementById('received_amt').value=0;
				window.location.href="agent-pending-work.php";
			}
		
		}
		</script>
</head>

<body>
  
    <div id="main-wrapper">
	 <?php include("header.php"); ?>
    
     <?php include("menu.php"); ?>
	
	
       
      
        <div class="page-wrapper">
            <!-- ============================================================== -->
            <!-- Bread crumb and right sidebar toggle -->
            <!-- ============================================================== -->
            <div class="page-breadcrumb">
                <div class="row">
                    <div class="col-12 d-flex no-block align-items-center">
                        <h4 class="page-title">Agent Pending Work</h4>
                        <div class="ml-auto text-right">
                            
                        </div>
                    </div>
                </div>
            </div>
            <div class="container-fluid" >
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body" style="float:left; width:100%; overflow-Y: auto; height: 500px;">
                                <div class="table-responsive">
                                    <table id="zero_config" class="table table-striped table-bordered">
                                        <thead>
                                            <tr>
											  <th><b>#</b></th>
											    <th><b>Agent Name</b></th>
											    <th><b>Customer Name</b></th>
											    <th><b>Policy Number</b></th>
												 <th><b>Premium Amount</b></th>
												 <th><b>Renewal Date</b></th>
												 <th><b>Status</b></th>
												 <th><b>Action</b></th>
											</tr>
                                        </thead>
                                        <tbody>
										  <?php 
									   $c=1;
										if($user_type=='Manager') 
											$selrenew3=mysqli_query($con,"SELECT *,tbl_customer_premium.status as sta FROM `tbl_customer_premium`,tbl_agent,tbl_customer,tbl_policy_master where tbl_agent.agent_id=tbl_customer_premium.agent_id and tbl_customer_premium.customer_id=tbl_customer.customer_id and tbl_customer_premium.status='Pending' and tbl_policy_master.policy_no=tbl_customer_premium.policy_no and tbl_customer_premium.manager_id='$dis_id'");
										else
											$selrenew3=mysqli_query($con,"SELECT *,tbl_customer_premium.status as sta FROM `tbl_customer_premium`,tbl_agent,tbl_customer,tbl_policy_master where tbl_agent.agent_id=tbl_customer_premium.agent_id and tbl_customer_premium.customer_id=tbl_customer.customer_id and tbl_customer_premium.status='Pending' and tbl_policy_master.policy_no=tbl_customer_premium.policy_no and tbl_customer_premium.manager_id in (select manager_id from tbl_manager where admin_id='$ver') ");
										if(mysqli_num_rows($selrenew3)>0)
										{
											while($recset=mysqli_fetch_array($selrenew3))
											{
										?>
										<tr>
										  <td><?php echo $c++; ?></td>
										  <td><?php echo $recset['agent_name']; ?></td>
										  <td><?php echo $recset['customer_name']; ?></td>
										  <td><a href="policy-detail.php?id=<?php echo base64_encode($recset['policy_no'])?>&user=pending"><?php echo $recset['policy_no']; ?></a></td>
										  <td><?php echo $recset['premium_amt']; ?></td>
										  <td><?php echo date("d-M-Y", strtotime($recset['renewal_date']) ); ?></td>
										  <td><?php echo $recset['sta']; ?></td>
										  <td>
										 
										 <?php if($imei_no=='0' || $imei_no=='') { ?>
												<a href="javascript:void(0)" data-toggle="modal" data-target="#add-new-event" style="font-size:12px;padding:1px 5px;" onClick="setagentid(<?php echo $recset['transaction_id']?>)" class="btn btn-success">Mark Complete</a>
												<a href="javascript:void(0)" data-toggle="modal" data-target="#add-new-event1" style="font-size:12px;padding:1px 5px;" onClick="setagentid3(<?php echo $recset['transaction_id']?>)" class="btn btn-danger">Mark Reject</a>
												  
										<?php } else { ?>
												<a href="#" data-toggle="modal" data-target="#add-new-event" style="font-size:12px;padding:1px 5px;" onClick="alert('You Are Already Login in App...First Logout Fom App then Mark Complete From Here')" class="btn btn-success">Mark Complete</a>
												<a href="#" data-toggle="modal" data-target="#add-new-event1" style="font-size:12px;padding:1px 5px;" onClick="alert('You Are Already Login in App...First Logout Fom App then Mark Reject From Here')" class="btn btn-danger">Mark Reject</a>
												
										<?php } ?>
										<input class="form-control form-white" placeholder="Commission Amount" id="transaction_id1" type="hidden"  value="<?php echo $recset['transaction_id']; ?>" name="transaction_id1" />
										 <input class="form-control form-white" placeholder="Commission Amount" id="policy_no1" type="hidden" name="policy_no1"  value="<?php echo $recset['policy_no']; ?>" /> 
										 <input class="form-control form-white" placeholder="Commission Amount" id="unique_id1" type="hidden" name="unique_id1"  value="<?php echo $recset['unique_id']; ?>" /> 
										
										 <input class="form-control form-white" placeholder="Commission Amount" id="agent_id1" type="hidden" name="agent_id1"   value="<?php echo $recset['agent_id']; ?>"/>
										 <input class="form-control form-white" placeholder="Commission Amount" id="premium_amt1" type="hidden" name="premium_amt1"   value="<?php echo $recset['premium_amt']; ?>"/>
										 
										  </td>
										</tr>
										<?php }} else { ?>
										 <tr>
										  <td colspan='5'>No Data Available</td>
										</tr>
										<?php } ?>
                                        </tbody>
                                       
                                    </table>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
             
            </div>
				<div class="modal fade none-border" id="add-new-event1">
									<div class="modal-dialog">
									<div class="modal-content">
								<div class="modal-header">
                             
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            </div>
                            <div class="modal-body">
                                <form   method="post"  enctype="multipart/form-data">
                                    
										<div class="row">
										<div class="col-md-12">
										<div class="form-group ">
                                            <label class="control-label">Reason</label>
                                             <input class="form-control form-white" placeholder="Reason" id="reason" type="text" name="reason"  />
                                        </div>
                                        </div>
                                        </div>
										
										<br>
										 <input class="form-control form-white" placeholder="Commission Amount" id="transaction_id3" type="hidden"  value="<?php echo $transaction_id; ?>" name="transaction_id3" /> 
										
										 <input class="form-control form-white" placeholder="Commission Amount" id="policy_no3" type="hidden" name="policy_no3"  value="<?php echo $policy_no; ?>" /> 
										 <input class="form-control form-white" placeholder="Commission Amount" id="unique_id3" type="hidden" name="unique_id3"  value="<?php echo $unique_id; ?>" /> 
										
										 <input class="form-control form-white" placeholder="Commission Amount" id="agent_id3" type="hidden" name="agent_id3"   value="<?php echo $agent_id; ?>"/>
										 
										
                                <button type="submit" class="btn btn-success" name="sub1" >Save</button>
                                <button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">Close</button>
                            </div>
                                </form>
								<?php 
								if(isset($_POST['sub1']))
								{
									$ver = $_SESSION['manager_id'];
	$policy_no=$_POST['policy_no3'];
	$unique_id=$_POST['unique_id3'];
	$transaction_id=$_POST['transaction_id3'];
	$status='Reject';
	$reason=$_POST['reason'];
	date_default_timezone_set('Asia/Calcutta');
	$cdate  = date("Y-m-d");
	$ctime=date('h:i:s');
	$res_product_stock = mysqli_query($con,"SELECT transaction_id FROM  tbl_customer_premium where manager_id='$ver' and policy_no='$policy_no' and status='Pending' and transaction_id='$transaction_id'");

	if(mysqli_num_rows($res_product_stock)>0)
	{	
		$uq2=mysqli_query($con,"update tbl_customer_premium set status='$status',complete_date='$cdate',complete_time='$ctime' where transaction_id='$transaction_id'"); 
		
		$uq4=mysqli_query($con,"update tbl_policy_master set rejection_reason='$reason' where unique_id='$unique_id' and status ='Pending'"); 
		
		$uq3=mysqli_query($con,"update tbl_policy_master set status='Expired',update_datetime='$cdate' where unique_id='$unique_id' and status in('Renewal Pending','Pending')"); 

	if($uq3)
			{
				header('location:agent-pending-work.php');
			}
	}
	else
	{
		?>
		<script>
	alert("Policy No Not Exists");
	</script>
	<?php
	}					}
								?>
                            </div>
                           
                        </div>
                    </div>
			<div class="modal fade none-border" id="add-new-event">
									<div class="modal-dialog">
									<div class="modal-content">
								<div class="modal-header">
                             
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            </div>
                            <div class="modal-body">
                                <form   method="post"  enctype="multipart/form-data">
                                    <div class="row">
										<div class="col-md-6">
										<div class="form-group ">
                                            <label class="control-label">Premium Amount</label>
                                            <input class="form-control form-white" placeholder="Commission Amount" id="pre_amt" type="text" value="<?php echo $pre_amt; ?>" name="pre_amt" />
                                        </div>
                                        </div>
										<div class="col-md-6">
										<div class="form-group ">
                                            <label class="control-label">Amount Received</label>
                                             <input class="form-control form-white" placeholder="Received Amount" id="received_amt" type="text" name="received_amt" onchange="setagentid1(this.value)" onkeypress='return event.charCode >= 48 && event.charCode <= 57'>
                                        </div>
                                        </div>
										
                                        </div>
										<div class="row">
										<div class="col-md-6">
										<div class="form-group ">
                                            <label class="control-label">New Policy Number</label>
                                             <input class="form-control form-white" placeholder="New Policy Number" id="new_policy_no" type="text" name="new_policy_no"  />
                                        </div>
                                        </div>
                                        </div>
										
										<br>
										 <input class="form-control form-white" placeholder="Commission Amount" id="transaction_id" type="hidden"  value="<?php echo $transaction_id; ?>" name="transaction_id" /> 
										
										 <input class="form-control form-white" placeholder="Commission Amount" id="policy_no" type="hidden" name="policy_no"  value="<?php echo $policy_no; ?>" /> 
										 <input class="form-control form-white" placeholder="Commission Amount" id="unique_id" type="hidden" name="unique_id"  value="<?php echo $unique_id; ?>" /> 
										
										 <input class="form-control form-white" placeholder="Commission Amount" id="agent_id" type="hidden" name="agent_id"   value="<?php echo $agent_id; ?>"/>
										 <input class="form-control form-white" placeholder="Commission Amount" id="difference_amt" type="hidden" name="difference_amt"  value="<?php echo $difference_amt; ?>" />
										
                                <button type="submit" class="btn btn-success" name="sub" >Save</button>
                                <button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">Close</button>
                            </div>
                                </form>
								<?php 
								if(isset($_POST['sub']))
								{
									
									$ver = $_SESSION['manager_id'];
	$policy_no=$_POST['policy_no'];
	$new_policy_no=$_POST['new_policy_no'];
	$unique_id=$_POST['unique_id'];
	$agent_id=$_POST['agent_id'];
	$transaction_id=$_POST['transaction_id'];
	$received_amt=$_POST['received_amt'];
	$difference_amt=$_POST['difference_amt'];
	$status='Complete';
	date_default_timezone_set('Asia/Calcutta');
	$cdate  = date("Y-m-d");
	$ctime=date('h:i:sa');

	if($new_policy_no==''){
		$new_policy_no = $policy_no;
	}
	
	$res_product_stock = mysqli_query($con,"SELECT transaction_id FROM  tbl_customer_premium where manager_id='$ver' and policy_no='$policy_no' and status='Pending' and transaction_id='$transaction_id'");

	if(mysqli_num_rows($res_product_stock)>0)
	{	
		$uq2=mysqli_query($con,"update tbl_customer_premium set status='$status' ,policy_no='$new_policy_no',agent_received_amt='$received_amt',complete_date='$cdate',complete_time='$ctime' where transaction_id='$transaction_id'"); 

		$query = mysqli_query($con,"INSERT INTO `tbl_agent_commission`(`agent_id`, `credit_amt`, `policy_no`, `transaction_date`, `transaction_time`, `status`) VALUES ('$agent_id','$difference_amt','$new_policy_no','$cdate','$ctime','Pending')");	

		$uq3=mysqli_query($con,"update tbl_policy_master set status='Renewed',update_datetime='$cdate' where unique_id='$unique_id' and status = 'Renewal Pending' "); 

		$uq4=mysqli_query($con,"update tbl_policy_master set status='Active',policy_no='$new_policy_no',update_datetime='$cdate' where unique_id='$unique_id' and status = 'Pending' "); 
			if($uq4)
			{
				header('location:agent-pending-work.php');
			}
	}
	else
	{
		?>
		<script>
	alert("Policy No Not Exists");
	</script>
	<?php
	}
								}
								?>
                            </div>
                           
                        </div>
                    </div>
                </div>
         
            <footer class="footer text-center">
                  2019 &copy; All Rights Reserved by BTCOVER. Designed and Developed by <a href="http://www.belltechnology.in/">Bell Technology</a>.
            </footer>
          
        </div>
      
    </div>
  
    <script src="assets/libs/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap tether Core JavaScript -->
    <script src="assets/libs/popper.js/dist/umd/popper.min.js"></script>
    <script src="assets/libs/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- slimscrollbar scrollbar JavaScript -->
    <script src="assets/libs/perfect-scrollbar/dist/perfect-scrollbar.jquery.min.js"></script>
    <script src="assets/extra-libs/sparkline/sparkline.js"></script>
    <!--Wave Effects -->
    <script src="dist/js/waves.js"></script>
    <!--Menu sidebar -->
    <script src="dist/js/sidebarmenu.js"></script>
    <!--Custom JavaScript -->
    <script src="dist/js/custom.min.js"></script>
    <!-- this page js -->
    <script src="assets/extra-libs/multicheck/datatable-checkbox-init.js"></script>
    <script src="assets/extra-libs/multicheck/jquery.multicheck.js"></script>
    <script src="assets/extra-libs/DataTables/datatables.min.js"></script>
    <script>
        /****************************************
         *       Basic Table                   *
         ****************************************/
        $('#zero_config').DataTable();
    </script>

</body>

</html>