<?php
/**
 * @version 7.9 [rev.7.9.02]
 */
/** @var \Ppb\Db\Table\Row\Sale $sale */
/** @var \Ppb\Db\Table\Rowset\SalesListings $salesListings */
$salesListings = $sale->findDependentRowset('\Ppb\Db\Table\SalesListings');
/** @var \Ppb\Db\Table\Row\User $buyer */
$buyer = $sale->findParentRow('\Ppb\Db\Table\Users', 'Buyer');

$postageDesc = (isset($postageDesc)) ? $postageDesc : false;
$directPaymentButton = (isset($directPaymentButton)) ? $directPaymentButton : false;
$type = (isset($type)) ? $type : '';
$inAdmin = (isset($inAdmin)) ? $inAdmin : false;
$caption = (isset($caption)) ? $caption : false;
$displayDownloads = (isset($displayDownloads)) ? $displayDownloads : false;
$invoice = (isset($invoice)) ? $invoice : false;

/** @var \Ppb\Db\Table\Row\User $seller */
if ($type != 'sold') {
    $seller = $sale->findParentRow('\Ppb\Db\Table\Users', 'Seller');
}

$insuranceAmount = $sale->getInsuranceAmount();
$taxAmount = $sale->getTaxAmount();

$rowspan = 1;
if ($insuranceAmount) {
    $rowspan++;
}

if ($taxAmount) {
    $rowspan++;
}

?>

