<h1 class="title"><?= __('Direct Market') ?></h1>

<ul class="breadcrumber">
    <li><a href="<?= url_to('/') ?>" data-ajax><?= __('Home') ?></a></li>
    <li class="active"><?= __('Direct Market') ?></li>
</ul>

<div class="">
    <a data-ajax href="<?= url_to('/direct-market/sell') ?>" class="btn"><?= __('Sell') ?></a>
</div>

<p class="saldo">
    <?= __('Your current balance is %s', [util('Number')->currency($balance)]) ?>

    <?php if ($balance > 0): ?>
        <a data-ajax href="<?= url_to('/direct-market/rescue') ?>" class="btn"><?= __('Rescue') ?></a>
    <?php endif ?>
</p>

<div class="box-panel">
    <div class="box-panel-title"><?= __('Your sales') ?></div>
    <div class="box-panel-content">
        <?php if (!empty($sellers)): ?>
            <table class="list">
                <thead>
                <tr>
                    <th><?= __('Name') ?></th>
                    <th><?= __('Target') ?></th>
                    <th><?= __('Price') ?></th>
                    <th><?= __('Status') ?></th>
                    <th width="10"></th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($sellers as $item): ?>
                    <tr data-tooltip data-tooltip-url="<?= tooltip_url($item['item']->getHex()) ?>">
                        <td>
                            <?= util('Item')->getFullName($item['item']) ?>
                        </td>
                        <td><?= $item['target'] ?></td>
                        <td><?= util('Number')->currency($item['price']) ?></td>
                        <td><?= $item['status'] ?></td>
                        <td>
                            <?php if ($item['is_cancelable']): ?>
                                <a data-ajax-action data-confirm="<?= __('You are sure?') ?>" href="<?= url_to('/direct-market/cancel/' . $item['id']) ?>"><?= __('Cancel') ?></a>
                            <?php endif ?>
                        </td>
                    </tr>
                <?php endforeach ?>
                </tbody>
            </table>
        <?php else: ?>
            <div class="msg">
                <?= __('There are no item sales') ?>
            </div>
        <?php endif ?>
    </div>
</div>

<div class="box-panel margin-top">
    <div class="box-panel-title"><?= __('Your offers') ?></div>
    <div class="box-panel-content">
        <?php if (!empty($items)): ?>
            <table class="list">
                <thead>
                <tr>
                    <th><?= __('Name') ?></th>
                    <th><?= __('Seller') ?></th>
                    <th><?= __('Price') ?></th>
                    <th><?= __('Status') ?></th>
                    <th width="10"></th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($items as $item): ?>
                    <tr data-tooltip data-tooltip-url="<?= tooltip_url($item['item']->getHex()) ?>">
                        <td>
                            <?= util('Item')->getFullName($item['item']) ?>
                        </td>
                        <td><?= $item['account'] ?></td>
                        <td><?= util('Number')->currency($item['price']) ?></td>
                        <td><?= $item['status'] ?></td>
                        <td style="white-space:nowrap">
                            <?php if ($item['status'] ===  'pending'): ?>
                                <a data-ajax href="<?= url_to('/direct-market/pay/' . $item['id']) ?>"><?= __('Pay') ?></a>
                            <?php elseif ($item['status'] === 'waiting'): ?>
                                <a data-ajax href="<?= url_to('/donate/' . $item['order']['gateway'] . '?order=' . $item['order']['id']) ?>"><?= __('Pay') ?></a>
                            <?php elseif ($item['status'] === 'payed'): ?>
                                <a data-ajax-action href="<?= url_to('/direct-market/delivery/' . $item['id']) ?>"><?= __('Get item') ?></a>
                            <?php endif ?>
                        </td>
                    </tr>
                <?php endforeach ?>
                </tbody>
            </table>
        <?php else: ?>
            <div class="msg">
                <?= __('There are no item offers') ?>
            </div>
        <?php endif ?>
    </div>
</div>

<div class="box-panel margin-top">
    <div class="box-panel-title"><?= __('Your rescues') ?></div>
    <div class="box-panel-content">
        <?php if (!empty($rescues)): ?>
            <table class="list">
                <thead>
                <tr>
                    <th><?= __('Total value') ?></th>
                    <th><?= __('Tax') ?></th>
                    <th><?= __('Value') ?></th>
                    <th><?= __('PIX') ?></th>
                    <th><?= __('Status') ?></th>
                    <th width="100"><?= __('Created at') ?></th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($rescues as $rescue): ?>
                    <tr>
                        <td><?= util('Number')->currency($rescue['value']) ?></td>
                        <td><?= $rescue['tax'] ?>%</td>
                        <td><?= util('Number')->currency($rescue['value'] - ($rescue['value'] * ($rescue['tax'] / 100))) ?></td>
                        <td><?= $rescue['pix'] ?></td>
                        <td><?= $rescue['status'] ?></td>
                        <td><?= util('Date')->dateTime($rescue['create_date']) ?></td>
                    </tr>
                <?php endforeach ?>
                </tbody>
            </table>
        <?php else: ?>
            <div class="msg">
                <?= __('No rescues found') ?>
            </div>
        <?php endif ?>
    </div>
</div>
