<?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 {

    if ($_SESSION['username'] <> "admin") die("<h4>You don't have right permission to access this file directly.</h4>");

    include "_header3.php";
    include "_navtop.php";
    include "_navside.php";
    include "_functions.php";
    require "conn.php";

    $pin = substr(str_shuffle("23456789ABCDEFGHJKLMNPQRSTUVWXYZ"), 0, 10);

    date_default_timezone_set("Asia/Kuala_Lumpur");
    $tarikh = date("Y-m-d H:i:s");

    if (isset($_POST['generate'])) {
        //

        $u = mysqli_real_escape_string($con, $_POST['a']);
        $qty = mysqli_real_escape_string($con, $_POST['b']);

        // periksa kewujudan user
        if (chkUsr($u) == 1) {

            // check id
            $id = reKod("id", "ahli", "username", $u);

            // janaan pin, maksima 5 pin
            for ($x = 1; $x <= $qty; $x++) {
                mysqli_query($con, "INSERT INTO bestpin (no, userid, datesold, ip) VALUES ('".randomPin()."', ".$id.", '".$tarikh."', '".$_SERVER['REMOTE_ADDR']."')");
                if ($x == $qty) {
                    $upd = true;
                }
            }
        }
    }

?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>PIN</h2>                          
                    </div>
                </div>
<? if ($upd) { ?>
    <script>
    window.setTimeout(function() {
        $(".alert").fadeTo(500, 0).slideUp(500, function(){
            $(this).remove();
        });
    }, 2000);
    </script>

    <div class="alert alert-success">
        <?= $qty." PIN created" ?>
    </div>
<? } ?>
                 <!-- /. ROW  -->
                 <hr />
               <div class="row">
                <div class="col-md-12">
                    <!-- Form Elements -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Generate PIN
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <form role="form" method="post">
                                        <div class="form-group">
                                            <label>Username</label>
                                            <input name="a" class="form-control" required />
                                        </div>                                       
                                        <div class="form-group">
                                            <label>Quantity</label>
                                            <input type="number" name="b" class="form-control" min="1" placeholder="Masukkan jumlah PIN" required>
                                        </div>

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

                                    </form>
                                    <br />
                                </div>
                            </div>
                        </div>
                    </div>
                     <!-- End Form Elements -->
                <hr />

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

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

                        <div class="panel-heading">
                             Generate PIN 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 Active     </th>
                                            <th>User Account    </th>
                                            <th>Status          </th>
                                        </tr>
                                    </thead>
                                    <tbody>

<?

$c = 1;  



 if ($_SESSION['username'] == "admin") { 
             $sql = "SELECT * FROM bestpin";
 }  else {
            $sql = "SELECT * FROM bestpin WHERE userid = ".$_SESSION['aidi']."";
 }

    $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>
             <!-- /. PAGE INNER  -->
            </div>
         <!-- /. PAGE WRAPPER  -->
        </div>
<?
    include "_footer.php";
}
?>
