<?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 submissions";
$counttotalinducted = "SELECT COUNT(*) as total_inducted FROM submissions WHERE cmo_induction_status = 'Inducted Member'";
$counttotanewinductee = "SELECT COUNT(*) as total_new_inductee FROM submissions WHERE cmo_induction_status = 'New Inductee'";
$pendingapproval = "SELECT COUNT(*) as total_pending_approval FROM submissions WHERE approval_status = 'In Progress'";


// 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_inducted = 0;
$result = $conn->query($counttotalinducted);
if ($result && $result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $total_inducted = $row["total_inducted"];
}

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

// 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, '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>Lacmon - 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/favicon.ico' />
  <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">
</head>

<body>
  <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/logo.jpeg"
                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 Lacmon 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.html"> <img alt="image" src="../images/logo.jpeg" class="header-logo" /> <span
                class="logo-name">LACMON</span>
            </a>
          </div>
          <ul class="sidebar-menu">
            <li class="menu-header">Menu</li>
            <li class="dropdown">
              <a href="#" class="nav-link"><i data-feather="monitor"></i><span>Data Page</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 Registration</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"> New Inductees</h5>
                          <h2 class="mb-3 font-18"><?php echo htmlspecialchars($total_inducted); ?></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">Old Inductees</h5>
                          <h2 class="mb-3 font-18"><?php echo htmlspecialchars($total_new_inductee); ?></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">
            <a href="export.php" class="btn btn-danger mb-3">Export to CSV</a>
            <button id="approveButton" onclick="performBulkApproval()" class="btn btn-success mb-3">Approve
                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>Submissions</h4>
                  </div>
                  <div class="card-body">
                    <div class="table-responsive">
                      <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>Title</th>
                            <th>First Name</th>
                            <th>Last Name</th>
                            <th>Phone Number</th>
                            <th>Email</th>
                            <th>ID Number</th>
                            <th>Year</th>
                            <th>Induction Status</th>
                            <th>Deanary</th>
                            <th>Parish</th>
                            <th>Blood group</th>
                            <th>Require Mattress</th>
                            <th>Require Accomodation</th>
                            <th>Accomodation duration</th>
                            <th>Marriage Duration</th>
                            <th>Approval Status</th>
                            <th>Passport</th>
                            <th>Action</th>
                            
                          </tr>
                        </thead>
                        <tbody>
                        <?php
                            // Connect to MySQL
                            include_once './dbconn.php';
                            
                            $sql = "SELECT * FROM `submissions` 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)) {
                                    echo "<tr>";
                                    // Add checkbox column
                                    echo "<td><input type='checkbox' name='selected[]' value='" . $row['id'] . "'></td>";
                                    // Output other table cells with data from database
                                    // Example: echo "<td>" . $row['column_name'] . "</td>";
                                    // Adjust other table cells accordingly
                                    echo "<td>" . $serial . "</td>";
                                    echo "<td>" . $row['title'] . "</td>";
                                    echo "<td>" . $row['first_name'] . "</td>";
                                    echo "<td>" . $row['last_name'] . "</td>";
                                    echo "<td>" . $row['phone'] . "</td>";
                                    echo "<td>" . $row['email'] . "</td>";
                                    echo "<td>" . $row['id_number'] . "</td>";
                                    echo "<td>" . $row['year'] . "</td>";
                                    echo "<td>" . $row['cmo_induction_status'] . "</td>";
                                    echo "<td>" . $row['deanery_name'] . "</td>";
                                    echo "<td>" . $row['parish_name'] . "</td>";
                                    echo "<td>" . $row['blood_group'] . "</td>";
                                    echo "<td>" . $row['do_you_require_a_mattress_to_sleep_over'] . "</td>";
                                    echo "<td>" . $row['do_you_require_a_hotel_accommodation'] . "</td>";
                                    echo "<td>" . $row['if_yes_click_a_range'] . "</td>";
                                    echo "<td>" . $row['how_long_have_you_been_canonically_married'] . "</td>";
                                    echo "<td>" . $row['approval_status'] . "</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='#' class='btn btn-primary' onclick='printIDCard(" . $row['id'] . ", \"" . $row['first_name'] . " " . $row['last_name'] . "\", \"" . $row['id_number'] . "\", \"" . $row['parish_name'] . "\", \"" . $row['blood_group'] . "\", \"" . $row['Created'] . "\", \"" . $row['passport_link'] . "\", \"" . $row['signature_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;
                            }
                        }


                        // Function to print ID card
                        function printIDCard(id, name, id_number, parish, blood_group, date_issued, image_url, sign_url) {
                            $.ajax({
                                url: 'idcard.php',
                                type: 'POST',
                                dataType: 'json',
                                data: {
                                    name: name,
                                    id_number: id_number,
                                    parish: parish, 
                                    blood_group: blood_group,
                                    date_issued: date_issued, 
                                    image_url: image_url,
                                    sign_url: sign_url
                                },
                                success: function(response) {
                                    var a = document.createElement('a');
                                    a.href = response.file_url;
                                    a.download = id_number; // 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>
</body>


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