<?php
require("header.php")
?>
<style>
  .btn-group>.btn:not(:last-child):not(.dropdown-toggle) {
    background: black;
  }

  .btn-group>.btn-group:not(:first-child)>.btn {
    background: black;
  }

  .page-item.active .page-link {
    background: black;
  }

  .dt-button-collection .dropdown-menu .buttons-columnVisibility {
    background-color: black;
  }
</style>
<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>View User and Reseller</h1> -->
        </div>
        <div class="col-sm-6">

        </div>
      </div>
    </div><!-- /.container-fluid -->
  </section>

  <!-- Main content -->
  <section class="content">
    <div class="container-fluid">
      <div class="row">
        <div class="col-12">
          <!-- /.card -->

          <div class="card card-secondary">
            <div class="card-header" style="background-color: black;">
              <h3 class="card-title">View all User</h3>
              <ol class="breadcrumb float-sm-right" style="background-color: black;">
                <li class="breadcrumb-item"><a href="dashboad.php">Home</a></li>
                <li class="breadcrumb-item">User</li>
                <li class="breadcrumb-item active">view all</li>
              </ol>
            </div>
            <!-- /.card-header -->
            <div class="card-body">
              <table id="example1" class="table table-bordered table-striped">
                <thead>
                  <tr>
                    <th>Date Time</th>
                    <th>Added By</th>
                    <th>Email</th>
                    <th>User Id</th>
                    <th>Credit</th>

                    <th>Action</th>
                    <th>Status</th>
                    <!-- <th>Status Edit</th> -->
                  </tr>
                </thead>
                <tbody>
                  <?php

                  include('connection.php');

                  $sql = "SELECT * FROM user WHERE user_type='user'  OR user_type='reseller' ORDER BY date_time ";
                  $query = mysqli_query($conn, $sql);
                  while ($row = mysqli_fetch_assoc($query)) {

                  ?>
                    <tr>

                      <td><?php echo $row['date_time']; ?></td>
                      <td><?php echo $row['added_by']; ?></td>
                      <td><?php echo $row['user_email']; ?></td>
                      <!--<td><?php echo $row['user_type']; ?></td>-->
                      <td>
                        <a href="#" class="user-id-linking" style="color:blue ;font-weight:600;" class="btn btn-default" data-toggle="modal" data-target="#modal-lg-id<?php echo $row['user_id']; ?>">

                          <?php echo $row['user_id']; ?>
                        </a>
                      </td>
                      <td>
                        <?php
                        $uid = $row['user_id'];

                        // Calculate the total credit removed from remove_credit table for the specific user
                        $sql_remove_credit = "SELECT SUM(credit) AS total_removed_credit FROM remove_credit WHERE user_id='$uid'";
                        $res_remove_credit = mysqli_query($conn, $sql_remove_credit);
                        $row_remove_credit = mysqli_fetch_assoc($res_remove_credit);
                        $totalRemovedCredit = $row_remove_credit['total_removed_credit'];

                        // Calculate the total credit added in credit table for the specific user
                        $sql_credit = "SELECT SUM(credit) AS total_credit FROM credit WHERE user_id='$uid'";
                        $res_credit = mysqli_query($conn, $sql_credit);
                        $row_credit = mysqli_fetch_assoc($res_credit);
                        $totalCredit = $row_credit['total_credit'];

                        // Calculate the net credit for the user (credit - removed_credit)
                        $netCredit = $totalCredit - $totalRemovedCredit;
                        ?>
                        <?php
                        if ($netCredit > 0) {
                        ?>
                          <p style="color:red ;font-weight:600;"><?php echo $netCredit; ?></p>
                        <?php } else {
                          echo '<p style="color:red ;font-weight:600;">0</p>';
                        } ?>
                      </td>
                      <td><a href="#" data-toggle="modal" data-target="#exampleModal<?php echo $row['id']; ?>"><i class="nav-icon fas fa-edit" style="color: black;"></i></a></td>
                      <td>

                        <form method="POST" action="update_status.php">

                          <input type="hidden" name="item_id" value="<?php echo $row['id']; ?>">
                          <?php if ($row['status'] == 1) { ?>
                            <input type="submit" name="block" value="Block" class="btn btn-danger" style="font-weight: 600;font-size: 12px;height: 30px;">
                          <?php } else { ?>
                            <input type="submit" name="activate" value="Activate" class="btn btn-success" style="font-weight: 600;font-size: 12px;height: 30px;">
                          <?php } ?>
                          <!-- <?php echo ($row['status'] == 1) ? "<div class='test rounded-circle'></div>" : "<div class='test2 rounded-circle'></div>"; ?> -->
                        </form>

                      </td>
                    </tr>

                    <!--view detail modal start -->
                    <div class="modal fade" id="exampleModal<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                      <div class="modal-dialog" role="document">
                        <div class="modal-content">
                          <div class="modal-header" style="background:black;">
                            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                              <span aria-hidden="true">&times;</span>
                            </button>
                          </div>
                          <div class="modal-body">
                            <div class="form-group">
                              <form action="update_view_all.php" method="post">

                                <div class="row">
                                  <div class="form-group col-sm-6">
                                    <label for="name">Name:</label>
                                    <input type="text" class="form-control" name="user_name" value="<?php echo $row['user_name']; ?>">
                                    <input type="hidden" class="form-control" name="id" value="<?php echo $row['id']; ?>">
                                  </div>

                                  <div class="form-group col-sm-6">
                                    <label for="email">Email:</label>
                                    <input type="email" class="form-control" name="user_email" value="<?php echo $row['user_email']; ?>">
                                  </div>

                                </div>
                                <div class="row">

                                  <div class="form-group col-sm-6">
                                    <label for="country">Country</label>
                                    <input type="text" class="form-control" name="country" value="<?php echo $row['country']; ?>">
                                  </div>
                                  <div class="form-group col-sm-6">
                                    <label for="city">City</label>
                                    <input type="text" class="form-control" name="city" value="<?php echo $row['city']; ?>">
                                  </div>
                                </div>

                                <div class="row">

                                  <div class="form-group col-sm-6">
                                    <label for="city">User Id</label>
                                    <input type="text" class="form-control" value="<?php echo $row['user_id']; ?>" readonly>
                                  </div>
                                  <div class="form-group col-sm-6">
                                    <label for="city">Password</label>
                                    <input type="text" class="form-control" value="<?php echo $row['password']; ?>" readonly>
                                  </div>
                                </div>
                                <div class="modal-footer justify-content-between">
                                  <button type="button" class="btn btn-dark" style="background:black;" data-dismiss="modal">Close</button>
                                  <button type="submit" name="submit" style="background:black;" class="btn btn-dark">Change</button>
                                </div>
                              </form>
                            </div>
                          </div>

                        </div>
                      </div>
                    </div>

                    <!-- /.modal -->

                  <?php
                  }
                  ?>

                  <!-- User ID Modal -->
                  <?php
                  $sql = "SELECT * FROM user ORDER BY date_time";
                  $res = mysqli_query($conn, $sql);

                  while ($row = mysqli_fetch_array($res)) {
                  ?>
                    <div class="modal fade" id="modal-lg-id<?php echo $row['user_id']; ?>" style="display: none;" aria-hidden="true">
                      <div class="modal-dialog modal-lg">
                        <div class="modal-content">
                          <div class="modal-header" style="background:black;">
                            <h6 class="modal-title float-left">
                              <?php
                              echo '<span style="font-weight: 600; text-transform: uppercase">Added by : &nbsp;</span>';
                              $addedByChain = [];
                              $currentUser = $row;
                              while (!empty($currentUser['added_by'])) {
                                $addedByChain[] = $currentUser['added_by'];
                                $sql = "SELECT * FROM user WHERE user_id = '{$currentUser['added_by']}'";
                                $result = mysqli_query($conn, $sql);
                                $currentUser = mysqli_fetch_assoc($result);
                              }
                              $addedByChain = array_reverse($addedByChain);
                              foreach ($addedByChain as $addedByUser) {
                                echo '<span style="color: white;text-transform: none;">' . $addedByUser . '</span>';
                                echo '&nbsp;<i class="fa fa-chevron-right" aria-hidden="true" style="font-size: 12px;"></i>&nbsp;';
                              }
                              echo '<span style="text-transform: none;">' . $row['user_id'] . '</span>';
                              ?>
                            </h6>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                              <span aria-hidden="true">×</span>
                            </button>
                          </div>
                          <div class="modal-body">
                            <div class="row">

                              <div class="col-12">
                                <span style="float:center;font-weight:600; text-transform:uppercase">Created
                                  On&nbsp;:
                                  &nbsp;</span> <span style="color:green">
                                  <?php echo $row['date_time']; ?>
                                </span>
                              </div>

                            </div>
                            <br>

                            <div class="row">

                              <div class="col-6">
                                <ul style="list-style-type: none">
                                  <li style="font-weight:600; text-transform:uppercase">
                                    <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Name:
                                    <span style="color:blueviolet;text-transform: none;margin-left:1%;font-weight:400;">
                                      <?php echo $row['user_name']; ?>
                                    </span>
                                  </li>
                                  <li style="font-weight:600; text-transform:uppercase">
                                    <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>City:
                                    <span style="color:blueviolet;text-transform: none;margin-left:1%;font-weight:400;">
                                      <?php echo $row['city']; ?>
                                    </span>
                                  </li>
                                  <li style="font-weight:600; text-transform:uppercase">
                                    <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Password:
                                    <span style="color:blueviolet;text-transform: none;margin-left:1%;font-weight:400;">
                                      <?php echo $row['password']; ?>
                                    </span>
                                  </li>
                                </ul>
                              </div>
                              <div class="col-6">
                                <ul style="list-style-type: none">
                                  <li style="font-weight:600; text-transform:uppercase">
                                    <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Email:
                                    <span style="color:blueviolet;text-transform: none;margin-left:1%;font-weight:400;">
                                      <?php echo $row['user_email']; ?>
                                    </span>
                                  </li>
                                  <li style="font-weight:600; text-transform:uppercase">
                                    <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Country:
                                    <span style="color:blueviolet;text-transform: none;margin-left:1%;font-weight:400;">
                                      <?php echo $row['country']; ?>
                                    </span>
                                  </li>
                                  <li style="font-weight:600; text-transform:uppercase">
                                    <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Security
                                    Answer: <span style="color:blueviolet;text-transform: none;margin-left:1%;font-weight:400;">
                                      <?php echo $row['answer']; ?>
                                    </span>
                                  </li>
                                </ul>
                              </div>

                            </div>


                          </div>

                        </div>

                      </div>

                    </div>
                  <?php } ?>
                  <!-- modal end -->
                </tbody>


              </table>
            </div>
          </div>
          <!-- /.card-body -->

        </div>

        <!-- /.card -->
      </div>

      <!-- /.col -->
    </div>
    <!-- /.row -->

    <!-- /.container-fluid -->
  </section>
  <!-- /.content -->
</div>
<!-- for searching -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<!-- searching end -->

<!-- pagination -->

<?php
require("footer.php")
?>