<?php
session_start();
$key = md5(date("Y-m-d").":".$_SERVER['REMOTE_ADDR']);
if (isset($_SESSION['aW1iZXN0cmEuY29tLm15']) <> $key) {
    header("location:".$_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME']); // cara baru !
} else {
    include "_header.php";
    include "_navtop.php";
    include "_navside.php";
    include "_functions.php";
    require "conn.php";    
?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>PIN Transfer</h2>                          
                    </div>
                </div>
            <hr />
<?
    // Use null coalescing operator to avoid undefined key warnings
    $pin = isset($_GET['pin']) ? mysqli_real_escape_string($con, $_GET['pin']) : '';
    $rid = isset($_GET['rid']) ? mysqli_real_escape_string($con, $_GET['rid']) : '';

	if ((empty($pin)) && (empty($rid)) ) {
?>
                <div class="row" >

                    <div class="col-md-12 col-sm-12 col-xs-12">

                    <div class="panel panel-default">
                        <div class="panel-heading">
                           Available PIN to transfer
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>PIN No</th>
                                            <th>Generate Date / Time</th>
                                            <th>User Account</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
<?php
 if ($_SESSION['username'] == "admin") { 
            $sql = "SELECT * FROM bestpin WHERE status='0'";
 }  else {
            $sql = "SELECT * FROM bestpin WHERE userid = '".$_SESSION['aidi']."' AND status ='0' ";
 }            
            $result = mysqli_query($con, $sql);
            $c = 1;
            while ($row = mysqli_fetch_array($result)) {
?>
                                        <tr>
                                            <td><?= $c++ ?></td>
                                            <td><?= $row[1] ?></td>
                                            <td><?= $row[4] ?></td>
                                            <td>
<?php
                                                    $sql_user       = "SELECT username FROM ahli WHERE id='".$row[2]."' ";
                                                    $result_user    = mysqli_query($con, $sql_user);

                                                    while ($row_user = mysqli_fetch_array($result_user)) {
                                                        echo '<code>'.$row_user[0].'</code>';
                                                    }
?>                                            
                                            </td>
                                            <td><a href="pin_transfer.php?pin=<?= $row[1] ?>&rid=<?= $row[0] ?>" class="btn btn-success btn-xs">Transfer <i style="padding-left:10px;" class="fa fa-arrow-right-from-bracket fa-1x"></i></a></td>
                                        </tr>
<?          } 
 
?>                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>

                <hr />

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

                    <!-- Advanced Tables -->
                    <div class="panel panel-default">

                        <div class="panel-heading">
                             PIN Transfer History
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables-example">

                                    <thead>
                                        <tr>
                                            <th>#               </th>
                                            <th>PIN Number      </th>
                                            <th>Date Generate   </th>
                                            <th>Date Transfer   </th>
                                            <th>User Account    </th>
                                            <th>Status          </th>
                                        </tr>
                                    </thead>
                                    <tbody>

<?

$c = 1;  



 if ($_SESSION['username'] == "admin") { 
             $sql = "SELECT * FROM bestpinlog WHERE status='2' ORDER BY no, dateactive DESC";
 }  else {
            $sql = "SELECT * FROM bestpinlog WHERE userid = '".$_SESSION['aidi']."' AND status='2' ORDER BY no, dateactive DESC";
 }

    $result = mysqli_query($con, $sql);



    while ($row = mysqli_fetch_array($result)) {

?>

                                        <tr class="even">

                                            <td><?= $c++ ?>
                                            <td><?= $row[1] ?></td>
                                            <td><?= $row[4] ?></td>
                                            <td><?= $row[5] ?></td>
                                            <td>
<?php
                                                    $sql_user       = "SELECT username FROM ahli WHERE id='".$row[2]."' ";
                                                    $result_user    = mysqli_query($con, $sql_user);

                                                    while ($row_user = mysqli_fetch_array($result_user)) {
                                                        echo '<code>'.$row_user[0].'</code>';
                                                    }
?>                                            
                                            </td>
                                            <td><?php 
                                            $stat = $row[7];
                                            //echo $stat;
                                                    if      ($stat == 0             ) { echo "<span class='label label-success' >Available      </span>";      }
                                                    elseif  ($stat == 1             ) { echo "<span class='label label-danger ' >Used           </span>";      }
                                                    elseif  ($stat == 2             ) { echo "<span class='label label-info '   >Transfered     </span>";      }
                                                    else                              { echo "<span class='label label-warning' >Suspend        </span>";      }
                                                    
                                                ?>
                                            </td>

                                        </tr>

 <? } ?>

                                    </tbody>

                                </table>

                            </div>

                            

                        </div>

                    </div>

                    <!--End Advanced Tables -->

                </div>

            </div>    


                    </div>
                </div>
<?
	} else {

		if (chkPin($pin) <> 1) {
			echo '<div class="alert alert-danger">';
			echo 'Error! Wrong PIN.';
			echo '</div>';
			goto kaki;
		}
		if (chkRid($rid) <> 1) {
			echo '<div class="alert alert-danger">';
			echo 'Error! Wrong Record ID.';
			echo '</div>';
			goto kaki;
		}
		
?>
			<div class="row">
                <div class="col-md-12">
                    <!-- Form Elements -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            PIN Transfer
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <form role="form" method="post" action="transfer.php">

                                        <div class="form-group">
                                            <label>Username</label>
                                            <input class="form-control" name="x" required />
                                        </div>
                                        <div class="form-group">
                                            <label>PIN</label>
                                            <input class="form-control" name="y" value="<?= $pin ?>" readonly />
                                            <input class="form-control" name="w" value="<?= $rid ?>" type="hidden" />
                                        </div>

                                        <button type="submit" name="z" class="btn btn-primary">Submit</button>

                                    </form>

                                </div>
                            </div>
                        </div>
                    </div>
                     <!-- End Form Elements -->
                </div>

			</div>
<?
	}	
?>

             <!-- /. PAGE INNER  -->
<? kaki: ?>			 
            </div>
         <!-- /. PAGE WRAPPER  -->
        </div>
<?
    include "_footer.php";
}
?>
