<?php 
//OJB: Check if for excel export process
if($export_excel == 1){
	ob_start();
	$this->load->view("partial/header_excel");
}else{
	$this->load->view("partial/header");
}


$safe_string_to_store = base64_encode(serialize($data));
$safe_string_to_payment = base64_encode(serialize($payment_data));
$safe_string_title = base64_encode(serialize($title));
$safe_string_subtitle = base64_encode(serialize($subtitle));

?>

<div id="page_title" style="margin-bottom:8px;"><?php echo $title ?></div>

<table style="width:100%">
<tr>
<td style="background-color:#327789;text-align:center;font-weight:bold;font-size: 17px;padding-top: 5px;padding-bottom: 5px;padding-left: 100px;color:#FFFFFF;">
<?php echo "Date Range : ".$subtitle ?>
</td>
<td style="background-color:#327789;text-align:right;font-weight:bold;font-size: 17px;padding: 5px;color:#FFFFFF;width:10%;padding-right:20px;">
<a class="notificationContainerReport" href="<?php echo LOCAL_PATH."/pdf_report/mpdf60/print/summary_all_staff_summary_sales.php?data=".$safe_string_to_store."&payment_data=".$safe_string_to_payment."&title=".$safe_string_title."&subtitle=".$safe_string_subtitle; ?>" target="_blank">
</a>
</td>
</tr>
</table>

<style>
thead{
    background-color:white;
}
table tr td:first-child::after {
   content: "";
   display: inline-block;
   vertical-align: middle;
   min-height: 22px;
}
table tr th:first-child::after {
   content: "";
   display: inline-block;
   vertical-align: middle;
   min-height: 22px;
}
</style>
<script type="text/javascript" language="javascript">
function moveScroll(){
	var scroll = $(window).scrollTop();
	var anchor_top = $("#maintable").offset().top;
	var anchor_bottom = $("#bottom_anchor").offset().top;
	if (scroll>anchor_top && scroll<anchor_bottom) {
		clone_table = $("#clone");
		if(clone_table.length == 0){
			clone_table = $("#maintable").clone();
			clone_table.attr('id', 'clone');
			clone_table.css({position:'fixed',
			'pointer-events': 'none',
			top:-13});
			clone_table.width($("#maintable").width()+3);
			$("#table-container").append(clone_table);
			$("#clone").css({visibility:'hidden'});
			$("#clone thead").css({'visibility':'visible','pointer-events':'auto'});
		}
	} else {
		$("#clone").remove();
	}
}
$(window).scroll(moveScroll);
</script>

<?php
include('application/config/config.php');
$total=count($employee_code)+2;
$per_space=(90/$total)."%";
?>

