<?php
session_start();
error_reporting(0);
include('includes/config.php');
if (strlen($_SESSION['signAdminlogin']) == 0) {
	header('location:index.php');
} else {
	
	$signConSql = "select * from sign_control where id=1 limit 1";
    		$signCon_result = mysqli_query($link, $signConSql);
    		if(mysqli_num_rows($signCon_result) > 0) {
    		    $signCon_row = mysqli_fetch_array($signCon_result);
    		}else {
    		    $signCon_row = [];
    		}
    		

	if (isset($_REQUEST['confirm'])) {
		$aeid = intval($_GET['confirm']);
		$getuserSql = "select b.* from cardcopy_sub a join users b on a.Sub_by = b.email where a.id = $aeid";
		$user_result = mysqli_query($link, $getuserSql);
    		if(mysqli_num_rows($user_result) > 0) {
    		    $user_row = mysqli_fetch_array($user_result);
    		}
    		$user_bal = $user_row['balance'];
    		$update_balance = $user_row['balance'] + $signCon_row['cc_card_price'];
    		$user_id = $user_row['id']; 

            $query = "UPDATE users SET balance = $update_balance WHERE id = $user_id";
            mysqli_query($link, $query);

    		
    		
		$comment = ($_GET['comment']);
		$memstatus = 3;
		$sql = "UPDATE cardcopy_sub SET comments=:cmts, status=:status WHERE  id=:aeid";
		$query = $dbh->prepare($sql);
		$query->bindParam(':cmts', $comment, PDO::PARAM_STR);
		$query->bindParam(':status', $memstatus, PDO::PARAM_STR);
		$query->bindParam(':aeid', $aeid, PDO::PARAM_STR);
		$query->execute();
		$msg = "Changes Sucessfully";
	}





?>

	<!doctype html>
	<html lang="en" class="no-js">

	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
		<meta name="description" content="">
		<meta name="author" content="">
		<meta name="theme-color" content="#3e454c">

		<title>All Submission List</title>
<script src="https://code.jquery.com/jquery-3.6.4.slim.min.js" integrity="sha256-a2yjHM4jnF9f54xUQakjZGaqYs/V1CYvWpoqZzC2/Bw=" crossorigin="anonymous"></script>
		<!-- Font awesome -->
		<!--<link rel="stylesheet" href="css/font-awesome.min.css">-->
		<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" integrity="sha512-z3gLpd7yknf1YoNbCzqRKc4qyor8gaKU1qmn+CShxbuBusANI9QpRohGBreCFkKxLhei6S9CQXFEbbKuqLg0DA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
		<!-- Sandstone Bootstrap CSS -->
		<link rel="stylesheet" href="css/bootstrap.min.css">
		<link rel="stylesheet" href="//cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css">
		<script src="//cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js" ></script>
		<!-- Admin Stye -->
		<link rel="stylesheet" href="css/style.css">
		<style>
			.errorWrap {
				padding: 10px;
				margin: 0 0 20px 0;
				background: #dd3d36;
				color: #fff;
				-webkit-box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
				box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
			}

			.succWrap {
				padding: 10px;
				margin: 0 0 20px 0;
				background: #5cb85c;
				color: #fff;
				-webkit-box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
				box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
			}
		</style>

	</head>

	<body>
		<?php include('includes/header.php'); ?>

		<div class="ts-main-content">
			<?php include('includes/leftbar.php'); ?>
			<div class="content-wrapper">
				<div class="container-fluid">

					<div class="row">
						<div class="col-md-12">

							<h2 class="page-title">Manage Submission</h2>

							<!-- Zero Configuration Table -->
							<div class="panel panel-default">
								<div class="panel-heading">List submission</div>
								<div class="panel-body">
									<?php if ($error) { ?><div class="errorWrap" id="msgshow"><?php echo htmlentities($error); ?> </div><?php } else if ($msg) { ?><div class="succWrap" id="msgshow"><?php echo htmlentities($msg); ?> </div><?php } ?>
									<table id="myTable" class="display table table-striped table-bordered table-hover" cellspacing="0" width="100%">
										<thead>
											<tr>
												<th>#</th>
												<th>Req Type</th>
												<th>Name</th>
												<th>NID/BRN/Form/Voter</th>
												<th>Phone Number</th>
												<th>Father NID</th>
												<th>Mother NID</th>
												<th>Submitted By</th>
												<th>Status</th>
												<th>Sub At</th>
												<th>Action</th>
											</tr>
										</thead>

										<tbody>

											<?php $sql = "SELECT a.*, b.name AS Sub_name
                                                        FROM cardcopy_sub a
                                                        JOIN users b ON a.Sub_by = b.email
                                                        ORDER BY a.id DESC
                                                            ";
											$query = $dbh->prepare($sql);
											$query->execute();
											$results = $query->fetchAll(PDO::FETCH_OBJ);
											$cnt = 1;
											if ($query->rowCount() > 0) {
												foreach ($results as $result) {				?>
													<tr>
														<td><?php echo htmlentities($cnt); ?></td>
														<td><?php echo htmlentities($result->sub_type); ?></td>
														<td><?php echo htmlentities($result->Name); ?></td>
														<td><?php echo htmlentities($result->nfvb); ?></td>
														<td><?php echo htmlentities($result->Phone_number); ?></td>
														<td><?php echo htmlentities($result->Father_nid); ?></td>
														<td><?php echo htmlentities($result->Mother_nid); ?></td>
														<td><?php echo htmlentities($result->Sub_name); ?>
														<td><?php 
														        if($result->status == 2){
														            echo '<p class="text-success">Approved</p>';
														        }
														        else if($result->status == 1){
														            echo '<p class="text-warning">Pending</p>';
														        }
														        
														        else if($result->status == 3){
														            echo '<p class="text-danger">Canceled</p>';
														        }
														        
														?>
														</td>
														</td>
														
														<td>
															<?php echo htmlentities($result->sub_at); ?>

														</td>

														<td>
														    <?php
														    if($result->status == 2){
														        $ttt = "Do you want to Edit";
														            echo '<a href="card_sub_edit.php?edit='.$result->id.'">&nbsp; <i class="fa-solid fa-pen-to-square"></i></a>&nbsp;&nbsp;';
														            echo '<a href="card_view_pdf.php?file='.$result->sign_copy.'&fileid='.$result->id.'">&nbsp; <i class="fa-solid fa-eye"></i></a>&nbsp;&nbsp;';
														        }
															
														    else if($result->status == 1){
														        $ttt = "Do you want to Approve";
														            echo '<a href="card_sub_edit.php?edit='.$result->id.'" onclick="return confirm('.$ttt.');">&nbsp; <i class="fa-solid fa-circle-check text-success"></i></a>&nbsp;&nbsp;';
														            echo '<a href="#" onclick="confirmAndModifyLink('.$result->id.')"><i class="fa-solid fa-circle-xmark text-danger"></i></a>';
														        }
															
															?>
															

															
														</td>


														
													</tr>
											<?php $cnt = $cnt + 1;
												}
											} ?>

										</tbody>
									</table>
								</div>
							</div>
						</div>
					</div>

				</div>
			</div>
		</div>

		<!-- Loading Scripts -->
		<script src="js/jquery.min.js"></script>
		<script src="js/bootstrap-select.min.js"></script>
		<script src="js/bootstrap.min.js"></script>
		<script src="js/main.js"></script>
		<script type="text/javascript">
		let table = new DataTable('#myTable');
			$(document).ready(function() {
				setTimeout(function() {
					$('.succWrap').slideUp("slow");
				}, 3000);
			});
			function confirmAndModifyLink(id) {
    if (confirm('Do you really want to Cancel the Account')) {
        
        var inputText = prompt('Reason Of Cancel:');
        if (inputText !== null) {
            var newHref = 'card-all.php?comment=' + encodeURIComponent(inputText) + '&confirm=' + id;
            window.location.href = newHref;
        }
    }
    // If the user cancels, do nothing
}

		</script>

	</body>

	</html>
<?php } ?>