<?php
// print_R($this); die;
?>
<div class="container component">
	<h2>Wholesale</h2>
	<?php
	$this->display('w-toolbar.phtml', array('baseUrl' => $this->baseUrl));
	print '<div style="clear:both;"></div>';
	?>
	<div class="tabbable">
		<ul class="nav nav-tabs">
			<?php
			foreach (array_keys($this->tbl_params) as $index => $v) :
//				echo '<li' . ($index == 0 ? ' class="active"' : '') . '><a id="menutab' . ($index + 1) . '" href="#tabs-' . ($index + 1) . '" data-toggle="tab">' . $this->tbl_params[$v]['title'] . '</a></li>';
				echo '<li><a id="menutab' . ($index + 1) . '" href="#tabs-' . ($index + 1) . '" data-toggle="tab">' . $this->tbl_params[$v]['title'] . '</a></li>';
			endforeach;
			echo '<li><a id="menutab' . ($index + 2) . '" href="#tabs-' . ($index + 2) . '" data-toggle="tab">Retail</a></li>';
			?>
		</ul>
		<div class="tab-content">
			<?php
			foreach (array_keys($this->tbl_params) as $index => $report_type) {
				$report_params = $this->tbl_params[$report_type];
				$report_data = $this->data[$report_type];
				foreach ($report_params['fields'] as $usage_type => $usage_fields) {
					foreach ($usage_fields as $field) {
						$table_totals[$usage_type][$field['value']] = 0;
					}
				}

//				echo '<div class="tab-pane' . (isset($report_params['default']) && $report_params['default'] ? ' active' : '') . '" id="tabs-' . ($index + 1) . '">
				echo '<div class="tab-pane" id="tabs-' . ($index + 1) . '">
					<h3>' . $report_params['title'] . '</h3>
					<table data-type="' . $report_params['direction'] . '" class="wholesale-table">';

				echo "<thead><tr>";
				echo '<th rowspan="2">' . $this->group_field_display . '</th>';
				foreach ($report_params['fields'] as $usage_type => $fields) {
					echo '<th colspan="' . count($fields) . '">' . $usage_type . '</th>';
				}
				if ($report_type == 'nr') {
					$total_cost = 0;
					echo '<th rowspan="2">Total cost</th>';
				}
				echo "</tr>";
				echo "<tr>";
				foreach ($report_params['fields'] as $fields) {
					foreach ($fields as $field) {
						echo '<th>' . $field['label'] . "</th>";
					}
				}
				echo "</tr></thead><tbody>";

				foreach ($report_data['available_group_values'] as $group_value) {
					echo '<tr><td>';
					if ($this->group_by == 'carrier') {
						echo "<input class=\"carrier\" type=\"hidden\" value=\"" . $group_value . "\">";
					}
					echo '<a onclick="openPopup(this);" href="#w-popup" data-type="update" data-toggle="modal" role="button" data-remote="" data-value="' . $group_value . '">' . $group_value . ($this->group_by == 'dayofmonth' ? ' (' . date("D", strtotime($group_value)) . ')' : '') . '</a></td>';
					foreach ($report_params['fields'] as $usage_type => $usage_fields) {
						foreach ($usage_fields as $field) {
							echo '<td class="num-cell">';
							if (isset($report_data['table_data'][$usage_type][$group_value][$field['display']])) {
								if ($field['decimal'] === false) {
									echo $report_data['table_data'][$usage_type][$group_value][$field['display']];
								} else if ($field['decimal'] == 0) {
									echo number_format(ceil($report_data['table_data'][$usage_type][$group_value][$field['display']]));
								} else {
									echo number_format($report_data['table_data'][$usage_type][$group_value][$field['display']], $field['decimal']);
								}
								$table_totals[$usage_type][$field['value']]+=$report_data['table_data'][$usage_type][$group_value][$field['value']];
							} else {
								echo '&nbsp;';
							}
							echo '</td>';
						}
					}
					if ($report_type == 'nr') {
						$row_cost = 0;
						foreach ($report_params['fields'] as $usage_type => $usage_fields) {
							foreach ($usage_fields as $field) {
								if ($field['value'] == 'cost') {
									if (isset($report_data['table_data'][$usage_type][$group_value][$field['value']])) {
										$row_cost+=$report_data['table_data'][$usage_type][$group_value][$field['value']];
									}
								}
							}
						}
						echo '<td class="num-cell">' . number_format($row_cost, 2) . '</td>';
						$total_cost+=$row_cost;
					}
					echo '</tr>';
				}
				echo '<tr class="totals">';
				echo '<td>Total</td>';
				foreach ($report_params['fields'] as $usage_type => $usage_fields) {
					foreach ($usage_fields as $field) {
						echo '<td class="num-cell">';
						if ($field['decimal'] === false) {
							echo $table_totals[$usage_type][$field['value']];
						} else if ($field['decimal'] == 0) {
							echo number_format(ceil($table_totals[$usage_type][$field['value']]));
						} else {
							echo number_format($table_totals[$usage_type][$field['value']], $field['decimal']);
						}
						echo '</td>';
					}
				}
				if ($report_type == 'nr') {
					echo '<td>' . number_format($total_cost, 2) . '</td>';
				}
				echo '</tr></tbody>';
				echo '</table>';
				$graph = Admin_Graphs::prepareGraph($this->data[$report_type]['table_data'], $this->group_field_display, $this->group_by, $report_params['fields'], array('graph_id' => ($report_type . '_call_graph'), 'graph_class' => 'wholesale-graph'));
				echo Admin_Graphs::printGraph($graph, false, 1, 'menutab' . ($index + 1));
				echo '</div>';
			}

			$table_totals = array();
			foreach (array_keys($retail_data[0]) as $fieldName) {
				$table_totals[$fieldName] = 0;
			}

			echo '<div class="tab-pane" id="tabs-' . ($index + 2) . '">
					<h3>Retail</h3>
					<table class="wholesale-table"><thead><tr>';
			foreach ($retail_tbl_params['fields'] as $field) {
				if (isset($field['commonColumn'])) {
					if (!isset($commonColumns[$field['commonColumn']])) {
						echo '<th colspan="' . $common_columns[$field['commonColumn']]['colspan'] . '">' . $common_columns[$field['commonColumn']]['label'] . '</th>';
						$commonColumns[$field['commonColumn']] = true;
					}
				} else {
					echo '<th rowspan="2">' . $field['label'] . '</th>';
				}
			}
			echo '</tr><tr>';
			foreach ($retail_tbl_params['fields'] as $field) {
				if (isset($field['commonColumn'])) {
					echo '<th>' . $field['label'] . '</th>';
				}
			}
			echo '</tr></thead><tbody>';
			foreach ($retail_data as $row) {
				echo '<tr>';
				foreach ($retail_tbl_params['fields'] as $field) {
					echo '<td' . ($field['decimal'] === false ? '' : ' class="num-cell"') . '>';
					if (is_object($field['display'])) {
						echo $field['display']($row[$field['value']]);
					} else if (isset($row[$field['display']])) {
						if ($field['decimal'] === false) {
							echo $row[$field['display']];
						} else if ($field['decimal'] == 0) {
							echo number_format(ceil($row[$field['display']]));
						} else {
							echo number_format($row[$field['display']], $field['decimal']);
						}
						$table_totals[$field['value']]+=$row[$field['value']];
					} else {
						echo '&nbsp;';
					}
					echo '</td>';
				}
				echo '</tr>';
			}
			echo '<tr class="totals">';
			foreach ($retail_tbl_params['fields'] as $field) {
				echo '<td' . ($field['decimal'] === false ? '' : ' class="num-cell"') . '>';
				if (isset($field['totals']) && !$field['totals']) {
					echo '&nbsp;';
				} else if ($field['decimal'] === false) {
					echo 'Total';
				} else if ($field['decimal'] == 0) {
					echo number_format(ceil($table_totals[$field['value']]));
				} else {
					echo number_format($table_totals[$field['value']], $field['decimal']);
				}
				echo '</td>';
			}
			echo '</tr></tbody>';
			echo '</table>';
			echo '</div>';
			?>
		</div>
	</div>
	<?php $this->display('w-popups.phtml', array()); ?>
	<hr>
	<footer style="clear: both">
		<?php $this->display('billrun.phtml', array()); ?>
	</footer>

</div> <!-- /container -->