<div id="table-container" style="width:100%">
<table id="maintable" style='border-collapse: collapse; border: 2px solid #318ee1;width: 97%;margin: 12px;box-shadow: 0px 8px 8px #000;'>
    <thead>
        <tr>
		<th style="vertical-align: middle;background-color: #0084C3; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;padding: 6px;">
		 Date 
		</th>
		<?php
		foreach ($employee_code as $key => $value) {
			?>
			<th colspan =2 width="<?php echo $per_space;?>" style="background-color: #0084C3; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;padding: 6px;">
			 	<?php echo $key."</br>".$value['full_name']?> 
			</th>
			<?php
		}
		?>
		<th  colspan =2 width="<?php echo $per_space;?>" style="background-color: #0084C3; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;padding: 6px;">
		 Customer Own 
		</th>
		<th  colspan =2 width="<?php echo $per_space;?>" style="background-color: #0084C3; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;padding: 6px;">
		 Day Total 
		</th>
	</tr>
	
    </thead>
    <tbody>
    
    <?php 
    $root_serial=1;
    ksort($data);



    function date_range($first, $last, $step = '+1 day', $output_format = 'd/m/Y' ) {
    	$dates = array();
    	$current = strtotime($first);
    	$last = strtotime($last);
    	while( $current <= $last ) {
    		$dates[date("Y-m-d", $current)] = date($output_format, $current);
    		$current = strtotime($step, $current);
    	}
    	return $dates;
    }


    $dates=date_range($start_date,$end_date);
    $i=0;

    $grand_total=array();
    $grand_total_qty=array();

    foreach ($dates as $date_key => $date_value) {
    	if($i%2==0){
    		$bg_color="#D2D2D2";
    	}else{
    		$bg_color="#FFFFFF";
    	}
    	$i++;
    	?>
    	<tr>
            <td style=" font-size:14px; font-weight:bold;background-color: <?php echo $bg_color;?>; color:#000000;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".$date_value." &nbsp;";?></td>
            
            <?php  
            $day_total=0;
            $day_total_qty=0;
            foreach ($employee_code as $emp_key => $emp_value) {

            	$amount_individual=$data[$date_key][$emp_key]['individual_sales'];
            	$amount_individual_sale=$data[$date_key][$emp_key]['individual_sales_qty'];
            	$day_total+=$amount_individual;
            	$day_total_qty+=$amount_individual_sale;

            	$grand_total[$emp_key]+=$amount_individual;
            	$grand_total_qty[$emp_key]+=$amount_individual_sale;

            ?>
            <td style=" font-size:14px; font-weight:bold;background-color: <?php echo $bg_color;?>; color:#000000;font-size:12px; text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".number_format($amount_individual_sale,2,'.',',')." &nbsp;";?></td>
            <td style=" font-size:14px; font-weight:bold;background-color: <?php echo $bg_color;?>; color:#000000;font-size:12px; text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".number_format($amount_individual,2,'.',',')." &nbsp;";?></td>
            

            <?php }

            $customer_own=$data[$date_key]['']['individual_sales'];
            $customer_own_qty=$data[$date_key]['']['individual_sales_qty'];
            $day_total+=$customer_own;
            $day_total_qty+=$customer_own_qty;
            $grand_total['customer_own']+=$customer_own;
            $grand_total['customer_own_qty']+=$customer_own_qty;
            ?>
            <td style=" font-size:14px; font-weight:bold;background-color: <?php echo $bg_color;?>; color:#000000;font-size:12px; text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".number_format($customer_own_qty,2,'.',',')." &nbsp;";?></td>
            <td style=" font-size:14px; font-weight:bold;background-color: <?php echo $bg_color;?>; color:#000000;font-size:12px; text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".number_format($customer_own,2,'.',',')." &nbsp;";?></td>
            <td style=" font-size:14px; font-weight:bold;background-color: <?php echo $bg_color;?>; color:#000000;font-size:12px; text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".number_format($day_total_qty,2,'.',',')." &nbsp;";?></td>
            <td style=" font-size:14px; font-weight:bold;background-color: <?php echo $bg_color;?>; color:#000000;font-size:12px; text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".number_format($day_total,2,'.',',')." &nbsp;";?></td>
        </tr>    	
    <?php	
    }

?>

<tr>
<td style=" font-size:14px; font-weight:bold;background-color: #0084C3; color:#FFFFFF;font-size:12px; text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;Grand Total&nbsp;"?></td>


<?php
$grand_day_total=0;
$grand_day_total_qty=0;
foreach ($employee_code as $emp_key => $emp_value) {
	$grand_day_total+=$grand_total[$emp_key];
	$grand_day_total_qty+=$grand_total_qty[$emp_key];
	?>
	<td style=" font-size:14px; font-weight:bold;background-color: #0084C3; color:#FFFFFF;font-size:12px; text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".number_format($grand_total_qty[$emp_key],2,'.',',')." &nbsp;";?></td>
	<td style=" font-size:14px; font-weight:bold;background-color: #0084C3; color:#FFFFFF;font-size:12px; text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".number_format($grand_total[$emp_key],2,'.',',')." &nbsp;";?></td>
	<?php
}
$grand_day_total+=$grand_total['customer_own'];
$grand_day_total_qty+=$grand_total['customer_own_qty'];
?>
<td style=" font-size:14px; font-weight:bold;background-color: #0084C3; color:#FFFFFF;font-size:12px; text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".number_format($grand_total['customer_own_qty'],2,'.',',')." &nbsp;";?></td>
<td style=" font-size:14px; font-weight:bold;background-color: #0084C3; color:#FFFFFF;font-size:12px; text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".number_format($grand_total['customer_own'],2,'.',',')." &nbsp;";?></td>
<td style=" font-size:14px; font-weight:bold;background-color: #0084C3; color:#FFFFFF;font-size:12px; text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".number_format($grand_day_total_qty,2,'.',',')." &nbsp;";?></td>
<td style=" font-size:14px; font-weight:bold;background-color: #0084C3; color:#FFFFFF;font-size:12px; text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".number_format($grand_day_total,2,'.',',')." &nbsp;";?></td>
        </tr>   

    </tbody>
    <tr>
    <td>
    &nbsp;
    </td>
    </tr>
</table>
<div id="bottom_anchor"></div>
</div>
<div style="height:20px;">&nbsp;</div>
<?php
$this->load->view("partial/footer");
?>
