<?php


session_start();

if (!isset($_SESSION['user'])) {
    // Redirect to login page if not logged in
    header('Location: ../login');
    exit();
}

$user = $_SESSION['user'];
//$email = $user['email_address'];


// Include database connection file
include_once './dbconn.php';

$counttotal = "SELECT COUNT(*) as total_items FROM web_app_enrollee_data";
$counttotalinducted = "SELECT COUNT(*) as total_principal FROM web_app_enrollee_data WHERE hmo_level = 'Principal'";
$counttotanewinductee = "SELECT COUNT(*) as total_dependant FROM web_app_enrollee_data WHERE hmo_level != 'Principal'";
$pendingapproval = "SELECT COUNT(*) as total_pending_approval FROM web_app_enrollee_data WHERE status = 'Pending'";


// Fetch total items
$total_items = 0;
$result = $conn->query($counttotal);
if ($result && $result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $total_items = $row["total_items"];
}

// Fetch total inducted items
$total_principal = 0;
$result = $conn->query($counttotalinducted);
if ($result && $result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $total_principal = $row["total_principal"];
}

// Fetch total new Inductee
$total_dependant = 0;
$result = $conn->query($counttotanewinductee);
if ($result && $result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $total_dependant = $row["total_dependant"];
}

// Fetch total Pending Approval
$total_pending_approval = 0;
$result = $conn->query($pendingapproval);
if ($result && $result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $total_pending_approval = $row["total_pending_approval"];
}




