<?php
if ( function_exists( 'date_default_timezone_set' ) )
date_default_timezone_set('Asia/Jakarta');
$this->load->view('vwHeader');
?>
		<script type="text/javascript">
		  function disable_link() 
		  { 
		   document.getElementById('link').style.visibility="hidden";
		  } 
		</script>
      <div id="page-wrapper">
        <div class="row">
          <div class="col-lg-12">
            <h1>Report <small> Backup Outstanding (ownCloud)</small></h1>
            <ol class="breadcrumb">
              <li><a href="outstanding"><i class="icon-dashboard"></i> Report Outstanding</a></li>
              <li class="active"><i class="icon-file-alt"></i> Report Outstanding</li>             
              <div style="clear: both;"></div>
            </ol>
          </div>
        </div><!-- /.row -->
            <div class="table-responsive">
			<div class="form-signin">
			 <form class="form-signin panel" method="post" action="<?php echo base_url(); ?>outstanding">
			 	
				<label>Tahun&nbsp;&nbsp;&nbsp;&nbsp;:</label>
				<?php $now=date("Y"); ?>
				<select class="form-filter"  name='thn' id='thn'>
				<option value=0>--Semua Tahun--</option>
                <?php  for ($a=$now;$a>=2012;$a--) { 
					if($_POST['thn'] == $a) $selected='selected=true'; else $selected= '';
				?>
                    <option <?php echo $selected; ?> value=<?php echo $a; ?>><?php echo $a; ?></option>";
                <?php  } ?>
                </select><br />
				
				<label>Period&nbsp;&nbsp;&nbsp;:</label>			 
				<select class="form-filter" name="period" id="period">
				<option value=0>--Pilih Periode--</option>
				<?php 
				  for ($i=1;$i<=12;$i++) {
				   		if($period == $i) $selected='selected=true'; else $selected= '';
				?>
					<option <?php echo $selected; ?> value=<?php echo $i; ?>><?php echo str_pad($i, 2, '0', STR_PAD_LEFT); ?></option>
				<?php } ?>	
				</select><br />
								
				<label>Subdist :</label>
				<select class="form-filter" name="subdist" id="subdist">
				<option value='0'>--Semua Subdist--</option>
				<?php 
				 foreach($subdist as $row) {
				 if($kd_subdist == $row->kd_subdist) $selected='selected=true'; else $selected= '';
				?>
				<option <?php echo $selected; ?> value='<?php echo $row->kd_subdist; ?>'>
				<?php echo $row->kd_subdist; ?> - <?php echo $row->subdist_name; ?> </option>
				<?php } ?>	
				</select><br /><br />
				 <button type="submit" class="btn btn-primary" type="submit" onclick="disable_link()"><i class="fa fa-search"></i> Search</button>
				 <a id="link" class="btn btn-outline btn-default" href="<?php echo base_url(); ?>index.php/outstanding/export"><i class="icon-ok-circle"></i> Export XLS</a>
			  </form>	 
			</div>
			
			<i>**Refresh data every 30 minutes</i>
			<div style="width:100%; overflow: auto;">
             <table class="table table-hover tablesorter" >
                <thead>
				  <tr>
					<th class="header" style="margin:0; white-space:nowrap;" colspan="2" ><p align="center">Subdist</p> </th>
					<th class="header" style="margin:0; white-space:nowrap;" >Year </th>
					  <!-- query pertama -->
					  <?php
					  $awal=($period-4);    
   					   if($awal >=1)
					   {
						echo ("<th class=header style='margin:0; white-space:nowrap;' colspan='2'><p align=center><strong>YTD-P".str_pad($awal, 1, '', STR_PAD_LEFT) ." </strong></p></th>");
						}
					  ?>
					  <?php
					  for ($i=($period-3);$i<=($period-1);$i++){
					  if($i >=1){   
// 					  print_r('angka i:'.$i.', period:'.$period);  // untuk catch nilai i
						  echo ("<th class=header style='margin:0; white-space:nowrap;' colspan='2'><p align=center><strong>P-".str_pad($i, 1, '', STR_PAD_LEFT) ." </strong></p></th>");
						  }
						}      
					  ?>
					  <?php 
					  if($period <> $curr_period){
  					  $week=$endweek-$startweek;
					  }
					  else
					  {
					  $week=$curr_week-$startweek;
					  }
					  $colspan=$week + 3;
					  ?>
				   <th class="header" style="margin:0; white-space:nowrap;" colspan=<?php echo $colspan; ?>><p align="center">P-<?php echo $period; ?></p></th>
				   <th class="header" style="margin:0; white-space:nowrap;" colspan="2">YTD-P<?php echo $period; ?></th>
				  </tr>
				  <tr>
                    <td class="header" style="margin:0; white-space:nowrap;" align="center"><strong>Kode</strong></td>
					<td class="header" style="margin:0; white-space:nowrap;" align="center"><strong>Nama</strong></td>
					<td class="header" style="margin:0; white-space:nowrap;">&nbsp;</td>
					<?php
					$awal=($period-4);    
					   if($awal >=1)
					   {
						echo ("<td class=header style='margin:0; white-space:nowrap;'><strong>Bck</strong></td>");
						echo ("<td class=header style='margin:0; white-space:nowrap;'><strong>Hit</strong></td>");				
					 }
					?>
					<?php
					for ($i=($period-3);$i<=($period-1);$i++){ 
					 if($i >= 1){ 
					echo ("<td class=header style='margin:0; white-space:nowrap;'><strong>Bck</strong></td>");
					echo ("<td class=header style='margin:0; white-space:nowrap;'><strong>Hit</strong></td>");				
					 }
					}
					?>
					<?php                                                                                         
	   				 for ($i=$period;$i<=$period;$i++){  
						for ($j=$startweek;$j<=$endweek;$j++){    
//					die(print_r($curr_week));                            
						 if($j <= $curr_week){
						 //	 die(print_r($curr_week));						  
						echo ("<td class=header style='margin:0; white-space:nowrap;'><strong>WK - ".str_pad($j, 2, '0', STR_PAD_LEFT) ." </strong></td>");					
						}
						  /*elseif($j > $curr_week)
						  {
						  echo "data belum ada";
						  exit;
						  }*/
						}
					}                                                                                           
			 	    ?>
                    <td class="header" style="margin:0; white-space:nowrap;" ><strong>Bck</strong></td>
					<td class="header" style="margin:0; white-space:nowrap;" ><strong>Hit</strong></td>
                    <td class="header" style="margin:0; white-space:nowrap;" ><strong>Bck</strong></td>
					<td class="header" style="margin:0; white-space:nowrap;" ><strong>Hit</strong></td>
				  </tr>
                </thead>
                <tbody>
				
				  <?php
				  $percentage = 0;
				  if($startweek > $curr_week){echo "data belum ada"; 
				  exit;}
				  elseif ($startweek > 0){ 
				  foreach($query->result_array() as $row){
			
				  ?>                                            
    	 <tr>                                                                                        
          	<td ><?php echo $row['subdist_id'];?></td>                                               
         	<td style="margin:0; white-space:nowrap;"><?php echo $row['subdist_name'];?></td> 
		  	<td><?php echo $row['year']=='' ? '-' : $row['year']; ?></td> 
		
			<!-- query pertama (ytd awal)-->	                                
			<?php
			if($ytdstartweek >= 1 && $ytdendweek >= 1){
			$prct=0;
			$prct_h=0;
			$countweek=0;
				 for ($j=$ytdstartweek;$j<=$ytdendweek;$j++){
				 
				 $countweek1=$ytdendweek - $ytdstartweek;
				 $number = str_pad($j, 2, '0', STR_PAD_LEFT);				   
					if ($row['wk_'.$number] == 'Y') {   
						 $prct = $prct+ 1;
						 }
					if ($row['flag_'.$number] == 'B') {   
						 $prct_h = $prct_h+ 1;
						 }
					 }
			$countweek=$countweek + $countweek1;
			echo "<td><b>";echo round_up(($prct/($countweek+1))*100,1);echo "%</b></td>";
			echo "<td><b>";echo round_up(($prct_h/($countweek+1))*100,1);echo "%</b></td>";
			}
			?>    			
		   <!-- batas query pertama -->
	   
		   <!-- query kedua (tiga bulan previous)-->	                                
			<?php
			if($tigabulansweek >= 1 && $tigabulaneweek >= 1){
			$prct=0;
			$prct_h=0;
			$countweek=0;
			for($j=$tigabulansweek;$j<=$tigabulaneweek;$j++){
				 
				 $countweek1=$tigabulaneweek - $tigabulansweek;
				// die(print_r($tigabulansweek));	
				 $number = str_pad($j, 2, '0', STR_PAD_LEFT);				   
				 //   die(print_r($number));	
					if ($row['wk_'.$number] == 'Y') {   
						 $prct = $prct+ 1;
						 }
					if ($row['flag_'.$number] == 'B') {   
						 $prct_h = $prct_h+ 1;
						 }						
					}
			$countweek=$countweek + $countweek1;
			echo "<td><b>";echo round_up(($prct/($countweek+1))*100,1);echo "%</b></td>";
			echo "<td><b>";echo round_up(($prct_h/($countweek+1))*100,1);echo "%</b></td>";
			}
			?>    			
		   <!-- batas query kedua -->
		   
		   <!-- query ketiga (dua bulan previous)-->	                                
			<?php
			if($duabulansweek >= 1 && $duabulaneweek >= 1){
			$prct=0;
			$prct_h=0;
			$countweek=0;
			for($j=$duabulansweek;$j<=$duabulaneweek;$j++){

				 $countweek1=$duabulaneweek - $duabulansweek;
				 $number = str_pad($j, 2, '0', STR_PAD_LEFT);				   
					if ($row['wk_'.$number] == 'Y') {   
						 $prct = $prct+ 1;
						 }
					if ($row['flag_'.$number] == 'B') {   
						 $prct_h = $prct_h+ 1;
						 }						
					}
			$countweek=$countweek + $countweek1;
			echo "<td><b>";echo round_up(($prct/($countweek+1))*100,1);echo "%</b></td>";
			echo "<td><b>";echo round_up(($prct_h/($countweek+1))*100,1);echo "%</b></td>";
			}
			?>    			
		   <!-- batas query ketiga -->
		   
		   <!-- query keempat (satu bulan previous)-->	                                
			<?php
			if($satubulansweek >= 1 && $satubulaneweek >= 1){
			$prct=0;
			$prct_h=0;
			$countweek=0;
			for($j=$satubulansweek;$j<=$satubulaneweek;$j++){

				 $countweek1=$satubulaneweek - $satubulansweek;
				 $number = str_pad($j, 2, '0', STR_PAD_LEFT);				   
					if ($row['wk_'.$number] == 'Y') {   
						 $prct = $prct+ 1;
						 }
					if ($row['flag_'.$number] == 'B') {   
						 $prct_h = $prct_h+ 1;
						 }						
					}
			$countweek=$countweek + $countweek1;
			echo "<td><b>";echo round_up(($prct/($countweek+1))*100,1);echo "%</b></td>";
			echo "<td><b>";echo round_up(($prct_h/($countweek+1))*100,1);echo "%</b></td>";
			}
			?>    			
		   <!-- batas query keempat -->
	   											                     
	   <?php              
	   $ii=0;
//	   $week=$curreweek-$currsweek;
	   $week=$endweek-$startweek;

	   for ($i=$startweek;$i<=$endweek;$i++){          
		if($i <= $curr_week){                                                         
//       for ($i=$startweek;$i<=$curr_week;$i++){                                                                   
	   $number = str_pad($i, 2, '0', STR_PAD_LEFT); ?>  
       <td style="width:0.3px">                       

	   <?php if ($row['wk_'.$number] == 'Y') {   ?>
	           <!--<img src="<?php echo asset_url();?>images/right.png" width="15" height="15"> -->
       <?php } else if ($row['wk_'.$number] == 'O' || $row['wk_'.$number] == '') { ?>
	           <!--<img src="<?php echo asset_url();?>images/wrong.png" width="15" height="15"> -->      
	   <?php  }  
	   
	        if ($row['loc_'.$number] == 'L')  
			   echo("<img src=".asset_url()."images/img_lokal.png width='15' height='15'>"); 
			else if ($row['loc_'.$number] == 'O')  
			   echo("<img src=".asset_url()."images/img_oc.png width='15' height='15'>"); 						
			else
			   echo("<img src=".asset_url()."images/img_not_ok.png width='15' height='15'>"); 
			
			if ($row['st_'.$number]=='C')
			  	echo("<img src=".asset_url()."images/img_complete.png width='15' height='15'>"); 
			else if ($row['st_'.$number]=='D')
			  	echo("<img src=".asset_url()."images/img_delete.png width='15' height='15'>"); 
			else if ($row['st_'.$number] == 'E')
			   echo("<img src=".asset_url()."images/img_evaluate.png width='15' height='15'>"); 
			else 
			   echo("<img src=".asset_url()."images/img_not_ok.png width='15' height='15'>"); 

			if ($row['flag_'.$number]=='B')
			  	echo("<img src=".asset_url()."images/img_better.png width='15' height='15'>"); 
			else if ($row['flag_'.$number]=='W')
			  	echo("<img src=".asset_url()."images/img_worst.png width='15' height='15'>"); 
			else if ($row['flag_'.$number]=='CD')
			  	echo("<img src=".asset_url()."images/img_cd.png width='15' height='15'>"); 
			else 
			    echo("<img src=".asset_url()."images/img_not_ok.png width='15' height='15'>"); 
			   
		?>
			   <!--<b><?php echo $row['st_'.$number]=='' ? '-' : $row['st_'.$number] ; ?></b>-->                                                  
	   
	    </td>
		
	    <?php
			}
	    }
		$prct=0;
		$prct_h=0;
//			 for ($i=$currsweek;$i<=$curreweek;$i++){
		 for ($i=$startweek;$i<=$endweek;$i++){
		 	if($i <= $curr_week){   
		 $number = str_pad($i, 2, '0', STR_PAD_LEFT);
		       
			if ($row['wk_'.$number] == 'Y') {   
				 $prct = $prct+ 1;
				 }
			if ($row['flag_'.$number] == 'B') {   
				 $prct_h = $prct_h+ 1;
				 }
			 }
		 }
 	  ?>       
	  	  	<td><b><?php echo round_up(($prct/($week+1))*100,1); ?>%</b></td>
	  		<td><b><?php echo round_up(($prct_h/($week+1))*100,1);?>%</b></td>  
			
			<!-- query kelima(ytd month to date)-->	                                
			<?php
			if($ytdakhirsweek >= 1 && $ytdakhireweek >= 1){
			$prct=0;
			$prct_h=0;
			$countweek=0;
			for($j=$ytdakhirsweek;$j<=$ytdakhireweek;$j++){

				 $countweek1=$ytdakhireweek - $ytdakhirsweek;
				 $number = str_pad($j, 2, '0', STR_PAD_LEFT);				   
					if ($row['wk_'.$number] == 'Y') {   
						 $prct = $prct+ 1;
						 }
					if ($row['flag_'.$number] == 'B') {   
						 $prct_h = $prct_h+ 1;
						 }						
					}
			$countweek=$countweek + $countweek1;
			}
			?>    		
			<td><b><?php echo round_up(($prct/($countweek+1))*100,1);?>%</b></td>
			<td><b><?php echo round_up(($prct_h/($countweek+1))*100,1);?>%</b></td>
		   <!-- batas query kelima -->
			                             
           </tr> 
     <?php 
	   }
	}
	?>  
                                          
     <tr>	
	 <?php if($ytdstartweek >= 1 && $ytdendweek >= 1) echo "<td colspan='2'></td>" ?>
	 <?php if($tigabulansweek >= 1 && $tigabulaneweek >= 1) echo "<td colspan='2'></td>" ?>
	 <?php if($duabulansweek >= 1 && $duabulaneweek >= 1) echo "<td colspan='2'></td>" ?>
	 <?php if($satubulansweek >= 1 && $satubulaneweek >= 1) echo "<td colspan='2'></td>" ?>
		 
	 <?php /*if($period < 5){$colspan2 = ($period+$week+3);}*/?>                                                                            
         <td colspan="3"><p align="right"><b>(%) by week :</b></p></td>                                               
         <?php 
		  foreach($count_data->result_array() as $row){
		 $row["jml"]=='0' ? $count_subdist : $row["jml"];
		 ?>                                            
	     <td><p align="center"><B><?php echo round_up(($row["jml"]/$count_subdist)*100,1); ?>%</B></p></td>
                                                                            
         <?php 
		   //}   
		  }
		  ?>                                                                                 
     </tr> 
		 	  
            </tbody>
              </table>
			  </div>
            </div>
			
        <?php 

		echo $links; ?>
      </div><!-- /#page-wrapper -->
	  
<!-- <table>
</table>-->

<?php
$this->load->view('vwFooter');
?>
