<?php  include "header.php";


if(isset($_GET['scheme'])){
    $scheme_no = $_GET['scheme'];
}else{
    $scheme_no = "";
}

?>

  
<div class="content-wrapper">
    
        <!--<section class="content-header">
            <h1>Users List</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active"> Users List</li>            
            </ol>
        </section>-->

        <div class="clearfix"></div>
        <div class="col-lg-12 alerts">
            <div id="custom-alerts" style="display:none;">
                <div class="alert alert-dismissable">
                    <div class="custom-msg"></div>
                </div>
            </div>
            
            <?php if(isset($_SESSION['message']) && $_SESSION['message']!=""){ ?>
            <div class="alert alert-success alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4>    <i class="icon fa fa-check"></i> Success</h4>
                <?=$_SESSION['message'];?>
            </div>
            <?php  unset($_SESSION['message']); } ?>
            
            
            <?php if ($error)  { ?>
            <div class="alert alert-danger alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-ban"></i> Error</h4>
                <?= $error; ?>
            </div>
            <?php } if ($warning) { ?>
            <div class="alert alert-warning alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-warning"></i> Warning</h4>
                <?= $warning; ?>
            </div>
            <?php } if ($message) { ?>
            <div class="alert alert-success alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4>    <i class="icon fa fa-check"></i> Success</h4>
                <?= $site_name; ?>
            </div>
            <?php } ?>
        </div>
        




<section class="content">
    <?php if($session_id=="1"){ ?>
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary" style="box-shadow: rgba(14, 30, 37, 0.12) 0px 2px 4px 0px, rgba(14, 30, 37, 0.32) 0px 2px 16px 0px;">
                <div class="box-header">

            
        <?php if($session_id=="1"){ ?> 
                <a  href="customers-scheme-add.php" class="btn btn-success btn-sm pull-right"><i class="fa fa-plus-circle"></i> Add New Customers Scheme</a>
        <?php } ?>
        <span class="btn btn-default btn-sm pull-right filter" style="margin-right: 5px;"> Show/Hide Filter</span>
        
        
                    <h3 class="box-title">Customers Scheme List</h3>
                </div>
                <div class="box-body">
                    
                    
    <div class="row" id="filter">
    
    
    <div class="col-md-3">
        <div class="form-group">
            <label class="" for="customer_id">Customers</label>                                   
            <select name="customer_id" class="form-control select2" style="width:100%" id="customer_id" required="required">
                <option value="">All Customers</option>
                <?php $statement = $pdo->prepare("SELECT * FROM `tbl_customers` where status=1");
                    $statement->execute(array());
                    $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                    foreach ($resultsss as $ress) { ?>
                        <option value="<?=$ress['id']?>"><?=$ress['customer_name']?> - <?=$ress['mobile_no']?></option>
                <?php  }  ?>
            </select>
        </div>
    </div>
    
    
    <div class="col-md-3">
        <div class="form-group">
            <label class="" for="cus_scheme_id">Scheme ID</label>                                   
            <select name="cus_scheme_id" class="form-control select2" style="width:100%" id="cus_scheme_id" required="required">
                <option value="">All Scheme ID</option>
                <?php $statement = $pdo->prepare("SELECT * FROM `tbl_customer_scheme` where status=1");
                    $statement->execute(array());
                    $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                    foreach ($resultsss as $ress) { ?>
                        <option value="<?=$ress['id']?>" <?php echo (isset($scheme_no) && $scheme_no == $ress['scheme_no'] ? 'selected' : '') ?>><?=$ress['scheme_no']?></option>
                <?php  }  ?>
            </select>
        </div>
    </div>
    
    <div class="col-md-3">
        <div class="form-group">
            <label class="" for="scheme_id">Schemes</label>                                   
            <select name="scheme_id" class="form-control select2" style="width:100%" id="scheme_id" required="required">
                <option value="">All Schemes</option>
                <?php $statement = $pdo->prepare("SELECT * FROM `tbl_schemes` where status=1");
                    $statement->execute(array());
                    $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                    foreach ($resultsss as $ress) { ?>
                        <option value="<?=$ress['id']?>"><?=$ress['name']?> - <?=number_format($ress['amount'],2)?></option>
                <?php  }  ?>
            </select>
        </div>
    </div>
    

    
    <div class="col-md-3">
        <div class="form-group">
            <label for="payment_month">Payment Month</label>
            <select name="payment_month" class="form-control select2 payment_month" style="width:100%" id="payment_month" required>
                <option value=""<?php echo (isset($scheme_no) ? 'selected' : '') ?> >All Months</option>
                <?php 
                    for($i=1;$i<=12;$i++){
                        if($i==date('m')){
                            echo "<option value='".$i."' ".(!isset($scheme_no) ? "selected" : "").">".str_pad($i, 2, "0", STR_PAD_LEFT)."-".date("F", mktime(0, 0, 0, $i, 10))."</option>";
                        }else{
                            echo "<option value='".$i."'>".str_pad($i, 2, "0", STR_PAD_LEFT)."-".date("F", mktime(0, 0, 0, $i, 10))."</option>";   
                        }
                    }
                ?>
                
            </select>
        </div>
    </div>
    
    <div class="col-md-2">
        <div class="form-group">
            <label class="control-label" for="year">Year</label>
            <select name="year" class="form-control select2" style="width:100%" id="year">
                <option value="2023" <?= (date('Y')==2023) ? "selected":"" ?>>2023</option>
                <option value="2024" <?= (date('Y')==2024) ? "selected":"" ?>>2024</option>
                <option value="2025" <?= (date('Y')==2025) ? "selected":"" ?>>2025</option>
                <option value="2026" <?= (date('Y')==2026) ? "selected":"" ?>>2026</option>
            </select>
        </div>
    </div>
    
    <div class="col-md-3" hidden>
        <div class="form-group">
            <label class="control-label" for="status">Status</label>
            <select name="status" class="form-control select2" style="width:100%" id="status">
                <option value="1">Active</option>
                <option value="0">In-Active</option>
                <option value="">All Status</option>
            </select>
        </div>
    </div>
    
    
    <div class="col-md-3">
        <div class="form-group">
            <label class="" for="payment_type">Payment Type</label>                                   
            <select name="payment_type" class="form-control select2" style="width:100%" id="payment_type" required="required">
                <option value="">All Payment Type</option>
                <?php $statement = $pdo->prepare("SELECT * FROM `tbl_payment_type` where status=1");
                    $statement->execute(array());
                    $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                    foreach ($resultsss as $ress) { ?>
                        <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
                <?php  }  ?>
            </select>
        </div>
    </div>
    
    
    
    <div class="col-md-3">
        <div class="form-group">
            <label class="control-label" for="payment_status">Payment Status</label>
            <select name="payment_status" class="form-control select2" style="width:100%" id="payment_status">
                <option value="">All Payment Status</option>
                <option value="1">Payment Completed</option>
                <option value="2">Payment Pending</option>
            </select>
        </div>
    </div>
    
    
    
    </div>


<div class="clearfix"></div>


                    <div class="table-responsive">
                        <table id="StData" class="table table-striped table-bordered table-condensed table-hover" style="margin-bottom:5px;">
                            <thead>
                                <tr>
                                    <td colspan="11" class="p0"><input type="text" class="form-control b0" name="search_table" id="search_table"  placeholder="Search Here" style="width:100%;background-color: darkblue;color: white;"></td>
                                </tr>
                                <tr class="active">
                                     <th style="min-width:150px;">Actions</th>
                                    <th>S.No</th>
                                    <th  style="min-width:150px;">Customer Name</th>
                                    <th  style="min-width:100px;">Mobile Number</th>
                                    <th  style="min-width:100px;">Scheme ID</th>
                                    <th  style="min-width:100px;">Scheme Details</th>
                                    <th  style="min-width:100px;">Amount</th> 
                                    <th  style="min-width:200px;">Payment Month - Year</th>
                                    <th  style="min-width:100px;">Payment Type</th>
                                    <th  style="min-width:150px;">Reference</th>
                                    <th  style="min-width:150px;">Created At</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td colspan="10" class="dataTables_empty"><center> Loading Pls Wait Data</center></td>
                                </tr>
                            </tbody>
                            
                        </table>
                    </div>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
<?php }else{ ?>

<?=$page_not_access?>

<?php } ?>
</section>

