<?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");
?>

<?php if($submit_type == 'Get Data') { ?>
	<table class="table table-bordered table-striped data_table filterTable" id="filterTable">
        <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">Contact No</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>
        <?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 : '' ) . ( !empty( $value->bk_name_3 ) ? ' / '.$value->bk_name_3 : '' ); ?></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>' : '' ). ( !empty( $value->bk_tamil_name_3 ) ? ' / <span class="lang-tamil">'.$value->bk_tamil_name_3.'</span>' : '' ); ?></td>
                    <td><?php echo ($value->contact_no) ? $value->contact_no : 'N/A';?></td>
					<td><?php echo text($value->star) . ( !empty( $value->star2 ) ? ' / '.$value->star2 : '' ) . ( !empty( $value->star3 ) ? ' / '.$value->star3 : '' ); ?></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 } ?>
	</table>
<?php } elseif($submit_type == 'Get Details Data') {?>
	<table class="table table-bordered table-striped filterTable" id="filterTable">
			<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>
			<?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 } ?>
		</tbody>
	</table>
<?php } ?>