<?php
session_start();
include_once('connection.php');
if($_COOKIE["adminLogAdmin"] == ''){
	header("Location: login.php");
	exit();
}else{}

$qrys_all = mysqli_query($conn,"SELECT `id` FROM `member` ORDER BY `added_on` ASC");
$cunt_all = mysqli_num_rows($qrys_all);

$qrys_form = mysqli_query($conn,"SELECT `id` FROM `member` WHERE `payment_status` = 'success' ORDER BY `added_on` ASC");
$cunt_form = mysqli_num_rows($qrys_form);

$qrys_bid = mysqli_query($conn,"SELECT `id` FROM `member`  WHERE `bidsubmit` = '1' ORDER BY `added_on` ASC");
$cunt_bid = mysqli_num_rows($qrys_bid);

$qrys_pauconfirm = mysqli_query($conn,"SELECT `id` FROM `member` WHERE `earnest_amount_payment_status` = '1' ORDER BY `added_on` ASC");
$cunt_pauconfirm = mysqli_num_rows($qrys_pauconfirm);


$queryas  = mysqli_query($conn,"SELECT * FROM `bid_option` WHERE `id` = '1'");
$rowhas = mysqli_fetch_array($queryas);
$statuss 		= $rowhas['status'];
if($statuss == '1'){
	$bidstatusDisplay = "Open";
}else{
	$bidstatusDisplay = "Close";
}

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
        <meta name="description" content="" />
        <meta name="author" content="" />
        <title>Dashboard</title>
        <link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" />
        <link href="css/styles.css" rel="stylesheet" />
        <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js" crossorigin="anonymous"></script>
    </head>
    <body class="sb-nav-fixed">
       <?php include('include/header.php'); ?>
        <div id="layoutSidenav">
            <?php include('include/sidebar.php'); ?>
            <div id="layoutSidenav_content">
                <main>
                    <div class="container-fluid px-4">
                       
<?php
$action = $_GET['action'];
if($action == 'all'){
	$qry = "SELECT * FROM `member` ORDER BY `added_on` ASC";
	$tst = "All Applications";
}elseif($action == 'form'){
	$qry = "SELECT * FROM `member` WHERE `payment_status` = 'success' ORDER BY `added_on` ASC";
	$tst = "Form Downloaded";
}elseif($action == 'bids'){
	$qry = "SELECT * FROM `member` WHERE `bidsubmit` = '1' ORDER BY `added_on` ASC";
	$tst = "Bid Submitted";
}elseif($action == 'conf'){
	$qry = "SELECT * FROM `member` WHERE `earnest_amount_payment_status` = '1' ORDER BY `added_on` ASC";
	$tst = "Earnest Money Confirmed";
}else{
	$qry = "SELECT * FROM `member` ORDER BY `added_on` ASC";
	$tst = "All Bids";
}
?>
					<?php if(empty($_GET['action'])) { ?>

					   <h1 class="mt-4">Dashboard</h1>
                        <ol class="breadcrumb mb-4">
                            <li class="breadcrumb-item active">Dashboard</li>
							<li class="breadcrumb-item active">Bid Status <?=$bidstatusDisplay;?></li>
                        </ol>
                        <div class="row">
                            <div class="col-xl-3 col-md-6">
                                <div class="card bg-primary text-white mb-4">
                                    <div class="card-body">Total <span class="float-end"><strong><?=$cunt_all;?></strong></span></div>
                                    <div class="card-footer d-flex align-items-center justify-content-between">
                                        <a class="small text-white stretched-link" href="index.php?action=all">View Details</a>
                                        <div class="small text-white"><i class="fas fa-angle-right"></i></div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-xl-3 col-md-6">
                                <div class="card bg-warning text-white mb-4">
                                    <div class="card-body">Form Downloaded <span class="float-end"><strong><?=$cunt_form;?></strong></span></div>
                                    <div class="card-footer d-flex align-items-center justify-content-between">
                                        <a class="small text-white stretched-link" href="index.php?action=form">View Details</a>
                                        <div class="small text-white"><i class="fas fa-angle-right"></i></div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-xl-3 col-md-6">
                                <div class="card bg-success text-white mb-4">
                                    <div class="card-body">Bid Submitted <span class="float-end"><strong><?=$cunt_bid;?></strong></span></div>
                                    <div class="card-footer d-flex align-items-center justify-content-between">
                                        <a class="small text-white stretched-link" href="index.php?action=bids">View Details</a>
                                        <div class="small text-white"><i class="fas fa-angle-right"></i></div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-xl-3 col-md-6">
                                <div class="card bg-info text-white mb-4">
                                    <div class="card-body">Earnest Money Confirmed <span class="float-end"><strong><?=$cunt_pauconfirm;?></strong></span></div>
                                    <div class="card-footer d-flex align-items-center justify-content-between">
                                        <a class="small text-white stretched-link" href="index.php?action=conf">View Details</a>
                                        <div class="small text-white"><i class="fas fa-angle-right"></i></div>
                                    </div>
                                </div>
                            </div>
                        </div>
						
					<?php }else{ ?>	
						
						<h1 class="mt-4"><?=$tst;?></h1>
                        <ol class="breadcrumb mb-4">
                            <li class="breadcrumb-item active"><?=$tst;?></li>
							<li class="breadcrumb-item active">Bid Status <?=$bidstatusDisplay;?></li>
                        </ol>
						
					<?php } ?>
                       



                        <div class="card mb-4">
                            <div class="card-header">
                                <i class="fas fa-table me-1"></i>
                               <?=$tst;?>
                            </div>
                            <div class="card-body">
                                <table id="datatablesSimple">
                                    <thead>
                                        <tr>
                                            <th>Name</th>
                                            <th>Email</th>
                                            <th>Contact No</th>
                                            <th>Bid Amount</th>
											<th>Applied On</th>
                                            <th>Payment Status</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    
                                    <tbody>
                                       