$url="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

  if(strpos($url, 'error') == true){


 echo '<script>
    setTimeout(function() {
        swal({
            title: "Error!",
            text: "Error Occured",
            type: "error"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
  </script>';
  

    }elseif (strpos($url, 'added') == true) {
        
 echo '<script>
    setTimeout(function() {
        swal({
            title: "Success!",
            text: "New Enrollee added successfully",
            type: "success"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
</script>';

    } elseif (strpos($url, 'depadd') == true) {
        
 echo '<script>
    setTimeout(function() {
        swal({
            title: "Success!",
            text: "New Dependant added successfully",
            type: "success"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
</script>';

    }
    elseif (strpos($url, 'bulk_approved') == true) {
        
 echo '<script>
    setTimeout(function() {
        swal({
            title: "Success!",
            text: "Bulk Approval Successful",
            type: "success"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
</script>';

    }elseif (strpos($url, 'bulk_deny') == true) {
        
 echo '<script>
    setTimeout(function() {
        swal({
            title: "Success!",
            text: "Bulk deny Successful",
            type: "success"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
</script>';

    }elseif (strpos($url, 'approved') == true) {
        
 echo '<script>
    setTimeout(function() {
        swal({
            title: "Success!",
            text: "Approved",
            type: "success"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
</script>';

    } elseif (strpos($url, 'success') == true) {
            
     echo '<script>
        setTimeout(function() {
            swal({
                title: "Success!",
                text: "Bulk Upload Completed",
                type: "success"
            }, function() {
                window.location = "#0";
            });
        }, 1000);
    </script>';

    } elseif (strpos($url, 'denied') == true) {
        
 echo '<script>
    setTimeout(function() {
        swal({
            title: "Success!",
            text: "Denied",
            type: "success"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
</script>';

    }




?>

<!DOCTYPE html>
<html lang="en">


<!-- datatables.html  21 Nov 2019 03:55:21 GMT -->
<head>
  <meta charset="UTF-8">
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, shrink-to-fit=no" name="viewport">
  <title>Health Assur - Admin Dashboard</title>
  <!-- General CSS Files -->
  <link rel="stylesheet" href="assets/css/app.min.css">
  <link rel="stylesheet" href="assets/bundles/datatables/datatables.min.css">
  <link rel="stylesheet" href="assets/bundles/datatables/DataTables-1.10.16/css/dataTables.bootstrap4.min.css">
  <!-- Template CSS -->
  <link rel="stylesheet" href="assets/css/style.css">
  <link rel="stylesheet" href="assets/css/components.css">
  <!-- Custom style CSS -->
  <link rel="stylesheet" href="assets/css/custom.css">
  <link rel='shortcut icon' type='image/x-icon' href='../images/Red-logo.png' />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">
  <style>
    /* Modal styles */
    .modal,
    .modal2 {
        display: none;
        position: fixed;
        z-index: 1000;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgba(0, 0, 0, 0.4);
    }

    .modal-content,
    .modal-content2 {
        background-color: #fefefe;
        margin: 2% auto;
        padding: 20px 50px;
        border: 1px solid #888;
        width: 70%;
        height: 90vh;
        overflow-y: auto;

    }

    .close,
    .close2 {
        color: #aaa;
        /* float: right; */
        font-size: 38px;
        font-weight: bold;
    }

    .close:hover,
    .close:focus,
    .close2:hover,
    .close2:focus {
        color: black;
        text-decoration: none;
        cursor: pointer;
    }



    .inputdiv {
        display: block;
    }

    .inputdiv input {
        padding: 10px 20px;
        outline: none;
        border-radius: 10px;
        border: 2px solid gray;

    }
    </style>
</head>

<body>
  <!-- Form Modal -->
  <div id="myModal" class="modal">

<!-- Modal content -->
<div class="modal-content">
    <h1 style="text-align: right;">
        <span class="close p-2 text-danger ">&times;</span>
    </h1>
    <h2 class='text-center text-black' >Add New Principal</h2>
    <form id="modalForm" action="./addnew/addnew.php" method="post" enctype="multipart/form-data">
        <div class="form-row row py-2">
            <div class="form-group col-md-12  py-2">
                <label for="group">Group</label>
                <select id="group" class="form-control select2" name="group" required>
                    <option selected>Choose...</option>
                    <?php
                    include_once './dbconn.php';
                        // Fetch data from the table
                        $sql = "SELECT company, link, company_code, groupplan FROM web_app_enrollment_form";
                        $result = $conn->query($sql);
                        
                        
                        // Check if there are any rows returned
                        if ($result->num_rows > 0) {
                            // Output data of each row
                            while($row = $result->fetch_assoc()) {
                                $company=$row["company"];
                                $link=$row["link"];
                                $company_code= $row["company_code"];
                                $groupplan=$row["groupplan"];
                                echo "<option value='" . $company . "'>" . $company . "</option>";
                                
                            }
                        } else {
                            echo "<option value=''>No data available</option>";
                        }
                                            
                    ?>
                    
                </select>
                
        <!--hidden inputs-->
        <!--<input type="hidden" name="company_code" value="<?=$company_code?>">-->
            </div>
            <div class="form-group col-md-6">
                <label for="firstName">First Name</label>
                <input type="text" class="form-control" id="firstName" name="firstname" required
                    placeholder="First Name">
            </div>
            <div class="form-group col-md-6">
                <label for="lastName">Last Name</label>
                <input type="text" class="form-control" id="lastName" name="lastname" required
                    placeholder="Last Name">
            </div>
        </div>
        <div class="form-row row py-2">
            <div class="form-group col-md-6">
                <label for="middlename">Middle Name</label>
                <input type="text" class="form-control" id="lastName" name="middlename"
                    placeholder="Last Name">
            </div>
            <div class="form-group col-md-6">
                <label for="phoneNumber">Phone Number</label>
                <input type="tel" class="form-control" id="phoneNumber" name="phonenumber" required
                    placeholder="Phone Number">
            </div>

        </div>
        <div class="form-row row py-2">
            <div class="form-group col-md-6">
                <label for="email">Email</label>
                <input type="email" class="form-control" id="email" name="email" required placeholder="Email">
            </div>
            <div class="form-group col-md-6">
                <label for="address">Employee ID</label>
                <input type="text" class="form-control" id="employeeid" name="employeeid" required
                    placeholder="Employee ID">
            </div>
        </div>
        <div class="form-row row py-2">
            <div class="form-group col-md-6">
                <label for="gender">Gender</label>
                <select id="gender" class="form-control" name="gender" required>
                    <option selected>Choose...</option>
                    <option>Male</option>
                    <option>Female</option>
                </select>
            </div>
            <div class="form-group col-md-6">
                <label for="dob">Date Of Birth</label>
                <input type="date" class="form-control" id="dob" name="dob" required>
            </div>


        </div>
        <div class="form-row row py-2">
            <div class="form-group col-md-6">
                <label for="state">State</label>
                <select class="form-control" id="state" onchange="populateLocalGovernments()" name="state"
                    required>
                    <option selected disabled>Select State</option>
                    <option value="Abia">Abia</option>
                    <option value="Adamawa">Adamawa</option>
                    <option value="Abuja">Abuja</option>
                    <option value="AkwaIbom">Akwa Ibom</option>
                    <option value="Anambra">Anambra</option>
                    <option value="Bauchi">Bauchi</option>
                    <option value="Bayelsa">Bayelsa</option>
                    <option value="Benue">Benue</option>
                    <option value="Borno">Borno</option>
                    <option value="CrossRiver">Cross River</option>
                    <option value="Delta">Delta</option>
                    <option value="Ebonyi">Ebonyi</option>
                    <option value="Edo">Edo</option>
                    <option value="Ekiti">Ekiti</option>
                    <option value="Enugu">Enugu</option>
                    <option value="Gombe">Gombe</option>
                    <option value="Imo">Imo</option>
                    <option value="Jigawa">Jigawa</option>
                    <option value="Kaduna">Kaduna</option>
                    <option value="Kano">Kano</option>
                    <option value="Katsina">Katsina</option>
                    <option value="Kebbi">Kebbi</option>
                    <option value="Kogi">Kogi</option>
                    <option value="Kwara">Kwara</option>
                    <option value="Lagos">Lagos</option>
                    <option value="Nassarawa">Nassarawa</option>
                    <option value="Niger">Niger</option>
                    <option value="Ogun">Ogun</option>
                    <option value="Ondo">Ondo</option>
                    <option value="Osun">Osun</option>
                    <option value="Oyo">Oyo</option>
                    <option value="Plateau">Plateau</option>
                    <option value="Rivers">Rivers</option>
                    <option value="Sokoto">Sokoto</option>
                    <option value="Taraba">Taraba</option>
                    <option value="Yobe">Yobe</option>
                    <option value="Zamfara">Zamfara</option>
                </select>
            </div>
            <div class="form-group col-md-6">
                <label for="lga">Local Government</label>
                <select class="form-control" name="lga" id="local-government">
                    <option selected disabled>Select Local Government</option>
                </select>
                <script src="./statelga.js"></script>
            </div>
        </div>
        <div class="form-row row py-2">
            <div class="form-group col-md-6">
                <label for="City">City</label>
                <input type="text" class="form-control" id="city" name="city" required placeholder="City">
            </div>
            <div class="form-group col-md-6">
                <label for="address">Address</label>
                <input type="text" class="form-control" id="address" name="address" required
                    placeholder="Address">
            </div>

        </div>
        <div class="form-row row py-2">
            <div class="form-group col-md-6">
                <label for="planType">Plan Type</label>
                <select class="form-control" class="form-control" id="planType" name="plantype" required>
                    <option value="Flex Plan"> Flex Plan </option>
            <option value="Executive Plan"> Executive Plan </option>
            <option value="Upper Plan"> Upper Plan </option>
            <option value="Business Plan"> Business Plan </option>
            <option value="Club Plan"> Club Plan </option>
            <option value="Social Plan"> Social Paln </option>
                </select>
            </div>
            <div class="form-group col-md-6">
                <label for="roup Type">Group Type</label>
                <select class="form-control select2" class="form-control" id="groupType" name="grouptype" required>
                    <option selected disabled>Select Plan</option>
                    <option value="Individual">Individual</option>
                    <option value="Family">Family</option>
                </select>
            </div>
        </div>
        <div class="form-row row py-2">
            <div class="form-group col-md-12">
                <label for="selectedProvider">Selected Provider</label>
                <select class="form-control select2" class="form-control select2" id="provider" name="provider" required>
                    <option selected disabled>Choose Provider</option>
                    <?php
                        // Fetch data from the table
                        $sql = "SELECT provider_name FROM web_app_provider_list";
                        $result = $conn->query($sql);
                        
                        
                        // Check if there are any rows returned
                        if ($result->num_rows > 0) {
                            // Output data of each row
                            while($row = $result->fetch_assoc()) {
                                $provider=$row["provider_name"];
                                echo "<option value='" . $provider . "'>" . $provider . "</option>";
                                
                            }
                        } else {
                            echo "<option value=''>No data available</option>";
                        }
                                            
                    ?>
                </select>
            </div>
        </div>



        <div class="form-row row py-2">
            <div class="form-group col-md-6">
                <label for="status">Status</label>
                <select class="form-control" class="form-control" id="status" name="status" required>
                    <option selected disabled>Status</option>
                    <option value="Approved">Approved</option>
                    <option value="Pending">Pending</option>
                    <option value="Denied">Denied</option>
                </select>
            </div>
            <div class="form-group col-md-6">
                <label for="passport">Passport</label>
                <input type="file" class="form-control form-control-file" id="passport" name="passport">
            </div>
        </div>

        <div class="form-row row py-2">
            <div class="form-group col-md-6">
                <label for="Genotype">Genotype</label>
                <select class="form-control" class="form-control" id="Genotype" name="genotype" required>
                    <option selected disabled>Select Genotype</option>
                    <option value="AA">AA</option>
                    <option value="AS">AS</option>
                    <option value="SS">SS</option>
                    <option value="AC">AC</option>
                    <option value="SC">SC</option>
                    <option value="CC">CC</option>
                </select>
            </div>
            <div class="form-group col-md-6">
                <label for="bloodgroup">Blood Group</label>
                <select class="form-control" class="form-control" id="bloodgroup" name="bloodgroup" required>
                    <option selected disabled>Select Blood Group</option>
                    <option value="A+">A+</option>
                    <option value="A-">A-</option>
                    <option value="B+">B+</option>
                    <option value="B-">B-</option>
                    <option value="AB+">AB+</option>
                    <option value="AB-">AB-</option>
                    <option value="O+">O+</option>
                    <option value="O-">O-</option>
                </select>
            </div>
        </div>

        <div class="form-row row py-2">
            <div class="form-group col-md-12">
                <label for="allergies">Allergies</label>
                <input type="text" class="form-control" id="allergies" name="allergies"
                    placeholder="Any Allergies">
            </div>
        </div>

        <div class="form-row row py-2">
            <div class="form-group col-md-6">
                <label for="pre-existing">Has Pre-Existing Condition</label>
                <select class="form-control" class="form-control" id="has-pre-existing" name="haspreexisting"
                    required>
                    <option selected disabled>Select...</option>
                    <option value="Yes">Yes</option>
                    <option value="No">No</option>
                </select>
            </div>
            <div class="form-group col-md-6">
                <label for="pre-existing">Pre-Existing Condition</label>
                <input type="text" class="form-control" id="pre-existing" name="preexisting"
                    placeholder="Pre-Existing Condition">
            </div>
        </div>

        <div class="form-row row py-2">
            <div class="form-group col-md-6">
                <label for="surgicalhist">Has Surgical History</label>
                <select class="form-control" class="form-control" id="hassurgicalhist" name="hassurgicalhist"
                    required>
                    <option selected disabled>Select...</option>
                    <option value="Yes">Yes</option>
                    <option value="No">No</option>
                </select>
            </div>
            <div class="form-group col-md-6">
                <label for="Surgical-History">Surgical History</label>
                <input type="text" class="form-control" id="SurgicalHistory" name="surgicalhist"
                    placeholder="Surgical History">
            </div>
        </div>


        <button type="submit" class="btn btn-primary px-4 mt-4" name="add">Add</button>
    </form>
</div>

</div>
  <div class="loader"></div>
  <div id="app">
    <div class="main-wrapper main-wrapper-1">
      <div class="navbar-bg"></div>
      <nav class="navbar navbar-expand-lg main-navbar sticky">
        <div class="form-inline mr-auto">
          <ul class="navbar-nav mr-3">
            <li><a href="#" data-toggle="sidebar" class="nav-link nav-link-lg
									collapse-btn"> <i data-feather="align-justify"></i></a></li>
            <li><a href="#" class="nav-link nav-link-lg fullscreen-btn">
                <i data-feather="maximize"></i>
              </a>
            </li>
          </ul>
        </div>
        <ul class="navbar-nav navbar-right">
          <li class="dropdown"><a href="#" data-toggle="dropdown"
              class="nav-link dropdown-toggle nav-link-lg nav-link-user"> <img alt="image" src="assets/img/user.png"
                class="user-img-radious-style"> <span class="d-sm-none d-lg-inline-block"></span></a>
            <div class="dropdown-menu dropdown-menu-right pullDown">
              <div class="dropdown-title">Hello Health Assur Admin</div>
              
              <div class="dropdown-divider"></div>
              <a href="../login/logout.php" class="dropdown-item has-icon text-danger"> <i class="fas fa-sign-out-alt"></i>
                Logout
              </a>
            </div>
          </li>
        </ul>
      </nav>
      <div class="main-sidebar sidebar-style-2">
        <aside id="sidebar-wrapper">
          <div class="sidebar-brand">
            <a href="index.php"> <img alt="image" src="../images/Red-logo.png" class="header-logo" /> <span
                class="logo-name"></span>
            </a>
          </div>
          <ul class="sidebar-menu">
            <li class="menu-header">Menu</li>
            <li class="dropdown">
              <a href="index.php" class="nav-link"><i data-feather="user"></i><span>Members</span></a>
              <a href="stafflist.php" class="nav-link"><i data-feather="users"></i><span>Staff List</span></a>
            </li>
          </ul>
        </aside>
      </div>
      <!-- Main Content -->
      <div class="main-content">
        <section class="section">
          <div class="row ">
            <div class="col-xl-3 col-lg-6 col-md-6 col-sm-6 col-xs-12">
              <div class="card">
                <div class="card-statistic-4">
                  <div class="align-items-center justify-content-between">
                    <div class="row ">
                      <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pr-0 pt-3">
                        <div class="card-content">
                          <h5 class="font-15">Total Members</h5>
                          <h2 class="mb-3 font-18"><?php echo htmlspecialchars($total_items); ?> </h2>
                        </div>
                      </div>
                      <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pl-0">
                        <div class="banner-img">
                          <img src="assets/img/banner/1.png" alt="">
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="col-xl-3 col-lg-6 col-md-6 col-sm-6 col-xs-12">
              <div class="card">
                <div class="card-statistic-4">
                  <div class="align-items-center justify-content-between">
                    <div class="row ">
                      <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pr-0 pt-3">
                        <div class="card-content">
                          <h5 class="font-15"> Principal</h5>
                          <h2 class="mb-3 font-18"><?php echo htmlspecialchars($total_principal); ?></h2>
                        </div>
                      </div>
                      <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pl-0">
                        <div class="banner-img">
                          <img src="assets/img/banner/2.png" alt="">
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="col-xl-3 col-lg-6 col-md-6 col-sm-6 col-xs-12">
              <div class="card">
                <div class="card-statistic-4">
                  <div class="align-items-center justify-content-between">
                    <div class="row ">
                      <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pr-0 pt-3">
                        <div class="card-content">
                          <h5 class="font-15">Dependents</h5>
                          <h2 class="mb-3 font-18"><?php echo htmlspecialchars($total_dependant); ?></h2>
                        </div>
                      </div>
                      <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pl-0">
                        <div class="banner-img">
                          <img src="assets/img/banner/3.png" alt="">
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="col-xl-3 col-lg-6 col-md-6 col-sm-6 col-xs-12">
              <div class="card">
                <div class="card-statistic-4">
                  <div class="align-items-center justify-content-between">
                    <div class="row ">
                      <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pr-0 pt-3">
                        <div class="card-content">
                          <h5 class="font-15">Pending Approval</h5>
                          <h2 class="mb-3 font-18"><?php echo htmlspecialchars($total_pending_approval); ?></h2>
                        </div>
                      </div>
                      <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pl-0">
                        <div class="banner-img">
                          <img src="assets/img/banner/4.png" alt="">
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="mb-3 mt-4">
          <form action="import.php" method="post" enctype="multipart/form-data" style="display: none;"
                id="importForm">
                <input type="file" name="importFile" id="importFile">
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>
            <a href="#" class="btn btn-info mb-3" onclick="document.getElementById('importFile').click();">Import from
                file</a>
            <a href="export.php" class="btn btn-secondary mb-3">Export all Data</a>
            <!--<a href="download_all_cards.php" class="btn btn-danger mb-3">Download selected E-ID</a> -->
            <button id="openModalBtn" class="btn btn-success mb-3">Add New</button>
            <a href="../admin/index.php" class="btn btn-primary mb-3">Add Company</a>
            <button id="approveButton" onclick="performBulkApproval()" class="btn btn-success mb-3">Approve
                Selected</button>
                <button id="denyButton" onclick="performbulkdeny()" class="btn btn-danger mb-3">Deny
                Selected</button>
                <button id="deactivateButton" onclick="performbulkdeactivate()" class="btn btn-danger mb-3">Deactivate Selected</button>
            <script src="./modal.js"></script>
        </div>
          <div class="section-body">
            <div class="row">
              <div class="col-12">
                <div class="card">
                  <div class="card-header">
                    <h4>Members</h4>
                  </div>
                  <div class="card-body">
                    <div class="table-responsive">
                      <!--<table class="table table-striped" id="tableExport">-->
                        <table class="table table-striped" id="table-2">
                        <thead>
                          <tr>
                            <th class="text-center pt-3">
                              <div class="custom-checkbox custom-checkbox-table custom-control">
                                <input type="checkbox" data-checkboxes="mygroup" onclick="toggle(this);" data-checkbox-role="dad"
                                  class="custom-control-input" id="checkbox-all">
                                <label for="checkbox-all" class="custom-control-label">&nbsp;</label>
                              </div>
                            </th>
                            <th>SN</th>
                            <th>Status</th>
                            <th>Group</th>
                            <th>First Name</th>
                            <th>Middle Name</th>
                            <th>Last Name</th>
                            <th>Phone Number</th>
                            <th>Email</th>
                            <th>HMO ID</th>
                            <th>Member Type</th>
                            <th>Gender</th>
                            <th>Date Of Birth</th>
                            <th>Plan Type</th>
                            <th>Plan Category</th>
                            <th>State</th>
                            <th>Address</th>
                            <th>Selected Provider</th>
                            <th>Employee ID</th>
                            <!--<th>Passport</th>-->
                            <th>Action</th>
                            
                          </tr>
                        </thead>
                        <tbody>
                        <?php
                            // Connect to MySQL
                            include_once './dbconn.php';
                            
                            $sql = "SELECT * FROM `web_app_enrollee_data` ORDER BY id DESC";
                            // Fetch data with pagination and filter
                            $result = mysqli_query($conn, $sql);
                            if (mysqli_num_rows($result) > 0) {
                                $serial = 1;
                                while($row = mysqli_fetch_assoc($result)) {
                                
                                
                                            // Convert date_of_birth using the specific format (m/d/Y)
        $date_of_birth = $row['date_of_birth'];
        $dob = DateTime::createFromFormat('d/m/Y', $date_of_birth); // Create DateTime object from dd/mm/yyyy format

        // Check if the date is valid
        if ($dob) {
            $now = new DateTime(); // Get the current date
            $age = $now->diff($dob)->y; // Calculate the age

            // Initialize the warning symbol
            $warning_symbol = '';

            // Check if conditions for warning symbol are met
            if ($row['hmo_level'] == 'Principal' && $age > 60) {
                // Condition for Principal: age > 60
                $warning_symbol = '⚠️';
            } elseif ($row['hmo_level'] != 'Principal' && $row['hmo_level'] != 'Spouse' && $age > 25) {
                // Condition for Non-Principal (excluding Spouse): age > 25
                $warning_symbol = '⚠️';
            } elseif ($row['hmo_level'] == 'Spouse' && $age > 60) {
                // Condition for Spouse: age > 60
                $warning_symbol = '⚠️';
            }
        } else {
            // Handle cases where the date format might be invalid
            $warning_symbol = ''; // No symbol if date is invalid
            $age = "N/A"; // Just for display purposes
        }
                                    
                                    echo "<tr>";
                                    // Add checkbox column
                                    echo "<td><input type='checkbox' name='selected[]' value='" . $row['id'] . "'></td>";
                                    echo "<td>" . $serial . "</td>";
                                    echo "<td>";
                                    if ($row['status'] == 'Deactivated' || $row['status'] == 'Denied') {
                                        echo "<span style='color: red;'>" . $row['status'] . "</span>";
                                    } elseif ($row['status'] == 'Pending') {
                                        echo "<span style='color: orange;'>" . $row['status'] . "</span>";
                                    } elseif ($row['status'] == 'Approved') {
                                        echo "<span style='color: green;'>" . $row['status'] . "</span>";
                                    } else {
                                        echo $row['status'];
                                    }
                                    echo "</td>";
                                    echo "<td>" . $row['company_name'] . "</td>";
                                    echo "<td>";
                                    if ($row['status'] == 'Deactivated') {
                                        echo "<span style='color: red;'>" . $row['first_name'] . "</span>";
                                    } else {
                                        echo $row['first_name'];
                                    }
                                    echo "</td>";
                                    echo "<td>";
                                    if ($row['status'] == 'Deactivated') {
                                        echo "<span style='color: red;'>" . $row['middle_name'] . "</span>";
                                    } else {
                                        echo $row['middle_name'];
                                    }
                                    echo "</td>";
                                    echo "<td>";
                                    if ($row['status'] == 'Deactivated') {
                                        echo "<span style='color: red;'>" . $row['surname'] . "</span>";
                                    } else {
                                        echo $row['surname'];
                                    }
                                    echo "</td>";
                                    echo "<td>" . $row['phone_number'] . "</td>";
                                    echo "<td>" . $row['email_address'] . "</td>";
                                    echo "<td>" . $row['hmo_id'] . "</td>";
                                    echo "<td>" . $row['hmo_level'] . "</td>";
                                    echo "<td>" . $row['gender'] . "</td>";
                                    echo "<td>";
                                    if (!empty($warning_symbol)) {
                                    echo "<span style='color: red;'>$warning_symbol</span>";
                                    }echo  $row['date_of_birth'];
                                    echo "</td>";
                                    echo "<td>" . $row['type_of_plan'] . "</td>";
                                    echo "<td>" . $row['group_of_plan'] . "</td>";
                                    echo "<td>" . $row['address_state'] . "</td>";
                                    echo "<td>" . $row['address_street'] . "</td>";
                                    echo "<td>" . $row['hospitals'] . "</td>";
                                    echo "<td>" . $row['employee_id'] . "</td>";
                                    //echo "<td><img src='" . $row['Passport_link'] . "' alt='Image' style='max-width: 60px; max-height: 60px;'></td>";
                                    echo "<td>";
                                    echo "<div class='btn-group'>";
                                    //echo "<a href='approved.php?id=" . $row['id'] . "&email=" . $row['email_address'] . "' class='btn btn-success'><i class='fas fa-check'></i></a>";
                                    echo "<a href='denied.php?id=" . $row['id'] . "' class='btn btn-danger'><i class='fas fa-times'></i></a>";
                                    echo "<a href='edit.php?id=" . $row['id'] . "' class='btn btn-warning'><i class='fas fa-pen'></i></a>";
                                    echo "<a href='adddependent.php?id=" . $row['id'] . "' class='btn btn-primary'><i class='fas fa-plus'></i></a>";
                                    echo "<button class='btn btn-danger' onclick='deleteItem(" . $row['id'] . ")'><i class='fas fa-trash'></i></button>";
                                    echo "<a href='#' class='btn btn-secondary' onclick='printIDCard(" . $row['id'] . ", \"" . $row['first_name'] . " " . $row['surname'] . "\", \"" . $row['hmo_id'] . "\", \"" . $row['company_name'] . "\", \"" . $row['type_of_plan'] . "\", \"" . $row['gender'] . "\", \"" . $row['Passport_link'] . "\")'><i class='fas fa-id-card'></i></a>";
                                    echo "</div>";
                                    echo "</td>";
                                    echo "</tr>";
                                    $serial++;
                                }
                            } else {
                                echo "<tr><td colspan='18'>No data found</td></tr>";
                            }
                            // Close connection
                            mysqli_close($conn);
                        ?>
                        </tbody>

                        <script>
                          function toggle(source) {
                              checkboxes = document.getElementsByName('selected[]');
                              for (var i = 0, n = checkboxes.length; i < n; i++) {
                                  checkboxes[i].checked = source.checked;
                              }
                          }

                          function performBulkApproval() {
                            var button = document.getElementById('approveButton');
                            button.disabled = true; // Disable the button
                            var checkboxes = document.getElementsByName('selected[]');
                            var selectedItems = [];
                            // Iterate through checkboxes to find selected items
                            for (var i = 0; i < checkboxes.length; i++) {
                                if (checkboxes[i].checked) {
                                    selectedItems.push(checkboxes[i].value);
                                }
                            }
                            // Check if any item is selected
                            if (selectedItems.length > 0) {
                                // Send selected item IDs to bulkapproval.php using AJAX
                                $.ajax({
                                    url: 'bulkapproval.php',
                                    type: 'POST',
                                    dataType: 'json',
                                    data: {
                                        selected: selectedItems
                                    },
                                    success: function(response) {
                                        // Handle success response if needed
                                        console.log(response);
                                        // Redirect to another page after successful bulk approval
                                        window.location.href = 'index.php?bulk_approved';
                                    },
                                    error: function(xhr, status, error) {
                                        console.error(xhr.responseText);
                                        // Handle error
                                        alert('Error occurred during bulk approval: ' + xhr.responseText);
                                    },
                                    complete: function() {
                                        button.disabled = false;
                                    }
                                });
                            } else {
                                // If no item is selected, show an alert
                                alert('Please select at least one item for bulk approval.');
                                button.disabled = false;
                            }
                        }
                        
                        
                        //bulk denial
                         function performbulkdeny() {
                            var button = document.getElementById('denyButton');
                            button.disabled = true; // Disable the button
                            var checkboxes = document.getElementsByName('selected[]');
                            var selectedItems = [];
                            // Iterate through checkboxes to find selected items
                            for (var i = 0; i < checkboxes.length; i++) {
                                if (checkboxes[i].checked) {
                                    selectedItems.push(checkboxes[i].value);
                                }
                            }
                            // Check if any item is selected
                            if (selectedItems.length > 0) {
                                // Send selected item IDs to bulkdeny.php using AJAX
                                $.ajax({
                                    url: 'bulkdeny.php',
                                    type: 'POST',
                                    dataType: 'json',
                                    data: {
                                        selected: selectedItems
                                    },
                                    success: function(response) {
                                        // Handle success response if needed
                                        console.log(response);
                                        // Redirect to another page after successful bulk approval
                                        window.location.href = 'index.php?bulk_deny';
                                    },
                                    error: function(xhr, status, error) {
                                        console.error(xhr.responseText);
                                        // Handle error
                                        alert('Error occurred during bulk denial: ' + xhr.responseText);
                                    },
                                    complete: function() {
                                        button.disabled = false;
                                    }
                                });
                            } else {
                                // If no item is selected, show an alert
                                alert('Please select at least one item for bulk denial.');
                                button.disabled = false;
                            }
                        }
                        
                        
                        
                        //bulk deactivation
                         function performbulkdeactivate() {
                            var button = document.getElementById('deactivateButton');
                            button.disabled = true; // Disable the button
                            var checkboxes = document.getElementsByName('selected[]');
                            var selectedItems = [];
                            // Iterate through checkboxes to find selected items
                            for (var i = 0; i < checkboxes.length; i++) {
                                if (checkboxes[i].checked) {
                                    selectedItems.push(checkboxes[i].value);
                                }
                            }
                            // Check if any item is selected
                            if (selectedItems.length > 0) {
                                // Send selected item IDs to bulkdeny.php using AJAX
                                $.ajax({
                                    url: 'bulkdeactivate.php',
                                    type: 'POST',
                                    dataType: 'json',
                                    data: {
                                        selected: selectedItems
                                    },
                                    success: function(response) {
                                        // Handle success response if needed
                                        console.log(response);
                                        // Redirect to another page after successful bulk approval
                                        window.location.href = 'index.php?bulk_deny';
                                    },
                                    error: function(xhr, status, error) {
                                        console.error(xhr.responseText);
                                        // Handle error
                                        alert('Error occurred during bulk deactivation: ' + xhr.responseText);
                                    },
                                    complete: function() {
                                        button.disabled = false;
                                    }
                                });
                            } else {
                                // If no item is selected, show an alert
                                alert('Please select at least one item for deactivation.');
                                button.disabled = false;
                            }
                        }



                          // Function to print ID card
                          function printIDCard(id, name, idNumber, client, plan, gender, imageUrl) {
                              $.ajax({
                                  url: 'idcard.php',
                                  type: 'POST',
                                  dataType: 'json',
                                  data: {
                                      name: name,
                                      id_number: idNumber,
                                      client: client, // Update with actual client data
                                      validity: gender, // Update with actual validity data
                                      plan: plan,
                                      image_url: imageUrl
                                  },
                                  success: function(response) {
                                      var a = document.createElement('a');
                                      a.href = response.file_url;
                                      a.download = idNumber; // You can change the name of the downloaded file here
                                      a.style.display = 'none';
                                      document.body.appendChild(a);
                                      a.click(); // Trigger the download
                                      document.body.removeChild(a); // Clean up
                                  },
                                  error: function(xhr, status, error) {
                                      console.error(xhr.responseText);
                                      // Handle error
                                  }
                              });
                          }

                          </script>
                      </table>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </section>
        <div class="settingSidebar">
          <a href="javascript:void(0)" class="settingPanelToggle"> <i class="fa fa-spin fa-cog"></i>
          </a>
          <div class="settingSidebar-body ps-container ps-theme-default">
            <div class=" fade show active">
              <div class="setting-panel-header">Setting Panel
              </div>
              <div class="p-15 border-bottom">
                <h6 class="font-medium m-b-10">Select Layout</h6>
                <div class="selectgroup layout-color w-50">
                  <label class="selectgroup-item">
                    <input type="radio" name="value" value="1" class="selectgroup-input-radio select-layout" checked>
                    <span class="selectgroup-button">Light</span>
                  </label>
                  <label class="selectgroup-item">
                    <input type="radio" name="value" value="2" class="selectgroup-input-radio select-layout">
                    <span class="selectgroup-button">Dark</span>
                  </label>
                </div>
              </div>
              <div class="p-15 border-bottom">
                <h6 class="font-medium m-b-10">Sidebar Color</h6>
                <div class="selectgroup selectgroup-pills sidebar-color">
                  <label class="selectgroup-item">
                    <input type="radio" name="icon-input" value="1" class="selectgroup-input select-sidebar">
                    <span class="selectgroup-button selectgroup-button-icon" data-toggle="tooltip"
                      data-original-title="Light Sidebar"><i class="fas fa-sun"></i></span>
                  </label>
                  <label class="selectgroup-item">
                    <input type="radio" name="icon-input" value="2" class="selectgroup-input select-sidebar" checked>
                    <span class="selectgroup-button selectgroup-button-icon" data-toggle="tooltip"
                      data-original-title="Dark Sidebar"><i class="fas fa-moon"></i></span>
                  </label>
                </div>
              </div>
              <div class="p-15 border-bottom">
                <h6 class="font-medium m-b-10">Color Theme</h6>
                <div class="theme-setting-options">
                  <ul class="choose-theme list-unstyled mb-0">
                    <li title="white" class="active">
                      <div class="white"></div>
                    </li>
                    <li title="cyan">
                      <div class="cyan"></div>
                    </li>
                    <li title="black">
                      <div class="black"></div>
                    </li>
                    <li title="purple">
                      <div class="purple"></div>
                    </li>
                    <li title="orange">
                      <div class="orange"></div>
                    </li>
                    <li title="green">
                      <div class="green"></div>
                    </li>
                    <li title="red">
                      <div class="red"></div>
                    </li>
                  </ul>
                </div>
              </div>
              <div class="p-15 border-bottom">
                <div class="theme-setting-options">
                  <label class="m-b-0">
                    <input type="checkbox" name="custom-switch-checkbox" class="custom-switch-input"
                      id="mini_sidebar_setting">
                    <span class="custom-switch-indicator"></span>
                    <span class="control-label p-l-10">Mini Sidebar</span>
                  </label>
                </div>
              </div>
              <div class="p-15 border-bottom">
                <div class="theme-setting-options">
                  <label class="m-b-0">
                    <input type="checkbox" name="custom-switch-checkbox" class="custom-switch-input"
                      id="sticky_header_setting">
                    <span class="custom-switch-indicator"></span>
                    <span class="control-label p-l-10">Sticky Header</span>
                  </label>
                </div>
              </div>
              <div class="mt-4 mb-4 p-3 align-center rt-sidebar-last-ele">
                <a href="#" class="btn btn-icon icon-left btn-primary btn-restore-theme">
                  <i class="fas fa-undo"></i> Restore Default
                </a>
              </div>
            </div>
          </div>
        </div>
      </div>
      <footer class="main-footer">
        <div class="footer-left">
          <a href="https://visuretech.com">Visure Tech Limited</a></a>
        </div>
        <div class="footer-right">
        </div>
      </footer>
    </div>
  </div>
  <!-- General JS Scripts -->
  <script src="assets/js/app.min.js"></script>
  <!-- JS Libraies -->
  <script src="assets/bundles/datatables/datatables.min.js"></script>
  <script src="assets/bundles/datatables/DataTables-1.10.16/js/dataTables.bootstrap4.min.js"></script>
  <script src="assets/bundles/jquery-ui/jquery-ui.min.js"></script>
  <!-- Page Specific JS File -->
  <script src="assets/js/page/datatables.js"></script>
  <!-- Template JS File -->
  <script src="assets/js/scripts.js"></script>
  <!-- Custom JS File -->
  <script src="assets/js/custom.js"></script>
  <script>
  function deleteItem(id) {
    if (confirm('Are you sure you want to delete this item?')) {
        $.ajax({
            url: 'delete.php',
            type: 'POST',
            dataType: 'json',
            data: { id: id },
            success: function(response) {
                if (response.success) {
                    alert('Item deleted successfully.');
                    location.reload(); // Reload the page to see the changes
                } else {
                    alert('Failed to delete the item.');
                }
            },
            error: function(xhr, status, error) {
                console.error(xhr.responseText);
                alert('Error occurred during deletion: ' + xhr.responseText);
            }
        });
    }
  }
</script>
<script>
function exportTableToCSV() {
    var csv = [];
    var rows = document.querySelectorAll("table tr");

    for (var i = 0; i < rows.length; i++) {
        var row = [], cols = rows[i].querySelectorAll("td, th");

        for (var j = 0; j < cols.length; j++) {
            row.push(cols[j].innerText);
        }

        csv.push(row.join(",")); 
    }

    // Download CSV file
    downloadCSV(csv.join("\n"), 'table_data.csv');
}

function downloadCSV(csv, filename) {
    var csvFile;
    var downloadLink;

    // CSV file
    csvFile = new Blob([csv], {type: "text/csv"});

    // Create download link
    downloadLink = document.createElement("a");

    // File name
    downloadLink.download = filename;

    // Link to the file
    downloadLink.href = window.URL.createObjectURL(csvFile);

    // Hide the link
    downloadLink.style.display = "none";

    // Add the link to the DOM
    document.body.appendChild(downloadLink);

    // Click the link to download
    downloadLink.click();
}
</script>
</body>


<!-- datatables.html  21 Nov 2019 03:55:25 GMT -->
</html>