<?php include "footer.php";?>
<script>
    $(document).ready(function(){
       /* $('#filter').hide();*/
        $('.filter').click(function(){
            $("#filter").slideToggle();
            return false;
        });
    });
</script>

<script type="text/javascript">
<?php
$columns = '0, 1, 2, 3, 4, 5, 6, 7, 8, 9';
$data_table = "var search_table = $('#search_table').val();
              var status = $('#status').val();
              var customer_id = $('#customer_id').val();
              var cus_scheme_id = $('#cus_scheme_id').val();
              var scheme_id = $('#scheme_id').val();
              var payment_month = $('#payment_month').val();
              var year = $('#year').val();
              var payment_status = $('#payment_status').val();
              var payment_type = $('#payment_type').val();
              
              
            var table = $('#StData').DataTable({

                    'ajax' : { 
                        url: 'reports-data-tables/scheme-payments-data-table.php', 
                        data:{search:search_table, status:status, customer_id:customer_id, cus_scheme_id:cus_scheme_id, scheme_id:scheme_id, payment_month:payment_month, year:year, payment_status:payment_status, payment_type:payment_type}, 
                        type: 'POST'},
                        bFilter: true,
                        bDestroy: true,
                        searching: true,
                    'buttons': [
                    { extend: 'copyHtml5', 'footer': false, exportOptions: { columns: [ $columns ] } },
                    { extend: 'excelHtml5', 'footer': false, exportOptions: { columns: [$columns] } },
                    { extend: 'csvHtml5', 'footer': false, exportOptions: { columns: [ $columns ] } },
                    { extend: 'pdfHtml5', orientation: 'landscape', pageSize: 'A4', 'footer': false,
                    exportOptions: { columns: [ $columns ] } },
                    { extend: 'colvis', text: 'Columns'},
                    ]

                });";

?>
    $(document).ready(function() {        
        emp_data();
        function emp_data(){
            <?=$data_table?>
        }
        
        $('#search_table').keyup(function() {  
            emp_data();
            document.getElementById("search_table").focus();
        });
        
        $('#status, #customer_id, #cus_scheme_id, #scheme_id, #payment_month, #year, #payment_status, #payment_type').change(function() {
            emp_data();
        });

    });
    function delete_Details(id){
            var result = confirm("Confirm Want to Delete This Scheme?");
            if (result) {
                    $.ajax({
                    type: 'POST',
                    url: 'api/delete.php',
                    data: {type:'customer_scheme_delete',delete_id:id},                  
                    success: function(response) {
                        console.log(response); 
                        emp_datas();
                    }
                });
            }
    }
    function emp_datas(){
            <?=$data_table?>
    }
</script>