<?php
session_start();
include '../assets/constant/config.php';

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>

<?php include('include/sidebar.php'); ?>
<!-- Top Bar End -->
<?php include('include/header.php'); ?>

<!--  connect author at mdkhairnar92@gmail.com  -->
<div class="page-content-wrapper ">

    <div class="container-fluid">

        <div class="row">
            <div class="col-sm-12">
                <div class="page-title-box">
                    <div class="btn-group float-right">
                        <?php if (in_array('add_campaign',$userroles) || ($admin['role']==0))
                  { ?>
                     <li><a href="add_campaign.php" class="btn btn-primary mb-3">Add Campaign</a></li>
                  <?php } ?> 
                       
                    </div>
                    <h4 class="page-title">Campaign </h4>
                </div>
            </div>
            <div class="clearfix"></div>
        </div>
        <!-- end page title end breadcrumb -->

        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        <div class="table-responsive">

                            <table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap" style="border-collapse: collapse; border-spacing: 0; width: 100%;" enctype="multipart/form-data">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Name </th>
                                        <th>Date</th>
                                        <th>Entries</th>
                                        <th>Progress</th>
                                        
                                        <th>Notes</th>
                                        <th>Status</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>

                                    <?php
                                    $stmt = $conn->prepare("SELECT * FROM `campaign` WHERE delete_status='0' ");
                                    $stmt->execute();
                                    $record = $stmt->fetchAll();
                                    $i = 1;
                                    foreach ($record as $key) { ?>


                                        <tr>

                                            <td><?php echo $key['id']; ?></td>
                                            
                                       
                                     <td><?php echo $key['cam_name']; ?></td>

                                           <td><h6>Start date:</h6><?php echo $key['sdate']; ?>,
                                        <h6>End date:</h6><?php echo $key['edate']; ?></td>
                                           





                                          <?php
// Execute the SQL query to count members for each campaign
$stmt1 = $conn->prepare("SELECT c.id, c.cam_name, COUNT(m.id) AS member_count FROM member m LEFT JOIN campaign c ON c.id = m.campaign GROUP BY c.id ,c.cam_name AND c.delete_status= '0'");
$stmt1->execute();

// Fetch the first row (if exists) from the result set
$record1 = $stmt1->fetch(PDO::FETCH_ASSOC);

// Check if $record1 is not false (i.e., if the query returned rows)
if ($record1 !== false) {
    // Output the total number of members
    echo "<td>" . $record1['member_count'] . "</td>";
} else {
    // If no rows returned, output a default value or handle the case accordingly
    echo "<td>No members</td>";
}
?>

                                                   
                                            <td>Goal:<?php echo $key['goal']; ?></td>   
                                                   
                                    
                                             <td><?php echo $key['notes'];?></td> 
                                             <!-- <td><?php echo $key['status']; ?></td>   -->
                                                <td><?php if($key['status']=="1"){?>
                                                    <span class="btn btn-danger toggle-on">Close</span>

                                                <?php }elseif($key['status']=="0"){ ?>
                                                    <span class="btn btn-success toggle-on">Open</span>
                                             <?php   $i++; } ?>
                                            </td>






<td>
<div class="dropdown">
   <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class=></i>Details</button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">

<a href="#"onclick="editForm(event, <?php echo $key['id'] ?>,'show_campaign.php')" class="dropdown-item" href="#">Show</a>

<?php if (in_array('edit_campaign',$userroles) || ($admin['role']==0))
                  { ?>
    <a href="#"onclick="editForm(event, <?php echo $key['id'] ?>,'edit_campaign.php')" class="dropdown-item" href="#"> Edit</a><?php }?>
    <a href="#"onclick="return confirm('Are you sure you want to delete this item?') && delForm1(event, <?php echo $key['id'] ?>,'app/campaign_crud.php')"class="dropdown-item" href="#")>Delete</a>
   
  </div>
</div>


  </td>
                                        </tr>
                         <?php } ?>
                                </tbody>
                               
                            </table>
 </div><!--end /tableresponsive-->
                    </div><!--end card-body-->
                </div><!--end card-->
            </div><!--end col-->


        </div><!--end row-->

 </div> <!-- Page content Wrapper -->

</div> <!-- content -->

<?php include('include/footer.php'); ?>
<!--  Author Name: Mayuri K. 
 for any PHP, Codeignitor or Laravel website or software development contact me at mayuri.infospace@gmail.com  -->

