<?php
/**
 * BusinessAdmin: administrative software for small companies
 * Copyright (C) 2015 Camil Staps (ViviSoft)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once(__DIR__ . '/../login.php');

$_offer = new Offer($_pdo, $_id);
?>
<div class="col-lg-6">
	<div class="panel panel-default">
		<div class="panel-heading">Assignments</div>
		<div class="panel-body table-responsive">
			<table class="table table-bordered table-striped">
				<thead>
					<tr>
						<th>#</th>
						<th>Briefing</th>
						<th>Time</th>
						<th>Price</th>
						<th>Tools</th>
					</tr>
				</thead>
				<tbody>
					<?php
					$assignments = BusinessAdmin::getAssignments($_pdo, array("offerId = {$_offer->id}"));
					foreach ($assignments as $assignment) {
						echo "<tr>
							<td class='col-min-width'>{$assignment->id}</td>
							<td class='col-max-width'>
								<b><a href='#' class='editable' id='editable-assignment-{$assignment->id}-title' data-type='text' data-pk='{$assignment->id}' data-url='".Constants::url_external."assignments/edit'>{$assignment->title}</a></b><br/>
								<p>{$assignment->getHTMLDescription()}</p>
							</td>
							<td class='col-min-width'><a href='#' class='editable' id='editable-assignment-{$assignment->id}-hours' data-type='text' data-pk='{$assignment->id}' data-url='".Constants::url_external."assignments/edit'>{$assignment->hours}</a>h</td>
							<td class='col-min-width'>
							   ".Constants::invoice_valuta."<a href='#' class='editable' id='editable-assignment-{$assignment->id}-price_per_hour' data-type='text' data-pk='{$assignment->id}' data-url='".Constants::url_external."assignments/edit'>{$assignment->price_per_hour}</a> / hr<br/>
								<a href='#' class='editable' id='editable-assignment-{$assignment->id}-vat' data-type='text' data-pk='{$assignment->id}' data-url='".Constants::url_external."assignments/edit'>{$assignment->VAT_percentage}</a>% VAT
							</td>
							<td class='col-min-width'>
								<a title='View' href='".Constants::url_internal."/assignments?id={$assignment->id}' class='btn btn-primary btn-circle fa fa-arrow-right'></a>
								<a title='Delete' href='".Constants::url_internal."/assignments?delete={$assignment->id}' class='btn btn-danger btn-circle fa fa-times'></a>
							</td>
						</tr>";
					}
					if (count($assignments) == 0) {
						echo "<tr><td colspan='5'>There are no assignments in the database.</td></tr>";
					}
					?>
				</tbody>
			</table>
		</div>
	</div>
</div>
<div class="col-lg-6">
	<div class="panel panel-default">
		<div class="panel-heading">Discounts</div>
		<div class="panel-body table-responsive">
			<table class="table table-bordered table-striped">
				<thead>
					<tr>
						<th>#</th>
						<th>Briefing</th>
						<th>Value</th>
						<th>Tools</th>
					</tr>
				</thead>
				<tbody>
					<?php
					$discounts = BusinessAdmin::getDiscounts($_pdo, array("offerId = {$_offer->id}"));
					foreach ($discounts as $discount) {
						echo "<tr>
							<td class='col-min-width'>{$discount->id}</td>
							<td class='col-max-width'>
								<b><a href='#' class='editable' id='editable-discount-{$discount->id}-title' data-type='text' data-pk='{$discount->id}' data-url='".Constants::url_external."discounts/edit'>{$discount->title}</a></b><br/>
								<p>{$discount->description}</p>
							</td>
							<td class='col-min-width'>
							   ".Constants::invoice_valuta."<a href='#' class='editable' id='editable-discount-{$discount->id}-value' data-type='text' data-pk='{$discount->id}' data-url='".Constants::url_external."discounts/edit'>{$discount->value}</a> / hr<br/>
								<a href='#' class='editable' id='editable-discount-{$discount->id}-vat' data-type='text' data-pk='{$discount->id}' data-url='".Constants::url_external."discounts/edit'>{$discount->VAT_percentage}</a>% VAT
							</td>
							<td class='col-min-width'>
								<a title='View' href='".Constants::url_internal."/discounts?id={$discount->id}' class='btn btn-primary btn-circle fa fa-arrow-right'></a>
								<a title='Delete' href='".Constants::url_internal."/discounts?delete={$discount->id}' class='btn btn-danger btn-circle fa fa-times'></a>
							</td>
						</tr>";
					}
					if (count($discounts) == 0) {
						echo "<tr><td colspan='4'>There are no discounts in the database.</td></tr>";
					}
					?>
				</tbody>
			</table>
		</div>
	</div>
</div>
<div class="col-md-6">
	<div class="panel panel-default">
		<div class="panel-heading">Payments</div>
		<div class="panel-body table-responsive">
			<table class="table table-bordered table-striped">
				<thead>
					<tr>
						<th>#</th>
						<th>Braintree ID</th>
						<th>Braintree status</th>
						<th>Tools</th>
					</tr>
				</thead>
				<tbody>
					<?php
					$payment = $_offer->getPayment();
					$payments = is_null($payment) ? [] : [$payment];
					foreach ($payments as $payment) {
						echo "<tr>
							<td class='col-min-width'>{$payment->id}</td>
							<td class='col-min-width'>{$payment->braintree_id}</td>
							<td class='col-max-width'>
								<a href='#payment-status-{$payment->id}' aria-expanded='false' aria-controls='#payment-status-{$payment->id}' data-toggle='collapse'>{$payment->braintree_status}</a>
								<div class='collapse' id='payment-status-{$payment->id}'>";
						foreach ($payment->getBraintreeStatusHistory() as $item) {
							echo "{$item->timestamp->format('Y-m-d H:i')}: {$item->status}<br/>";
						}
						echo "</div>
							</td>
							<td class='col-min-width'>
								<a title='Refresh' href='?id={$_offer->id}&refresh_payment={$payment->id}' class='btn btn-info btn-circle fa fa-refresh'></a>
							</td>
						</tr>";
					}
					if (count($payments) == 0) {
						echo "<tr><td colspan='4'>There are no payments in the database.</td></tr>";
					}
					?>
				</tbody>
			</table>
		</div>
	</div>
</div>
<div class="col-lg-12">
	<div class="panel panel-default" id="panel-timeline">
		<div class="panel-heading">
			<i class="fa fa-clock-o fa-fw"></i> Timeline
		</div>
		<!-- /.panel-heading -->
		<div class="panel-body">
			<ul class="timeline">
				<?php
				$list = array();
				$sort_list = array();

				$temp = array(
					'id' => $_offer->id,
					'contact' => $_offer->getContact()->name,
					'assignments' => '',
					'assignments_header' => ''
				);
				foreach ($_offer->getAssignments() as $assignment) {
					$temp['assignments'] .= "<b>{$assignment->title}</b><br/><span class='smaller'>(".Constants::invoice_valuta."{$assignment->calculate(Calculatable::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(Calculatable::TOTAL)} incl. VAT)</span><br/><p>{$assignment->getHTMLDescription()}</p>";
					$temp['assignments_header'] .= "<b>{$assignment->title}</b><br/><span class='smaller'>(".Constants::invoice_valuta."{$assignment->calculate(Calculatable::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(Calculatable::TOTAL)} incl. VAT)</span><br/>";
				}
				$list[] = array_merge($temp, array('type' => 'start', 'time' => $_offer->start_date, 'description' => 'Offer started'));
				$sort_list[] = $_offer->start_date . $_offer->id . 0;
				$list[] = array_merge($temp, array('type' => 'end', 'time' => $_offer->end_date, 'description' => 'Offer ended'));
				$sort_list[] = $_offer->end_date . $_offer->id . 1;
				if ($_offer->invoice_date > 0) {
					$list[] = array_merge($temp, array('type' => 'invoice', 'time' => $_offer->invoice_date, 'description' => 'Invoice sent'));
					$sort_list[] = $_offer->invoice_date . $_offer->id . 2;
					if ($_offer->getPaymentReceived() > 0) {
						$list[] = array_merge($temp, array('type' => 'payment_received', 'time' => $_offer->getPaymentReceived(), 'description' => 'Payment received'));
						$sort_list[] = $_offer->getPaymentReceived() . $_offer->id . 3;
					}
				}

				array_multisort($sort_list, SORT_DESC, $list);
				$i = 0;
				foreach ($list as $item) {
					if ($item['time'] > time()) {
						continue;
					}
					echo "<li" . ($i++ % 2 == 0 ? ' class="timeline-inverted"' : '') . ">";
					switch ($item['type']) {
						case 'start':             echo "<div class='timeline-badge info' title='{$item['description']}'><i class='fa fa-circle-o-notch'></i></div>"; break;
						case 'end':               echo "<div class='timeline-badge primary' title='{$item['description']}'><i class='fa fa-circle-o'></i></div>"; break;
						case 'invoice':           echo "<div class='timeline-badge warning' title='{$item['description']}'><i class='fa fa-check-circle-o'></i></div>"; break;
						case 'payment_received':  echo "<div class='timeline-badge success' title='{$item['description']}'><i class='fa fa-eur'></i></div>"; break;
					}
					echo "<div class='timeline-panel'>";
					echo "<div class='timeline-heading'><h4 class='timeline-title'>#{$item['id']} to {$item['contact']}: {$item['description']}</h4><p><small class='text-muted'><i class='fa fa-clock-o fa-fw'></i> ".BusinessAdmin::formatDate($item['time'],false,true,true)."</small></p></div>";
					switch ($item['type']) {
						case 'start':             echo "<div class='timeline-body'>{$item['assignments']}</div>"; break;
						default:                  echo "<div class='timeline-body'>{$item['assignments_header']}</div>";
					}
					echo "</div>";
					echo "</li>";
				}
				?>
			</ul>
		</div>
		<!-- /.panel-body -->
	</div>
	<!-- /.panel -->
</div>