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

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

$page = "Accounts";
$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['mngUsr']) && $_POST['mngUsr'] == 'mngUsr')
{
    $userno = clean_data($_POST['userNo']);
    redirect(PROFILE.'?user='.$userno);
}


if (isset($_POST['delUsr']) && $_POST['delUsr'] == 'delUsr')
{
    $userno = clean_data($_POST['userNo']);
    if (delete_user($userno)) {
        delete_useracc($userno);
        delete_usertrans($userno);
        delete_usertranf($userno);
		log_info($_SERVER['REMOTE_ADDR'], $_SESSION['admin_login'], $userno, 'Account Successfully Deleted');
		store_msg("Account Successfully Deleted", "success"); redirect(ACCOUNTS);
	}else{
    store_msg("Account 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">


          <div class="box">
            <div class="box-header">
              <h2>Accounts</h2>
              <small>List of all existing accounts</small>
            </div>
            <div class="table-responsive">
              <table ui-jp="dataTable" class="table table-striped b-t b-b">
                <thead>
                    <tr>
                      <th>User ID</th>
            		  <th>Full Name</th>
            		  <th>Email</th>
                      <th>Telephone</th>
            		  <th>Manage</th>
                      <th>Edit</th>
            		  <th>Delete</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
        			    dbconnect();
        			    $sql = "SELECT * FROM `".USERS_TABLE_NAME."` WHERE user_status = 1 ORDER BY user_id DESC";
        			    $stmt = $pdo->query($sql);
                        if ($stmt->rowCount() > 0) {
        			    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        			?>
                        <tr>
                        <td><?php echo $row['user_login'] ?></td>
                        <td><?php echo $row['user_names'] ?></td>
                        <td><?php echo $row['user_email'] ?></td>
                        <td><?php echo $row['user_phone'] ?></td>
                        <td>
                            <form id="usrMng" method="post" action="">
                                <input type="hidden" name="userNo" value="<?php echo $row['user_number'] ?>" />
                                <button type="submit" name="mngUsr" value="mngUsr" class="btn btn-sm rounded btn-outline b-info text-info">Manage</button>
                            </form>
                        </td>
                        <td>
                            <form id="usrEdt" method="post" action="<?php echo EDIT ?>" onsubmit="return confirm('Are you sure you want to edit this account?');">
                                <input type="hidden" name="userNo" value="<?php echo $row['user_number'] ?>" />
                                <button type="submit" name="edtUsr" value="edtUsr" class="btn btn-sm rounded btn-outline b-info text-info">Edit</button>
                            </form>
                        </td>
                        <td>
                            <form id="usrDel" method="post" action="" onsubmit="return confirm('Are you sure you want to delete this account?');">
                                <input type="hidden" name="userNo" value="<?php echo $row['user_number'] ?>" />
                                <button type="submit" name="delUsr" value="delUsr" class="btn btn-sm rounded btn-outline b-danger text-danger">Delete</button>
                            </form>
                        </td>

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

                    	';
                    	}
                        ?>
                  </tbody>
                </table>
            </div>
          </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>