<table class="table table-sale <?php echo $invoice ? 'well' : ''; ?>">
    <?php if ($caption === true) { ?>
        <caption>
            <?php if (!$inAdmin) { ?>
                <?php $btnClass = 'btn btn-default btn-sm' . (($sale->isActive()) ? '' : ' ' . 'disabled'); ?>
                <div class="btn-group pull-left">
                    <a class="<?php echo $btnClass; ?>"
                       href="<?php echo $this->url(array('module' => 'members', 'controller' => 'invoices', 'action' => 'view', 'id' => $sale['id'])); ?>"
                       target="_blank"
                       title="<?php echo $this->_('View & Print Invoice'); ?>">
                        <i class="fa fa-print"></i> <?php echo $this->_('View & Print Invoice'); ?>
                    </a>
                    <?php if ($sale->messagingEnabled()) { ?>
                        <a class="<?php echo $btnClass; ?>"
                           href="<?php echo $this->url($sale->messagingLink()); ?>"
                           title="<?php echo $this->_('Message Board'); ?>">
                            <i class="fa fa-comments"></i> <?php echo $this->_('Message Board'); ?>
                        </a>
                    <?php } ?>
                    <?php
                    if ($this->settings['enable_reputation']) {
                        if (!empty($this->loggedInUser['id'])) {
                            $reputationLink = $sale->reputationLink($this->loggedInUser['id']);
                            if ($reputationLink) { ?>
                                <a class="<?php echo $btnClass; ?>"
                                   href="<?php echo $this->url($reputationLink); ?>"
                                   title="<?php echo $this->_('Leave Feedback'); ?>">
                                    <i class="fa fa-commenting"></i> <?php echo $this->_('Leave Feedback'); ?>
                                </a>
                            <?php } ?>
                        <?php } ?>
                    <?php } ?>
                </div>
                <div class="clearfix"></div>
            <?php } ?>
            <div class="pull-left pdl-5">
                <strong><?php echo $this->_('Invoice #'); ?><?php echo $sale['id']; ?></strong>
                <?php echo $this->_('from'); ?>
                <strong><?php echo $this->date(max(array($sale['created_at'], $sale['updated_at'])),
                        true); ?></strong>
                -
                <?php if ($type != 'sold') { ?>
                    <strong><?php echo $this->_('Seller'); ?></strong>:
                    <?php echo $this->userDetails()->setUser($seller)->display(); ?>
                <?php } ?>

                <?php if ($type != 'bought') { ?>
                    <strong><?php echo $this->_('Buyer'); ?></strong>:
                    <?php echo $this->userDetails()->setUser($buyer)->display(); ?>
                <?php } ?>
            </div>

            <div class="pull-right">
                <?php echo $this->saleOptions($sale); ?>
            </div>
        </caption>
        <?php
    }
    else if (!empty($caption)) {
        ?>
        <caption><?php echo $caption; ?></caption>
    <?php } ?>
    <thead>
    <tr>
        <th><?php echo $this->_('Listing'); ?></th>
        <th class="size-mini"><?php echo $this->_('Quantity'); ?></th>
        <th class="size-mini"><?php echo $this->_('Price'); ?></th>
        <th class="size-mini"><?php echo $this->_('Subtotal'); ?></th>
    </tr>
    </thead>
    <tbody>
    <?php
    /** @var \Ppb\Db\Table\Row\SaleListing $saleListing */
    foreach ($salesListings as $saleListing) {
        /** @var \Ppb\Db\Table\Row\Listing $listing */
        $listing = $saleListing->findParentRow('\Ppb\Db\Table\Listings');
        ?>
        <tr>
            <td>
                <?php if ($listing) { ?>
                    <div>
                        <a href="<?php echo $this->url($listing->link()); ?>"><?php echo $listing['name']; ?></a>
                        <small>(#<?php echo $listing['id']; ?>)</small>
                    </div>
                    <?php if (!empty($saleListing['product_attributes'])) { ?>
                        <div>
                            <small><?php echo $this->productAttributes($saleListing['product_attributes'])->display(); ?></small>
                        </div>
                    <?php } ?>
                    <?php
                }
                else {
                    ?>
                    <em><?php echo $this->_('Listing Deleted'); ?></em>
                <?php } ?>
            </td>
            <td><?php echo $saleListing['quantity']; ?></td>
            <td><?php echo $this->amount($saleListing->price(), $sale['currency']); ?></td>
            <td><?php echo $this->amount(($saleListing->price() * $saleListing['quantity']), $sale['currency']); ?></td>
        </tr>
        <?php if (($digitalDownloads = $saleListing->getDigitalDownloads()) !== false && $displayDownloads === true) {
            ?>
            <tr>
                <td colspan="4">
                    <?php
                    /** @var \Ppb\Db\Table\Row\ListingMedia $digitalDownload */
                    foreach ($digitalDownloads as $digitalDownload) {
                        ?>
                        <div class="download-box">
                            <?php if ($sale->isBuyer()) { ?>
                                <div class="pull-left">
                                    <a href="<?php echo $this->url($digitalDownload['download_link'], null, false, null,
                                        true, false); ?>"
                                       class="btn btn-sm btn-primary<?php echo ($digitalDownload['active']) ? '' : ' disabled'; ?>">
                                        <i class="fa fa-download"></i> <?php echo $this->_('Download'); ?></a>
                                </div>
                            <?php } ?>
                            <div class="pull-left">
                                <?php echo $digitalDownload['value']; ?> <br>
                                <?php if ($digitalDownload['active']) { ?>
                                    <small class="text-success"><?php echo $this->_('Download Active'); ?></small>
                                    <?php
                                }
                                else {
                                    ?>
                                    <small class="text-danger"><?php echo $this->_('Download Inactive'); ?></small>
                                <?php } ?>
                                &middot;
                                <small class="text-info">
                                    <?php echo sprintf(
                                        $this->_('Downloaded %s times'),
                                        intval($digitalDownload['nb_downloads'])); ?>
                                </small>
                                &middot;
                                <small class="text-primary">
                                    <?php echo sprintf(
                                        $this->_('Added on %s'),
                                        $this->date($digitalDownload['created_at'], true)); ?>
                                </small>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                    <?php } ?>
                    <?php if ($sale->isSeller()) { ?>
                        <a class="btn btn-sm btn-default confirm-box"
                           href="<?php echo $this->url(array('module' => 'members', 'controller' => 'invoices', 'action' => 'update-download-links', 'sale_listing_id' => $saleListing->getData('id'))); ?>"
                           data-message="<?php echo $this->_('Update the status of the download links for this listing?'); ?>">
                            <?php if ($saleListing->getData('downloads_active')) { ?>
                                <i class="fa fa-times"></i> <?php echo $this->_('Inactivate Downloads'); ?>
                                <?php
                            }
                            else {
                                ?>
                                <i class="fa fa-check"></i> <?php echo $this->_('Activate Downloads'); ?>
                            <?php } ?>
                        </a>
                    <?php } ?>
                </td>
            </tr>
        <?php } ?>
    <?php } ?>

    <?php if ($hasPostage = $sale->hasPostage()) { ?>
        <tr>
            <td>
                <?php if ($postageDesc) {
                    $postageMethod = $sale->getPostageMethod();
                    ?>
                    <small>
                        <?php echo $this->_('Postage Method:'); ?> <?php echo $this->_($postageMethod); ?>
                        <?php if ($sale->isActive()) { ?>
                            <?php if (!empty($sale['tracking_link'])) { ?>
                                <a href="<?php echo $sale['tracking_link']; ?>"
                                   target="_blank"><?php echo $this->_('Track this package'); ?></a>
                            <?php } ?>
                            <?php if ($buyer) {
                                $this->userDetails()->setUser($buyer);
                                if ($postageMethod != \Ppb\Model\Shipping::VALUE_PICK_UP) {
                                    $this->userDetails()->setAddress($sale['shipping_address_id']);
                                    ?>
                                    <div>
                                        <?php echo $this->_('Delivery Address:'); ?>
                                        <?php echo $this->userDetails()->displayFullName() . ', '; ?>
                                        <?php echo $this->userDetails()->displayAddress(', '); ?>
                                    </div>
                                <?php }
                                else { ?>
                                    <div>
                                        <?php echo $this->_('Name:'); ?>
                                        <?php echo $this->userDetails()->displayFullName(); ?>
                                    </div>
                                <?php } ?>

                                <div>
                                    <?php echo $this->_('Email:'); ?>
                                    <?php echo $buyer['email']; ?>
                                </div>
                                <?php if ($this->settings['sale_phone_numbers']) { ?>
                                    <div>
                                        <?php echo $this->_('Phone:'); ?>
                                        <?php echo $buyer['phone']; ?>
                                    </div>
                                <?php } ?>
                            <?php } ?>

                            <?php if (!empty($sale['shipping_comments'])) { ?>
                                <div>
                                    <?php echo $this->_('Comments:'); ?>
                                    <?php echo $sale['shipping_comments']; ?>
                                </div>
                            <?php } ?>
                        <?php } ?>
                    </small>
                <?php } ?>
            </td>
            <td></td>
            <td>
                <small><?php echo $this->_('Postage'); ?></small>
            </td>
            <td><?php echo $this->amount($sale['postage_amount'], $sale['currency'], null, true); ?></td>
        </tr>

        <?php if ($insuranceAmount) { ?>
            <tr>
                <td></td>
                <td></td>
                <td>
                    <small><?php echo $this->_('Insurance'); ?></small>
                </td>
                <td><?php echo $this->amount($insuranceAmount, $sale['currency'], null, true); ?></td>
            </tr>
        <?php } ?>
    <?php } ?>

    <?php if ($taxAmount) { ?>
        <tr>
            <td></td>
            <td></td>
            <td>
                <small><?php echo $this->_('Tax'); ?></small>
            </td>
            <td><?php echo $this->amount($taxAmount, $sale['currency'], null, true); ?></td>
        </tr>
    <?php } ?>

    <tr>
        <td>
            <?php if (!$sale->isActive(false)) { ?>
                <?php if ($sale->canPayFee() && $directPaymentButton) { ?>
                    <a class="btn btn-sm btn-danger"
                       href="<?php echo $this->url(array('module' => 'app', 'controller' => 'payment', 'action' => 'sale-transaction', 'id' => $sale['id'])); ?>"
                       title="<?php echo $this->_('Make Transaction Sale Fee Payment'); ?>">
                        <?php echo $this->_('Make Sale Transaction Fee Payment'); ?>
                    </a>
                <?php }
                else { ?>
                    <span class="label label-danger"><?php echo $this->_('Sale Transaction Fee Not Paid'); ?></span>
                <?php } ?>
            <?php } ?>

            <?php if ($inAdmin) { ?>
                <?php if ($sale['seller_deleted']) { ?>
                    <span class="label label-danger"><?php echo $this->_('Seller Deleted'); ?></span>
                <?php } ?>
                <?php if ($sale['buyer_deleted']) { ?>
                    <span class="label label-danger"><?php echo $this->_('Buyer Deleted'); ?></span>
                <?php } ?>
            <?php } ?>

            <?php if ($sale->canPayDirectPayment() !== false && $directPaymentButton) { ?>
                <a href="<?php echo $this->url(array('module' => 'app', 'controller' => 'payment', 'action' => 'direct-payment', 'id' => $sale['id'])); ?>"
                   class="btn btn-sm btn-success"><?php echo $this->_('Make Payment'); ?></a>
            <?php } ?>
            <?php if ($sale['expires_at']) { ?>
                <div>
                    <small class="text-danger">
                        <?php echo sprintf(
                            $this->_('The sale will be cancelled if not paid until %s'),
                            $this->date($sale['expires_at'])); ?>
                    </small>
                </div>
            <?php } ?>

        </td>
        <td></td>
        <td>
            <small><strong><?php echo $this->_('Total'); ?></strong></small>
        </td>
        <td><strong><?php echo $this->amount($sale->calculateTotal(), $sale['currency']); ?></strong></td>
    </tr>
    </tbody>
</table>