<?php
session_start();
require_once('../config/config.php');
require_once('../class/functions.php');
dbconnect();

if (!is_admin()) {
    redirect(LOGIN);
}

$page = "Dashboard";
$geo = ip2location($_SERVER['REMOTE_ADDR']);
$country = $geo->country;
$city = $geo->city;
visit_log($_SERVER['REMOTE_ADDR'], $country, $city, $_SERVER['HTTP_USER_AGENT'], getBrowser($_SERVER['HTTP_USER_AGENT']), getOS($_SERVER['HTTP_USER_AGENT']), $page, $_SESSION['admin_login']);


if (isset($_POST['delUsr']) && $_POST['delUsr'] == 'delUsr')
{
    $userno = clean_data($_POST['userNo']);
    if (delete_user($userno)) {
		log_info($_SERVER['REMOTE_ADDR'], $_SESSION['admin_login'], $userno, 'Application Successfully Deleted');
		store_msg("Application Successfully Deleted", "success"); redirect(DASHBOARD);
	}else{
    store_msg("Application Delete Error", "danger");
    }
}

require_once('head.php'); 
?>

<body>
  <div class="app" id="app">

<!-- ############ LAYOUT START-->
  <!-- aside -->
  <?php require_once('aside.php'); ?>
  <!-- / aside -->



  <!-- content -->
  <div id="content" class="app-content box-shadow-z3" role="main">

    <?php require_once('header.php'); ?>

    <?php require_once('footer.php'); ?>

    <div ui-view class="app-body" id="view">
    <!-- ############ PAGE START-->
    <div class="item">
        <div class="item-bg">
          <img src="../users/user.png" class="blur opacity-3">
        </div>
        <div class="p-a-md">
          <div class="row m-t">
            <div class="col-sm-10">
              <a href class="pull-left m-r-md">
                <span class="avatar w-96">
                  <img src="../users/user.png">
                  <i class="on b-white"></i>
                </span>
              </a>
              <div class="clear m-b">
                <p class="text-md profile-status">Welcome</p>
                <h3 class="m-a-0 m-b-xs">Administrator</h3>
                <p class="text-muted">
                    <?php
                    $lastlogin = alast_login($_SESSION['admin_login']);
                    foreach($lastlogin as $last){
                        if ($last['last_ts'] == '0000-00-00 00:00:00') {
                            echo '';
                        }else{
                            $lastdate = date('l jS F Y', strtotime($last['last_ts']));
                            $lastip = $last['last_ip'];
                            echo'<span class="m-r">Last login: <strong>'.$lastdate.'</strong></span>';
                            echo' From: <small><strong><i class="fa fa-map-marker m-r-xs"></i>'.$lastip.'</strong> </small><br>';
                            echo' <a href="http://api.hostip.info/get_html.php?ip='.$lastip.'" target="_blank"><img src="http://api.hostip.info/flag.php?ip='.$lastip.'" alt="Country" width="39" height="24"></a> ';
                        }
                    } ?>
                </p>
              </div>
            </div>
          </div>
        </div>
    </div>


    <?php if (!empty($_SESSION['msg_text'])) {
    $msg = create_alert($_SESSION['msg_text'], $_SESSION['msg_type']);
    echo $msg;
    clear_msg(); }
    ?>


    <div class="padding">


      <div class="row">
        <div class="col-sm-12 col-md-12">
          <div class="box-header">
              <h2 class="m-a-0 m-b-sm text-lg">Unread Messages</h2>
              <small>Unread messages from account holders</small>
          </div>
          <div class="m-b" id="accordion">
            <?php
			$sql = "SELECT * FROM `".MSGS_TABLE_NAME."` WHERE msg_rcvr = '{$_SESSION['admin_login']}' OR msg_sender = '{$_SESSION['admin_login']}' AND adm_read = 0 ORDER BY msg_date DESC";
			$stmt = $pdo->query($sql);
            if ($stmt->rowCount() > 0) {
			while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
		    ?>
            <div class="panel box no-border m-b-xs" style="background-color:<?php if ($row['adm_read'] == "0") { echo '#d9edf7'; }else{ echo "";} ?>;">
              <div class="box-header p-y-sm">
                <span class="pull-right label text-sm info"><?php echo date('jS F Y', strtotime($row['msg_date'])); ?></span>
                <a data-toggle="collapse" data-parent="#accordion" data-target="#c_<?php echo $row['msg_id']; ?>">
                  <strong><?php echo $row['msg_subject']; ?></strong>
                </a>
              </div>
              <div id="c_<?php echo $row['msg_id']; ?>" class="collapse">
                <div class="box-header">
                  <span class="pull-left label text-sm success">From: <?php if ($row['msg_sender'] == $_SESSION['admin_login']) { echo "You"; }else{ echo $row['sender_name'];} ?></span>
                </div>
                <div class="box-body">
                  <p>
                  <?php echo $row['msg_cnt']; ?>
                  </p>

                  	<div class="streamline b-l m-l">
                        <div class="sl-item b-success">
                          <div class="sl-icon">
                            <i class="fa fa-share"></i>
                          </div>
                          <div class="sl-content">
                            <div><strong>Replies</strong></div>
                          </div>
                        </div>
                        <?php
                        $reply = get_replies($row['msg_id']);
                        if (empty($reply)) {
                        echo '<div class="text-muted">No replies added yet</div>';
                        }else{
                        foreach($reply as $rep)
                        {
                        ?>
                        <div class="sl-item <?php if ($rep['rply_sender'] == $_SESSION['admin_login']) { echo
					  "b-info"; } else{ echo "b-danger";} ?>">
                          <div class="sl-content">
                            <div class="sl-date text-muted">Replied by: <strong><?php if ($rep['rply_sender'] == $_SESSION['admin_login']) { echo "You"; } else{ echo $rep['sender_name'];} ?></strong> on <strong><?php if ($rep['rply_date'] == "") { echo ""; }else{ echo date('jS F Y', strtotime($rep['rply_date']));} ?></strong></div>
                            <div><?php echo $rep['rply_cnt']; ?></div>
                          </div>
                        </div>
                        <?php
                        }}
                        ?>
                    </div>

    			  	<div class="box-footer">
    			  		<a href="<?php echo MESSAGES ?>" class="btn btn-sm rounded btn-outline b-info text-info">View All Messages</a>
    			  	</div>
                </div>
              </div>
            </div>
            <?php
            }
            }else{
                echo '<div class="text-muted">No unread messages</div>';
            } ?>
          </div>
        </div>
      </div>



      <div class="row">
        <div class="col-sm-12">
          <div class="box">
            <div class="box-header">
              <h3>Newly Submitted Applications</h3>
              <small>Find below newly submitted applications click on the activate button to activate them</small>
            </div>
            <div class="table-responsive">
                <table class="table">
                  <thead>
                    <tr>
                      <th>Date</th>
            		  <th>Full Name</th>
            		  <th>Email</th>
            		  <th>View</th>
            		  <th>Activate</th>
            		  <th>Delete</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
        			    dbconnect();
        			    $sql = "SELECT * FROM `".USERS_TABLE_NAME."` WHERE user_status= 0 ORDER BY user_id DESC";
        			    $stmt = $pdo->query($sql);
                        if ($stmt->rowCount() > 0) {
        			    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        			?>
                        <tr>
                        <td><?php echo date('jS F Y', strtotime($row['user_registered'])) ?></td>
                        <td><?php echo $row['user_names']; ?></td>
                        <td><?php echo $row['user_email']; ?></td>
                        <td><button class="btn btn-sm rounded btn-outline b-info text-info" data-toggle="modal" data-target="#viewUser">View</button></td>
                        <td><a href="<?php echo ACTIVATE ?>?user=<?php echo $row['user_id']; ?>" class="btn btn-sm rounded btn-outline b-success text-success">Activate</a></td>
                        <td>
                            <form id="usrDel" method="post" action="" onsubmit="return confirm('Are you sure you want to delete this application?');">
                                <input type="hidden" name="userNo" value="<?php echo $row['user_id']; ?>" />
                                <button type="submit" name="delUsr" value="delUsr" class="btn btn-sm rounded btn-outline b-danger text-danger">Delete</button>
                            </form>
                        </td>

                        <!-- view user modal -->
                        <div id="viewUser" class="modal" data-backdrop="true">
                          <div class="modal-dialog">
                            <div class="modal-content">
                              <div class="modal-header">
                              	<h5 class="modal-title">Application Details</h5>
                              </div>
                              <div class="modal-body p-lg">
                                <div class="col-sm-12 col-lg-12">
                                    <h6>Personal Information</h6>
                                    <div class="row m-b">
                                      <div class="col-xs-6">
                                        <small class="text-muted">First Name</small>
                                        <div class="_500"><?php echo $row['first_name']; ?></div>
                                      </div>
                                      <div class="col-xs-6">
                                        <small class="text-muted">Last Name</small>
                                        <div class="_500"><?php echo $row['last_name']; ?></div>
                                      </div>
                                    </div>
                                    <div class="row m-b">
                                      <div class="col-xs-6">
                                        <small class="text-muted">Date of Birth</small>
                                        <div class="_500"><?php if ($row['dob'] == "0000-00-00") { echo ""; }else{ echo date('jS F Y', strtotime($row['dob']));} ?></div>
                                      </div>
                                      <div class="col-xs-6">
                                        <small class="text-muted">Gender</small>
                                        <div class="_500"><?php echo $row['user_gender']; ?></div>
                                      </div>
                                    </div>
                                    <div class="row m-b">
                                      <div class="col-xs-6">
                                        <small class="text-muted">Email</small>
                                        <div class="_500"><?php echo $row['user_email']; ?></div>
                                      </div>
                                      <div class="col-xs-6">
                                        <small class="text-muted">Mobile</small>
                                        <div class="_500"><?php echo $row['user_phone']; ?></div>
                                      </div>
                                    </div>
                                    <div class="row m-b">
                                      <div class="col-xs-12">
                                        <small class="text-muted">Address</small>
                                        <div class="_500"><?php echo $row['user_add']; ?></div>
                                      </div>
                                    </div>
                                    <div class="row m-b">
                                      <div class="col-xs-6">
                                        <small class="text-muted">City</small>
                                        <div class="_500"><?php echo $row['user_city']; ?></div>
                                      </div>
                                      <div class="col-xs-6">
                                        <small class="text-muted">State</small>
                                        <div class="_500"><?php echo $row['user_state']; ?></div>
                                      </div>
                                    </div>
                                    <div class="row m-b">
                                      <div class="col-xs-6">
                                        <small class="text-muted">Zip Code</small>
                                        <div class="_500"><?php echo $row['postcode']; ?></div>
                                      </div>
                                      <div class="col-xs-6">
                                        <small class="text-muted">Country</small>
                                        <div class="_500"><?php echo $row['country']; ?></div>
                                      </div>
                                    </div>
                                    <h6>Personal Identification</h6>
                                    <div class="row m-b">
                                      <div class="col-xs-6">
                                        <small class="text-muted">ID Type</small>
                                        <div class="_500"><?php echo $row['id_type']; ?></div>
                                      </div>
                                      <div class="col-xs-6">
                                        <small class="text-muted">ID Number</small>
                                        <div class="_500"><?php echo $row['id_number']; ?></div>
                                      </div>
                                    </div>
                                    <div class="row m-b">
                                      <div class="col-xs-6">
                                        <small class="text-muted">Expiry Date</small>
                                        <div class="_500"><?php if ($row['id_expiry'] == "") { echo ""; }else{ echo date('jS F Y', strtotime($row['id_expiry']));} ?></div>
                                      </div>
                                      <div class="col-xs-6">
                                        <small class="text-muted">Application Submitted</small>
                                        <div class="_500"><?php if ($row['user_registered'] == "0000-00-00 00:00:00") { echo ""; }else{ echo date('jS F Y', strtotime($row['user_registered']));} ?></div>
                                      </div>
                                    </div>
                              </div>
                              </div>
                              <div class="modal-footer">
                                <button type="button" class="btn rounded btn-outline b-info text-info" data-dismiss="modal">Close</button>
                              </div>
                            </div><!-- /.modal-content -->
                          </div>
                        </div>
                        <!-- / .modal -->


                        <!-- view user modal -->
                        <div id="actUser" class="modal" data-backdrop="true">
                          <div class="modal-dialog">
                          <form id="ActForm" method="post" action="" enctype="multipart/form-data" ui-jp="parsley">
                            <div class="modal-content">
                              <div class="modal-header">
                              	<h5 class="modal-title">Activate Application</h5>
                                <small>Complete and submit form to activate account, change codes if you wish</small>
                              </div>
                              <div class="modal-body p-lg">
                                <div class="row row-sm">
                                    <div class="col-sm-12">
                                    <div class="md-form-group">
                                      <input type="text" value="<?php echo get_userid($row['user_id']); ?>" name="usrId" class="md-input" data-parsley-error-message="User ID is required" required>
                                      <label>User ID</label>
                                    </div>
                                  </div>
                                </div>
                                <div class="row row-sm">
                                  <div class="col-sm-6">
                                    <div class="md-form-group">
                                      <input type="text" value="<?php echo generateStrongPassword(); ?>" name="usrPs" class="md-input" data-parsley-error-message="Password is required" required>
                                      <label>Password</label>
                                    </div>
                                  </div>
                                  <div class="col-sm-6">
                                    <div class="md-form-group">
                                      <input type="text" value="<?php echo get_pin(4); ?>" name="usrPn" class="md-input" data-parsley-error-message="PIN is required" required>
                                      <label>PIN</label>
                                    </div>
                                  </div>
                                </div>
                                <div class="row row-sm">
                                  <div class="col-sm-6">
                                    <div class="md-form-group">
                                      <input type="text" value="<?php echo account_number(); ?>" name="usrAc" class="md-input" data-parsley-error-message="Account Number is required" required>
                                      <label>Account Number</label>
                                    </div>
                                  </div>
                                  <?php
            					  if (CODE1_TRUE) { echo '
                                  <div class="col-sm-6">
                                    <div class="md-form-group">
                                      <input type="text" value="'.getRandomString(6).'" name="usrCd1" class="md-input" data-parsley-error-message="'.CODE1_NAME.' is required" required>
                                      <label>'.CODE1_NAME.'</label>
                                    </div>
                                  </div>
                                  ';}else{ echo '';} ?>
                                </div>
                                <div class="row row-sm">
                                  <?php
            					  if (CODE2_TRUE) { echo '
                                  <div class="col-sm-6">
                                    <div class="md-form-group">
                                      <input type="text" value="'.getRandomString(9).'" name="usrCd2" class="md-input" data-parsley-error-message="'.CODE2_NAME.' is required" required>
                                      <label>'.CODE2_NAME.'</label>
                                    </div>
                                  </div>
                                  ';}else{ echo '';} ?>
                                  <?php
            					  if (CODE3_TRUE) { echo '
                                  <div class="col-sm-6">
                                    <div class="md-form-group">
                                      <input type="text" value="'.getRandomString(13).'" name="usrCd3" class="md-input" data-parsley-error-message="'.CODE3_NAME.' is required" required>
                                      <label>'.CODE3_NAME.'</label>
                                    </div>
                                  </div>
                                  ';}else{ echo '';} ?>
                                </div>
                                <div class="row row-sm">
                                  <div class="col-sm-6">
                                    <div class="md-form-group">
                                      <select name="accSts" class="md-input" data-parsley-error-message="Select Account Status" required>
                                        <option value="">Select Account Status</option>
                                        <option value="active">Active</option>
            							<option value="inactive">Inactive</option>
                                        <option value="on-hold">On Hold</option>
                                        <option value="suspended">Suspended</option>
            							<option value="dormant">Dormant</option>
                                      </select>
                                      <label>Select Account Status</label>
                                    </div>
                                  </div>
                                  <div class="col-sm-6">
                                    <div class="md-form-group">
                                      <input type="text" name="acctyp" class="md-input" data-parsley-error-message="Account Type is required" required>
                                      <label>Account Type</label>
                                    </div>
                                  </div>
                                </div>
                                <div class="row row-sm">
                                  <div class="col-sm-6">
                                    <div class="md-form-group">
                                      <select name="accCur" class="md-input" data-parsley-error-message="Select Currency" required>
                                        <option value="">Select Currency</option>
                                        <?php
										foreach($currency as $Cur)
										{
										echo '<option value="'. $Cur .'">'. $Cur .'</option>';
										}
    									?>
                                      </select>
                                      <label>Currency</label>
                                    </div>
                                  </div>
                                  <div class="col-sm-6">
                                    <?php if(empty(user_photo(get_login($row['user_id'])))) {
                                        echo '
                                        <div class="md-form-group">
                                          <input type="file" name="photo" class="md-input">
                                          <label>Upload your photo</label>
                                        </div>
                                        ';
                                    }  ?>
                                  </div>
                                </div>
                                <div class="row row-sm">
                                  <div class="col-sm-6">
                                    <label class="md-check">
                                        <input type="checkbox" name="sndEml">
                                        <i class="indigo"></i>
                                        Send Details as Email
                                    </label>
                                  </div>
                                  <?php
								    if (USE_SMS) { echo '
                                  <div class="col-sm-6">
                                    <label class="md-check">
                                        <input type="checkbox" name="sndSms">
                                        <i class="indigo"></i>
                                        Send Details as SMS
                                    </label>
                                  </div>
                                  ';}else{ echo '';}
								?>
                              </div>
                              </div>
                              <div class="modal-footer">
                                <input type="hidden" name="usrNo" value="<?php echo $row['user_id'] ?>">
                                <button type="submit" name="accAct" value="accAct" class="btn rounded btn-outline b-success text-success p-x-md">Activate</button>
                                <button type="button" class="btn rounded btn-outline b-danger text-danger p-x-md" data-dismiss="modal">Cancel</button>
                              </div>
                            </div><!-- /.modal-content -->
                          </form>
                          </div>
                          </div>
                        </div>
                        <!-- / .modal -->

                        </tr>
                    <?php
                	}
                	}else{
                	echo '<tr>';
                	echo '<td colspan="6">No account registered yet</td>';
                	echo '</tr>';
                	echo '

                	';
                	}
                    ?>
                  </tbody>
                </table>
            </div>
          </div>
        </div>
      </div>

	<div class="row">
	    <div class="col-sm-6 col-md-8">
			<div class="box">
				<div class="box-header">
					<h3>Recent Website Activity</h3>
				</div>
				<div class="box-body">
				  	<div class="streamline b-l m-l">
				        <div class="sl-item b-success">
				          <div class="sl-icon">
				            <i class="fa fa-wifi"></i>
				          </div>
				          <div class="sl-content">
				            <div class="sl-date text-muted">Recent activities on the website</div>
				          </div>
				        </div>
                        <?php
                        $recentlogs = recent_activity();
                        if (empty($recentlogs)) {
                            echo '<div>No activity yet</div>';
                            }else{
        				foreach($recentlogs as $logs)
        				{
        				echo '
				        <div class="sl-item b-warning">
				          <div class="sl-content">
				            <div class="sl-date text-muted">'.date('jS F Y', strtotime($logs['log_date'])).'</div>
				            <div>'.$logs['log_details'].'</div>
				          </div>
				        </div>
                        ';
            			}
                            }
            			?>
				    </div>
				</div>
			  	<div class="box-footer">
			  		<a href="<?php echo LOGS ?>" class="btn btn-sm rounded btn-outline b-info text-info">All Activities</a>
			  	</div>
		  	</div>
		</div>

	    <div class="col-sm-6 col-md-4">
	        <div class="box">
	            <div class="box-header">
	              <h3>Account Holders Online</h3>
	            </div>
	            <ul class="list no-border p-b">
	              <?php
                  $usersonline = users_online();
                    if (empty($usersonline)) {
                        echo '<li class="list-item"><p class="text-muted">No user online</p></li>';
                        }else{
    				foreach($usersonline as $user)
    				{
    				echo '
	              <li class="list-item">
	                <a href="'.ACTIVITY.'?user='.$user['user_number'].'" class="list-left">
	                	<span class="w-40 avatar">
		                  <img src="../users/'.$user['user_photo'].'" alt="Online">
		                  <i class="on b-white bottom"></i>
	                    </span>
	                </a>
	                <div class="list-body">
	                  <div><a href="'.ACTIVITY.'?user='.$user['user_number'].'">'.$user['user_login'].'</a></div>
	                  <small class="text-muted text-ellipsis">'.$user['user_names'].'</small>
	                </div>
	              </li>
                    ';
        			}
                        }
        			?>
	            </ul>
	        </div>
	    </div>
	</div>

    </div>


    <!-- ############ PAGE END-->
    </div>



    </div>
<!-- ############ LAYOUT END-->

  </div>
<!-- build:js scripts/app.html.js -->
<!-- jQuery -->
  <script src="../libs/jquery/jquery/dist/jquery.js"></script>

<!-- Bootstrap -->
  <script src="../libs/jquery/tether/dist/js/tether.min.js"></script>
  <script src="../libs/jquery/bootstrap/dist/js/bootstrap.js"></script>
<!-- core -->
  <script src="../libs/jquery/underscore/underscore-min.js"></script>
  <script src="../libs/jquery/jQuery-Storage-API/jquery.storageapi.min.js"></script>
  <script src="../libs/jquery/PACE/pace.min.js"></script>

  <script src="../scripts/config.lazyload2.js"></script>

  <script src="../scripts/palette.js"></script>
  <script src="../scripts/ui-load.js"></script>
  <script src="../scripts/ui-jp.js"></script>
  <script src="../scripts/ui-include.js"></script>
  <script src="../scripts/ui-device.js"></script>
  <script src="../scripts/ui-form.js"></script>
  <script src="../scripts/ui-nav.js"></script>
  <script src="../scripts/ui-screenfull2.js"></script>
  <script src="../scripts/ui-scroll-to.js"></script>
  <script src="../scripts/ui-toggle-class.js"></script>

  <script src="../scripts/app.js"></script>

  <!-- ajax -->
  <script src="../libs/jquery/jquery-pjax/jquery.pjax.js"></script>
  <script src="../scripts/ajax.js"></script>
<!-- endbuild -->
</body>
</html>
