<?php
/**
 * @author      Open Source Team
 * @copyright   2022 Pagar.me (https://pagar.me)
 * @license     https://pagar.me Copyright
 * @link        https://pagar.me
 */

/** @var ChargeActions $this */

declare(strict_types = 1);

use Woocommerce\Pagarme\Block\Adminhtml\Sales\Order\MetaBox\ChargeActions;
use Woocommerce\Pagarme\Block\Adminhtml\Sales\Order\MetaBox\Modal;

if (!function_exists('add_action')) {
    exit(0);
}

$charges = $this->getCharges($this->getOrder()->ID);
?>
<?php if ($charges) :
    $gmtOffset = floatval(get_option('gmt_offset'));
    $minutesOffset = intval(3600 * $gmtOffset);
    ?>
    <div class="wrapper">
        <table>
            <thead>
            <tr>
                <?php foreach ($this->getHeaderGrid() as $head) : ?>
                    <th><?= __($head, 'woo-pagarme-payments') ?></th>
                <?php endforeach; ?>
            </tr>
            </thead>
            <?php foreach ($charges as $charge) :
                $chargeTimestamp = $charge->getLastTransaction()->getCreatedAt()->getTimestamp();
                $offsetTimestamp = $chargeTimestamp + $minutesOffset;

                $chargeID = $charge->getPagarmeId()->getValue();
                $chargeUrl = $this->getChargeUrl($chargeID);
                if ($chargeUrl) {
                    $chargeID = sprintf(
                        '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
                        $chargeUrl,
                        $chargeID
                    );
                }
                ?>
                <tr>
                    <td><?= date('d/m/Y H:i:s', $offsetTimestamp) ?></td>
                    <td><?= $chargeID ?></td>
                    <td><?= strtoupper($charge->getLastTransaction()->getTransactionType()->getType()) ?></td>
                    <?php foreach ($this->getTotals() as $total) : ?>
                        <td>
                            <?=
                            $this->setIncludeContainer()->formatElement(
                                $this->getAmount($charge, $total),
                                ['span' => ['class' => true]]
                            )
                            ?>
                        </td>
                    <?php endforeach; ?>
                    <td><?= strtoupper(__(ucfirst($charge->getStatus()->getStatus()), 'woo-pagarme-payments')) ?></td>
                    <td>
                        <button data-type="cancel"
                            <?= $this->getChargeInstance()->is_allowed_cancel($charge) ? '' : 'disabled=disabled' ?>
                                class="button-primary"><?= __('Cancel', 'woo-pagarme-payments') ?></button>

                        <?php if ($this->getTransaction($charge)->getTransactionType()->getType() === 'credit_card') : ?>
                            <button data-type="capture" data-ref="<?= $charge->getPagarmeId()->getValue() ?>"
                                <?= $this->getChargeInstance()->is_allowed_capture($charge) ? '' : 'disabled=disabled' ?>
                                    class="button-primary"><?= __('Capture', 'woo-pagarme-payments') ?></button>
                        <?php endif; ?>
                    </td>
                    <?= $this->createBlock(
                        '\Woocommerce\Pagarme\Block\Adminhtml\Sales\Order\MetaBox\Modal',
                        'pagarme.sales.order.metabox.modal.capture',
                        [
                            'charge'     => $charge,
                            'modal_type' => Modal::MODAL_TYPE_CAPTURE
                        ]
                    )->toHtml() ?>
                </tr>
            <?php endforeach; ?>
        </table>
    </div>
<?php else : ?>
    <?= $this->setIncludeContainer()->formatElement(
        '<p class="title">' . __('No records found.', 'woo-pagarme-payments') . '</p>',
        ['p' => []]
    ) ?>
<?php endif; ?>
<script type="text/javascript">
    if (typeof pagarmeCapture === 'object') {
        pagarmeCapture.start();
        pagarmeCancel.start();
    }
</script>
