<?php 

session_name('super_admin_session');

if (!is_dir(dirname(__DIR__) . '/session_data/super')) { mkdir(dirname(__DIR__) . '/session_data/super', 0700, true); } session_save_path(dirname(__DIR__) . '/session_data/super');

session_set_cookie_params(['path' => '/', 'httponly' => true, 'samesite' => 'Strict']);

session_start();

error_reporting(0);

include('includes/dbconnection.php');
require_once 'includes/session_term_helper.php';
$stResult = resolveCurrentSessionTerm($con);
$currentSessionName = $stResult['session_name'];
$currentTermName    = $stResult['term_name'];

include('includes/flash.php');

if (strlen($_SESSION['sid']==0)) {

  header('location:logout.php');

} 

if(isset($_GET['delid']))

{

  $rid=intval($_GET['delid']);

  $sql="update tblusers set status='3' where id=:rid";

  $query=$dbh->prepare($sql);

  $query->bindParam(':rid',$rid,PDO::PARAM_STR);

  $query->execute();

  flash_alert(' blocked successfuly'); 

  header('Location: userregister.php'); exit;

}

?>

<?php @include("includes/head.php"); ?>

<style>
    /* Breadcrumb */
    .breadcrumb {
        background: none;
        padding: 0;
    }
</style>

<body class="hold-transition sidebar-mini layout-fixed">

  <div class="wrapper">

    <!-- Navbar -->

    <?php @include("includes/header.php"); ?>

    <!-- /.navbar -->

    <!-- Main Sidebar Container -->

    <?php @include("includes/sidebar.php"); ?>

    <!-- Content Wrapper. Contains page content -->

    <div class="content-wrapper">

      <!-- Content Header (Page header) -->

<section class="content-header premium-header">

    <div class="container-fluid">

        <div class="header-glass-card">

            <div class="row align-items-center">

                <div class="col-md-8">

                    <div class="title-section">

                        <div class="icon-box bg-primary-gradient shadow-sm">

                            <i class="fas fa-user"></i>

                        </div>

                        <div>

                            <h1 class="premium-title text-primary">

                                User Management

                                <span class="premium-subtitle text-secondary">Manage User</span>

                            </h1>
                            <div class="mt-1">
                              <?php if ($currentSessionName): ?>
                                <span class="badge badge-primary mr-1">
                                  <i class="fas fa-calendar-alt"></i>
                                  <?= htmlspecialchars($currentSessionName) ?>
                                </span>
                              <?php endif; ?>
                              <?php if ($currentTermName): ?>
                                <span class="badge badge-success">
                                  <i class="fas fa-bookmark"></i>
                                  <?= htmlspecialchars($currentTermName) ?>
                                </span>
                              <?php else: ?>
                                <span class="badge badge-warning">
                                  <i class="fas fa-exclamation-triangle"></i> No active term
                                </span>
                              <?php endif; ?>
                            </div>

                        </div>

                    </div>

                </div>

                <div class="col-md-4">

                    <nav aria-label="breadcrumb">

                        <ol class="breadcrumb premium-breadcrumb">

                            <li class="breadcrumb-item"><a href="dashboard.php" class="text-primary">Home</a></li>

                            <li class="breadcrumb-item active" aria-current="page">User</li>

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section>

