<?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_batch',$userroles) || ($admin['role']==0))
                  { ?>
                     <li><a href="add_batch.php" class="btn btn-primary mb-3">Add batch</a></li>
                  <?php } ?>
                        
                    </div>
                    <h4 class="page-title">batch</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%;" >
                                <thead>
                                    <tr>
                                        <th>SN</th>
                                         <th>Name</th>
                                          <th>date</th>
                                           <th>status</th>
                                          <th>entries</th>
                                           <th>total</th>

                                         <th>Action</th>
                                        
                                    </tr>
                                </thead>
                                <tbody>

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


                                        <tr>

                                            <td><?php echo $i; ?></td>
                                           <td> <?php echo $key['name']; ?></td> 
                                                 <td> <?php echo $key['date']; ?></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> 
<?php
$stmt1= $conn->prepare("SELECT b.id, b.member, count(b.name) AS total FROM batch b JOIN member m ON m.batch_id = b.id GROUP BY b.id, b.name AND b.delete_status= '0' ");
                                    $stmt1->execute();
                                    $record1 = $stmt1->fetch();
                                   
                                  echo "<td>" . $record1['total'] . "</td>";?>

                                            


               <?php
$stmt1= $conn->prepare("SELECT b.id, b.name, b.amount, SUM(amount) AS total 
                        FROM batch b 
                        JOIN member m ON m.batch_id = b.id 
                        GROUP BY b.id, b.name AND b.delete_status= '0' ");
                                    $stmt1->execute();
                                    $record1 = $stmt1->fetch();
                                   
                                  echo "<td>" . $record1['total'] . "</td>";?>


                                           <td>

                                              <a href="#" onclick="editForm(event, <?php echo $key['id']; ?>,'edit_batch.php')" class="btn btn-primary waves-effect waves-light"><i class="fa fa-edit" aria-hidden="true"></i>
                                                </a>


                                                <a href="#" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this item?') && delForm1(event, <?php echo $key['id']; ?>,'app/batch_crud.php');"><i class="fa fa-trash-alt" aria-hidden="true"></i></a>

                                            </td>
                                        </tr>
                                    <?php $i++;
                                    } ?>
                                </tbody>
                                </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  -->

