<?php
$ti=time();
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=statistical_report".$ti.".xls");
header("Pragma: no-cache");
header("Expires: 0");
?>
<?php if($data_type == 'Excel 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 == 'Excel 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 } ?> 