<?php
$ti=time();
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Kattalai_archanai_report".$ti.".xls");
header("Pragma: no-cache");
header("Expires: 0");

#echo "<pre>";print_r($filters);echo "<br>";
#echo "<pre>";print_r($submit_type);echo "<br>";print_r($results);echo "</pre>";
#echo "</br> : ".count($item);
#echo "HERE"
#exit;

$field = count($filters);
?>                            
<?php if($submit_type == 'Get Data') { ?>   
    <table width="1000" border="1" cellpadding="3" cellspacing="0" class="filterTable" id="filterTable">
        <thead>
            <tr>
                <th style="padding:1%;">S.No</th>
                <?php
                    foreach($filters as $value){
                       echo '<th style="padding:1%;">'.$value.'</th>';
                    }
                ?>                
            </tr>
        </thead>
        <tbody>
            <?php $i=0; if(count($results) > 0) {  $total_amt = 0;?>
                <?php foreach($results as $key=>$value) { ?>
                    
                    <?php if($value->bki_day_type == 1) {
                           # $count = $value->bki_no_days;
                        #$bki_multi_date = $value->bki_dates;
                        $multiple_date = explode(',',$value->bki_dates);
                        $start = ($start_date != '') ?  $start_date : date('d-m-Y');
                        $end = ($end_date != '') ?  $end_date : date('d-m-Y');
                        $count = 0;
                        $bki_date = '';
                        foreach($multiple_date as $k=>$row) {
                            $bki_date .= ($start <= trim($row) && $end >= trim($row)) ? '<span style="color:#fff;background-color: #F44336;"><b>'. $row .'</b></span>,' : $row . ',';
                            if($start <= trim($row) && $end >= trim($row)) {
                                $count += 1;
                            }
                        }
                        $bki_multi_date = ' - ';
                    } else if ($value->bki_day_type == 2) {
                        $multiple_date = explode(',',$value->bki_dates);
                        $start = ($start_date != '') ?  $start_date : date('d-m-Y');
                        $end = ($end_date != '') ?  $end_date : date('d-m-Y');
                        $count = 0;
                        $bki_date = '';
                        foreach($multiple_date as $k=>$row) {
                            $bki_date .= ($start <= trim($row) && $end >= trim($row)) ? '<span style="color:#fff;background-color: #F44336;"><b>'. $row .'</b></span>,' : $row . ',';
                            if($start <= trim($row) && $end >= trim($row)) {
                                if(date('w', strtotime($row)) == $value->bki_week_day) {
                                    $count += 1;    
                                }
                            }
                        }
                        #$bki_multi_date = rtrim($bki_date,',');
                        $bki_multi_date = ' - ';
                    } elseif($value->bki_day_type == 3) {
                        $multiple_date = explode(',',$value->bki_dates);
                        $start = ($start_date != '') ?  $start_date : date('d-m-Y');
                        $end = ($end_date != '') ?  $end_date : date('d-m-Y');
                        $count = 0;
                        $bki_date = '';
                        foreach($multiple_date as $k=>$row) {
                            $bki_date .= ($start <= trim($row) && $end >= trim($row)) ? '<span style="color:#fff;background-color: #F44336;"><b>'. $row .'</b></span>,' : $row . ',';
                            if($start <= trim($row) && $end >= trim($row)) {
                                $count += 1;
                            }
                        }
                        $bki_multi_date = rtrim($bki_date,',');
                    } ?>                    
                    <?php if($count > 0) {  $total_amt += $value->bki_amount; ?>
                        <tr>
                            <td align="center" style="padding:1%;"><?php echo ++$i;?></td>
                            <?php if (in_array("Receipt No / Type", $filters)) {?><td style="padding:1%;"><?php echo ((($general->prefix == 1) ? $value->prefix : '') . $value->bk_rec_no) . ' / ' . text($value->service_name);?></td><?php } ?>
                            <?php if (in_array("Name(s)", $filters)) {?><td style="padding:1%;"><?php echo text($value->bk_name_1) . ( !empty( $value->bk_name_2 ) ? ' / '.$value->bk_name_2 : '' ); ?></td><?php } ?>
                            <?php if (in_array("Tamil Name(s)", $filters)) {?><td style="padding:1%;"><?php echo text($value->bk_tamil_name_1) . ( !empty( $value->bk_tamil_name_2 ) ? ' / '.$value->bk_tamil_name_2 : '' ); ?></td><?php } ?>
                            <?php if (in_array("Star(s)", $filters)) {?><td style="padding:1%;"><?php echo text($value->star) . ( !empty( $value->star2 ) ? ' / '.$value->star2 : '' ); ?></td><?php } ?>
                            <?php if (in_array("Deity", $filters)) {?><td style="padding:1%;">
                                <?php if($value->bki_god_ids != '') {
                                    $god_id = explode(',',$value->bki_god_ids);
                                    $god_name = $this->kattalai_archanai_report_model->get_deity_name($god_id);
                                    echo $god_name->deity;     
                                } ?>
                            </td><?php } ?>
                            <?php if (in_array("From Date", $filters)) {?><td style="padding:1%;"><?php echo text(date('d-m-Y',strtotime($value->bki_from_date)));?></td><?php } ?>
                            <?php if (in_array("To Date", $filters)) {?><td style="padding:1%;"><?php echo text(date('d-m-Y',strtotime($value->bki_to_date)));?></td><?php } ?>
                            <?php if (in_array("Booked Date", $filters)) {?><td style="padding:1%;"><?php echo text(date('d-m-Y',strtotime($value->created_on)));?></td><?php } ?>
                            <?php if (in_array("Day Type", $filters)) {?><td style="padding:1%;"><?php echo $value->day_type;?></td><?php } ?>
                            <?php if (in_array("Day Count", $filters)) {?><td style="padding:1%;" align="center"><?php echo $count;?></td><?php } ?>
                            <?php if (in_array("Total Days", $filters)) {?><td style="padding:1%;" align="center"><?php echo $value->bki_no_days;?></td><?php } ?>
                            <?php if (in_array("Multiple Dates", $filters)) {?><td style="padding:1%;"><?php echo $bki_multi_date;?></td><?php } ?>
                            <?php if (in_array("Desc", $filters)) {?><td style="padding:1%;"><?php echo $value->bk_desc;?></td><?php } ?>
                            <?php if (in_array("Amount", $filters)) {?><td style="padding:1%;" align="right"><?php echo $value->bki_amount;?></td><?php } ?>
                        </tr>
                    <?php } ?>
                <?php } ?>
            <?php } else {?>
                <tr>
                    <td colspan="<?php echo $field+1; ?>" style="color: #0B6CCB;"><center>No Records Found!</center></td>
                </tr>
            <?php } ?>
        </tbody>
        <tfoot>
            <?php if($total_amt > 0) { ?>
                <tr>
                    <td style="padding:1%;" colspan="<?php echo $field; ?>" align="right"><b>Total Amount</b></td>
                    <td style="padding:1%;" align="right"><b><?php echo number_format($total_amt,2,'.','');?></b></td>
                </tr>
            <?php } ?>
                
        </tfoot>
    </table>
<?php } elseif($submit_type == 'XLS Details Data') {?>
    <table width="1000" border="1" cellpadding="3" cellspacing="0" class="filterTable" id="filterTable">
        <thead>
            <tr>
                <th style="padding:1%;">S.No</th>
                <?php
                    foreach($filters as $value){
                       echo '<th style="padding:1%;">'.$value.'</th>';
                    }
                ?>               
            </tr>
        </thead>
        <tbody>
        <?php 
            if(count($god_details) > 0) {  $ii = 0;
            foreach($god_details as $k => $row) {
                $count = 0;
               foreach($row as $i => $td) {   
                    foreach($td as $index=> $val) {  #echo '<pre>';print_r($val);
                   
                        if($val->bki_day_type == 1) {
                            #$count = $val->bki_no_days;
                            #$bki_multi_date = $value->bki_dates;
                            $multiple_date = explode(',',$val->bki_dates);
                            $start = ($start_date != '') ?  $start_date : date('d-m-Y');
                            $end = ($end_date != '') ?  $end_date : date('d-m-Y');
                            #$count = 0;
                            $bki_date = '';
                            foreach($multiple_date as $ki=>$Row) {
                                $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? '<span style="color:#fff;background-color: #F44336;"><b>'. $Row .'</b></span>,' : $Row . ',';
                                if($start <= trim($Row) && $end >= trim($Row)) {
                                    $count += 1;
                                }
                            }
                            $bki_multi_date = ' - ';
                        } else if ($val->bki_day_type == 2) {
                            $multiple_date = explode(',',$val->bki_dates);
                            $start = ($start_date != '') ?  $start_date : date('d-m-Y');
                            $end = ($end_date != '') ?  $end_date : date('d-m-Y');
                            #$count = 0;
                            $bki_date = '';
                            foreach($multiple_date as $ki=>$Row) {
                                $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? '<span style="color:#fff;background-color: #F44336;"><b>'. $Row .'</b></span>,' : $Row . ',';
                                if($start <= trim($Row) && $end >= trim($Row)) {
                                    if(date('w', strtotime($Row)) == $val->bki_week_day) {
                                        $count += 1;    
                                    }
                                }
                            }
                            #$bki_multi_date = rtrim($bki_date,',');
                            $bki_multi_date = ' - ';
                        } elseif($val->bki_day_type == 3) {
                            $multiple_date = explode(',',$val->bki_dates);
                            $start = ($start_date != '') ?  $start_date : date('d-m-Y');
                            $end = ($end_date != '') ?  $end_date : date('d-m-Y');
                            #$count = 0;
                            $bki_date = '';
                            foreach($multiple_date as $ki=>$Row) {
                                $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? '<span style="color:#fff;background-color: #F44336;"><b>'. $Row .'</b></span>,' : $Row . ',';
                                if($start <= trim($Row) && $end >= trim($Row)) {
                                    $count += 1;
                                }
                            }
                            $bki_multi_date = rtrim($bki_date,',');
                        }
                    }
                }
            ?> 
            <?php if($count > 0) { ?>
                <tr>
                    <td colspan="<?php echo $field+1; ?>" align="center"><b><?php echo $row[0];?></b></td>
                </tr>
            <?php } ?>
            
            <?php unset($row[0]) ;
                
                
                foreach($row as $i => $td) {   
                   foreach($td as $index=> $val) {  #echo '<pre>';print_r($val);
                   
                        if($val->bki_day_type == 1) {
                            #$count = $val->bki_no_days;
                            #$bki_multi_date = $value->bki_dates;
                            $multiple_date = explode(',',$val->bki_dates);
                            $start = ($start_date != '') ?  $start_date : date('d-m-Y');
                            $end = ($end_date != '') ?  $end_date : date('d-m-Y');
                            $count = 0;
                            $bki_date = '';
                            foreach($multiple_date as $ki=>$Row) {
                                $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? '<span style="color:#fff;background-color: #F44336;"><b>'. $Row .'</b></span>,' : $Row . ',';
                                if($start <= trim($Row) && $end >= trim($Row)) {
                                    $count += 1;
                                }
                            }
                            $bki_multi_date = ' - ';
                        } else if ($val->bki_day_type == 2) {
                            $multiple_date = explode(',',$val->bki_dates);
                            $start = ($start_date != '') ?  $start_date : date('d-m-Y');
                            $end = ($end_date != '') ?  $end_date : date('d-m-Y');
                            $count = 0;
                            $bki_date = '';
                            foreach($multiple_date as $ki=>$Row) {
                                $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? '<span style="color:#fff;background-color: #F44336;"><b>'. $Row .'</b></span>,' : $Row . ',';
                                if($start <= trim($Row) && $end >= trim($Row)) {
                                    if(date('w', strtotime($Row)) == $val->bki_week_day) {
                                        $count += 1;    
                                    }
                                }
                            }
                            #$bki_multi_date = rtrim($bki_date,',');
                            $bki_multi_date = ' - ';
                        } elseif($val->bki_day_type == 3) {
                            $multiple_date = explode(',',$val->bki_dates);
                            $start = ($start_date != '') ?  $start_date : date('d-m-Y');
                            $end = ($end_date != '') ?  $end_date : date('d-m-Y');
                            $count = 0;
                            $bki_date = '';
                            foreach($multiple_date as $ki=>$Row) {
                                $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? '<span style="color:#fff;background-color: #F44336;"><b>'. $Row .'</b></span>,' : $Row . ',';
                                if($start <= trim($Row) && $end >= trim($Row)) {
                                    $count += 1;
                                }
                            }
                            $bki_multi_date = rtrim($bki_date,',');
                        }
                    
                       $god_names = $this->kattalai_archanai_report_model->getGodName($val->bki_god_ids);
                ?>
                        <?php if($count > 0) { ?>
                            <tr>
                               <td align="center" style="padding:1%;"><?php echo ++$ii;?></td>
                               <?php if (in_array("Receipt No", $filters)) {?><td style="padding:1%;"><?php echo (($general->prefix == 1) ? $val->prefix : '') . $val->bk_rec_no;?></td><?php } ?>
                               <?php if (in_array("God Name", $filters)) {?><td style="padding:1%;"><?php echo $god_names->deity;?></td><?php } ?>
                               <?php if (in_array("Name", $filters)) {?><td style="padding:1%;"><?php echo ($val->bk_name_1  . ' - '. $val->star . ((!empty($val->bk_name_2)) ? '<br/>' .$val->bk_name_2 . ((!empty(bk_star_2)) ? ' - ' . $val->star2 : ''): '') );?></td><?php } ?>
                               <?php if (in_array("Tamil Name(s)", $filters)) {?><td style="padding:1%;"><?php echo text($val->bk_tamil_name_1) . ( !empty( $val->bk_tamil_name_2 ) ? ' / '.$val->bk_tamil_name_2 : '' ); ?></td><?php } ?>
                               <?php if (in_array("Booked Date", $filters)) {?><td style="padding:1%;"><?php echo date('d-m-Y',strtotime($val->created_on));?></td><?php } ?>
                               <?php if (in_array("From Date", $filters)) {?><td style="padding:1%;"><?php echo date('d-m-Y',strtotime($val->bki_from_date));?></td><?php } ?>
                               <?php if (in_array("To Date", $filters)) {?><td style="padding:1%;"><?php echo date('d-m-Y',strtotime($val->bki_to_date));?></td><?php } ?>
                               <?php if (in_array("Type", $filters)) {?><td style="padding:1%;"><?php if($val->bki_day_type == 1) { echo 'Daily'; } elseif($val->bki_day_type == 2) { echo 'Weekly'; } elseif( $val->bki_day_type == 3 ) { echo 'Multiple Date(s)'; }?></td><?php } ?>
                               <?php if (in_array("Day", $filters)) {?><td style="padding:1%;">
                               <?php if($val->bki_day_type == 2) {
                                    if($val->bki_week_day == 0) { echo 'Sunday'; } 
                                    elseif($val->bki_week_day == 1) { echo 'Monday'; }
                                    elseif($val->bki_week_day == 2) { echo 'Tuesday'; }
                                    elseif($val->bki_week_day == 3) { echo 'Wednesday'; }
                                    elseif($val->bki_week_day == 4) { echo 'Thursday'; }
                                    elseif($val->bki_week_day == 5) { echo 'Friday'; }
                                    elseif($val->bki_week_day == 6) { echo 'Saturday'; }
                               } else {
                                   echo '';
                               }
                               ?>
                               </td><?php } ?>
                               <?php if (in_array("Total weeks", $filters)) {?><td align="center" style="padding:1%;"><?php echo $val->bki_week_day ;?></td><?php } ?>
                               <?php if (in_array("Total Days", $filters)) {?><td align="center" style="padding:1%;" ><?php echo $val->bki_no_days ;?></td><?php } ?>
                               <?php if (in_array("Amount", $filters)) {?><td align="right" style="padding:1%;"><?php echo $val->bki_amount ;?></td><?php } ?>
                            </tr>
                        <?php } ?>
                   <?php } ?>
                <?php } ?>
           
            <?php } ?>
        <?php } else {?>
            <tr>
                <td colspan="<?php echo $field+1; ?>"><center>No Records Found!</center></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
<?php } ?>                        