<?php 
include('authentication.php');
include('includes/header.php');
include('includes/topbar.php');
include('includes/sidebar.php');
?>


<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>Users</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="#">Home</a></li>
                        <li class="breadcrumb-item active">Users</li>
                    </ol>
                </div>
            </div>
        </div><!-- /.container-fluid -->
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">


                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Users List</h3>
                        </div>
                        <!-- /.card-header -->
                        <div class="card-body">
                            <table id="example1" class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>S No.</th>
                                        <th>Name</th>
                                        <th>Number</th>
                                        <th>States</th>
                                        <th>Roles</th>
                                        <th>Points</th>
                                        <th>QR Code Reddemed</th>
                                        <th>Status</th>
                                        <th>Created At</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                        $i = 1;
                        $ref = "Users/";
                        $fetchdata = $database->getReference($ref)->getValue();
                        if($fetchdata>0){
                            foreach ($fetchdata as $key => $row) {
                                ?>
                                    <tr>
                                        <td><?=$i?></td>
                                        <td><?=$row['first']?></td>
                                        <td><?=$row['number']?></td>
                                        <td><?=$row['state']?></td>
                                        <td><?php if($row['roles'] == 1){
                                                    echo "Carpenter";
                                                } else if($row['roles'] == 2){
                                                    echo "DEMO";
                                                } ?></td>
                                        <td><?=$row['points']?></td>    
                                        <?php
                                         $ref1 = "Redeem History/".$key;
                                         $fetchdata1 = $database->getReference($ref1)->getValue();
                                           foreach ($fetchdata1 as $key1 => $row1) {
                                               $name = $name . " , " .  $row1['qr_code'];
                                             
                                           }
                                           
                                        ?>
                                        <td><?=$name?></td>
                                        
                                        <td><?php if($row['status'] == 0){ echo "InReview"; }else if ($row['status'] == 1){ echo "Accepted"; } else if($row['status'] == 2){
                                            echo "Rejected";
                                        }?></td>
                                        <td><?php echo date('d/m/Y', $row['created_at']/1000); ?></td>
                                        <td>
                                            <div class="row">
                                                <a href="edit-user.php?id=<?=$key?>"
                                                    class="btn btn-info btn-sm">Edit</a>

                                                 <a href="ledger.php?id=<?=$key?>"
                                                    class="btn btn-success btn-sm ml-1">Ledger</a>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php
                                    $name = "";
                                $i++;
                            }
                        }else{

                        }
                      
                      ?>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <th>S No.</th>
                                        <th>Name</th>
                                        <th>Number</th>
                                        <th>States</th>
                                        <th>Roles</th>
                                        <th>Points</th>
                                        <th>QR Code Reddemed</th>
                                        <th>Status</th>
                                        <th>Created At</th>
                                        <th>Action</th>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                        <!-- /.card-body -->
                    </div>
                    <!-- /.card -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </div>
        <!-- /.container-fluid -->
    </section>
    <!-- /.content -->
</div>


<?php
include('includes/script.php');
?>
<script>
$(document).ready(function() {
    $('.delete_btn').click(function(e) {
        e.preventDefault();
        var id = $(this).closest('tr').find('.id').text();
        console.log(id);
        $('#delete_id').val(id);
        $('#modal-danger').modal('show');

    });
});
</script>
<?php
include('includes/footer.php');
?>