<?php header('Content-Type: text/html; charset=utf-8'); ?>
<aside class="right-side">
    <section class="content-header">
        <div class="title_bar">
            <h1 class="major_txt_color"><?php echo $title;?></h1>
            <div class="breadcrumb_container"><?php echo breadcrumb_links();?></div>
        </div>
    </section>
    <?php $general = get_general_setting();
    #$this->statistical_report_model->get_test_array();
    ?>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <!-- /.box -->
                <div class="box box-primary">
                    <div class="box-body table-responsive">
                        <form name="get_data_search" action="<?php echo $form_action;?>" method="post" class="search_form validate_form">
                            <input type="hidden" name="hidden_id" class="hidden_id" value="<?php echo (!empty($hidden_id)) ? $hidden_id : '';?>" />
                             
                            <div class="col-xs-12">
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label >Devotee Mobile No</label>
                                        <div class="input-group">
                                            <div class="input-group-addon">
                                                <i class="fa fa-phone"></i>
                                            </div>
                                            <input type="text" class="mobile_no" style="padding: 6px 12px;margin-right:5%;width:100%;" id="mobile_no" name="mobile_no" value="<?php echo (!empty($mobile)) ? $mobile : '';?>" autocomplete="off" >
                                            
                                        </div>
                                    </div>
                                    
                                </div>
                                
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label>Top</label>
                                        <input type="text" class="top" style="padding: 6px 12px;margin-right:5%;width:100%;" id="top" name="top" value="<?php echo (!empty($top)) ? $top : '';?>" autocomplete="off" >
                                    </div>
                                </div>
                                <?php $startMonth = (!empty($start_month)) ? $start_month : date('m'); ?>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label >Start Month</label>
                                        <select class="form-control select2" name="start_month">
                                            <option value="">Month</option>
                                            <?php foreach($month_list as $key=>$value) {?>
                                                <option value="<?php echo $key;?>" <?php echo ($startMonth == $key ) ? 'selected' : '';?>><?php echo $value;?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>
                                
                                <?php $startYear = (!empty($start_year)) ? $start_year : date('Y'); ?>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label >Start Year</label>
                                        <select class="form-control select2" name="start_year">
                                            <option value="">Year</option>
                                            <?php for($i = date('Y',strtotime('-10 year')); $i <= date('Y'); $i++){?>
                                                <option value="<?php echo $i;?>" <?php echo ($i == $startYear) ? 'selected' : '' ?>><?php echo $i;?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            
                            <?php $endMonth = (!empty($end_month)) ? $end_month : date('m'); ?>
                            <div class="col-xs-12">
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label >End Month</label>
                                        <select class="form-control select2" name="end_month">
                                            <option value="">Month</option>
                                            <?php foreach($month_list as $key=>$value) {?>
                                                <option value="<?php echo $key;?>" <?php echo ($endMonth == $key ) ? 'selected' : '';?>><?php echo $value;?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>
                                <?php $endYear = (!empty($end_year)) ? $end_year : date('Y'); ?>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label >End Year</label>
                                        <select class="form-control select2" name="end_year">
                                            <option value="">Year</option>
                                            <?php for($ii = date('Y',strtotime('-10 year')); $ii <= date('Y'); $ii++){?>
                                                <option value="<?php echo $ii;?>" <?php echo ($ii == $endYear) ? 'selected' : '' ?> ><?php echo $ii;?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="col-xs-12">
                                <div class="col-md-2">&nbsp;</div>
                                <div class="col-md-2">
                                    <input type="submit" value="Get Summary Data" name="submit" class="btn btn-danger btn-sm" style="background-color: #e33739;
    border-color: #e33739;" />
                                </div>
                                <div class="col-md-2">
                                    <input type="submit" value="Print Summary Data" name="submit" class="btn btn-primary btn-sm" />
                                </div>
                                <div class="col-md-2">
                                    <input type="submit" value="Get Details Data" name="submit" class="btn btn-danger btn-sm" style="background-color: #e33739;
    border-color: #e33739;" />
                                </div>
                                <div class="col-md-2">
                                    <input type="submit" value="Print Details Data" name="submit" class="btn btn-primary btn-sm" />
                                </div>
                                <div class="col-md-2">&nbsp;</div>
                            </div>
                            
                            <!-- Filtered Columns to send JS - Start -->
                            <div class="col-xs-12" style="margin-bottom:4%;">
                                <div class="col-xs-12 blck" style="margin-bottom: -35px;">
                                    <div class="sub-title" style="color:#0B45B3; background:#f3f3f3 !important;" ><h4><i class="fa fa-fire" style="font-size: 14px;"></i> Filter Columns</h4><i class="ctrl opener opened fa fa-chevron-up" style="float: right; margin-top: -25px;"></i></div>
                                    <div class="blck-content col-xs-12" style="border-bottom: 1px inset rgba(209, 216, 216, 0.6); background: rgba(3, 12, 11, 0.05); margin-top: -15px;">
                                        <div id="grpChkBox">
                                            <?php $start_month1 = $start_month;                                               
                                                for($j=$start_year; $j<=$end_year; $j++) {
                                                    for($oj=$start_month1; $oj<=12; $oj++) {
                                                        if(($oj > $end_month && $j == $end_year) || ($oj < $start_month1 && $j == $start_year)) { 
                                                           continue; 
                                                        }
                                                       $start_month1 = ($oj == 12) ? 1 : $start_month1;
                                                        $D = '01-'.$oj .'-' . $j;?>
                                                        <div class="col-md-2" style="margin: 1%;">
                                                            <input type="hidden" name="month_years[]" value="<?php echo str_replace(' ','_',date('F Y',strtotime($D)));?>" />
                                                            
                                                            <input type="checkbox" class="check" value="<?php echo date('F Y',strtotime($D));?>" name="<?php echo str_replace(' ','_',date('F Y',strtotime($D)));?>" <?php echo ((!empty($monthYear)) && (in_array(date('F Y',strtotime($D)),$monthYear))) ? 'checked' : '';?> />&nbsp;<?php echo date('F Y',strtotime($D));?>
                                                        </div>
                                              <?php }    
                                                }
                                            ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- Filtered Columns to send JS - End -->
                            
                        </form>
                    </div>
                    <div class="box-body table-responsive body_content">
                    <?php if($data_type == 'Get Summary Data') { ?>
                        <table class="table table-bordered table-striped data_table" id="someTable">
                            <thead>
                                <tr>
                                    <th>S.No</th>
                                    <th>Devotee Mobile</th>
                                    <th>Devotee Name</th>
                                    <?php $start_month2 = $start_month;
                                    for($m=$start_year; $m<=$end_year; $m++) {
                                        for($om=$start_month2; $om<=12; $om++) {
                                            if(($om > $end_month && $m == $end_year) || ($om < $start_month2 && $o == $start_year)) { 
                                               continue; 
                                            }
                                            $start_month2 = ($om == 12) ? 1 : $start_month2;
                                            $D = '01-'.$om .'-' . $m;?>
                                            <th class="align-center <?php echo str_replace(' ','_',date('F Y',strtotime($D)));?>"><?php echo date('F Y',strtotime($D));?></th>
                                  <?php }
                                    }
                                    ?>
                                    <th>Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach($results as $key=>$value) { $total_amt = 0;?>
                                    <tr>
                                        <td><?php echo ++$key;?></td>
                                        <td><?php echo $value->bd_contact_no;?></td>
                                        <td><?php echo $value->bd_name;?></td>
                                        <?php $start_month6 = $start_month;
                                            for($m=$start_year; $m<=$end_year; $m++) {
                                                for($om=$start_month6; $om<=12; $om++) {
                                                    if(($om > $end_month && $m == $end_year) || ($om < $start_month6 && $m == $start_year)) { 
                                                       continue; 
                                                    }
                                                    $start_month6 = ($om == 12) ? 1 : $start_month6;
                                                    $D = '01-'.$om .'-' . $m;
                                                    $mY = $om .'-' . $m;
                                                    $details = $this->statistical_report_model->get_monthwise_data($mY,$value->bd_contact_no,$top,1);
                                                    ?>
                                                    <td class="align-right <?php echo str_replace(' ','_',date('F Y',strtotime($D)));?>"><?php
                                                        if(count($details) > 0) { $month_amt = 0;
                                                            foreach($details as $index=>$val) { 
                                                                $total_amt +=$val->amount; 
                                                                $month_amt +=$val->amount; 
                                                            }
                                                            echo number_format($month_amt,2,'.','');
                                                        } else {
                                                            echo '0.00';
                                                        } ?>
                                                    </td>
                                           <?php  }
                                            }
                                        ?>
                                        <td class="align-right"><?php echo number_format($total_amt,2,'.','');?></td>
                                    </tr>
                                <?php }?>
                            </tbody>
                            <tfoot>
                            </tfoot>
                        </table>
                    <?php } elseif($data_type == 'Get Details Data') { ?>
                        <table class="table table-bordered table-striped filterTable" id="someTable">
                            <thead>
                                <tr>
                                    <th width="5%" class="align-center">&nbsp;</th>
                                    <th width="5%" class="align-center">S.No</th>
                                    <th class="align-center">Devotee Mobile</th>
                                    <th class="align-center">Devotee Name</th>
                                    <?php $start_month3 = $start_month;
                                    for($m=$start_year; $m<=$end_year; $m++) {
                                        for($om=$start_month3; $om<=12; $om++) {
                                            if(($om > $end_month && $m == $end_year) || ($om < $start_month3 && $m == $start_year)) { 
                                               continue; 
                                            }
                                            $start_month3 = ($om == 12) ? 1 : $start_month3;
                                            $D = '01-'.$om .'-' . $m;?>
                                            <th class="align-center <?php echo str_replace(' ','_',date('F Y',strtotime($D)));?>"><?php echo date('F Y',strtotime($D));?></th>
                                   <?php } 
                                    }
                                    ?>
                                    <th>Total</th>
                            </thead>
                            
                            <tbody>
                                <?php $start_month4 = $start_month; 
                                foreach($results as $key=>$value) { 
                                    $total_amt = 0; 
                                    $statistical_data = array(); 
                                    $value->count_str = FALSE;
                                    for($m=$start_year; $m<=$end_year; $m++) {
                                        for($om=$start_month4; $om<=12; $om++) {
                                            if(($om > $end_month && $m == $end_year) || ($om < $start_month4 && $m == $start_year)) { 
                                               continue;
                                            }                                            
                                            $start_month4 = ($om == 12) ? 1 : $start_month4;
                                            $mY = $om .'-' . $m;
                                            $details_data = $this->statistical_report_model->get_monthwise_data($mY,$value->bd_contact_no,$top,2);
                                            array_push($statistical_data, $details_data);
                                            if(count($details_data) > 0) {
                                                $value->count_str = TRUE;
                                            }
                                        } 
                                    }
                                    
                                ?>                                
                                    <tr>
                                        <td>
                                            <?php if($value->count_str) { ?>
                                                <div class="expand_icon plus"></div>
                                            <?php } ?>
                                        </td>
                                        <td><?php echo ++$key;?></td>
                                        <td><?php echo $value->bd_contact_no;?></td>
                                        <td><?php echo $value->bd_name;?></td>
                                        <?php $start_month5 = $start_month;
                                        for($m=$start_year; $m<=$end_year; $m++) {
                                            for($om=$start_month5; $om<=12; $om++) {
                                                if(($om > $end_month && $m == $end_year) || ($om < $start_month5 && $m == $start_year)) { 
                                                   continue; 
                                                }
                                                $start_month5 = ($om == 12) ? 1 : $start_month5;
                                                $D = '01-'.$om .'-' . $m;
                                                $mY = $om .'-' . $m;
                                                $details = $this->statistical_report_model->get_monthwise_data($mY,$value->bd_contact_no,$top,1);
                                                ?>
                                                <td class="align-right <?php echo str_replace(' ','_',date('F Y',strtotime($D)));?>"><?php
                                                    if(count($details) > 0) { $month_amt = 0;
                                                        foreach($details as $index=>$val) { 
                                                            $total_amt +=$val->amount; 
                                                            $month_amt +=$val->amount; 
                                                        }
                                                        echo number_format($month_amt,2,'.','');
                                                    } else {
                                                        echo '0.00';
                                                    } ?>
                                                </td>
                                        <?php } 
                                        }
                                        ?>
                                        <td class="align-right"><?php echo number_format($total_amt,2,'.','');?></td>
                                    </tr>
                                    
                                    <?php if($value->count_str) { ?>        
                                        <tr class="remove_container">
                                            <td colspan="6" class="expand_container">
                                                <div class="tbl-expand" style="color:#0B45B3;">
                                                    <table class="table table-bordered table-striped" style="border: 3px solid #ddd !important;">
                                                       <!--<thead>-->
                                                            <tr>
                                                                <th width="10%" class="align-center">S.No</th>
                                                                <th class="align-center">Date</th>
                                                                <th class="align-center">Type</th>
                                                                <th class="align-center">Amount</th>
                                                            </tr>
                                                        <!--</thead>-->
                                                        <tbody>
                                                            <?php foreach($statistical_data as $k=>$val) { 
                                                                foreach($val as $index=>$row) { ?>
                                                                    <tr class="subTable">
                                                                        <td><?php echo ++$index;?></td>
                                                                        <td><?php echo $row->date_str;?></td>
                                                                        <td><?php echo $row->service_name;?></td>
                                                                        <td class="align-right"><?php echo $row->amount;?></td>
                                                                    </tr>
                                                            <?php } } ?>
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php } ?>
                                <?php } ?>
                            </tbody>
                        </table>
                    <?php } ?> 
                    </div><!-- /.box-body -->
                    
                </div><!-- /.box -->
            </div>
        </div>
    </section><!-- /.content -->
</aside><!-- /.right-side -->
<!-- ./wrapper -->

<script type="text/javascript">
    $(function () {
        var $chk = $("#grpChkBox input[type='checkbox']");
        var $tbl = $("#someTable");
        
        if($('.hidden_id').val() == '') {
            $("#grpChkBox > div:nth-child(-n+8) input[type='checkbox']").prop('checked', true);
        }
        
        $(".check").each(function() {
            if($(this).is(':checked')) {
                var colToShow = $("#someTable").find("." + $(this).attr("name"));
                $(colToShow).show();
                var $chkd_length = $('#grpChkBox input[type="checkbox"]:checked').length;
                $('#someTable .remove_container .expand_container').attr('colspan',(parseInt(5) + parseInt($chkd_length)));
            } else {
                var colToHide = $("#someTable").find("." + $(this).attr("name"));
                $(colToHide).hide();
            }
        });
        
        $chk.on('ifUnchecked', function(event){
            var colToHide = $tbl.find("." + $(this).attr("name"));
            $(colToHide).hide();
        });
        
        $chk.on('ifChecked', function(event){
            var colToShow = $tbl.find("." + $(this).attr("name"));
            $(colToShow).show();
            var $chkd_length = $('#grpChkBox input[type="checkbox"]:checked').length;
            $('#someTable .remove_container .expand_container').attr('colspan',(parseInt(5) + parseInt($chkd_length)));
        });
        
        $('.expand_icon').on('click', function() {
            if ($(this).closest('tr').next().children().children().hasClass('tbl-expand')) {
                if ($(this).hasClass('plus')) {
                    $(this).closest('tr').next().children().children().slideDown('1000');
                    $(this).removeClass('plus').addClass('minus');
                }
                else {
                    $(this).closest('tr').next().children().children().slideUp();
                    $(this).removeClass('minus').addClass('plus');
                }
            }
        });
        
        var $chkd_length = $('#grpChkBox input[type="checkbox"]:checked').length;
        $('#someTable .remove_container .expand_container').attr('colspan',(parseInt(5) + parseInt($chkd_length)));
        
        $('[name="end_year"]').change(function() {
            var end_year = $(this).val();
            var start_year = $('[name="start_year"]').val();
            if(parseInt(start_year) > parseInt(end_year)) {
                $(this).val(start_year);
            }
        });
        
        $('[name="start_year"]').change(function() {
            var start_year = $(this).val();
            var end_year = $('[name="end_year"]').val();
            if(parseInt(start_year) > parseInt(end_year)) {
                $('[name="end_year"]').val(start_year);
            }
        });
        
        var end_year = $('[name="end_year"]').val();
        var start_year = $('[name="start_year"]').val();
       $('[name="end_month"]').change(function() {
            var end_month = $(this).val();
            var start_month = $('[name="start_month"]').val();
            
            if(start_year == end_year && start_month > end_month) {
                alert('End Month should be greater than Start Month!');
                $(this).val(start_month);
            }
        });
        
        $('[name="start_month"]').change(function() {
            var start_month = $(this).val();
            var end_month = $('[name="end_month"]').val();
            
            if(start_year == end_year && start_month > end_month) {
                alert('Start Month should be less than End Month!');
                $(this).val(end_month);
            }
        });
    });
     
    
</script>