<section class="content">

          <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6 px-3">
                        <?php
                        $dashboard_items = [
                            ["query" => "SELECT * from tblusers where name !='Developer' AND lastname !='Account' AND username !='Developer' AND status BETWEEN 1 AND 2", "label" => "TOTAL USERS", "icon" => "fas fa-users", "bg" => "bg-info", "link" => "#"],
                            ["query" => "SELECT * FROM tblusers WHERE status =2 AND name !='Developer' AND lastname !='Account' AND username !='Developer'", "label" => "ACTIVE TEACHER", "icon" => "fas fa-user-tie", "bg" => "bg-success", "link" => "#"],
                            ["query" => "SELECT * FROM tblusers WHERE status = 1 AND name !='Developer' AND lastname !='Account' AND username !='Developer'", "label" => "ACTIVE ADMIN", "icon" => "fas fa-user-shield", "bg" => "bg-warning", "link" => "#"],
                            ["query" => "SELECT * FROM tblusers WHERE status > 2 AND name !='Developer' AND lastname !='Account' AND username !='Developer'", "label" => "BLOCKED USERS", "icon" => "fas fa-user-lock", "bg" => "bg-danger", "link" => "#"],
                        ];

                        $styleMap = [
                            "bg-info"    => ["bg" => "bg-cyan-50",    "text" => "text-cyan-600"],
                            "bg-success" => ["bg" => "bg-emerald-50", "text" => "text-emerald-600"],
                            "bg-warning" => ["bg" => "bg-orange-50",  "text" => "text-orange-600"],
                            "bg-danger"  => ["bg" => "bg-rose-50",    "text" => "text-rose-600"],
                        ];

                        foreach ($dashboard_items as $item):
                            $res   = mysqli_query($con, $item['query']);
                            $count = mysqli_num_rows($res);
                            $st    = $styleMap[$item['bg']] ?? ["bg" => "bg-slate-50", "text" => "text-slate-600"];
                        ?>
                        <div class="bento-card group">
                            <div class="flex justify-between items-start mb-3">
                                <div class="w-10 h-10 <?= $st['bg'] ?> <?= $st['text'] ?> rounded-xl flex items-center justify-center text-base group-hover:scale-110 transition-transform">
                                    <i class="<?= $item['icon'] ?>"></i>
                                </div>
                                <a href="<?= $item['link'] ?>" class="text-[12px] font-black text-slate-400 group-hover:text-slate-900 transition-colors tracking-tighter">
                                    <i class="fas fa-arrow-right"></i> VIEW
                                </a>
                            </div>
                            <h2 class="text-2xl font-black text-slate-800 tracking-tight"><?= number_format($count) ?></h2>
                            <p class="text-[10px] font-bold text-slate-500 uppercase tracking-tight truncate"><?= $item['label'] ?></p>
                            <div class="mt-4 w-full h-1 bg-slate-200/50 rounded-full overflow-hidden">
                                <div class="h-full <?= str_replace('text-', 'bg-', $st['text']) ?> opacity-40 w-full transform -translate-x-1/2 group-hover:translate-x-0 transition-transform duration-1000"></div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>

      </section><!-- /.section -->

      <!-- /.content-wrapper -->

      <!-- Main content -->

      <section class="content">

        <div class="container-fluid">

          <div class="row">

            <div class="col-12">

              <div class="card card-primary">

                <div class="card-header">

                  <h5 class="card-title font-weight-bold"><i class="fas fa-user-plus"></i> Register New user</h5>

                  <div class="card-tools">

                   <button type="button" class="user btn btn-outline btn-primary" data-toggle="modal" data-target="#edit_data" style= "background-color: #007BFF; color: white; border:none;" ><i class="fas fa-ban" ></i> See blocked users

                   </button>

                   <button type="button" class="user btn btn-outline btn-success" data-toggle="modal" data-target="#edit_data1" style= "background-color: #007BFF; color: white; border: none;" ><i class="fas fa-plus" ></i> Register New User

                   </button>

                 </div>

               </div>

               <!-- /.card-header -->

               <!-- Register New User Modal -->
               <div class="modal fade" id="edit_data1" tabindex="-1" role="dialog" aria-labelledby="registerModalLabel" aria-hidden="true">
                 <div class="modal-dialog modal-xl modal-dialog-centered modal-dialog-scrollable">
                   <div class="modal-content" style="border:none;border-radius:20px;overflow:hidden;box-shadow:0 25px 60px rgba(15,23,42,.25);">
                     <div class="modal-header" style="background:linear-gradient(135deg,#0f172a,#1e3a5f);border:none;padding:22px 28px;">
                       <div style="display:flex;align-items:center;gap:12px;">
                         <div style="width:38px;height:38px;background:rgba(255,255,255,.12);border-radius:10px;display:flex;align-items:center;justify-content:center;">
                           <i class="fas fa-user-plus" style="color:#fff;font-size:.9rem;"></i>
                         </div>
                         <h5 class="modal-title" id="registerModalLabel" style="margin:0;font-weight:700;color:#f8fafc;font-size:1rem;">Register New User</h5>
                       </div>
                       <button type="button" class="close" data-dismiss="modal" aria-label="Close" style="color:#94a3b8;opacity:1;font-size:1.4rem;margin:0;">
                         <span aria-hidden="true">&times;</span>
                       </button>
                     </div>
                     <div class="modal-body" style="padding:28px;background:#f8fafc;">
                       <!-- loaded via AJAX -->
                     </div>
                   </div>
                 </div>
               </div>

               <!-- Blocked Users Modal -->
               <div class="modal fade" id="edit_data" tabindex="-1" role="dialog" aria-labelledby="blockedModalLabel" aria-hidden="true">
                 <div class="modal-dialog modal-xl modal-dialog-centered modal-dialog-scrollable">
                   <div class="modal-content" style="border:none;border-radius:20px;overflow:hidden;box-shadow:0 25px 60px rgba(15,23,42,.25);">
                     <div class="modal-header" style="background:linear-gradient(135deg,#7f1d1d,#dc2626);border:none;padding:22px 28px;">
                       <div style="display:flex;align-items:center;gap:12px;">
                         <div style="width:38px;height:38px;background:rgba(255,255,255,.12);border-radius:10px;display:flex;align-items:center;justify-content:center;">
                           <i class="fas fa-ban" style="color:#fff;font-size:.9rem;"></i>
                         </div>
                         <h5 class="modal-title" id="blockedModalLabel" style="margin:0;font-weight:700;color:#f8fafc;font-size:1rem;">Blocked Users</h5>
                       </div>
                       <button type="button" class="close" data-dismiss="modal" aria-label="Close" style="color:#fca5a5;opacity:1;font-size:1.4rem;margin:0;">
                         <span aria-hidden="true">&times;</span>
                       </button>
                     </div>
                     <div class="modal-body" style="padding:28px;background:#f8fafc;">
                       <!-- loaded via AJAX -->
                     </div>
                     <div class="modal-footer" style="background:#f1f5f9;border-top:1px solid #e2e8f0;padding:16px 28px;">
                       <button type="button" class="btn" data-dismiss="modal" style="background:#e2e8f0;color:#475569;border:none;border-radius:10px;padding:8px 20px;font-weight:600;font-size:.85rem;">Close</button>
                     </div>
                   </div>
                 </div>
               </div>

               <!-- Edit User Modal -->
               <div id="editData" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
                 <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
                   <div class="modal-content" style="border:none;border-radius:20px;overflow:hidden;box-shadow:0 25px 60px rgba(15,23,42,.25);">
                     <div class="modal-header" style="background:linear-gradient(135deg,#0f172a,#3b82f6);border:none;padding:22px 28px;">
                       <div style="display:flex;align-items:center;gap:12px;">
                         <div style="width:38px;height:38px;background:rgba(255,255,255,.12);border-radius:10px;display:flex;align-items:center;justify-content:center;">
                           <i class="fas fa-user-edit" style="color:#fff;font-size:.9rem;"></i>
                         </div>
                         <h5 class="modal-title" id="editModalLabel" style="margin:0;font-weight:700;color:#f8fafc;font-size:1rem;">Edit User Details</h5>
                       </div>
                       <button type="button" class="close" data-dismiss="modal" aria-label="Close" style="color:#94a3b8;opacity:1;font-size:1.4rem;margin:0;">
                         <span aria-hidden="true">&times;</span>
                       </button>
                     </div>
                     <div class="modal-body" id="info_update" style="padding:28px;background:#f8fafc;">
                       <!-- loaded via AJAX -->
                     </div>
                   </div>
                 </div>
               </div>



                <div class="card-body">

                  <table id="example1" class="table table-bordered table-striped">

                    <thead>

                      <tr>

                        <th class="text-center">Name</th>

                        <th class="text-center">Mobile</th>

                        <th class="text-center">Email</th>

                        <th class="text-center">Permission</th>

                        <th class="text-center">Action</th>

                      </tr>

                    </thead>

                    <tbody>

                     <?php

                     $aid=$_SESSION['cpmsaid'];

                     $usertype = $_SESSION['usertype'];

                     $property = $_SESSION['property'];

                     $sql="SELECT * from tblusers where name !='Developer' AND lastname !='Account' AND username !='Developer'AND status BETWEEN 1 AND 2";

                     $query = $dbh -> prepare($sql);

                     $query->execute();

                     $results=$query->fetchAll(PDO::FETCH_OBJ);



                     $cnt=1;

                     if($query->rowCount() > 0)

                     {

                      foreach($results as $row)

                        {               ?>

                         <tr>

                          <td class="text-left"><?php  echo htmlentities($row->name);?> <?php  echo htmlentities($row->lastname);?></td>

                          <td class="text-left"><?php  echo htmlentities($row->mobile);?></td>

                          <td class="text-left" ><?php  echo htmlentities($row->email);?></td>

                          <td class="text-left"><?php  echo htmlentities($row->permission);?></td>

                          <td class="text-left">

                           <button class="edit_data btn btn-xs btn-primary" id="<?php echo  ($row->id); ?>" title="click for edit"><i class="fas fa-edit"></i> Edit</button>

                           <a class="btn btn-xs btn-danger" href="userregister.php?delid=<?php echo ($row->id);?>" title="click for block" onclick="return confirm('sure to block ?')" ><i class="fas fa-ban"></i> Ban</a>

                         </td>

                       </tr>



                       <?php 

                     }

                   } ?>

                 </tbody>



               </table>

             </div>

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

           </div>

           <!-- /.card -->

         </div>

         <!-- /.col -->

       </div>

       <!-- /.row -->

     </div>

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

   </section>

   <!-- /.content -->

 </div>

 <!-- /.content-wrapper -->

 <?php @include("includes/footer.php"); ?>



 <!-- Control Sidebar -->

 <aside class="control-sidebar control-sidebar-dark">

  <!-- Control sidebar content goes here -->

</aside>

<!-- /.control-sidebar -->

</div>

<!-- ./wrapper -->

<?php @include("includes/foot.php"); ?>

<!-- page script -->

<script type="text/javascript">
  $(document).ready(function(){

    // Load Register New User form
    $('#edit_data1').on('show.bs.modal', function() {
      $(this).find('.modal-body').load('newuser-form.php', function() {
        $(this).find('.modal-select2').select2({ dropdownParent: $('#edit_data1'), width: '100%' });
      });
    });

    // Load Blocked Users list
    $('#edit_data').on('show.bs.modal', function() {
      $(this).find('.modal-body').load('blockedusers.php');
    });

    // Edit user
    $(document).on('click','.edit_data',function(){
      var edit_id=$(this).attr('id');
      $.ajax({
        url:"edituser-form.php",
        type:"post",
        data:{edit_id:edit_id},
        success:function(data){
          $("#info_update").html(data);
          $("#info_update").find('.modal-select2').select2({ dropdownParent: $('#editData'), width: '100%' });
          $("#editData").modal('show');
        }
      });
    });

  });
</script>

</body>

</html>