<?php

$querya  = mysqli_query($conn,$qry);
		while($rowha = mysqli_fetch_array($querya)) {
			
			$stlid = $rowha['id'];
			$stlidEnCode = base64_encode($stlid);
			
			$name 			= $rowha['name'];
			$email 			= $rowha['email'];
			$contact 		= $rowha['contact'];
			$bid_amount 	= $rowha['bid_amount'];
			$earnest_amount_payment_status 			= $rowha['earnest_amount_payment_status'];
			$bidsubmit 			= $rowha['bidsubmit'];
			$payment_status 			= $rowha['payment_status'];
			$paymentdata 			= $rowha['paymentdata'];
			
			
			$winner 				= $rowha['winner'];
			$winner_on 			= $rowha['winner_on'];
			if($winner_on != ''){
				$winner_on 			= date('d/m/Y',$winner_on);
			}else{
				$winner_on = '';
			}
			
			$added_on 			= $rowha['added_on'];
			$added_on 			= date('d/m/Y',$added_on);

			/*
			$apply_last_date 	= date('d/m/Y',$apply_last_date);
			$tender_open_date 	= date('d/m/Y',$tender_open_date);
			$added_on 			= date('d/m/Y',$added_on);
			*/
			$statusDisplay = '';
			
			if($winner == 1){
				$statusDisplay = "Winner ($winner_on)";
			}else{
				if($earnest_amount_payment_status == '2'){
					$statusDisplay = "Earnest amount checked but not received";
				}elseif($earnest_amount_payment_status == '1'){
					$statusDisplay = "Earnest amount paid";
				}else{
					if($bidsubmit == '1'){
						$statusDisplay = "Bid Submitted But Payment not Confirmed";
					}else{
						if($payment_status == 'success'){ 
							$statusDisplay = "Form downloaded but Bid not submitted"; 
						}else{
							$statusDisplay = "None"; 
						}
					}
				}
			}
			
			
?>
									   <tr>
                                            <td><?=$name;?></td>
                                            <td><?=$email;?></td>
                                            <td><?=$contact;?></td>
                                            <td><?=$bid_amount;?></td>
											<td><?=$added_on;?></td>
											<td><?=$statusDisplay;?></td>
                                            <td>
												<a href="view.php?ids=<?=$stlidEnCode;?>" class="btn btn-sm btn-danger">View</a>
												<?php if($paymentdata == '' AND $payment_status != 'success'){ ?>
												<a href="markpaid.php?ids=<?=$stlidEnCode;?>" onclick="if (confirm('Are you sure to proceed?')){return true;}else{event.stopPropagation(); event.preventDefault();};" class="btn btn-sm btn-info">Mark Paid</a>												
												<?php } ?>
											</td>
                                        </tr>
<?php } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </main>
               <?php include('include/footer.php'); ?>
            </div>
        </div>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
        <script src="js/scripts.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" crossorigin="anonymous"></script>
        <script src="js/datatables-simple-demo.js"></script>
    </body>
</html>
