<?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>
            <?php if(!IS_KSK_CMS) { ?><div class="breadcrumb_container"><?php echo breadcrumb_links();?></div><?php } ?>
        </div>
    </section>
    <?php $general = get_general_setting();?>
    <!-- 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">
                    
                        <!-- Filtered Columns to send JS - Start -->
                        <input type="hidden" name="hidden_print" class="hidden_print" value="<?php echo (!empty($start_date)) ? 1 : 0 ;?>" />
                        <?php foreach($filters as $key=>$val) : ?>
                            <input type="hidden" name="hidden_filter_array[]" class="hidden_filter_array" value="<?php echo $val;?>" />
                        <?php endforeach;?>
                        <!-- Filtered Columns to send JS - End -->
                        
                        <?php if(!IS_KSK_CMS) { ?>
                            <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 >Date Type</label>
                                            <div class="input-group">
                                                <div class="input-group-addon">
                                                    <i class="fa fa-soccer-ball-o"></i>
                                                </div>
                                                <select name="date_type" class="form-control select2 date_type">
                                                    <option value="1" <?php echo ($date_type == 1) ? 'selected' : '';?>>Function Date</option>
                                                    <option value="2" <?php echo ($date_type == 2) ? 'selected' : '';?>>Booked Date</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label >Start Date</label>
                                            <div class="input-group">
                                                
                                                <div class="input-group-addon">
                                                    <i class="fa fa-calendar"></i>
                                                </div>
                                                <input type="text" class="start_date" placeholder="DD-MM-YYYY" style="padding: 6px 12px;margin-right:5%;width:100%;" id="start_date" name="start_date" value="<?php echo (!empty($start_date)) ? $start_date : date('d-m-Y');?>" autocomplete="off" >
                                                
                                            </div>
                                        </div>
                                        
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label >End Date</label>
                                            <div class="input-group">
                                                
                                                <div class="input-group-addon">
                                                    <i class="fa fa-calendar"></i>
                                                </div>
                                                <input type="text" class="end_date" placeholder="DD-MM-YYYY" style="padding: 6px 12px;margin-right:5%;width:100%;"  id="end_date" name="end_date" value="<?php echo (!empty($end_date)) ? $end_date : date('d-m-Y');?>" autocomplete="off" >
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label >Type</label>
                                            <div class="input-group">
                                                <div class="input-group-addon">
                                                    <i class="fa fa-soccer-ball-o"></i>
                                                </div>
                                                <select name="type_id" class="form-control select2">
                                                    <option value="">Select Type</option>
                                                    <?php foreach($types as $key=>$row) {?>
                                                        <option value="<?php echo $row->id;?>" <?php echo ($row->id == $type_id) ? 'selected' : '';?>><?php echo $row->service_name;?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>    
                                        </div>
                                    </div>
                                </div>
                                <div class="col-xs-12">
                                    <div class="col-md-3">&nbsp;</div>
                                    <div class="col-md-6">
                                        <input type="submit" value="Get Data" name="submit" class="btn btn-danger btn-sm" style="background-color: #e33739; border-color: #e33739;" />
                                    &nbsp;&nbsp;
                                        <input type="submit" value="Print Data" name="submit" class="btn btn-primary btn-sm" />
                                    &nbsp;&nbsp;
                                        <input type="submit" value="Get Details Data" name="submit" class="btn btn-danger btn-sm" style="background-color: #e33739; border-color: #e33739;" />
                                   &nbsp;&nbsp;
                                        <input type="submit" value="Print Details Data" name="submit" class="btn btn-primary btn-sm" />
                                    </div>
                                   <div class="col-md-3">&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" class="grpChkBox">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <!-- Filtered Columns to send JS - End -->
                                
                            </form>
                        <?php } else {?>
                            <div class="col-xs-12">
                                <div class="col-md-4">&nbsp;</div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <span style="padding-left: 116px; font-size: 16px;">Date :&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $start_date;?></span>
                                    </div>
                                </div>
                                <div class="col-md-2">&nbsp;</div>
                            </div>
                        <?php } ?>
                    </div>
                    <div class="box-body table-responsive">
                        <?php if($submit_type == 'Get Data') { ?>
                            <table class="table table-bordered table-striped data_table filterTable" id="filterTable">
                                <thead>
                                    <tr>
                                        <th width="5%" class="align-center">S.No</th>
                                        <th class="align-center">Receipt No / Type</th>
                                        <th class="align-center">Name(s)</th>
                                        <th class="align-center">Tamil Name(s)</th>
                                        <th class="align-center">Star(s)</th>
                                        <th class="align-center">Deity</th>
                                        <th class="align-center">From Date</th>
                                        <th class="align-center">To Date</th>
                                        <th class="align-center">Booked Date</th>
                                        <th class="align-center">Day Type</th>
                                        <th class="align-center">Day Count</th>
                                        <th class="align-center">Total Days</th>
                                        <th width="10%" class="align-center">Multiple Dates</th>
                                        <th width="10%" class="align-center">Desc</th>
                                        <th class="align-center">Amount</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $i=0; 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,',');
                                        } //echo "<pre>Data: "; print_r( $value ); die;
                                        ?>
                                        <?php if($count > 0) { ?>
                                            <tr>
                                                <td><?php echo ++$i;?></td>
                                                <td><?php echo ((($general->prefix == 1) ? $value->prefix : '') . $value->bk_rec_no) . ' / ' . text($value->service_name);?></td>
                                                
                                                <td><?php echo text($value->bk_name_1) . ( !empty( $value->bk_name_2 ) ? ' / '.$value->bk_name_2 : '' ); ?></td>
                                                <td><?php echo ((!empty($value->bk_tamil_name_1)) ? '<span class="lang-tamil">'.$value->bk_tamil_name_1 .'</span>' : 'N/A'). ( !empty( $value->bk_tamil_name_2 ) ? ' / <span class="lang-tamil">'.$value->bk_tamil_name_2.'</span>' : '' ); ?></td>
                                                <td><?php echo text($value->star) . ( !empty( $value->star2 ) ? ' / '.$value->star2 : '' ); ?></td>
                                                <td>
                                                    <?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>
                                                <td><?php echo text(date('d-m-Y',strtotime($value->bki_from_date)));?></td>
                                                <td><?php echo text(date('d-m-Y',strtotime($value->bki_to_date)));?></td>
                                                <td><?php echo text(date('d-m-Y',strtotime($value->created_on)));?></td>
                                                <td><?php echo $value->day_type;?></td>
                                                <td align="center"><?php echo $count;?></td>
                                                <td align="center"><?php echo $value->bki_no_days;?></td>
                                                <td><?php echo $bki_multi_date;?></td>
                                                <td><?php echo $value->bk_desc;?></td>
                                                <td align="right"><?php echo $value->bki_amount;?></td>
                                            </tr>
                                        <?php } ?>
                                    <?php } ?>
                                </tbody>
                            </table>
                        <?php } elseif($submit_type == 'Get Details Data') {?>
                            <table class="table table-bordered table-striped filterTable" id="filterTable">
                                <thead>
                                    <tr>
                                        <th width="5%" class="align-center">S.No</th>
                                        <th class="align-center">Receipt No</th>
                                        <th class="align-center">God Name</th>
                                        <th class="align-center">Name</th>
                                        <th class="align-center">Tamil Name(s)</th>
                                        <th class="align-center">Booked Date</th>
                                        <th class="align-center">From Date</th>
                                        <th class="align-center">To Date</th>
                                        <th class="align-center">Type</th>
                                        <th class="align-center">Day</th>
                                        <th class="align-center">Total weeks</th>
                                        <th width="10%" class="align-center">Total Days</th>
                                        <th class="align-center">Amount</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $no_record = 0;
                                        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="13" 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) {
                                                         $no_record = 1;
                                                    ?>
                                                        <tr>
                                                           <td><?php echo ++$ii;?></td>
                                                           <td><?php echo (($general->prefix == 1) ? $val->prefix : '' ) . $val->bk_rec_no;?></td>
                                                           <td><?php echo $god_names->deity;?></td>
                                                           <td><?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>
                                                           <td><?php echo (!empty($val->bk_tamil_name_1) ? '<span class="lang-tamil">' . ($val->bk_tamil_name_1) . '</span>' : 'N/A') . ( !empty( $val->bk_tamil_name_2 ) ? ' / <span class="lang-tamil">'.$val->bk_tamil_name_2 .'</span>' : '' ); ?></td>
                                                           <td><?php echo date('d-m-Y',strtotime($val->created_on));?></td>
                                                           <td><?php echo date('d-m-Y',strtotime($val->bki_from_date));?></td>
                                                           <td><?php echo date('d-m-Y',strtotime($val->bki_to_date));?></td>
                                                           <td><?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>
                                                           <td>
                                                           <?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>
                                                           <td align="center"><?php echo $val->bki_week_day ;?></td>
                                                           <td align="center"><?php echo $val->bki_no_days ;?></td>
                                                           <td align="right"><?php echo $val->bki_amount ;?></td>
                                                        </tr>
                                                    <?php } ?>
                                               <?php } ?>
                                            <?php } ?>
                                       
                                        <?php } ?>
                                    <?php } else {?>
                                        <tr>
                                            <td colspan="13"><center>No Records Found!</center></td>
                                        </tr>
                                    <?php } ?>
                                    
                                    <?php if($no_record == 0) {?>
                                        <tr>
                                            <td colspan="13"><center>No Records Found!</center></td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        <?php } ?>
                    </div><!-- /.box-body -->
                </div><!-- /.box -->
            </div>
        </div>
    </section><!-- /.content -->
</aside><!-- /.right-side -->
<!-- ./wrapper -->

<script type="text/javascript">
    $(document).ready(function() {
        $('.start_date').bootstrapMaterialDatePicker({ 
            time: false, 
            format: 'DD-MM-YYYY'
        }). on('change', function(e, date) {
            if($('.end_date').val() != '') {
                $('.end_date').val('');
                var cur_date = new Date(date);
                $('.end_date').val(getTwoDigits(cur_date.getDate()) + '-'+ getTwoDigits((cur_date.getMonth() + 1)) + '-'+ cur_date.getFullYear());
                change_todate(getTwoDigits(cur_date.getDate()) + '-'+ getTwoDigits((cur_date.getMonth() + 1)) + '-'+ cur_date.getFullYear());
            }
        });
        
        $('.end_date').bootstrapMaterialDatePicker({ 
            time: false, 
            format: 'DD-MM-YYYY'
        });
        
        var date = $('.start_date').val();
        change_todate(date);
    });
   
    function change_todate(date) {
       $('.end_date').bootstrapMaterialDatePicker('setMinDate', date);
    }
</script>