<?php 
    include '../includes/connection.php';
 // $_SESSION['mpid'];
 $mid= $_SESSION['Mid'];
if(isset($_SESSION['payment'])){
	echo $_SESSION['payment'];
	unset($_SESSION['payment']);
}else{
	echo"";
}

if($_SESSION['mpid']==2){
        header("location: http://localhost/LoanApprovalPredction/Members/index.php");
    }
    if($_SESSION['mpid']==1){
        header("location: http://localhost/LoanApprovalPredction/Admin/index.php");
    }
    if($_SESSION['mpid']==3){
        header("location: http://localhost/LoanApprovalPredction/Members/index.php");
    }
    $name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
 ?>

 <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title><?php echo htmlspecialchars($name); ?></title>
     <link rel="icon" href="../images/Icon.png">
    <script src="../CSS/tailwindcss.css"></script>
  </head>
  <body>
    <div>
      <header id="header" class="">
        <?php
          include '../includes/navbar.php';
        ?>
        </header><!-- /header -->
        
 <body>
 <form class="flex justify-center">
 	<div class="bg-gray-100 mt-10 gap-4">
 	<div class="p-2">
 		<p>Apply For Payments</p>
 	</div>
 		
 	<div class="p-2">
 		<input type="text" name="title" value="" placeholder="Work Title">
 	</div>
 	<div class="p-2">
 		<textarea placeholder="Work Description(Optional)" name="desc"></textarea>
 	</div>
 	<div>
 		<?php 
 		$wkd="SELECT * FROM complain WHERE Mid<>1";
 		$wkdr=mysqli_query($conn,$wkd);
 		if(mysqli_num_rows($wkdr)>=0){
 		 ?>
 		<select name="workdone">
 			<option selected disabled>Select you work done</option>
 			<?php while($wkdro=mysqli_fetch_assoc($wkdr)){ 
 				if($wkdro['status']=='onworking'){
 					$disabled="disabled";
 				}else{
 					$disabled="";
 				}
 				?>
 			<option value="<?php echo $wkdro['complain_id'];?>" <?php echo $disabled;?>><?php echo $wkdro['complain']; ?></option>
 			<?php } ?>
 		</select>
 	<?php } ?>
 	</div>
 	<div class="p-2">
 		<input type="number" name="payment" min="1" placeholder="Required Payment">
 	</div>
 	<div class="p-2">
 		<input type="submit" name="submit" value="Apply For Payment" class="border bg-blue-400 rounded p-2">
 	</div>
 	</div>
 </form>
 </body>
 </html>
<div class="flex justify-center mt-10">
	<?php 
	$sql1="SELECT * FROM payment";
	$result1=mysqli_query($conn,$sql1);
	if(mysqli_num_rows($result1)>0){
		$id=0;
	 ?>
	<table>
		<caption>table title and/or explanatory text</caption>
		<thead>
			<tr>
				<th>id</th>
				<th>Title of Work</th>
				<th>Amount</th>
				<th>Payment Status</th>
			</tr>
		</thead>
		<?php while($rows1=mysqli_fetch_assoc($result1)){ $id++;?>
		<tbody>
			<tr>
				<td><?php echo $id; ?></td>
				<td><?php echo $rows1['payment_title']; ?></td>
				<td><?php echo $rows1['payment_amount']; ?></td>
				<td><?php echo $rows1['status']; ?></td>
			</tr>
		</tbody>
	<?php } ?>
	</table>
	<?php } ?>
</div>


<?php 
if(isset($_REQUEST['submit'])){
	$date=date('Y-d-m');
	$ivn=time();
	$title=$_REQUEST['title'];
	echo $workdone=$_REQUEST['workdone'];
	$payment=$_REQUEST['payment'];
	$sql="INSERT INTO `payment`(`payment_amount`, `mid`, `payment_date`, `invoice`, `status`, `payment_title`,`complain_id`) VALUES ('$payment','$mid','$date','$ivn','pending','$title','$workdone')";
	if(mysqli_query($conn,$sql)){
		$_SESSION['payment']="<script>('Aply for payment successfully..!')</script>";
        header("location: http://localhost/LoanApprovalPredction/Members/Payment.php");
	}
}
?>